1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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. 13 * 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. 24 */ 25 26 #include "config.h" 27 28 #include "PlatformImage.h" 29 30 #if USE(SKIA) 31 #include "NativeImageSkia.h" 32 #include "PlatformContextSkia.h" 33 #elif USE(CG) 34 #include <CoreGraphics/CGBitmapContext.h> 35 #include <CoreGraphics/CGContext.h> 36 #include <CoreGraphics/CGImage.h> 37 #include <wtf/RetainPtr.h> 38 #else 39 #error "Need to implement for your platform" 40 #endif 41 42 namespace WebCore { 43 44 PlatformImage::PlatformImage() 45 { 46 } 47 48 void PlatformImage::updateFromImage(NativeImagePtr nativeImage) 49 { 50 #if USE(SKIA) 51 // The layer contains an Image. 52 NativeImageSkia* skiaImage = static_cast<NativeImageSkia*>(nativeImage); 53 const SkBitmap* skiaBitmap = skiaImage; 54 55 IntSize bitmapSize(skiaBitmap->width(), skiaBitmap->height()); 56 ASSERT(skiaBitmap); 57 #elif USE(CG) 58 // NativeImagePtr is a CGImageRef on Mac OS X. 59 int width = CGImageGetWidth(nativeImage); 60 int height = CGImageGetHeight(nativeImage); 61 IntSize bitmapSize(width, height); 62 #endif 63 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); 68 m_size = bitmapSize; 69 } 70 71 #if USE(SKIA) 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); 76 #elif USE(CG) 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: 92 break; 93 default: 94 colorSpaceReleaser.adoptCF(CGColorSpaceCreateDeviceRGB()); 95 colorSpace = colorSpaceReleaser.get(); 96 break; 97 } 98 RetainPtr<CGContextRef> tempContext(AdoptCF, CGBitmapContextCreate(m_pixelData.get(), 99 width, height, 8, tempRowBytes, 100 colorSpace, 101 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); 102 CGContextSetBlendMode(tempContext.get(), kCGBlendModeCopy); 103 CGContextDrawImage(tempContext.get(), 104 CGRectMake(0, 0, static_cast<CGFloat>(width), static_cast<CGFloat>(height)), 105 nativeImage); 106 #else 107 #error "Need to implement for your platform." 108 #endif 109 } 110 111 } // namespace WebCore 112