2 * Copyright (C) 2007 Apple Computer, Kevin Ollivier. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "AffineTransform.h"
30 #include "BitmapImage.h"
31 #include "FloatConversion.h"
32 #include "FloatRect.h"
33 #include "GraphicsContext.h"
34 #include "ImageObserver.h"
35 #include "NotImplemented.h"
36 #include "SharedBuffer.h"
42 #include <wx/bitmap.h>
44 #include <wx/dcmemory.h>
45 #include <wx/dcgraph.h>
46 #include <wx/graphics.h>
48 #include <wx/thread.h>
52 // this is in GraphicsContextWx.cpp
53 int getWxCompositingOperation(CompositeOperator op, bool hasAlpha);
55 bool FrameData::clear(bool clearMetadata)
58 m_haveMetadata = false;
68 // ================================================
70 // ================================================
72 PassRefPtr<Image> Image::loadPlatformResource(const char *name)
74 // FIXME: We need to have some 'placeholder' graphics for things like missing
75 // plugins or broken images.
77 RefPtr<Image> img = BitmapImage::create();
78 RefPtr<SharedBuffer> buffer = SharedBuffer::create(arr.data(), arr.size());
79 img->setData(buffer, true);
83 void BitmapImage::initPlatformData()
90 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dst, const FloatRect& src, ColorSpace styleColorSpace, CompositeOperator op)
92 if (!m_source.initialized())
95 if (mayFillWithSolidColor()) {
96 fillWithSolidColor(ctxt, dst, solidColor(), styleColorSpace, op);
101 wxGCDC* context = (wxGCDC*)ctxt->platformContext();
102 wxGraphicsContext* gc = context->GetGraphicsContext();
103 wxGraphicsBitmap* bitmap = frameAtIndex(m_currentFrame);
105 wxWindowDC* context = ctxt->platformContext();
106 wxBitmap* bitmap = frameAtIndex(m_currentFrame);
110 if (!bitmap) // If it's too early we won't have an image yet.
113 // If we're drawing a sub portion of the image or scaling then create
114 // a pattern transformation on the image and draw the transformed pattern.
115 // Test using example site at http://www.meyerweb.com/eric/css/edge/complexspiral/demo.html
120 // Set the compositing operation.
121 ctxt->setCompositeOperation(op);
124 float scaleX = src.width() / dst.width();
125 float scaleY = src.height() / dst.height();
127 FloatRect adjustedDestRect = dst;
128 FloatSize selfSize = currentFrameSize();
130 if (src.size() != selfSize) {
131 adjustedDestRect.setLocation(FloatPoint(dst.x() - src.x() / scaleX, dst.y() - src.y() / scaleY));
132 adjustedDestRect.setSize(FloatSize(selfSize.width() / scaleX, selfSize.height() / scaleY));
135 gc->Clip(dst.x(), dst.y(), dst.width(), dst.height());
136 #if wxCHECK_VERSION(2,9,0)
137 gc->DrawBitmap(*bitmap, adjustedDestRect.x(), adjustedDestRect.y(), adjustedDestRect.width(), adjustedDestRect.height());
139 gc->DrawGraphicsBitmap(*bitmap, adjustedDestRect.x(), adjustedDestRect.y(), adjustedDestRect.width(), adjustedDestRect.height());
143 IntRect srcIntRect(src);
144 IntRect dstIntRect(dst);
145 bool rescaling = false;
146 if ((dstIntRect.width() != srcIntRect.width()) || (dstIntRect.height() != srcIntRect.height()))
149 wxImage img = bitmap->ConvertToImage();
150 img.Rescale(dstIntRect.width(), dstIntRect.height());
151 bitmap = new wxBitmap(img);
155 ASSERT(bitmap->GetRefData());
156 mydc.SelectObject(*bitmap);
158 context->Blit((wxCoord)dstIntRect.x(),(wxCoord)dstIntRect.y(), (wxCoord)dstIntRect.width(), (wxCoord)dstIntRect.height(), &mydc,
159 (wxCoord)srcIntRect.x(), (wxCoord)srcIntRect.y(), wxCOPY, true);
160 mydc.SelectObject(wxNullBitmap);
162 // NB: delete is causing crashes during page load, but not during the deletion
163 // itself. It occurs later on when a valid bitmap created in frameAtIndex
164 // suddenly becomes invalid after returning. It's possible these errors deal
165 // with reentrancy and threding problems.
176 if (ImageObserver* observer = imageObserver())
177 observer->didDraw(this);
180 void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace, CompositeOperator, const FloatRect& dstRect)
183 wxGCDC* context = (wxGCDC*)ctxt->platformContext();
184 wxGraphicsBitmap* bitmap = nativeImageForCurrentFrame();
186 wxWindowDC* context = ctxt->platformContext();
187 wxBitmap* bitmap = nativeImageForCurrentFrame();
190 if (!bitmap) // If it's too early we won't have an image yet.
194 ctxt->clip(IntRect(dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height()));
199 adjustedX = phase.x() - dstRect.x() *
200 narrowPrecisionToFloat(patternTransform.a());
201 adjustedY = (phase.y() - dstRect.y() *
202 narrowPrecisionToFloat(patternTransform.d()));
205 wxGraphicsContext* gc = context->GetGraphicsContext();
206 gc->ConcatTransform(patternTransform);
209 mydc.SelectObject(*bitmap);
212 float currentW = adjustedX;
213 float currentH = adjustedY;
215 while (currentW <= dstRect.x() + dstRect.width()) {
216 while (currentH <= dstRect.y() + dstRect.height()) {
218 #if wxCHECK_VERSION(2,9,0)
219 gc->DrawBitmap(*bitmap, currentW, currentH, (wxDouble)srcRect.width(), (wxDouble)srcRect.height());
221 gc->DrawGraphicsBitmap(*bitmap, currentW, currentH, (wxDouble)srcRect.width(), (wxDouble)srcRect.height());
224 context->Blit((wxCoord)dstRect.x() + currentW, (wxCoord)dstRect.y() + currentH,
225 (wxCoord)srcRect.width(), (wxCoord)srcRect.height(), &mydc,
226 (wxCoord)srcRect.x(), (wxCoord)srcRect.y(), wxCOPY, true);
228 currentH += srcRect.height();
230 currentW += srcRect.width();
231 currentH = adjustedY;
236 mydc.SelectObject(wxNullBitmap);
239 // NB: delete is causing crashes during page load, but not during the deletion
240 // itself. It occurs later on when a valid bitmap created in frameAtIndex
241 // suddenly becomes invalid after returning. It's possible these errors deal
242 // with reentrancy and threding problems.
247 if (ImageObserver* observer = imageObserver())
248 observer->didDraw(this);
251 void BitmapImage::checkForSolidColor()
253 m_checkedForSolidColor = true;
256 void BitmapImage::invalidatePlatformData()