OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
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 "SkData.h" | 8 #include "SkData.h" |
9 #include "SkDecodingImageGenerator.h" | 9 #include "SkDecodingImageGenerator.h" |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
11 #include "SkImageInfo.h" | 11 #include "SkImageInfo.h" |
12 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkStream.h" | 14 #include "SkStream.h" |
15 #include "SkUtils.h" | 15 #include "SkUtils.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 /** | 18 /** |
19 * Special allocator used by getPixels(). Uses preallocated memory | 19 * Special allocator used by getPixels(). Uses preallocated memory |
20 * provided. | 20 * provided. |
21 */ | 21 */ |
22 class TargetAllocator : public SkBitmap::Allocator { | 22 class TargetAllocator : public SkBitmap::Allocator { |
23 public: | 23 public: |
24 TargetAllocator(void* target, | 24 TargetAllocator(void* target, |
25 size_t rowBytes, | 25 size_t rowBytes, |
26 int width, | 26 int width, |
27 int height, | 27 int height, |
28 SkBitmap::Config config) | 28 SkColorType colorType) |
29 : fTarget(target) | 29 : fTarget(target) |
30 , fRowBytes(rowBytes) | 30 , fRowBytes(rowBytes) |
31 , fWidth(width) | 31 , fWidth(width) |
32 , fHeight(height) | 32 , fHeight(height) |
33 , fConfig(config) { } | 33 , fColorType(colorType) { } |
34 | 34 |
35 bool isReady() { return (fTarget != NULL); } | 35 bool isReady() { return (fTarget != NULL); } |
36 | 36 |
37 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { | 37 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { |
38 if ((NULL == fTarget) | 38 if ((NULL == fTarget) |
39 || (fConfig != bm->config()) | 39 || (fColorType != bm->colorType()) |
40 || (fWidth != bm->width()) | 40 || (fWidth != bm->width()) |
41 || (fHeight != bm->height()) | 41 || (fHeight != bm->height()) |
42 || (ct != NULL)) { | 42 || (ct != NULL)) { |
43 // Call default allocator. | 43 // Call default allocator. |
44 return bm->allocPixels(NULL, ct); | 44 return bm->allocPixels(NULL, ct); |
45 } | 45 } |
| 46 |
| 47 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight, fColorType, |
| 48 bm->alphaType()); |
46 // make sure fRowBytes is correct. | 49 // make sure fRowBytes is correct. |
47 bm->setConfig(fConfig, fWidth, fHeight, fRowBytes, bm->alphaType()); | 50 bm->installPixels(info, fTarget, fRowBytes, NULL, NULL); |
48 // TODO(halcanary): verify that all callers of this function | |
49 // will respect new RowBytes. Will be moot once rowbytes belongs | |
50 // to PixelRef. | |
51 bm->setPixels(fTarget, NULL); | |
52 fTarget = NULL; // never alloc same pixels twice! | 51 fTarget = NULL; // never alloc same pixels twice! |
53 return true; | 52 return true; |
54 } | 53 } |
55 | 54 |
56 private: | 55 private: |
57 void* fTarget; // Block of memory to be supplied as pixel memory | 56 void* fTarget; // Block of memory to be supplied as pixel memory |
58 // in allocPixelRef. Must be large enough to hold | 57 // in allocPixelRef. Must be large enough to hold |
59 // a bitmap described by fWidth, fHeight, and | 58 // a bitmap described by fWidth, fHeight, and |
60 // fRowBytes. | 59 // fRowBytes. |
61 size_t fRowBytes; // rowbytes for the destination bitmap | 60 size_t fRowBytes; // rowbytes for the destination bitmap |
62 int fWidth; // Along with fHeight and fConfig, the information | 61 int fWidth; // Along with fHeight and fConfig, the information |
63 int fHeight; // about the bitmap whose pixels this allocator is | 62 int fHeight; // about the bitmap whose pixels this allocator is |
64 // expected to allocate. If they do not match the | 63 // expected to allocate. If they do not match the |
65 // bitmap passed to allocPixelRef, it is assumed | 64 // bitmap passed to allocPixelRef, it is assumed |
66 // that the bitmap will be copied to a bitmap with | 65 // that the bitmap will be copied to a bitmap with |
67 // the correct info using this allocator, so the | 66 // the correct info using this allocator, so the |
68 // default allocator will be used instead of | 67 // default allocator will be used instead of |
69 // fTarget. | 68 // fTarget. |
70 SkBitmap::Config fConfig; | 69 SkColorType fColorType; |
71 typedef SkBitmap::Allocator INHERITED; | 70 typedef SkBitmap::Allocator INHERITED; |
72 }; | 71 }; |
73 | 72 |
74 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h | 73 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h |
75 #ifdef SK_DEBUG | 74 #ifdef SK_DEBUG |
76 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) | 75 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) |
77 #else | 76 #else |
78 #define SkCheckResult(expr, value) (void)(expr) | 77 #define SkCheckResult(expr, value) (void)(expr) |
79 #endif | 78 #endif |
80 | 79 |
81 #ifdef SK_DEBUG | 80 #ifdef SK_DEBUG |
82 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) { | 81 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) { |
83 return ((reported == actual) | 82 return ((reported == actual) |
84 || ((reported == kPremul_SkAlphaType) | 83 || ((reported == kPremul_SkAlphaType) |
85 && (actual == kOpaque_SkAlphaType))); | 84 && (actual == kOpaque_SkAlphaType))); |
86 } | 85 } |
87 #endif // SK_DEBUG | 86 #endif // SK_DEBUG |
88 | 87 |
89 } // namespace | 88 } // namespace |
90 //////////////////////////////////////////////////////////////////////////////// | 89 //////////////////////////////////////////////////////////////////////////////// |
91 | 90 |
92 SkDecodingImageGenerator::SkDecodingImageGenerator( | 91 SkDecodingImageGenerator::SkDecodingImageGenerator(SkData* data, |
93 SkData* data, | 92 SkStreamRewindable* stream, |
94 SkStreamRewindable* stream, | 93 const SkImageInfo& info, |
95 const SkImageInfo& info, | 94 int sampleSize, |
96 int sampleSize, | 95 bool ditherImage, |
97 bool ditherImage, | 96 SkColorType requestedColorTyp
e) |
98 SkBitmap::Config requestedConfig) | |
99 : fData(data) | 97 : fData(data) |
100 , fStream(stream) | 98 , fStream(stream) |
101 , fInfo(info) | 99 , fInfo(info) |
102 , fSampleSize(sampleSize) | 100 , fSampleSize(sampleSize) |
103 , fDitherImage(ditherImage) | 101 , fDitherImage(ditherImage) |
104 , fRequestedConfig(requestedConfig) { | 102 , fRequestedColorType(requestedColorType) |
| 103 { |
105 SkASSERT(stream != NULL); | 104 SkASSERT(stream != NULL); |
106 SkSafeRef(fData); // may be NULL. | 105 SkSafeRef(fData); // may be NULL. |
107 } | 106 } |
108 | 107 |
109 SkDecodingImageGenerator::~SkDecodingImageGenerator() { | 108 SkDecodingImageGenerator::~SkDecodingImageGenerator() { |
110 SkSafeUnref(fData); | 109 SkSafeUnref(fData); |
111 fStream->unref(); | 110 fStream->unref(); |
112 } | 111 } |
113 | 112 |
114 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { | 113 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { |
(...skipping 29 matching lines...) Expand all Loading... |
144 size_t rowBytes) { | 143 size_t rowBytes) { |
145 if (NULL == pixels) { | 144 if (NULL == pixels) { |
146 return false; | 145 return false; |
147 } | 146 } |
148 if (fInfo != info) { | 147 if (fInfo != info) { |
149 // The caller has specified a different info. This is an | 148 // The caller has specified a different info. This is an |
150 // error for this kind of SkImageGenerator. Use the Options | 149 // error for this kind of SkImageGenerator. Use the Options |
151 // to change the settings. | 150 // to change the settings. |
152 return false; | 151 return false; |
153 } | 152 } |
154 int bpp = SkBitmap::ComputeBytesPerPixel(fRequestedConfig); | 153 int bpp = SkColorTypeBytesPerPixel(fRequestedColorType); |
155 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) { | 154 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) { |
156 // The caller has specified a bad rowBytes. | 155 // The caller has specified a bad rowBytes. |
157 return false; | 156 return false; |
158 } | 157 } |
159 | 158 |
160 SkAssertResult(fStream->rewind()); | 159 SkAssertResult(fStream->rewind()); |
161 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 160 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
162 if (NULL == decoder.get()) { | 161 if (NULL == decoder.get()) { |
163 return false; | 162 return false; |
164 } | 163 } |
165 decoder->setDitherImage(fDitherImage); | 164 decoder->setDitherImage(fDitherImage); |
166 decoder->setSampleSize(fSampleSize); | 165 decoder->setSampleSize(fSampleSize); |
167 | 166 |
168 SkBitmap bitmap; | 167 SkBitmap bitmap; |
169 TargetAllocator allocator(pixels, rowBytes, info.fWidth, | 168 TargetAllocator allocator(pixels, rowBytes, info.fWidth, |
170 info.fHeight, fRequestedConfig); | 169 info.fHeight, fRequestedColorType); |
171 decoder->setAllocator(&allocator); | 170 decoder->setAllocator(&allocator); |
172 bool success = decoder->decode(fStream, &bitmap, fRequestedConfig, | 171 bool success = decoder->decode(fStream, &bitmap, fRequestedColorType, |
173 SkImageDecoder::kDecodePixels_Mode); | 172 SkImageDecoder::kDecodePixels_Mode); |
174 decoder->setAllocator(NULL); | 173 decoder->setAllocator(NULL); |
175 if (!success) { | 174 if (!success) { |
176 return false; | 175 return false; |
177 } | 176 } |
178 if (allocator.isReady()) { // Did not use pixels! | 177 if (allocator.isReady()) { // Did not use pixels! |
| 178 // HACK: need access to colortype versions of these calls |
| 179 SkBitmap::Config config = SkColorTypeToBitmapConfig(fRequestedColorType)
; |
179 SkBitmap bm; | 180 SkBitmap bm; |
180 SkASSERT(bitmap.canCopyTo(fRequestedConfig)); | 181 SkASSERT(bitmap.canCopyTo(config)); |
181 if (!bitmap.copyTo(&bm, fRequestedConfig, &allocator) | 182 if (!bitmap.copyTo(&bm, config, &allocator) |
182 || allocator.isReady()) { | 183 || allocator.isReady()) { |
183 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); | 184 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
184 // Earlier we checked canCopyto(); we expect consistency. | 185 // Earlier we checked canCopyto(); we expect consistency. |
185 return false; | 186 return false; |
186 } | 187 } |
187 SkASSERT(check_alpha(fInfo.fAlphaType, bm.alphaType())); | 188 SkASSERT(check_alpha(fInfo.fAlphaType, bm.alphaType())); |
188 } else { | 189 } else { |
189 SkASSERT(check_alpha(fInfo.fAlphaType, bitmap.alphaType())); | 190 SkASSERT(check_alpha(fInfo.fAlphaType, bitmap.alphaType())); |
190 } | 191 } |
191 return true; | 192 return true; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // We do not support indexed color with SkImageGenerators, | 231 // We do not support indexed color with SkImageGenerators, |
231 return NULL; | 232 return NULL; |
232 } | 233 } |
233 SkAssertResult(autoStream->rewind()); | 234 SkAssertResult(autoStream->rewind()); |
234 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); | 235 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); |
235 if (NULL == decoder.get()) { | 236 if (NULL == decoder.get()) { |
236 return NULL; | 237 return NULL; |
237 } | 238 } |
238 SkBitmap bitmap; | 239 SkBitmap bitmap; |
239 decoder->setSampleSize(opts.fSampleSize); | 240 decoder->setSampleSize(opts.fSampleSize); |
240 if (!decoder->decode(stream, &bitmap, | 241 if (!decoder->decode(stream, &bitmap, kUnknown_SkColorType, |
241 SkImageDecoder::kDecodeBounds_Mode)) { | 242 SkImageDecoder::kDecodeBounds_Mode)) { |
242 return NULL; | 243 return NULL; |
243 } | 244 } |
244 if (bitmap.config() == SkBitmap::kNo_Config) { | 245 if (bitmap.config() == SkBitmap::kNo_Config) { |
245 return NULL; | 246 return NULL; |
246 } | 247 } |
247 | 248 |
248 SkImageInfo info; | 249 SkImageInfo info; |
249 SkBitmap::Config config; | 250 SkColorType requestedColorType; |
250 | 251 |
251 if (!opts.fUseRequestedColorType) { | 252 if (!opts.fUseRequestedColorType) { |
252 // Use default config. | 253 // Use default config. |
253 if (SkBitmap::kIndex8_Config == bitmap.config()) { | 254 if (kIndex_8_SkColorType == bitmap.colorType()) { |
254 // We don't support kIndex8 because we don't support | 255 // We don't support kIndex8 because we don't support |
255 // colortables in this workflow. | 256 // colortables in this workflow. |
256 config = SkBitmap::kARGB_8888_Config; | 257 requestedColorType = kPMColor_SkColorType; |
| 258 |
257 info.fWidth = bitmap.width(); | 259 info.fWidth = bitmap.width(); |
258 info.fHeight = bitmap.height(); | 260 info.fHeight = bitmap.height(); |
259 info.fColorType = kPMColor_SkColorType; | 261 info.fColorType = requestedColorType; |
260 info.fAlphaType = bitmap.alphaType(); | 262 info.fAlphaType = bitmap.alphaType(); |
261 } else { | 263 } else { |
262 config = bitmap.config(); // Save for later! | 264 requestedColorType = bitmap.colorType(); // Save for later! |
263 if (!bitmap.asImageInfo(&info)) { | 265 if (!bitmap.asImageInfo(&info)) { |
264 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed."); | 266 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed."); |
265 return NULL; | 267 return NULL; |
266 } | 268 } |
267 } | 269 } |
268 } else { | 270 } else { |
269 config = SkColorTypeToBitmapConfig(opts.fRequestedColorType); | 271 requestedColorType = opts.fRequestedColorType; |
270 if (!bitmap.canCopyTo(config)) { | 272 if (!bitmap.canCopyTo(requestedColorType)) { |
271 SkASSERT(bitmap.config() != config); | 273 SkASSERT(bitmap.colorType() != requestedColorType); |
272 return NULL; // Can not translate to needed config. | 274 return NULL; // Can not translate to needed config. |
273 } | 275 } |
274 info.fWidth = bitmap.width(); | 276 info.fWidth = bitmap.width(); |
275 info.fHeight = bitmap.height(); | 277 info.fHeight = bitmap.height(); |
276 info.fColorType = opts.fRequestedColorType; | 278 info.fColorType = opts.fRequestedColorType; |
277 info.fAlphaType = bitmap.alphaType(); | 279 info.fAlphaType = bitmap.alphaType(); |
278 } | 280 } |
279 return SkNEW_ARGS(SkDecodingImageGenerator, | 281 return SkNEW_ARGS(SkDecodingImageGenerator, |
280 (data, autoStream.detach(), info, | 282 (data, autoStream.detach(), info, |
281 opts.fSampleSize, opts.fDitherImage, config)); | 283 opts.fSampleSize, opts.fDitherImage, requestedColorType))
; |
282 } | 284 } |
OLD | NEW |