initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / wx / ImageWx.cpp
1 /*
2  * Copyright (C) 2007 Apple Computer, Kevin Ollivier.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "Image.h"
28
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"
37
38 #include <math.h>
39 #include <stdio.h>
40
41 #include <wx/defs.h>
42 #include <wx/bitmap.h>
43 #include <wx/dc.h>
44 #include <wx/dcmemory.h>
45 #include <wx/dcgraph.h>
46 #include <wx/graphics.h>
47 #include <wx/image.h>
48 #include <wx/thread.h>
49
50 namespace WebCore {
51
52 // this is in GraphicsContextWx.cpp
53 int getWxCompositingOperation(CompositeOperator op, bool hasAlpha);
54
55 bool FrameData::clear(bool clearMetadata)
56 {
57     if (clearMetadata)
58         m_haveMetadata = false;
59
60     if (m_frame) {
61         delete m_frame;
62         m_frame = 0;
63         return true;
64     }
65     return false;
66 }
67
68 // ================================================
69 // Image Class
70 // ================================================
71
72 PassRefPtr<Image> Image::loadPlatformResource(const char *name)
73 {
74     // FIXME: We need to have some 'placeholder' graphics for things like missing
75     // plugins or broken images.
76     Vector<char> arr;
77     RefPtr<Image> img = BitmapImage::create();
78     RefPtr<SharedBuffer> buffer = SharedBuffer::create(arr.data(), arr.size());
79     img->setData(buffer, true);
80     return img.release();
81 }
82
83 void BitmapImage::initPlatformData()
84 {
85     // FIXME: NYI
86 }
87
88 // Drawing Routines
89
90 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dst, const FloatRect& src, ColorSpace styleColorSpace, CompositeOperator op)
91 {
92     if (!m_source.initialized())
93         return;
94
95     if (mayFillWithSolidColor()) {
96         fillWithSolidColor(ctxt, dst, solidColor(), styleColorSpace, op);
97         return;
98     }
99
100 #if USE(WXGC)
101     wxGCDC* context = (wxGCDC*)ctxt->platformContext();
102     wxGraphicsContext* gc = context->GetGraphicsContext();
103     wxGraphicsBitmap* bitmap = frameAtIndex(m_currentFrame);
104 #else
105     wxWindowDC* context = ctxt->platformContext();
106     wxBitmap* bitmap = frameAtIndex(m_currentFrame);
107 #endif
108
109     startAnimation();
110     if (!bitmap) // If it's too early we won't have an image yet.
111         return;
112     
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
116     // FIXME: NYI
117    
118     ctxt->save();
119
120     // Set the compositing operation.
121     ctxt->setCompositeOperation(op);
122     
123 #if USE(WXGC)
124     float scaleX = src.width() / dst.width();
125     float scaleY = src.height() / dst.height();
126
127     FloatRect adjustedDestRect = dst;
128     FloatSize selfSize = currentFrameSize();
129     
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));
133     }
134
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());
138 #else
139     gc->DrawGraphicsBitmap(*bitmap, adjustedDestRect.x(), adjustedDestRect.y(), adjustedDestRect.width(), adjustedDestRect.height());
140 #endif
141
142 #else // USE(WXGC)
143     IntRect srcIntRect(src);
144     IntRect dstIntRect(dst);
145     bool rescaling = false;
146     if ((dstIntRect.width() != srcIntRect.width()) || (dstIntRect.height() != srcIntRect.height()))
147     {
148         rescaling = true;
149         wxImage img = bitmap->ConvertToImage();
150         img.Rescale(dstIntRect.width(), dstIntRect.height());
151         bitmap = new wxBitmap(img);
152     } 
153     
154     wxMemoryDC mydc; 
155     ASSERT(bitmap->GetRefData());
156     mydc.SelectObject(*bitmap); 
157     
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);
161     
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.
166     //delete bitmap;
167     if (rescaling)
168     {
169         delete bitmap;
170         bitmap = NULL;
171     }
172 #endif
173
174     ctxt->restore();
175
176     if (ImageObserver* observer = imageObserver())
177         observer->didDraw(this);
178 }
179
180 void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace, CompositeOperator, const FloatRect& dstRect)
181 {
182 #if USE(WXGC)
183     wxGCDC* context = (wxGCDC*)ctxt->platformContext();
184     wxGraphicsBitmap* bitmap = nativeImageForCurrentFrame();
185 #else
186     wxWindowDC* context = ctxt->platformContext();
187     wxBitmap* bitmap = nativeImageForCurrentFrame();
188 #endif
189
190     if (!bitmap) // If it's too early we won't have an image yet.
191         return;
192
193     ctxt->save();
194     ctxt->clip(IntRect(dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height()));
195     
196     float adjustedX = 0;
197     float adjustedY = 0;
198     
199     adjustedX = phase.x() - dstRect.x() *
200                       narrowPrecisionToFloat(patternTransform.a());
201     adjustedY = (phase.y() - dstRect.y() *
202                       narrowPrecisionToFloat(patternTransform.d()));
203     
204 #if USE(WXGC)
205     wxGraphicsContext* gc = context->GetGraphicsContext();
206     gc->ConcatTransform(patternTransform);
207 #else
208     wxMemoryDC mydc;
209     mydc.SelectObject(*bitmap);
210 #endif
211
212     float currentW = adjustedX;
213     float currentH = adjustedY;
214
215     while (currentW <= dstRect.x() + dstRect.width()) {
216         while (currentH <= dstRect.y() + dstRect.height()) {
217 #if USE(WXGC)
218 #if wxCHECK_VERSION(2,9,0)
219             gc->DrawBitmap(*bitmap, currentW, currentH, (wxDouble)srcRect.width(), (wxDouble)srcRect.height());
220 #else
221             gc->DrawGraphicsBitmap(*bitmap, currentW, currentH, (wxDouble)srcRect.width(), (wxDouble)srcRect.height());
222 #endif
223 #else
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); 
227 #endif
228             currentH += srcRect.height();
229         }
230         currentW += srcRect.width();
231         currentH = adjustedY;
232     }
233     ctxt->restore();
234
235 #if !USE(WXGC)
236     mydc.SelectObject(wxNullBitmap);
237 #endif    
238     
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.
243     //delete bitmap;
244
245     startAnimation();
246
247     if (ImageObserver* observer = imageObserver())
248         observer->didDraw(this);
249 }
250
251 void BitmapImage::checkForSolidColor()
252 {
253     m_checkedForSolidColor = true;
254 }
255
256 void BitmapImage::invalidatePlatformData()
257 {
258
259 }
260
261 }