initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / cairo / ImageBufferCairo.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2007 Holger Hans Peter Freyther <zecke@selfish.org>
4  * Copyright (C) 2008, 2009 Dirk Schulze <krit@webkit.org>
5  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "ImageBuffer.h"
31
32 #include "Base64.h"
33 #include "BitmapImage.h"
34 #include "CairoUtilities.h"
35 #include "Color.h"
36 #include "GraphicsContext.h"
37 #include "ImageData.h"
38 #include "MIMETypeRegistry.h"
39 #include "NotImplemented.h"
40 #include "Pattern.h"
41 #include "PlatformContextCairo.h"
42 #include "PlatformString.h"
43 #include "RefPtrCairo.h"
44 #include <cairo.h>
45 #include <wtf/Vector.h>
46
47 using namespace std;
48
49 namespace WebCore {
50
51 ImageBufferData::ImageBufferData(const IntSize& size)
52     : m_surface(0)
53     , m_platformContext(0)
54 {
55 }
56
57 ImageBuffer::ImageBuffer(const IntSize& size, ColorSpace, RenderingMode, bool& success)
58     : m_data(size)
59     , m_size(size)
60 {
61     success = false;  // Make early return mean error.
62     m_data.m_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
63                                                   size.width(),
64                                                   size.height());
65     if (cairo_surface_status(m_data.m_surface) != CAIRO_STATUS_SUCCESS)
66         return;  // create will notice we didn't set m_initialized and fail.
67
68     RefPtr<cairo_t> cr = adoptRef(cairo_create(m_data.m_surface));
69     m_data.m_platformContext.setCr(cr.get());
70     m_context = adoptPtr(new GraphicsContext(&m_data.m_platformContext));
71     success = true;
72 }
73
74 ImageBuffer::~ImageBuffer()
75 {
76     cairo_surface_destroy(m_data.m_surface);
77 }
78
79 size_t ImageBuffer::dataSize() const
80 {
81     return m_size.width() * m_size.height() * 4;
82 }
83
84 GraphicsContext* ImageBuffer::context() const
85 {
86     return m_context.get();
87 }
88
89 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior) const
90 {
91     ASSERT(copyBehavior == CopyBackingStore);
92     // BitmapImage will release the passed in surface on destruction
93     return BitmapImage::create(copyCairoImageSurface(m_data.m_surface).leakRef());
94 }
95
96 void ImageBuffer::clip(GraphicsContext* context, const FloatRect& maskRect) const
97 {
98     context->platformContext()->pushImageMask(m_data.m_surface, maskRect);
99 }
100
101 void ImageBuffer::draw(GraphicsContext* context, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect,
102                        CompositeOperator op , bool useLowQualityScale)
103 {
104     // BitmapImage will release the passed in surface on destruction
105     RefPtr<Image> image = BitmapImage::create(cairo_surface_reference(m_data.m_surface));
106     context->drawImage(image.get(), styleColorSpace, destRect, srcRect, op, useLowQualityScale);
107 }
108
109 void ImageBuffer::drawPattern(GraphicsContext* context, const FloatRect& srcRect, const AffineTransform& patternTransform,
110                               const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect)
111 {
112     // BitmapImage will release the passed in surface on destruction
113     RefPtr<Image> image = BitmapImage::create(cairo_surface_reference(m_data.m_surface));
114     image->drawPattern(context, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
115 }
116
117 void ImageBuffer::platformTransformColorSpace(const Vector<int>& lookUpTable)
118 {
119     ASSERT(cairo_surface_get_type(m_data.m_surface) == CAIRO_SURFACE_TYPE_IMAGE);
120
121     unsigned char* dataSrc = cairo_image_surface_get_data(m_data.m_surface);
122     int stride = cairo_image_surface_get_stride(m_data.m_surface);
123     for (int y = 0; y < m_size.height(); ++y) {
124         unsigned* row = reinterpret_cast<unsigned*>(dataSrc + stride * y);
125         for (int x = 0; x < m_size.width(); x++) {
126             unsigned* pixel = row + x;
127             Color pixelColor = colorFromPremultipliedARGB(*pixel);
128             pixelColor = Color(lookUpTable[pixelColor.red()],
129                                lookUpTable[pixelColor.green()],
130                                lookUpTable[pixelColor.blue()],
131                                pixelColor.alpha());
132             *pixel = premultipliedARGBFromColor(pixelColor);
133         }
134     }
135     cairo_surface_mark_dirty_rectangle (m_data.m_surface, 0, 0, m_size.width(), m_size.height());
136 }
137
138 template <Multiply multiplied>
139 PassRefPtr<ByteArray> getImageData(const IntRect& rect, const ImageBufferData& data, const IntSize& size)
140 {
141     ASSERT(cairo_surface_get_type(data.m_surface) == CAIRO_SURFACE_TYPE_IMAGE);
142
143     RefPtr<ByteArray> result = ByteArray::create(rect.width() * rect.height() * 4);
144     unsigned char* dataSrc = cairo_image_surface_get_data(data.m_surface);
145     unsigned char* dataDst = result->data();
146
147     if (rect.x() < 0 || rect.y() < 0 || (rect.x() + rect.width()) > size.width() || (rect.y() + rect.height()) > size.height())
148         memset(dataDst, 0, result->length());
149
150     int originx = rect.x();
151     int destx = 0;
152     if (originx < 0) {
153         destx = -originx;
154         originx = 0;
155     }
156     int endx = rect.maxX();
157     if (endx > size.width())
158         endx = size.width();
159     int numColumns = endx - originx;
160
161     int originy = rect.y();
162     int desty = 0;
163     if (originy < 0) {
164         desty = -originy;
165         originy = 0;
166     }
167     int endy = rect.maxY();
168     if (endy > size.height())
169         endy = size.height();
170     int numRows = endy - originy;
171
172     int stride = cairo_image_surface_get_stride(data.m_surface);
173     unsigned destBytesPerRow = 4 * rect.width();
174
175     unsigned char* destRows = dataDst + desty * destBytesPerRow + destx * 4;
176     for (int y = 0; y < numRows; ++y) {
177         unsigned* row = reinterpret_cast<unsigned*>(dataSrc + stride * (y + originy));
178         for (int x = 0; x < numColumns; x++) {
179             int basex = x * 4;
180             unsigned* pixel = row + x + originx;
181             Color pixelColor;
182             if (multiplied == Unmultiplied)
183                 pixelColor = colorFromPremultipliedARGB(*pixel);
184             else
185                 pixelColor = Color(*pixel);
186             destRows[basex]     = pixelColor.red();
187             destRows[basex + 1] = pixelColor.green();
188             destRows[basex + 2] = pixelColor.blue();
189             destRows[basex + 3] = pixelColor.alpha();
190         }
191         destRows += destBytesPerRow;
192     }
193
194     return result.release();
195 }
196
197 PassRefPtr<ByteArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect) const
198 {
199     return getImageData<Unmultiplied>(rect, m_data, m_size);
200 }
201
202 PassRefPtr<ByteArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect) const
203 {
204     return getImageData<Premultiplied>(rect, m_data, m_size);
205 }
206
207 template <Multiply multiplied>
208 void putImageData(ByteArray*& source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, ImageBufferData& data, const IntSize& size)
209 {
210     ASSERT(cairo_surface_get_type(data.m_surface) == CAIRO_SURFACE_TYPE_IMAGE);
211
212     unsigned char* dataDst = cairo_image_surface_get_data(data.m_surface);
213
214     ASSERT(sourceRect.width() > 0);
215     ASSERT(sourceRect.height() > 0);
216
217     int originx = sourceRect.x();
218     int destx = destPoint.x() + sourceRect.x();
219     ASSERT(destx >= 0);
220     ASSERT(destx < size.width());
221     ASSERT(originx >= 0);
222     ASSERT(originx <= sourceRect.maxX());
223
224     int endx = destPoint.x() + sourceRect.maxX();
225     ASSERT(endx <= size.width());
226
227     int numColumns = endx - destx;
228
229     int originy = sourceRect.y();
230     int desty = destPoint.y() + sourceRect.y();
231     ASSERT(desty >= 0);
232     ASSERT(desty < size.height());
233     ASSERT(originy >= 0);
234     ASSERT(originy <= sourceRect.maxY());
235
236     int endy = destPoint.y() + sourceRect.maxY();
237     ASSERT(endy <= size.height());
238     int numRows = endy - desty;
239
240     unsigned srcBytesPerRow = 4 * sourceSize.width();
241     int stride = cairo_image_surface_get_stride(data.m_surface);
242
243     unsigned char* srcRows = source->data() + originy * srcBytesPerRow + originx * 4;
244     for (int y = 0; y < numRows; ++y) {
245         unsigned* row = reinterpret_cast<unsigned*>(dataDst + stride * (y + desty));
246         for (int x = 0; x < numColumns; x++) {
247             int basex = x * 4;
248             unsigned* pixel = row + x + destx;
249             Color pixelColor(srcRows[basex],
250                     srcRows[basex + 1],
251                     srcRows[basex + 2],
252                     srcRows[basex + 3]);
253             if (multiplied == Unmultiplied)
254                 *pixel = premultipliedARGBFromColor(pixelColor);
255             else
256                 *pixel = pixelColor.rgb();
257         }
258         srcRows += srcBytesPerRow;
259     }
260     cairo_surface_mark_dirty_rectangle (data.m_surface,
261                                         destx, desty,
262                                         numColumns, numRows);
263 }
264
265 void ImageBuffer::putUnmultipliedImageData(ByteArray* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint)
266 {
267     putImageData<Unmultiplied>(source, sourceSize, sourceRect, destPoint, m_data, m_size);
268 }
269
270 void ImageBuffer::putPremultipliedImageData(ByteArray* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint)
271 {
272     putImageData<Premultiplied>(source, sourceSize, sourceRect, destPoint, m_data, m_size);
273 }
274
275 #if !PLATFORM(GTK)
276 static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned int length)
277 {
278     Vector<char>* in = reinterpret_cast<Vector<char>*>(closure);
279     in->append(data, length);
280     return CAIRO_STATUS_SUCCESS;
281 }
282
283 String ImageBuffer::toDataURL(const String& mimeType, const double*) const
284 {
285     cairo_surface_t* image = cairo_get_target(context()->platformContext()->cr());
286     if (!image)
287         return "data:,";
288
289     String actualMimeType("image/png");
290     if (MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType))
291         actualMimeType = mimeType;
292
293     Vector<char> in;
294     // Only PNG output is supported for now.
295     cairo_surface_write_to_png_stream(image, writeFunction, &in);
296
297     Vector<char> out;
298     base64Encode(in, out);
299
300     return "data:" + actualMimeType + ";base64," + String(out.data(), out.size());
301 }
302 #endif
303
304 } // namespace WebCore