1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "skia/ext/skia_utils_ios.h"
7 #import <ImageIO/ImageIO.h>
8 #import <UIKit/UIKit.h>
10 #include "base/logging.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
16 SkBitmap CGImageToSkBitmap(CGImageRef image, CGSize size, bool is_opaque) {
21 if (!bitmap.tryAllocN32Pixels(size.width, size.height, is_opaque))
24 void* data = bitmap.getPixels();
26 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
27 // recommends these flags for improved CG performance.
28 #define HAS_ARGB_SHIFTS(a, r, g, b) \
29 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
30 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
31 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
32 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
33 CGColorSpaceCreateDeviceRGB());
34 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
41 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
43 #error We require that Skia's and CoreGraphics's recommended \
44 image memory layout match.
46 #undef HAS_ARGB_SHIFTS
52 CGRect imageRect = CGRectMake(0.0, 0.0, size.width, size.height);
53 CGContextSetBlendMode(context, kCGBlendModeCopy);
54 CGContextDrawImage(context, imageRect, image);
59 UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skia_bitmap,
61 CGColorSpaceRef color_space) {
62 if (skia_bitmap.isNull())
65 // First convert SkBitmap to CGImageRef.
66 base::ScopedCFTypeRef<CGImageRef> cg_image(
67 SkCreateCGImageRefWithColorspace(skia_bitmap, color_space));
69 // Now convert to UIImage.
70 return [UIImage imageWithCGImage:cg_image.get()
72 orientation:UIImageOrientationUp];
75 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) {
77 base::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary(
78 CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL));
79 std::vector<SkBitmap> frames;
81 base::ScopedCFTypeRef<CGImageSourceRef> source(
82 CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary));
84 size_t count = CGImageSourceGetCount(source);
85 for (size_t index = 0; index < count; ++index) {
86 base::ScopedCFTypeRef<CGImageRef> cg_image(
87 CGImageSourceCreateImageAtIndex(source, index, empty_dictionary));
89 CGSize size = CGSizeMake(CGImageGetWidth(cg_image),
90 CGImageGetHeight(cg_image));
91 const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false);
93 frames.push_back(bitmap);
96 DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size()
97 << " frames for " << count << " expected.";