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 bool equal_modulo_alpha(const SkImageInfo& a, const SkImageInfo& b) { | 18 bool equal_modulo_alpha(const SkImageInfo& a, const SkImageInfo& b) { |
19 return a.width() == b.width() && a.height() == b.height() && | 19 return a.width() == b.width() && a.height() == b.height() && |
20 a.colorType() == b.colorType(); | 20 a.colorType() == b.colorType(); |
21 } | 21 } |
22 | 22 |
23 class DecodingImageGenerator : public SkImageGenerator { | 23 class DecodingImageGenerator : public SkImageGenerator { |
24 public: | 24 public: |
25 virtual ~DecodingImageGenerator(); | 25 virtual ~DecodingImageGenerator(); |
26 | 26 |
27 SkData* fData; | 27 SkData* fData; |
28 SkStreamRewindable* fStream; | 28 SkAutoTDelete<SkStreamRewindable> fStream; |
29 const SkImageInfo fInfo; | 29 const SkImageInfo fInfo; |
30 const int fSampleSize; | 30 const int fSampleSize; |
31 const bool fDitherImage; | 31 const bool fDitherImage; |
32 | 32 |
33 DecodingImageGenerator(SkData* data, | 33 DecodingImageGenerator(SkData* data, |
34 SkStreamRewindable* stream, | 34 SkStreamRewindable* stream, |
35 const SkImageInfo& info, | 35 const SkImageInfo& info, |
36 int sampleSize, | 36 int sampleSize, |
37 bool ditherImage); | 37 bool ditherImage); |
38 | 38 |
39 protected: | 39 protected: |
40 SkData* onRefEncodedData() SK_OVERRIDE; | 40 SkData* onRefEncodedData() SK_OVERRIDE; |
41 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { | 41 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 , fInfo(info) | 121 , fInfo(info) |
122 , fSampleSize(sampleSize) | 122 , fSampleSize(sampleSize) |
123 , fDitherImage(ditherImage) | 123 , fDitherImage(ditherImage) |
124 { | 124 { |
125 SkASSERT(stream != NULL); | 125 SkASSERT(stream != NULL); |
126 SkSafeRef(fData); // may be NULL. | 126 SkSafeRef(fData); // may be NULL. |
127 } | 127 } |
128 | 128 |
129 DecodingImageGenerator::~DecodingImageGenerator() { | 129 DecodingImageGenerator::~DecodingImageGenerator() { |
130 SkSafeUnref(fData); | 130 SkSafeUnref(fData); |
131 fStream->unref(); | |
132 } | 131 } |
133 | 132 |
134 SkData* DecodingImageGenerator::onRefEncodedData() { | 133 SkData* DecodingImageGenerator::onRefEncodedData() { |
135 // This functionality is used in `gm --serialize` | 134 // This functionality is used in `gm --serialize` |
136 // Does not encode options. | 135 // Does not encode options. |
137 if (NULL == fData) { | 136 if (NULL == fData) { |
138 // TODO(halcanary): SkStreamRewindable needs a refData() function | 137 // TODO(halcanary): SkStreamRewindable needs a refData() function |
139 // which returns a cheap copy of the underlying data. | 138 // which returns a cheap copy of the underlying data. |
140 if (!fStream->rewind()) { | 139 if (!fStream->rewind()) { |
141 return NULL; | 140 return NULL; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 219 } |
221 | 220 |
222 // A contructor-type function that returns NULL on failure. This | 221 // A contructor-type function that returns NULL on failure. This |
223 // prevents the returned SkImageGenerator from ever being in a bad | 222 // prevents the returned SkImageGenerator from ever being in a bad |
224 // state. Called by both Create() functions | 223 // state. Called by both Create() functions |
225 SkImageGenerator* CreateDecodingImageGenerator( | 224 SkImageGenerator* CreateDecodingImageGenerator( |
226 SkData* data, | 225 SkData* data, |
227 SkStreamRewindable* stream, | 226 SkStreamRewindable* stream, |
228 const SkDecodingImageGenerator::Options& opts) { | 227 const SkDecodingImageGenerator::Options& opts) { |
229 SkASSERT(stream); | 228 SkASSERT(stream); |
230 SkAutoTUnref<SkStreamRewindable> autoStream(stream); // always unref this. | 229 SkAutoTDelete<SkStreamRewindable> autoStream(stream); // always delete this |
231 SkAssertResult(autoStream->rewind()); | 230 SkAssertResult(autoStream->rewind()); |
232 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); | 231 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); |
233 if (NULL == decoder.get()) { | 232 if (NULL == decoder.get()) { |
234 return NULL; | 233 return NULL; |
235 } | 234 } |
236 SkBitmap bitmap; | 235 SkBitmap bitmap; |
237 decoder->setSampleSize(opts.fSampleSize); | 236 decoder->setSampleSize(opts.fSampleSize); |
238 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul); | 237 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul); |
239 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) { | 238 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) { |
240 return NULL; | 239 return NULL; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 272 |
274 SkImageGenerator* SkDecodingImageGenerator::Create( | 273 SkImageGenerator* SkDecodingImageGenerator::Create( |
275 SkData* data, | 274 SkData* data, |
276 const SkDecodingImageGenerator::Options& opts) { | 275 const SkDecodingImageGenerator::Options& opts) { |
277 SkASSERT(data != NULL); | 276 SkASSERT(data != NULL); |
278 if (NULL == data) { | 277 if (NULL == data) { |
279 return NULL; | 278 return NULL; |
280 } | 279 } |
281 SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data)); | 280 SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data)); |
282 SkASSERT(stream != NULL); | 281 SkASSERT(stream != NULL); |
283 SkASSERT(stream->unique()); | |
284 return CreateDecodingImageGenerator(data, stream, opts); | 282 return CreateDecodingImageGenerator(data, stream, opts); |
285 } | 283 } |
286 | 284 |
287 SkImageGenerator* SkDecodingImageGenerator::Create( | 285 SkImageGenerator* SkDecodingImageGenerator::Create( |
288 SkStreamRewindable* stream, | 286 SkStreamRewindable* stream, |
289 const SkDecodingImageGenerator::Options& opts) { | 287 const SkDecodingImageGenerator::Options& opts) { |
290 SkASSERT(stream != NULL); | 288 SkASSERT(stream != NULL); |
291 SkASSERT(stream->unique()); | 289 if (stream == NULL) { |
292 if ((stream == NULL) || !stream->unique()) { | |
293 SkSafeUnref(stream); | |
294 return NULL; | 290 return NULL; |
295 } | 291 } |
296 return CreateDecodingImageGenerator(NULL, stream, opts); | 292 return CreateDecodingImageGenerator(NULL, stream, opts); |
297 } | 293 } |
OLD | NEW |