blob: f3cec6cb140434be6a979d87a800d20896d3e035 [file] [log] [blame]
rwlbuis98021982006-08-23 21:04:08 +00001/*
2 * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
3 * Copyright (C) 2006 Zack Rusin <zack@kde.org>
4 * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "Image.h"
32
eseidel61952852006-12-29 01:57:44 +000033#include "BitmapImage.h"
rwlbuis98021982006-08-23 21:04:08 +000034#include "FloatRect.h"
apc5efe7e2006-09-06 17:17:42 +000035#include "PlatformString.h"
rwlbuis98021982006-08-23 21:04:08 +000036#include "GraphicsContext.h"
larsf6076a42007-01-17 19:04:39 +000037#include "AffineTransform.h"
kevino64ee95c2007-05-28 00:39:59 +000038#include "NotImplemented.h"
rwlbuis98021982006-08-23 21:04:08 +000039
40#include <QPixmap>
41#include <QPainter>
42#include <QImage>
43#include <QImageReader>
staikos309162b2007-01-23 18:18:19 +000044#if QT_VERSION >= 0x040300
larsf6076a42007-01-17 19:04:39 +000045#include <QTransform>
staikos309162b2007-01-23 18:18:19 +000046#endif
rwlbuis98021982006-08-23 21:04:08 +000047
larsf6076a42007-01-17 19:04:39 +000048#include <QDebug>
rwlbuis98021982006-08-23 21:04:08 +000049
50#include <math.h>
51
staikoscdc883d2007-06-22 03:29:41 +000052// This function loads resources into WebKit
53QPixmap loadResourcePixmap(const char*);
rwlbuis98021982006-08-23 21:04:08 +000054
55namespace WebCore {
56
57void FrameData::clear()
58{
59 if (m_frame) {
60 m_frame = 0;
aroben3bad10442006-08-25 16:46:35 +000061 m_duration = 0.0f;
rwlbuis98021982006-08-23 21:04:08 +000062 m_hasAlpha = true;
63 }
64}
65
larsf6076a42007-01-17 19:04:39 +000066
67
rwlbuis98021982006-08-23 21:04:08 +000068// ================================================
69// Image Class
70// ================================================
71
ap3924f3a2006-08-26 09:51:46 +000072Image* Image::loadPlatformResource(const char* name)
rwlbuis98021982006-08-23 21:04:08 +000073{
staikoscdc883d2007-06-22 03:29:41 +000074 BitmapImage* img = new BitmapImage(loadResourcePixmap(name));
rwlbuis98021982006-08-23 21:04:08 +000075 return img;
76}
77
larsf6076a42007-01-17 19:04:39 +000078
79void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
80 const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect)
81{
82 notImplemented();
83}
84
staikoscdc883d2007-06-22 03:29:41 +000085BitmapImage::BitmapImage(const QPixmap &pixmap, ImageObserver *observer)
86 : Image(observer)
87 , m_currentFrame(0)
88 , m_frames(0)
89 , m_frameTimer(0)
90 , m_repetitionCount(0)
91 , m_repetitionsComplete(0)
92 , m_isSolidColor(false)
93 , m_animatingImageType(true)
94 , m_animationFinished(false)
95 , m_allDataReceived(false)
96 , m_haveSize(false)
97 , m_sizeAvailable(false)
98 , m_decodedSize(0)
99{
100 m_pixmap = new QPixmap(pixmap);
101}
larsf6076a42007-01-17 19:04:39 +0000102
103void BitmapImage::initPlatformData()
104{
staikoscdc883d2007-06-22 03:29:41 +0000105 m_pixmap = 0;
larsf6076a42007-01-17 19:04:39 +0000106}
107
108void BitmapImage::invalidatePlatformData()
109{
staikoscdc883d2007-06-22 03:29:41 +0000110 delete m_pixmap;
111 m_pixmap = 0;
larsf6076a42007-01-17 19:04:39 +0000112}
113
rwlbuis98021982006-08-23 21:04:08 +0000114// Drawing Routines
eseidel61952852006-12-29 01:57:44 +0000115void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dst,
larsf6076a42007-01-17 19:04:39 +0000116 const FloatRect& src, CompositeOperator op)
rwlbuis98021982006-08-23 21:04:08 +0000117{
larsf6076a42007-01-17 19:04:39 +0000118 QPixmap* image = nativeImageForCurrentFrame();
119 if (!image)
rwlbuis98021982006-08-23 21:04:08 +0000120 return;
larsf6076a42007-01-17 19:04:39 +0000121
122 if (mayFillWithSolidColor()) {
123 fillWithSolidColor(ctxt, dst, solidColor(), op);
rwlbuis98021982006-08-23 21:04:08 +0000124 return;
larsf6076a42007-01-17 19:04:39 +0000125 }
rwlbuis98021982006-08-23 21:04:08 +0000126
127 IntSize selfSize = size();
128 FloatRect srcRect(src);
129 FloatRect dstRect(dst);
130
131 ctxt->save();
132
133 // Set the compositing operation.
134 ctxt->setCompositeOperation(op);
135
aroben8c8a13e2006-08-24 18:32:46 +0000136 QPainter* painter(ctxt->platformContext());
rwlbuis98021982006-08-23 21:04:08 +0000137
138 // Test using example site at
139 // http://www.meyerweb.com/eric/css/edge/complexspiral/demo.html
zackbfb63512007-01-15 17:19:24 +0000140 painter->drawPixmap(dst, *image, src);
rwlbuis98021982006-08-23 21:04:08 +0000141
142 ctxt->restore();
143
144 startAnimation();
145}
146
larsf6076a42007-01-17 19:04:39 +0000147void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
148 const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect)
rwlbuis98021982006-08-23 21:04:08 +0000149{
larsf6076a42007-01-17 19:04:39 +0000150 QPixmap* framePixmap = nativeImageForCurrentFrame();
151 if (!framePixmap) // If it's too early we won't have an image yet.
rwlbuis98021982006-08-23 21:04:08 +0000152 return;
zackd2a180a2007-01-26 08:25:32 +0000153
larsf6076a42007-01-17 19:04:39 +0000154 QPixmap pixmap = *framePixmap;
155 QRect tr = QRectF(tileRect).toRect();
zackb9f54862007-01-26 08:54:19 +0000156 if (tr.x() || tr.y() || tr.width() != pixmap.width() || tr.height() != pixmap.height()) {
larsf6076a42007-01-17 19:04:39 +0000157 pixmap = pixmap.copy(tr);
zackb9f54862007-01-26 08:54:19 +0000158 }
159
160 if (patternTransform.isIdentity()) {
161 ctxt->save();
162 ctxt->setCompositeOperation(op);
163 QPainter* p = ctxt->platformContext();
164 p->setBrushOrigin(phase);
165 p->drawTiledPixmap(destRect, pixmap);
166 ctxt->restore();
167 } else {
168 QBrush b(pixmap);
169 b.setMatrix(patternTransform);
170 ctxt->save();
171 ctxt->setCompositeOperation(op);
172 QPainter* p = ctxt->platformContext();
173 p->setBrushOrigin(phase);
174 p->fillRect(destRect, b);
175 ctxt->restore();
176 }
eseidel61952852006-12-29 01:57:44 +0000177}
178
179void BitmapImage::checkForSolidColor()
rwlbuis98021982006-08-23 21:04:08 +0000180{
181 // FIXME: It's easy to implement this optimization. Just need to check the RGBA32 buffer to see if it is 1x1.
182 m_isSolidColor = false;
183}
184
zackd2a180a2007-01-26 08:25:32 +0000185QPixmap* BitmapImage::getPixmap() const
186{
staikoscdc883d2007-06-22 03:29:41 +0000187 if (!m_pixmap)
188 return const_cast<BitmapImage*>(this)->frameAtIndex(0);
189 else
190 return m_pixmap;
zackd2a180a2007-01-26 08:25:32 +0000191}
192
rwlbuis98021982006-08-23 21:04:08 +0000193}
194
eseidel61952852006-12-29 01:57:44 +0000195
rwlbuis98021982006-08-23 21:04:08 +0000196// vim: ts=4 sw=4 et