| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "skia/ext/skia_utils_ios.h" | 5 #include "skia/ext/skia_utils_ios.h" |
| 6 | 6 |
| 7 #import <ImageIO/ImageIO.h> | 7 #import <ImageIO/ImageIO.h> |
| 8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 bitmap.setIsOpaque(is_opaque); | 25 bitmap.setIsOpaque(is_opaque); |
| 26 void* data = bitmap.getPixels(); | 26 void* data = bitmap.getPixels(); |
| 27 | 27 |
| 28 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple | 28 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
| 29 // recommends these flags for improved CG performance. | 29 // recommends these flags for improved CG performance. |
| 30 #define HAS_ARGB_SHIFTS(a, r, g, b) \ | 30 #define HAS_ARGB_SHIFTS(a, r, g, b) \ |
| 31 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ | 31 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
| 32 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) | 32 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
| 33 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) | 33 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) |
| 34 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 34 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| 35 CGColorSpaceCreateDeviceRGB()); | 35 CGColorSpaceCreateDeviceRGB()); |
| 36 base::mac::ScopedCFTypeRef<CGContextRef> context( | 36 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate( |
| 37 CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4, | 37 data, |
| 38 color_space, | 38 size.width, |
| 39 kCGImageAlphaPremultipliedFirst | | 39 size.height, |
| 40 kCGBitmapByteOrder32Host)); | 40 8, |
| 41 size.width * 4, |
| 42 color_space, |
| 43 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); |
| 41 #else | 44 #else |
| 42 #error We require that Skia's and CoreGraphics's recommended \ | 45 #error We require that Skia's and CoreGraphics's recommended \ |
| 43 image memory layout match. | 46 image memory layout match. |
| 44 #endif | 47 #endif |
| 45 #undef HAS_ARGB_SHIFTS | 48 #undef HAS_ARGB_SHIFTS |
| 46 | 49 |
| 47 DCHECK(context); | 50 DCHECK(context); |
| 48 if (!context) | 51 if (!context) |
| 49 return bitmap; | 52 return bitmap; |
| 50 | 53 |
| 51 CGRect imageRect = CGRectMake(0.0, 0.0, size.width, size.height); | 54 CGRect imageRect = CGRectMake(0.0, 0.0, size.width, size.height); |
| 52 CGContextSetBlendMode(context, kCGBlendModeCopy); | 55 CGContextSetBlendMode(context, kCGBlendModeCopy); |
| 53 CGContextDrawImage(context, imageRect, image); | 56 CGContextDrawImage(context, imageRect, image); |
| 54 | 57 |
| 55 return bitmap; | 58 return bitmap; |
| 56 } | 59 } |
| 57 | 60 |
| 58 UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skia_bitmap, | 61 UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skia_bitmap, |
| 59 CGFloat scale, | 62 CGFloat scale, |
| 60 CGColorSpaceRef color_space) { | 63 CGColorSpaceRef color_space) { |
| 61 if (skia_bitmap.isNull()) | 64 if (skia_bitmap.isNull()) |
| 62 return nil; | 65 return nil; |
| 63 | 66 |
| 64 // First convert SkBitmap to CGImageRef. | 67 // First convert SkBitmap to CGImageRef. |
| 65 base::mac::ScopedCFTypeRef<CGImageRef> cg_image( | 68 base::ScopedCFTypeRef<CGImageRef> cg_image( |
| 66 SkCreateCGImageRefWithColorspace(skia_bitmap, color_space)); | 69 SkCreateCGImageRefWithColorspace(skia_bitmap, color_space)); |
| 67 | 70 |
| 68 // Now convert to UIImage. | 71 // Now convert to UIImage. |
| 69 return [UIImage imageWithCGImage:cg_image.get() | 72 return [UIImage imageWithCGImage:cg_image.get() |
| 70 scale:scale | 73 scale:scale |
| 71 orientation:UIImageOrientationUp]; | 74 orientation:UIImageOrientationUp]; |
| 72 } | 75 } |
| 73 | 76 |
| 74 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) { | 77 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) { |
| 75 DCHECK(image_data); | 78 DCHECK(image_data); |
| 76 base::mac::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary( | 79 base::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary( |
| 77 CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL)); | 80 CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL)); |
| 78 std::vector<SkBitmap> frames; | 81 std::vector<SkBitmap> frames; |
| 79 | 82 |
| 80 base::mac::ScopedCFTypeRef<CGImageSourceRef> source( | 83 base::ScopedCFTypeRef<CGImageSourceRef> source( |
| 81 CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary)); | 84 CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary)); |
| 82 | 85 |
| 83 size_t count = CGImageSourceGetCount(source); | 86 size_t count = CGImageSourceGetCount(source); |
| 84 for (size_t index = 0; index < count; ++index) { | 87 for (size_t index = 0; index < count; ++index) { |
| 85 base::mac::ScopedCFTypeRef<CGImageRef> cg_image( | 88 base::ScopedCFTypeRef<CGImageRef> cg_image( |
| 86 CGImageSourceCreateImageAtIndex(source, index, empty_dictionary)); | 89 CGImageSourceCreateImageAtIndex(source, index, empty_dictionary)); |
| 87 | 90 |
| 88 CGSize size = CGSizeMake(CGImageGetWidth(cg_image), | 91 CGSize size = CGSizeMake(CGImageGetWidth(cg_image), |
| 89 CGImageGetHeight(cg_image)); | 92 CGImageGetHeight(cg_image)); |
| 90 const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false); | 93 const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false); |
| 91 if (!bitmap.empty()) | 94 if (!bitmap.empty()) |
| 92 frames.push_back(bitmap); | 95 frames.push_back(bitmap); |
| 93 } | 96 } |
| 94 | 97 |
| 95 DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size() | 98 DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size() |
| 96 << " frames for " << count << " expected."; | 99 << " frames for " << count << " expected."; |
| 97 return frames; | 100 return frames; |
| 98 } | 101 } |
| 99 | 102 |
| 100 } // namespace gfx | 103 } // namespace gfx |
| OLD | NEW |