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 case kCGImageAlphaFirst:
75 case kCGImageAlphaNoneSkipFirst:
77 case kCGImageAlphaPremultipliedLast:
78 // This is a special case for texImage2D with HTMLCanvasElement input,
79 // in which case image->data() should be null.
80 ASSERT(!image->data());
81 if (!premultiplyAlpha)
82 neededAlphaOp = kAlphaDoUnmultiply;
83 switch (componentsPerPixel) {
85 srcDataFormat = kSourceFormatRA8;
88 srcDataFormat = kSourceFormatRGBA8;
94 case kCGImageAlphaLast:
96 neededAlphaOp = kAlphaDoPremultiply;
97 switch (componentsPerPixel) {
99 srcDataFormat = kSourceFormatA8;
102 srcDataFormat = kSourceFormatRA8;
105 srcDataFormat = kSourceFormatRGBA8;
111 case kCGImageAlphaNoneSkipLast:
112 switch (componentsPerPixel) {
114 srcDataFormat = kSourceFormatRA8;
117 srcDataFormat = kSourceFormatRGBA8;
122 case kCGImageAlphaNone:
123 switch (componentsPerPixel) {
125 srcDataFormat = kSourceFormatR8;
128 srcDataFormat = kSourceFormatRGB8;
137 RetainPtr<CFDataRef> pixelData;
138 pixelData.adoptCF(CGDataProviderCopyData(CGImageGetDataProvider(cgImage)));
141 const UInt8* rgba = CFDataGetBytePtr(pixelData.get());
142 outputVector.resize(width * height * 4);
143 unsigned int srcUnpackAlignment = 0;
144 size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
145 unsigned int padding = bytesPerRow - componentsPerPixel * width;
147 srcUnpackAlignment = padding + 1;
148 while (bytesPerRow % srcUnpackAlignment)
149 ++srcUnpackAlignment;
151 bool rt = packPixels(rgba, srcDataFormat, width, height, srcUnpackAlignment,
152 format, type, neededAlphaOp, outputVector.data());
156 void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, CGContextRef context)
158 if (!imagePixels || imageWidth <= 0 || imageHeight <= 0 || canvasWidth <= 0 || canvasHeight <= 0 || !context)
160 int rowBytes = imageWidth * 4;
161 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithData(0, imagePixels, rowBytes * imageHeight, 0));
162 RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
163 RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(imageWidth, imageHeight, 8, 32, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
164 dataProvider.get(), 0, false, kCGRenderingIntentDefault));
165 // CSS styling may cause the canvas's content to be resized on
166 // the page. Go back to the Canvas to figure out the correct
167 // width and height to draw.
168 CGRect rect = CGRectMake(0, 0, canvasWidth, canvasHeight);
169 // We want to completely overwrite the previous frame's
170 // rendering results.
171 CGContextSaveGState(context);
172 CGContextSetBlendMode(context, kCGBlendModeCopy);
173 CGContextSetInterpolationQuality(context, kCGInterpolationNone);
174 CGContextDrawImage(context, rect, cgImage.get());
175 CGContextRestoreGState(context);
178 } // namespace WebCore
180 #endif // ENABLE(3D_CANVAS)