OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2008 The Android Open Source Project | 2 * Copyright 2008 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCGUtils.h" | 8 #include "SkCGUtils.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); | 43 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); |
44 CGDataProviderRelease(data); | 44 CGDataProviderRelease(data); |
45 return imageSrc; | 45 return imageSrc; |
46 } | 46 } |
47 | 47 |
48 class SkImageDecoder_CG : public SkImageDecoder { | 48 class SkImageDecoder_CG : public SkImageDecoder { |
49 protected: | 49 protected: |
50 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode); | 50 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode); |
51 }; | 51 }; |
52 | 52 |
| 53 static void argb_4444_force_opaque(void* row, int count) { |
| 54 uint16_t* row16 = (uint16_t*)row; |
| 55 for (int i = 0; i < count; ++i) { |
| 56 row16[i] |= 0xF000; |
| 57 } |
| 58 } |
| 59 |
| 60 static void argb_8888_force_opaque(void* row, int count) { |
| 61 // can use RGBA or BGRA, they have the same shift for alpha |
| 62 const uint32_t alphaMask = 0xFF << SK_RGBA_A32_SHIFT; |
| 63 uint32_t* row32 = (uint32_t*)row; |
| 64 for (int i = 0; i < count; ++i) { |
| 65 row32[i] |= alphaMask; |
| 66 } |
| 67 } |
| 68 |
| 69 static void alpha_8_force_opaque(void* row, int count) { |
| 70 memset(row, 0xFF, count); |
| 71 } |
| 72 |
| 73 static void force_opaque(SkBitmap* bm) { |
| 74 SkAutoLockPixels alp(*bm); |
| 75 if (!bm->getPixels()) { |
| 76 return; |
| 77 } |
| 78 |
| 79 void (*proc)(void*, int); |
| 80 switch (bm->colorType()) { |
| 81 case kARGB_4444_SkColorType: |
| 82 proc = argb_4444_force_opaque; |
| 83 break; |
| 84 case kRGBA_8888_SkColorType: |
| 85 case kBGRA_8888_SkColorType: |
| 86 proc = argb_8888_force_opaque; |
| 87 break; |
| 88 case kAlpha_8_SkColorType: |
| 89 proc = alpha_8_force_opaque; |
| 90 break; |
| 91 default: |
| 92 return; |
| 93 } |
| 94 |
| 95 char* row = (char*)bm->getPixels(); |
| 96 for (int y = 0; y < bm->height(); ++y) { |
| 97 proc(row, bm->width()); |
| 98 row += bm->rowBytes(); |
| 99 } |
| 100 bm->setAlphaType(kOpaque_SkAlphaType); |
| 101 } |
| 102 |
53 #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast) | 103 #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast) |
54 | 104 |
55 bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { | 105 bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
56 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); | 106 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); |
57 | 107 |
58 if (NULL == imageSrc) { | 108 if (NULL == imageSrc) { |
59 return false; | 109 return false; |
60 } | 110 } |
61 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); | 111 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
62 | 112 |
(...skipping 19 matching lines...) Expand all Loading... |
82 | 132 |
83 if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), im
age)) { | 133 if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), im
age)) { |
84 return false; | 134 return false; |
85 } | 135 } |
86 | 136 |
87 CGImageAlphaInfo info = CGImageGetAlphaInfo(image); | 137 CGImageAlphaInfo info = CGImageGetAlphaInfo(image); |
88 switch (info) { | 138 switch (info) { |
89 case kCGImageAlphaNone: | 139 case kCGImageAlphaNone: |
90 case kCGImageAlphaNoneSkipLast: | 140 case kCGImageAlphaNoneSkipLast: |
91 case kCGImageAlphaNoneSkipFirst: | 141 case kCGImageAlphaNoneSkipFirst: |
92 SkASSERT(SkBitmap::ComputeIsOpaque(*bm)); | 142 // We're opaque, but we can't rely on the data always having 0xFF |
93 bm->setAlphaType(kOpaque_SkAlphaType); | 143 // in the alpha slot (which Skia wants), so we have to ram it in |
| 144 // ourselves. |
| 145 force_opaque(bm); |
94 break; | 146 break; |
95 default: | 147 default: |
96 // we don't know if we're opaque or not, so compute it. | 148 // we don't know if we're opaque or not, so compute it. |
97 if (SkBitmap::ComputeIsOpaque(*bm)) { | 149 if (SkBitmap::ComputeIsOpaque(*bm)) { |
98 bm->setAlphaType(kOpaque_SkAlphaType); | 150 bm->setAlphaType(kOpaque_SkAlphaType); |
99 } | 151 } |
100 } | 152 } |
101 if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) { | 153 if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) { |
102 // CGBitmapContext does not support unpremultiplied, so the image has be
en premultiplied. | 154 // CGBitmapContext does not support unpremultiplied, so the image has be
en premultiplied. |
103 // Convert to unpremultiplied. | 155 // Convert to unpremultiplied. |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 334 |
283 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); | 335 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
284 const CFStringRef name = CGImageSourceGetType(imageSrc); | 336 const CFStringRef name = CGImageSourceGetType(imageSrc); |
285 if (NULL == name) { | 337 if (NULL == name) { |
286 return SkImageDecoder::kUnknown_Format; | 338 return SkImageDecoder::kUnknown_Format; |
287 } | 339 } |
288 return UTType_to_Format(name); | 340 return UTType_to_Format(name); |
289 } | 341 } |
290 | 342 |
291 static SkImageDecoder_FormatReg gFormatReg(get_format_cg); | 343 static SkImageDecoder_FormatReg gFormatReg(get_format_cg); |
OLD | NEW |