2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "GraphicsContext3D.h"
35 #include <CoreGraphics/CGBitmapContext.h>
36 #include <CoreGraphics/CGContext.h>
37 #include <CoreGraphics/CGDataProvider.h>
38 #include <CoreGraphics/CGImage.h>
40 #include <wtf/RetainPtr.h>
44 bool GraphicsContext3D::getImageData(Image* image,
47 bool premultiplyAlpha,
48 Vector<uint8_t>& outputVector)
53 RetainPtr<CGImageRef> decodedImage;
55 ImageSource decoder(false);
56 decoder.setData(image->data(), true);
57 if (!decoder.frameCount())
59 decodedImage = decoder.createFrameAtIndex(0);
60 cgImage = decodedImage.get();
62 cgImage = image->nativeImageForCurrentFrame();
65 size_t width = CGImageGetWidth(cgImage);
66 size_t height = CGImageGetHeight(cgImage);
67 if (!width || !height || CGImageGetBitsPerComponent(cgImage) != 8)
69 size_t componentsPerPixel = CGImageGetBitsPerPixel(cgImage) / 8;
70 SourceDataFormat srcDataFormat = kSourceFormatRGBA8;
71 AlphaOp neededAlphaOp = kAlphaDoNothing;
72 switch (CGImageGetAlphaInfo(cgImage)) {
73 case kCGImageAlphaPremultipliedFirst:
74 // This path is only accessible for MacOS earlier than 10.6.4.
75 // This is a special case for texImage2D with HTMLCanvasElement input,
76 // in which case image->data() should be null.
77 ASSERT(!image->data());
78 if (!premultiplyAlpha)
79 neededAlphaOp = kAlphaDoUnmultiply;
80 switch (componentsPerPixel) {
82 srcDataFormat = kSourceFormatAR8;
85 srcDataFormat = kSourceFormatARGB8;
91 case kCGImageAlphaFirst:
92 // This path is only accessible for MacOS earlier than 10.6.4.
94 neededAlphaOp = kAlphaDoPremultiply;
95 switch (componentsPerPixel) {
97 srcDataFormat = kSourceFormatA8;
100 srcDataFormat = kSourceFormatAR8;
103 srcDataFormat = kSourceFormatARGB8;
109 case kCGImageAlphaNoneSkipFirst:
110 // This path is only accessible for MacOS earlier than 10.6.4.
111 switch (componentsPerPixel) {
113 srcDataFormat = kSourceFormatAR8;
116 srcDataFormat = kSourceFormatARGB8;
122 case kCGImageAlphaPremultipliedLast:
123 // This is a special case for texImage2D with HTMLCanvasElement input,
124 // in which case image->data() should be null.
125 ASSERT(!image->data());
126 if (!premultiplyAlpha)
127 neededAlphaOp = kAlphaDoUnmultiply;
128 switch (componentsPerPixel) {
130 srcDataFormat = kSourceFormatRA8;
133 srcDataFormat = kSourceFormatRGBA8;
139 case kCGImageAlphaLast:
140 if (premultiplyAlpha)
141 neededAlphaOp = kAlphaDoPremultiply;
142 switch (componentsPerPixel) {
144 srcDataFormat = kSourceFormatA8;
147 srcDataFormat = kSourceFormatRA8;
150 srcDataFormat = kSourceFormatRGBA8;
156 case kCGImageAlphaNoneSkipLast:
157 switch (componentsPerPixel) {
159 srcDataFormat = kSourceFormatRA8;
162 srcDataFormat = kSourceFormatRGBA8;
168 case kCGImageAlphaNone:
169 switch (componentsPerPixel) {
171 srcDataFormat = kSourceFormatR8;
174 srcDataFormat = kSourceFormatRGB8;
183 RetainPtr<CFDataRef> pixelData;
184 pixelData.adoptCF(CGDataProviderCopyData(CGImageGetDataProvider(cgImage)));
187 const UInt8* rgba = CFDataGetBytePtr(pixelData.get());
188 outputVector.resize(width * height * 4);
189 unsigned int srcUnpackAlignment = 0;
190 size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
191 unsigned int padding = bytesPerRow - componentsPerPixel * width;
193 srcUnpackAlignment = padding + 1;
194 while (bytesPerRow % srcUnpackAlignment)
195 ++srcUnpackAlignment;
197 bool rt = packPixels(rgba, srcDataFormat, width, height, srcUnpackAlignment,
198 format, type, neededAlphaOp, outputVector.data());
202 void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, CGContextRef context)
204 if (!imagePixels || imageWidth <= 0 || imageHeight <= 0 || canvasWidth <= 0 || canvasHeight <= 0 || !context)
206 int rowBytes = imageWidth * 4;
207 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithData(0, imagePixels, rowBytes * imageHeight, 0));
208 RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
209 RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(imageWidth, imageHeight, 8, 32, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
210 dataProvider.get(), 0, false, kCGRenderingIntentDefault));
211 // CSS styling may cause the canvas's content to be resized on
212 // the page. Go back to the Canvas to figure out the correct
213 // width and height to draw.
214 CGRect rect = CGRectMake(0, 0, canvasWidth, canvasHeight);
215 // We want to completely overwrite the previous frame's
216 // rendering results.
217 CGContextSaveGState(context);
218 CGContextSetBlendMode(context, kCGBlendModeCopy);
219 CGContextSetInterpolationQuality(context, kCGInterpolationNone);
220 CGContextDrawImage(context, rect, cgImage.get());
221 CGContextRestoreGState(context);
224 } // namespace WebCore
226 #endif // ENABLE(3D_CANVAS)