#include "config.h"
#include "PlatformImage.h"
#if USE(SKIA)
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#elif USE(CG)
#include <CoreGraphics/CGBitmapContext.h>
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGImage.h>
#include <wtf/RetainPtr.h>
#else
#error "Need to implement for your platform"
#endif
namespace WebCore {
PlatformImage::PlatformImage()
{
}
void PlatformImage::updateFromImage(NativeImagePtr nativeImage)
{
#if USE(SKIA)
NativeImageSkia* skiaImage = static_cast<NativeImageSkia*>(nativeImage);
ASSERT(skiaImage);
const SkBitmap& skiaBitmap = skiaImage->bitmap();
IntSize bitmapSize(skiaBitmap.width(), skiaBitmap.height());
#elif USE(CG)
int width = CGImageGetWidth(nativeImage);
int height = CGImageGetHeight(nativeImage);
IntSize bitmapSize(width, height);
#endif
size_t bufferSize = bitmapSize.width() * bitmapSize.height() * 4;
if (m_size != bitmapSize) {
m_pixelData = adoptArrayPtr(new uint8_t[bufferSize]);
memset(m_pixelData.get(), 0, bufferSize);
m_size = bitmapSize;
}
#if USE(SKIA)
SkAutoLockPixels lock(skiaBitmap);
ASSERT(skiaBitmap.config()== SkBitmap::kARGB_8888_Config);
skiaBitmap.copyPixelsTo(m_pixelData.get(), bufferSize);
#elif USE(CG)
int tempRowBytes = width * 4;
RetainPtr<CGColorSpaceRef> colorSpaceReleaser;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(nativeImage);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
switch (colorSpaceModel) {
case kCGColorSpaceModelMonochrome:
case kCGColorSpaceModelRGB:
case kCGColorSpaceModelCMYK:
case kCGColorSpaceModelLab:
case kCGColorSpaceModelDeviceN:
break;
default:
colorSpaceReleaser.adoptCF(CGColorSpaceCreateDeviceRGB());
colorSpace = colorSpaceReleaser.get();
break;
}
RetainPtr<CGContextRef> tempContext(AdoptCF, CGBitmapContextCreate(m_pixelData.get(),
width, height, 8, tempRowBytes,
colorSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
CGContextSetBlendMode(tempContext.get(), kCGBlendModeCopy);
CGContextDrawImage(tempContext.get(),
CGRectMake(0, 0, static_cast<CGFloat>(width), static_cast<CGFloat>(height)),
nativeImage);
#else
#error "Need to implement for your platform."
#endif
}
}