2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * 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 AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "PlatformImage.h"
31 #include "NativeImageSkia.h"
32 #include "PlatformContextSkia.h"
34 #include <CoreGraphics/CGBitmapContext.h>
35 #include <CoreGraphics/CGContext.h>
36 #include <CoreGraphics/CGImage.h>
37 #include <wtf/RetainPtr.h>
39 #error "Need to implement for your platform"
44 PlatformImage::PlatformImage()
48 void PlatformImage::updateFromImage(NativeImagePtr nativeImage)
51 // The layer contains an Image.
52 NativeImageSkia* skiaImage = static_cast<NativeImageSkia*>(nativeImage);
54 const SkBitmap& skiaBitmap = skiaImage->bitmap();
56 IntSize bitmapSize(skiaBitmap.width(), skiaBitmap.height());
58 // NativeImagePtr is a CGImageRef on Mac OS X.
59 int width = CGImageGetWidth(nativeImage);
60 int height = CGImageGetHeight(nativeImage);
61 IntSize bitmapSize(width, height);
64 size_t bufferSize = bitmapSize.width() * bitmapSize.height() * 4;
65 if (m_size != bitmapSize) {
66 m_pixelData = adoptArrayPtr(new uint8_t[bufferSize]);
67 memset(m_pixelData.get(), 0, bufferSize);
72 SkAutoLockPixels lock(skiaBitmap);
73 // FIXME: do we need to support more image configurations?
74 ASSERT(skiaBitmap.config()== SkBitmap::kARGB_8888_Config);
75 skiaBitmap.copyPixelsTo(m_pixelData.get(), bufferSize);
77 // FIXME: we should get rid of this temporary copy where possible.
78 int tempRowBytes = width * 4;
79 // Note we do not zero this vector since we are going to
80 // completely overwrite its contents with the image below.
81 // Try to reuse the color space from the image to preserve its colors.
82 // Some images use a color space (such as indexed) unsupported by the bitmap context.
83 RetainPtr<CGColorSpaceRef> colorSpaceReleaser;
84 CGColorSpaceRef colorSpace = CGImageGetColorSpace(nativeImage);
85 CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
86 switch (colorSpaceModel) {
87 case kCGColorSpaceModelMonochrome:
88 case kCGColorSpaceModelRGB:
89 case kCGColorSpaceModelCMYK:
90 case kCGColorSpaceModelLab:
91 case kCGColorSpaceModelDeviceN:
94 colorSpaceReleaser.adoptCF(CGColorSpaceCreateDeviceRGB());
95 colorSpace = colorSpaceReleaser.get();
98 RetainPtr<CGContextRef> tempContext(AdoptCF, CGBitmapContextCreate(m_pixelData.get(),
99 width, height, 8, tempRowBytes,
101 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
102 CGContextSetBlendMode(tempContext.get(), kCGBlendModeCopy);
103 CGContextDrawImage(tempContext.get(),
104 CGRectMake(0, 0, static_cast<CGFloat>(width), static_cast<CGFloat>(height)),
107 #error "Need to implement for your platform."
111 } // namespace WebCore