#include "config.h"
#include "BitmapImage.h"
#if PLATFORM(CG)
#include "AffineTransform.h"
#include "FloatConversion.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageObserver.h"
#include "PDFDocumentImage.h"
#include "PlatformString.h"
#include <ApplicationServices/ApplicationServices.h>
#include "WebCoreSystemInterface.h"
namespace WebCore {
void FrameData::clear()
{
if (m_frame) {
CGImageRelease(m_frame);
m_frame = 0;
m_duration = 0.0f;
m_hasAlpha = true;
}
}
void BitmapImage::checkForSolidColor()
{
if (frameCount() > 1)
m_isSolidColor = false;
else {
CGImageRef image = frameAtIndex(0);
if (image && CGImageGetWidth(image) == 1 && CGImageGetHeight(image) == 1) {
unsigned char pixel[4]; CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef bmap = CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), space,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if (bmap) {
GraphicsContext(bmap).setCompositeOperation(CompositeCopy);
CGRect dst = { {0, 0}, {1, 1} };
CGContextDrawImage(bmap, dst, image);
if (pixel[3] == 0)
m_solidColor = Color(0, 0, 0, 0);
else
m_solidColor = Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]);
m_isSolidColor = true;
CFRelease(bmap);
}
CFRelease(space);
}
}
}
CGImageRef BitmapImage::getCGImageRef()
{
return frameAtIndex(0);
}
void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp)
{
if (!m_source.initialized())
return;
CGRect fr = ctxt->roundToDevicePixels(srcRect);
CGRect ir = ctxt->roundToDevicePixels(dstRect);
CGImageRef image = frameAtIndex(m_currentFrame);
if (!image) return;
if (mayFillWithSolidColor()) {
fillWithSolidColor(ctxt, ir, solidColor(), compositeOp);
return;
}
CGContextRef context = ctxt->platformContext();
ctxt->save();
CGSize selfSize = size(); float curHeight = CGImageGetHeight(image);
CGSize adjustedSize = selfSize;
if (curHeight < selfSize.height) {
adjustedSize.height *= curHeight / selfSize.height;
if (CGRectGetMaxY(fr) > adjustedSize.height) {
float frHeight = adjustedSize.height - fr.origin.y; if (frHeight <= 0)
return; ir.size.height *= (frHeight / fr.size.height); fr.size.height = frHeight;
}
}
ctxt->setCompositeOperation(compositeOp);
CGContextTranslateCTM(context, ir.origin.x, ir.origin.y);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -ir.size.height);
ir.origin.x = ir.origin.y = 0;
if (fr.size.width != adjustedSize.width || fr.size.height != adjustedSize.height) {
float xscale = adjustedSize.width / selfSize.width;
float yscale = adjustedSize.height / curHeight; fr.origin.x /= xscale;
fr.origin.y /= yscale;
fr.size.width /= xscale;
fr.size.height /= yscale;
image = CGImageCreateWithImageInRect(image, fr);
if (image) {
CGContextDrawImage(context, ir, image);
CFRelease(image);
}
} else CGContextDrawImage(context, ir, image);
ctxt->restore();
startAnimation();
if (imageObserver())
imageObserver()->didDraw(this);
}
void Image::drawPatternCallback(void* info, CGContextRef context)
{
Image* data = (Image*)info;
CGImageRef image = data->nativeImageForCurrentFrame();
float w = CGImageGetWidth(image);
float h = CGImageGetHeight(image);
CGContextDrawImage(context, GraphicsContext(context).roundToDevicePixels(FloatRect(0, -h, w, h)), image);
}
void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect)
{
CGContextRef context = ctxt->platformContext();
#ifndef BUILDING_ON_TIGER
CGImageRef tileImage = nativeImageForCurrentFrame();
float w = CGImageGetWidth(tileImage);
float h = CGImageGetHeight(tileImage);
if (w == size().width() && h == size().height() && tileRect.size() == size()) {
ctxt->save();
CGContextClipToRect(context, destRect);
ctxt->setCompositeOperation(op);
CGContextTranslateCTM(context, destRect.x(), destRect.y());
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -destRect.height());
float scaledTileWidth = tileRect.width() * narrowPrecisionToCGFloat(patternTransform.a());
float scaledTileHeight = tileRect.height() * narrowPrecisionToCGFloat(patternTransform.d());
float adjustedX = phase.x() - destRect.x(); float adjustedY = destRect.height() - (phase.y() - destRect.y() + scaledTileHeight);
CGContextDrawTiledImage(context, FloatRect(adjustedX, adjustedY, scaledTileWidth, scaledTileHeight), tileImage);
ctxt->restore();
} else {
#endif
static const CGPatternCallbacks patternCallbacks = { 0, drawPatternCallback, NULL };
CGPatternRef pattern = CGPatternCreate(this, FloatRect(tileRect.x(), -tileRect.y() - tileRect.height(), tileRect.width(), tileRect.height()),
CGAffineTransform(patternTransform), tileRect.width(), tileRect.height(),
kCGPatternTilingConstantSpacing, true, &patternCallbacks);
if (!pattern)
return;
ctxt->save();
wkSetPatternPhaseInUserSpace(context, phase);
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetFillColorSpace(context, patternSpace);
CGColorSpaceRelease(patternSpace);
CGFloat patternAlpha = 1;
CGContextSetFillPattern(context, pattern, &patternAlpha);
ctxt->setCompositeOperation(op);
CGContextFillRect(context, destRect);
ctxt->restore();
CGPatternRelease(pattern);
#ifndef BUILDING_ON_TIGER
}
#endif
if (imageObserver())
imageObserver()->didDraw(this);
}
}
#endif // PLATFORM(CG)