GraphicsContext3DCG.cpp [plain text]
#include "config.h"
#if ENABLE(3D_CANVAS)
#include "GraphicsContext3D.h"
#include "Image.h"
#include <CoreGraphics/CGBitmapContext.h>
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGImage.h>
namespace WebCore {
bool GraphicsContext3D::getImageData(Image* image,
unsigned int format,
unsigned int type,
bool premultiplyAlpha,
Vector<uint8_t>& outputVector)
{
if (!image)
return false;
CGImageRef cgImage = image->nativeImageForCurrentFrame();
if (!cgImage)
return false;
int width = CGImageGetWidth(cgImage);
int height = CGImageGetHeight(cgImage);
int tempRowBytes = width * 4;
Vector<uint8_t> tempVector;
tempVector.resize(height * tempRowBytes);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
bool releaseColorSpace = false;
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
switch (colorSpaceModel) {
case kCGColorSpaceModelMonochrome:
case kCGColorSpaceModelRGB:
case kCGColorSpaceModelCMYK:
case kCGColorSpaceModelLab:
case kCGColorSpaceModelDeviceN:
break;
default:
colorSpace = CGColorSpaceCreateDeviceRGB();
releaseColorSpace = true;
break;
}
CGContextRef tempContext = CGBitmapContextCreate(tempVector.data(),
width, height, 8, tempRowBytes,
colorSpace,
kCGImageAlphaPremultipliedLast);
if (releaseColorSpace)
CGColorSpaceRelease(colorSpace);
if (!tempContext)
return false;
CGContextSetBlendMode(tempContext, kCGBlendModeCopy);
CGContextDrawImage(tempContext,
CGRectMake(0, 0, static_cast<CGFloat>(width), static_cast<CGFloat>(height)),
cgImage);
CGContextRelease(tempContext);
unsigned long componentsPerPixel, bytesPerComponent;
if (!computeFormatAndTypeParameters(format, type, &componentsPerPixel, &bytesPerComponent))
return false;
int rowBytes = width * componentsPerPixel * bytesPerComponent;
outputVector.resize(height * rowBytes);
CGImageAlphaInfo info = CGImageGetAlphaInfo(cgImage);
bool hasAlphaChannel = (info != kCGImageAlphaNone
&& info != kCGImageAlphaNoneSkipLast
&& info != kCGImageAlphaNoneSkipFirst);
AlphaOp neededAlphaOp = kAlphaDoNothing;
if (!premultiplyAlpha && hasAlphaChannel)
neededAlphaOp = kAlphaDoUnmultiply;
return packPixels(tempVector.data(), kSourceFormatRGBA8, width, height, 0,
format, type, neededAlphaOp, outputVector.data());
}
}
#endif // ENABLE(3D_CANVAS)