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" |
(...skipping 20 matching lines...) Expand all Loading... |
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() override; | 40 SkData* onRefEncodedData() override; |
41 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
42 Result onGetPixels(const SkImageInfo& info, | |
43 void* pixels, size_t rowBytes, const Options&, | |
44 SkPMColor ctable[], int* ctableCount) override; | |
45 #else | |
46 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, | 41 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
47 SkPMColor ctable[], int* ctableCount) override; | 42 SkPMColor ctable[], int* ctableCount) override; |
48 #endif | |
49 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], | 43 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
50 SkYUVColorSpace* colorSpace) override; | 44 SkYUVColorSpace* colorSpace) override; |
51 | 45 |
52 private: | 46 private: |
53 typedef SkImageGenerator INHERITED; | 47 typedef SkImageGenerator INHERITED; |
54 }; | 48 }; |
55 | 49 |
56 /** | 50 /** |
57 * Special allocator used by getPixels(). Uses preallocated memory | 51 * Special allocator used by getPixels(). Uses preallocated memory |
58 * provided if possible, else fall-back on the default allocator | 52 * provided if possible, else fall-back on the default allocator |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 return NULL; | 136 return NULL; |
143 } | 137 } |
144 size_t length = fStream->getLength(); | 138 size_t length = fStream->getLength(); |
145 if (length) { | 139 if (length) { |
146 fData = SkData::NewFromStream(fStream, length); | 140 fData = SkData::NewFromStream(fStream, length); |
147 } | 141 } |
148 } | 142 } |
149 return SkSafeRef(fData); | 143 return SkSafeRef(fData); |
150 } | 144 } |
151 | 145 |
152 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
153 SkImageGenerator::Result DecodingImageGenerator::onGetPixels(const SkImageInfo&
info, | |
154 void* pixels, size_t rowBytes, const Options& options, SkPMColor ctableE
ntries[], | |
155 int* ctableCount) { | |
156 #else | |
157 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, | 146 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, |
158 SkPMColor ctableEntries[], int* ctableC
ount) { | 147 SkPMColor ctableEntries[], int* ctableC
ount) { |
159 #endif | |
160 if (fInfo != info) { | 148 if (fInfo != info) { |
161 // The caller has specified a different info. This is an | 149 // The caller has specified a different info. This is an |
162 // error for this kind of SkImageGenerator. Use the Options | 150 // error for this kind of SkImageGenerator. Use the Options |
163 // to change the settings. | 151 // to change the settings. |
164 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
165 if (info.dimensions() != fInfo.dimensions()) { | |
166 return kInvalidScale; | |
167 } | |
168 return kInvalidConversion; | |
169 #else | |
170 return false; | 152 return false; |
171 #endif | |
172 } | 153 } |
173 | 154 |
174 SkAssertResult(fStream->rewind()); | 155 SkAssertResult(fStream->rewind()); |
175 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 156 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
176 if (NULL == decoder.get()) { | 157 if (NULL == decoder.get()) { |
177 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
178 return kInvalidInput; | |
179 #else | |
180 return false; | 158 return false; |
181 #endif | |
182 } | 159 } |
183 decoder->setDitherImage(fDitherImage); | 160 decoder->setDitherImage(fDitherImage); |
184 decoder->setSampleSize(fSampleSize); | 161 decoder->setSampleSize(fSampleSize); |
185 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); | 162 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); |
186 | 163 |
187 SkBitmap bitmap; | 164 SkBitmap bitmap; |
188 TargetAllocator allocator(fInfo, pixels, rowBytes); | 165 TargetAllocator allocator(fInfo, pixels, rowBytes); |
189 decoder->setAllocator(&allocator); | 166 decoder->setAllocator(&allocator); |
190 const SkImageDecoder::Result decodeResult = decoder->decode(fStream, &bitmap
, info.colorType(), | 167 const SkImageDecoder::Result decodeResult = decoder->decode(fStream, &bitmap
, info.colorType(), |
191 SkImageDecoder::
kDecodePixels_Mode); | 168 SkImageDecoder::
kDecodePixels_Mode); |
192 decoder->setAllocator(NULL); | 169 decoder->setAllocator(NULL); |
193 if (SkImageDecoder::kFailure == decodeResult) { | 170 if (SkImageDecoder::kFailure == decodeResult) { |
194 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
195 return kInvalidInput; | |
196 #else | |
197 return false; | 171 return false; |
198 #endif | |
199 } | 172 } |
200 if (allocator.isReady()) { // Did not use pixels! | 173 if (allocator.isReady()) { // Did not use pixels! |
201 SkBitmap bm; | 174 SkBitmap bm; |
202 SkASSERT(bitmap.canCopyTo(info.colorType())); | 175 SkASSERT(bitmap.canCopyTo(info.colorType())); |
203 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); | 176 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); |
204 if (!copySuccess || allocator.isReady()) { | 177 if (!copySuccess || allocator.isReady()) { |
205 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); | 178 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
206 // Earlier we checked canCopyto(); we expect consistency. | 179 // Earlier we checked canCopyto(); we expect consistency. |
207 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
208 return kInvalidConversion; | |
209 #else | |
210 return false; | 180 return false; |
211 #endif | |
212 } | 181 } |
213 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); | 182 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); |
214 } else { | 183 } else { |
215 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); | 184 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); |
216 } | 185 } |
217 | 186 |
218 if (kIndex_8_SkColorType == info.colorType()) { | 187 if (kIndex_8_SkColorType == info.colorType()) { |
219 if (kIndex_8_SkColorType != bitmap.colorType()) { | 188 if (kIndex_8_SkColorType != bitmap.colorType()) { |
220 // they asked for Index8, but we didn't receive that from decoder | 189 // they asked for Index8, but we didn't receive that from decoder |
221 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
222 return kInvalidConversion; | |
223 #else | |
224 return false; | 190 return false; |
225 #endif | |
226 } | 191 } |
227 SkColorTable* ctable = bitmap.getColorTable(); | 192 SkColorTable* ctable = bitmap.getColorTable(); |
228 if (NULL == ctable) { | 193 if (NULL == ctable) { |
229 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
230 return kInvalidConversion; | |
231 #else | |
232 return false; | 194 return false; |
233 #endif | |
234 } | 195 } |
235 const int count = ctable->count(); | 196 const int count = ctable->count(); |
236 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); | 197 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); |
237 *ctableCount = count; | 198 *ctableCount = count; |
238 } | 199 } |
239 #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS | |
240 if (SkImageDecoder::kPartialSuccess == decodeResult) { | |
241 return kIncompleteInput; | |
242 } | |
243 return kSuccess; | |
244 #else | |
245 return true; | 200 return true; |
246 #endif | |
247 } | 201 } |
248 | 202 |
249 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], | 203 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], |
250 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { | 204 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { |
251 if (!fStream->rewind()) { | 205 if (!fStream->rewind()) { |
252 return false; | 206 return false; |
253 } | 207 } |
254 | 208 |
255 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 209 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
256 if (NULL == decoder.get()) { | 210 if (NULL == decoder.get()) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 280 |
327 SkImageGenerator* SkDecodingImageGenerator::Create( | 281 SkImageGenerator* SkDecodingImageGenerator::Create( |
328 SkStreamRewindable* stream, | 282 SkStreamRewindable* stream, |
329 const SkDecodingImageGenerator::Options& opts) { | 283 const SkDecodingImageGenerator::Options& opts) { |
330 SkASSERT(stream != NULL); | 284 SkASSERT(stream != NULL); |
331 if (stream == NULL) { | 285 if (stream == NULL) { |
332 return NULL; | 286 return NULL; |
333 } | 287 } |
334 return CreateDecodingImageGenerator(NULL, stream, opts); | 288 return CreateDecodingImageGenerator(NULL, stream, opts); |
335 } | 289 } |
OLD | NEW |