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/ios/ios_util.h" | |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/macros.h" | |
12 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | 14 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
13 | 15 |
16 namespace { | |
17 | |
18 const uint8 kICOHeaderMagic[4] = {0x00, 0x00, 0x01, 0x00}; | |
19 | |
20 // Returns whether the data encodes an ico image. | |
21 bool EncodesIcoImage(NSData* image_data) { | |
22 if (image_data.length < arraysize(kICOHeaderMagic)) | |
23 return false; | |
24 return memcmp(kICOHeaderMagic, image_data.bytes, | |
25 arraysize(kICOHeaderMagic)) == 0; | |
26 } | |
27 | |
28 } // namespace | |
29 | |
14 namespace gfx { | 30 namespace gfx { |
15 | 31 |
16 SkBitmap CGImageToSkBitmap(CGImageRef image, CGSize size, bool is_opaque) { | 32 SkBitmap CGImageToSkBitmap(CGImageRef image, CGSize size, bool is_opaque) { |
17 SkBitmap bitmap; | 33 SkBitmap bitmap; |
18 if (!image) | 34 if (!image) |
19 return bitmap; | 35 return bitmap; |
20 | 36 |
21 if (!bitmap.tryAllocN32Pixels(size.width, size.height, is_opaque)) | 37 if (!bitmap.tryAllocN32Pixels(size.width, size.height, is_opaque)) |
22 return bitmap; | 38 return bitmap; |
23 | 39 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 SkCreateCGImageRefWithColorspace(skia_bitmap, color_space)); | 83 SkCreateCGImageRefWithColorspace(skia_bitmap, color_space)); |
68 | 84 |
69 // Now convert to UIImage. | 85 // Now convert to UIImage. |
70 return [UIImage imageWithCGImage:cg_image.get() | 86 return [UIImage imageWithCGImage:cg_image.get() |
71 scale:scale | 87 scale:scale |
72 orientation:UIImageOrientationUp]; | 88 orientation:UIImageOrientationUp]; |
73 } | 89 } |
74 | 90 |
75 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) { | 91 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) { |
76 DCHECK(image_data); | 92 DCHECK(image_data); |
93 | |
94 // On iOS 8.1.1 |CGContextDrawImage| crashes when processing images included | |
95 // in .ico files that are larger then 88x88 (http://crbug.com/435068). | |
Justin Novosad
2015/02/19 16:10:44
then -> than
sdefresne
2015/02/19 16:46:02
Done.
| |
96 bool skip_images_larger_than_88x88 = | |
97 base::ios::IsRunningOnOrLater(8, 1, 1) && EncodesIcoImage(image_data); | |
98 | |
77 base::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary( | 99 base::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary( |
78 CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL)); | 100 CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL)); |
79 std::vector<SkBitmap> frames; | 101 std::vector<SkBitmap> frames; |
80 | 102 |
81 base::ScopedCFTypeRef<CGImageSourceRef> source( | 103 base::ScopedCFTypeRef<CGImageSourceRef> source( |
82 CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary)); | 104 CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary)); |
83 | 105 |
84 size_t count = CGImageSourceGetCount(source); | 106 size_t count = CGImageSourceGetCount(source); |
85 for (size_t index = 0; index < count; ++index) { | 107 for (size_t index = 0; index < count; ++index) { |
86 base::ScopedCFTypeRef<CGImageRef> cg_image( | 108 base::ScopedCFTypeRef<CGImageRef> cg_image( |
87 CGImageSourceCreateImageAtIndex(source, index, empty_dictionary)); | 109 CGImageSourceCreateImageAtIndex(source, index, empty_dictionary)); |
88 | 110 |
89 CGSize size = CGSizeMake(CGImageGetWidth(cg_image), | 111 CGSize size = CGSizeMake(CGImageGetWidth(cg_image), |
90 CGImageGetHeight(cg_image)); | 112 CGImageGetHeight(cg_image)); |
91 const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false); | 113 if (!skip_images_larger_than_88x88 || |
92 if (!bitmap.empty()) | 114 (size.width < 88 && size.height < 88)) { |
Justin Novosad
2015/02/19 16:10:44
Shouldn't the conditions be '<=' instead of '<'?
sdefresne
2015/02/19 16:46:02
That's correct. Changed to use <=.
| |
93 frames.push_back(bitmap); | 115 const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false); |
116 if (!bitmap.empty()) | |
117 frames.push_back(bitmap); | |
118 } | |
94 } | 119 } |
95 | 120 |
96 DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size() | 121 DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size() |
97 << " frames for " << count << " expected."; | 122 << " frames for " << count << " expected."; |
98 return frames; | 123 return frames; |
99 } | 124 } |
100 | 125 |
101 } // namespace gfx | 126 } // namespace gfx |
OLD | NEW |