• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008, Google Inc. All rights reserved.
3  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "config.h"
33 #include "ImageBuffer.h"
34 
35 #include "Base64.h"
36 #include "BitmapImage.h"
37 #include "BitmapImageSingleFrameSkia.h"
38 #include "GraphicsContext.h"
39 #include "ImageData.h"
40 #include "PlatformContextSkia.h"
41 #include "PNGImageEncoder.h"
42 #include "SkiaUtils.h"
43 
44 using namespace std;
45 
46 namespace WebCore {
47 
48 // We pass a technically-uninitialized canvas to the platform context here since
49 // the canvas initialization completes in ImageBuffer::ImageBuffer. But
50 // PlatformContext doesn't actually need to use the object, and this makes all
51 // the ownership easier to manage.
ImageBufferData(const IntSize & size)52 ImageBufferData::ImageBufferData(const IntSize& size)
53     : m_platformContext(0)  // Canvas is set in ImageBuffer constructor.
54 {
55 }
56 
ImageBuffer(const IntSize & size,ImageColorSpace imageColorSpace,bool & success)57 ImageBuffer::ImageBuffer(const IntSize& size, ImageColorSpace imageColorSpace, bool& success)
58     : m_data(size)
59     , m_size(size)
60 {
61     if (!m_data.m_canvas.initialize(size.width(), size.height(), false)) {
62         success = false;
63         return;
64     }
65 
66     m_data.m_platformContext.setCanvas(&m_data.m_canvas);
67     m_context.set(new GraphicsContext(&m_data.m_platformContext));
68 #if PLATFORM(WIN_OS)
69     m_context->platformContext()->setDrawingToImageBuffer(true);
70 #endif
71 
72     // Make the background transparent. It would be nice if this wasn't
73     // required, but the canvas is currently filled with the magic transparency
74     // color. Can we have another way to manage this?
75     m_data.m_canvas.drawARGB(0, 0, 0, 0, SkXfermode::kClear_Mode);
76     success = true;
77 }
78 
~ImageBuffer()79 ImageBuffer::~ImageBuffer()
80 {
81 }
82 
context() const83 GraphicsContext* ImageBuffer::context() const
84 {
85     return m_context.get();
86 }
87 
image() const88 Image* ImageBuffer::image() const
89 {
90     if (!m_image) {
91         // This creates a COPY of the image and will cache that copy. This means
92         // that if subsequent operations take place on the context, neither the
93         // currently-returned image, nor the results of future image() calls,
94         // will contain that operation.
95         //
96         // This seems silly, but is the way the CG port works: image() is
97         // intended to be used only when rendering is "complete."
98         m_image = BitmapImageSingleFrameSkia::create(
99             *m_data.m_platformContext.bitmap());
100     }
101     return m_image.get();
102 }
103 
platformTransformColorSpace(const Vector<int> & lookUpTable)104 void ImageBuffer::platformTransformColorSpace(const Vector<int>& lookUpTable)
105 {
106     const SkBitmap& bitmap = *context()->platformContext()->bitmap();
107     ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config);
108     SkAutoLockPixels bitmapLock(bitmap);
109     for (int y = 0; y < m_size.height(); ++y) {
110         uint32_t* srcRow = bitmap.getAddr32(0, y);
111         for (int x = 0; x < m_size.width(); ++x) {
112             SkColor color = SkPMColorToColor(srcRow[x]);
113             srcRow[x] = SkPreMultiplyARGB(lookUpTable[SkColorGetA(color)],
114                                           lookUpTable[SkColorGetR(color)],
115                                           lookUpTable[SkColorGetG(color)],
116                                           lookUpTable[SkColorGetB(color)]);
117         }
118     }
119 }
120 
getImageData(const IntRect & rect) const121 PassRefPtr<ImageData> ImageBuffer::getImageData(const IntRect& rect) const
122 {
123     ASSERT(context());
124 
125     RefPtr<ImageData> result = ImageData::create(rect.width(), rect.height());
126     unsigned char* data = result->data()->data()->data();
127 
128     if (rect.x() < 0 || rect.y() < 0 ||
129         (rect.x() + rect.width()) > m_size.width() ||
130         (rect.y() + rect.height()) > m_size.height())
131         memset(data, 0, result->data()->length());
132 
133     int originX = rect.x();
134     int destX = 0;
135     if (originX < 0) {
136         destX = -originX;
137         originX = 0;
138     }
139     int endX = rect.x() + rect.width();
140     if (endX > m_size.width())
141         endX = m_size.width();
142     int numColumns = endX - originX;
143 
144     int originY = rect.y();
145     int destY = 0;
146     if (originY < 0) {
147         destY = -originY;
148         originY = 0;
149     }
150     int endY = rect.y() + rect.height();
151     if (endY > m_size.height())
152         endY = m_size.height();
153     int numRows = endY - originY;
154 
155     const SkBitmap& bitmap = *context()->platformContext()->bitmap();
156     ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config);
157     SkAutoLockPixels bitmapLock(bitmap);
158 
159     unsigned destBytesPerRow = 4 * rect.width();
160     unsigned char* destRow = data + destY * destBytesPerRow + destX * 4;
161 
162     for (int y = 0; y < numRows; ++y) {
163         uint32_t* srcRow = bitmap.getAddr32(originX, originY + y);
164         for (int x = 0; x < numColumns; ++x) {
165             SkColor color = SkPMColorToColor(srcRow[x]);
166             unsigned char* destPixel = &destRow[x * 4];
167             destPixel[0] = SkColorGetR(color);
168             destPixel[1] = SkColorGetG(color);
169             destPixel[2] = SkColorGetB(color);
170             destPixel[3] = SkColorGetA(color);
171         }
172         destRow += destBytesPerRow;
173     }
174 
175     return result;
176 }
177 
putImageData(ImageData * source,const IntRect & sourceRect,const IntPoint & destPoint)178 void ImageBuffer::putImageData(ImageData* source, const IntRect& sourceRect,
179                                const IntPoint& destPoint)
180 {
181     ASSERT(sourceRect.width() > 0);
182     ASSERT(sourceRect.height() > 0);
183 
184     int originX = sourceRect.x();
185     int destX = destPoint.x() + sourceRect.x();
186     ASSERT(destX >= 0);
187     ASSERT(destX < m_size.width());
188     ASSERT(originX >= 0);
189     ASSERT(originX < sourceRect.right());
190 
191     int endX = destPoint.x() + sourceRect.right();
192     ASSERT(endX <= m_size.width());
193 
194     int numColumns = endX - destX;
195 
196     int originY = sourceRect.y();
197     int destY = destPoint.y() + sourceRect.y();
198     ASSERT(destY >= 0);
199     ASSERT(destY < m_size.height());
200     ASSERT(originY >= 0);
201     ASSERT(originY < sourceRect.bottom());
202 
203     int endY = destPoint.y() + sourceRect.bottom();
204     ASSERT(endY <= m_size.height());
205     int numRows = endY - destY;
206 
207     const SkBitmap& bitmap = *context()->platformContext()->bitmap();
208     ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config);
209     SkAutoLockPixels bitmapLock(bitmap);
210 
211     unsigned srcBytesPerRow = 4 * source->width();
212 
213     const unsigned char* srcRow = source->data()->data()->data() + originY * srcBytesPerRow + originX * 4;
214 
215     for (int y = 0; y < numRows; ++y) {
216         uint32_t* destRow = bitmap.getAddr32(destX, destY + y);
217         for (int x = 0; x < numColumns; ++x) {
218             const unsigned char* srcPixel = &srcRow[x * 4];
219             destRow[x] = SkPreMultiplyARGB(srcPixel[3], srcPixel[0],
220                                            srcPixel[1], srcPixel[2]);
221         }
222         srcRow += srcBytesPerRow;
223     }
224 }
225 
toDataURL(const String &) const226 String ImageBuffer::toDataURL(const String&) const
227 {
228     // Encode the image into a vector.
229     Vector<unsigned char> pngEncodedData;
230     PNGImageEncoder::encode(*context()->platformContext()->bitmap(), &pngEncodedData);
231 
232     // Convert it into base64.
233     Vector<char> base64EncodedData;
234     base64Encode(*reinterpret_cast<Vector<char>*>(&pngEncodedData), base64EncodedData);
235     // Append with a \0 so that it's a valid string.
236     base64EncodedData.append('\0');
237 
238     // And the resulting string.
239     return String::format("data:image/png;base64,%s", base64EncodedData.data());
240 }
241 
242 } // namespace WebCore
243