#include "config.h"
#include "Image.h"
#include "AffineTransform.h"
#include "BitmapImage.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageObserver.h"
#include "PlatformString.h"
#include "ShadowBlur.h"
#include "StillImageQt.h"
#include <QCoreApplication>
#include <QDebug>
#include <QImage>
#include <QImageReader>
#include <QPainter>
#include <QPixmap>
#include <QTransform>
#include <math.h>
typedef QHash<QByteArray, QPixmap> WebGraphicHash;
Q_GLOBAL_STATIC(WebGraphicHash, _graphics)
static void earlyClearGraphics()
{
_graphics()->clear();
}
static WebGraphicHash* graphics()
{
WebGraphicHash* hash = _graphics();
if (hash->isEmpty()) {
qAddPostRoutine(earlyClearGraphics);
hash->insert("missingImage", QPixmap(QLatin1String(":webkit/resources/missingImage.png")));
hash->insert("nullPlugin", QPixmap(QLatin1String(":webkit/resources/nullPlugin.png")));
hash->insert("urlIcon", QPixmap(QLatin1String(":webkit/resources/urlIcon.png")));
hash->insert("textAreaResizeCorner", QPixmap(QLatin1String(":webkit/resources/textAreaResizeCorner.png")));
hash->insert("deleteButton", QPixmap(QLatin1String(":webkit/resources/deleteButton.png")));
hash->insert("inputSpeech", QPixmap(QLatin1String(":webkit/resources/inputSpeech.png")));
}
return hash;
}
static QPixmap loadResourcePixmap(const char *name)
{
return graphics()->value(name);
}
namespace WebCore {
bool FrameData::clear(bool clearMetadata)
{
if (clearMetadata)
m_haveMetadata = false;
if (m_frame) {
delete m_frame;
m_frame = 0;
return true;
}
return false;
}
PassRefPtr<Image> Image::loadPlatformResource(const char* name)
{
return StillImage::create(loadResourcePixmap(name));
}
void Image::setPlatformResource(const char* name, const QPixmap& pixmap)
{
WebGraphicHash* h = graphics();
if (pixmap.isNull())
h->remove(name);
else
h->insert(name, pixmap);
}
void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
const FloatPoint& phase, ColorSpace, CompositeOperator op, const FloatRect& destRect)
{
QPixmap* framePixmap = nativeImageForCurrentFrame();
if (!framePixmap) return;
QRectF dr = QRectF(destRect).normalized();
QRect tr = QRectF(tileRect).toRect().normalized();
if (!dr.width() || !dr.height() || !tr.width() || !tr.height())
return;
QPixmap pixmap = *framePixmap;
if (tr.x() || tr.y() || tr.width() != pixmap.width() || tr.height() != pixmap.height())
pixmap = pixmap.copy(tr);
CompositeOperator previousOperator = ctxt->compositeOperation();
ctxt->setCompositeOperation(!pixmap.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
QPainter* p = ctxt->platformContext();
QTransform transform(patternTransform);
if (transform.type() == QTransform::TxScale) {
QRectF tileRectInTargetCoords = (transform * QTransform().translate(phase.x(), phase.y())).mapRect(tr);
bool tileWillBePaintedOnlyOnce = tileRectInTargetCoords.contains(dr);
if (!tileWillBePaintedOnlyOnce) {
QSizeF scaledSize(float(pixmap.width()) * transform.m11(), float(pixmap.height()) * transform.m22());
QPixmap scaledPixmap(scaledSize.toSize());
if (pixmap.hasAlpha())
scaledPixmap.fill(Qt::transparent);
{
QPainter painter(&scaledPixmap);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.setRenderHints(p->renderHints());
painter.drawPixmap(QRect(0, 0, scaledPixmap.width(), scaledPixmap.height()), pixmap);
}
pixmap = scaledPixmap;
transform = QTransform::fromTranslate(transform.dx(), transform.dy());
}
}
transform *= QTransform().translate(phase.x(), phase.y());
transform.translate(tr.x(), tr.y());
QBrush b(pixmap);
b.setTransform(transform);
p->fillRect(dr, b);
ctxt->setCompositeOperation(previousOperator);
if (imageObserver())
imageObserver()->didDraw(this);
}
BitmapImage::BitmapImage(QPixmap* pixmap, ImageObserver* observer)
: Image(observer)
, m_currentFrame(0)
, m_frames(0)
, m_frameTimer(0)
, m_repetitionCount(cAnimationNone)
, m_repetitionCountStatus(Unknown)
, m_repetitionsComplete(0)
, m_decodedSize(0)
, m_frameCount(1)
, m_isSolidColor(false)
, m_checkedForSolidColor(false)
, m_animationFinished(true)
, m_allDataReceived(true)
, m_haveSize(true)
, m_sizeAvailable(true)
, m_haveFrameCount(true)
{
initPlatformData();
int width = pixmap->width();
int height = pixmap->height();
m_decodedSize = width * height * 4;
m_size = IntSize(width, height);
m_frames.grow(1);
m_frames[0].m_frame = pixmap;
m_frames[0].m_hasAlpha = pixmap->hasAlpha();
m_frames[0].m_haveMetadata = true;
checkForSolidColor();
}
void BitmapImage::initPlatformData()
{
}
void BitmapImage::invalidatePlatformData()
{
}
void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dst,
const FloatRect& src, ColorSpace styleColorSpace, CompositeOperator op)
{
QRectF normalizedDst = dst.normalized();
QRectF normalizedSrc = src.normalized();
startAnimation();
if (normalizedSrc.isEmpty() || normalizedDst.isEmpty())
return;
QPixmap* image = nativeImageForCurrentFrame();
if (!image)
return;
if (mayFillWithSolidColor()) {
fillWithSolidColor(ctxt, normalizedDst, solidColor(), styleColorSpace, op);
return;
}
CompositeOperator previousOperator = ctxt->compositeOperation();
ctxt->setCompositeOperation(!image->hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
if (ctxt->hasShadow()) {
ShadowBlur* shadow = ctxt->shadowBlur();
GraphicsContext* shadowContext = shadow->beginShadowLayer(ctxt, normalizedDst);
if (shadowContext) {
QPainter* shadowPainter = shadowContext->platformContext();
shadowPainter->drawPixmap(normalizedDst, *image, normalizedSrc);
shadow->endShadowLayer(ctxt);
}
}
ctxt->platformContext()->drawPixmap(normalizedDst, *image, normalizedSrc);
ctxt->setCompositeOperation(previousOperator);
if (imageObserver())
imageObserver()->didDraw(this);
}
void BitmapImage::checkForSolidColor()
{
m_isSolidColor = false;
m_checkedForSolidColor = true;
if (frameCount() > 1)
return;
QPixmap* framePixmap = frameAtIndex(0);
if (!framePixmap || framePixmap->width() != 1 || framePixmap->height() != 1)
return;
m_isSolidColor = true;
m_solidColor = QColor::fromRgba(framePixmap->toImage().pixel(0, 0));
}
#if OS(WINDOWS)
PassRefPtr<BitmapImage> BitmapImage::create(HBITMAP hBitmap)
{
return BitmapImage::create(new QPixmap(QPixmap::fromWinHBITMAP(hBitmap)));
}
#endif
}