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 * Copyright (C) 2007 Ryan Leavengood <leavengood@gmail.com>
6 * Copyright (C) 2008 Andrea Anzani <andrea.anzani@gmail.com>
7 * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "BitmapImage.h"
37 #include "FloatRect.h"
38 #include "GraphicsContext.h"
39 #include "ImageObserver.h"
40 #include "NotImplemented.h"
41 #include "PlatformString.h"
42 #include "SharedBuffer.h"
43 #include "TransformationMatrix.h"
44 #include <Application.h>
48 // This function loads resources from WebKit
49 Vector<char> loadResourceIntoArray(const char*);
54 bool FrameData::clear(bool clearMetadata)
57 m_haveMetadata = false;
70 WTF::PassRefPtr<Image> Image::loadPlatformResource(const char* name)
72 Vector<char> array = loadResourceIntoArray(name);
73 WTF::PassRefPtr<BitmapImage> image = BitmapImage::create();
74 RefPtr<SharedBuffer> buffer = SharedBuffer::create(array.data(), array.size());
75 image->setData(buffer, true);
80 void BitmapImage::initPlatformData()
84 void BitmapImage::invalidatePlatformData()
89 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dst, const FloatRect& src, ColorSpace styleColorSpace, CompositeOperator op)
91 if (!m_source.initialized())
94 // Spin the animation to the correct frame before we try to draw it, so we
95 // don't draw an old frame and then immediately need to draw a newer one,
96 // causing flicker and wasting CPU.
99 BBitmap* image = nativeImageForCurrentFrame();
100 if (!image || !image->IsValid()) // If the image hasn't fully loaded.
103 if (mayFillWithSolidColor()) {
104 fillWithSolidColor(ctxt, dst, solidColor(), styleColorSpace, op);
109 ctxt->setCompositeOperation(op);
114 // Test using example site at
115 // http://www.meyerweb.com/eric/css/edge/complexspiral/demo.html
116 ctxt->platformContext()->SetDrawingMode(B_OP_ALPHA);
117 ctxt->platformContext()->DrawBitmapAsync(image, srcRect, dstRect);
121 imageObserver()->didDraw(this);
124 void Image::drawPattern(GraphicsContext* context, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace, CompositeOperator op, const FloatRect& dstRect)
126 BBitmap* image = nativeImageForCurrentFrame();
127 if (!image || !image->IsValid()) // If the image hasn't fully loaded.
130 // Figure out if the image has any alpha transparency, we can use faster drawing if not
131 bool hasAlpha = false;
133 uint8* bits = reinterpret_cast<uint8*>(image->Bits());
134 uint32 width = image->Bounds().IntegerWidth() + 1;
135 uint32 height = image->Bounds().IntegerHeight() + 1;
137 uint32 bytesPerRow = image->BytesPerRow();
138 for (uint32 y = 0; y < height && !hasAlpha; y++) {
140 for (uint32 x = 0; x < width && !hasAlpha; x++) {
141 hasAlpha = p[3] < 255;
149 context->platformContext()->SetDrawingMode(B_OP_ALPHA);
151 context->platformContext()->SetDrawingMode(B_OP_COPY);
152 context->clip(enclosingIntRect(dstRect));
153 float currentW = phase.x();
154 BRect bTileRect(tileRect);
155 while (currentW < dstRect.x() + dstRect.width()) {
156 float currentH = phase.y();
157 while (currentH < dstRect.y() + dstRect.height()) {
158 BRect bDstRect(currentW, currentH, currentW + width - 1, currentH + height - 1);
159 context->platformContext()->DrawBitmapAsync(image, bTileRect, bDstRect);
167 imageObserver()->didDraw(this);
170 void BitmapImage::checkForSolidColor()
172 m_isSolidColor = false;
173 m_checkedForSolidColor = true;
175 if (frameCount() > 1)
178 BBitmap* image = getBBitmap();
179 if (!image || !image->Bounds().IsValid()
180 || image->Bounds().IntegerWidth() > 0 || image->Bounds().IntegerHeight() > 0) {
184 m_isSolidColor = true;
185 uint8* bits = reinterpret_cast<uint8*>(image->Bits());
186 m_solidColor = Color(bits[2], bits[1], bits[0], bits[3]);
189 BBitmap* BitmapImage::getBBitmap() const
191 return const_cast<BitmapImage*>(this)->frameAtIndex(0);
194 } // namespace WebCore