| 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 24 matching lines...) Expand all Loading... |
| 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 { |
| 42 *info = fInfo; | 42 *info = fInfo; |
| 43 return true; | 43 return true; |
| 44 } | 44 } |
| 45 virtual bool onGetPixels(const SkImageInfo& info, | 45 virtual Result onGetPixelsEnum(const SkImageInfo& info, |
| 46 void* pixels, size_t rowBytes, | 46 void* pixels, size_t rowBytes, |
| 47 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE; | 47 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE; |
| 48 virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBy
tes[3], | 48 virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBy
tes[3], |
| 49 SkYUVColorSpace* colorSpace) SK_OVERRIDE; | 49 SkYUVColorSpace* colorSpace) SK_OVERRIDE; |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 typedef SkImageGenerator INHERITED; | 52 typedef SkImageGenerator INHERITED; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Special allocator used by getPixels(). Uses preallocated memory | 56 * Special allocator used by getPixels(). Uses preallocated memory |
| 57 * provided if possible, else fall-back on the default allocator | 57 * provided if possible, else fall-back on the default allocator |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 return NULL; | 140 return NULL; |
| 141 } | 141 } |
| 142 size_t length = fStream->getLength(); | 142 size_t length = fStream->getLength(); |
| 143 if (length) { | 143 if (length) { |
| 144 fData = SkData::NewFromStream(fStream, length); | 144 fData = SkData::NewFromStream(fStream, length); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 return SkSafeRef(fData); | 147 return SkSafeRef(fData); |
| 148 } | 148 } |
| 149 | 149 |
| 150 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, | 150 SkImageGenerator::Result DecodingImageGenerator::onGetPixelsEnum(const SkImageIn
fo& info, |
| 151 void* pixels, size_t rowBytes, | 151 void* pixels, size_t rowBytes, SkPMColor ctableEntries[], int* ctableCou
nt) { |
| 152 SkPMColor ctableEntries[], int* ctableC
ount) { | |
| 153 if (fInfo != info) { | 152 if (fInfo != info) { |
| 154 // The caller has specified a different info. This is an | 153 // The caller has specified a different info. This is an |
| 155 // error for this kind of SkImageGenerator. Use the Options | 154 // error for this kind of SkImageGenerator. Use the Options |
| 156 // to change the settings. | 155 // to change the settings. |
| 157 return false; | 156 if (info.dimensions() != fInfo.dimensions()) { |
| 157 return kInvalidScale; |
| 158 } |
| 159 return kInvalidConversion; |
| 158 } | 160 } |
| 159 | 161 |
| 160 SkAssertResult(fStream->rewind()); | 162 SkAssertResult(fStream->rewind()); |
| 161 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 163 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 162 if (NULL == decoder.get()) { | 164 if (NULL == decoder.get()) { |
| 163 return false; | 165 return kInvalidInput; |
| 164 } | 166 } |
| 165 decoder->setDitherImage(fDitherImage); | 167 decoder->setDitherImage(fDitherImage); |
| 166 decoder->setSampleSize(fSampleSize); | 168 decoder->setSampleSize(fSampleSize); |
| 167 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); | 169 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); |
| 168 | 170 |
| 169 SkBitmap bitmap; | 171 SkBitmap bitmap; |
| 170 TargetAllocator allocator(fInfo, pixels, rowBytes); | 172 TargetAllocator allocator(fInfo, pixels, rowBytes); |
| 171 decoder->setAllocator(&allocator); | 173 decoder->setAllocator(&allocator); |
| 172 bool success = decoder->decode(fStream, &bitmap, info.colorType(), | 174 const SkImageDecoder::Result decodeResult = decoder->decode(fStream, &bitmap
, info.colorType(), |
| 173 SkImageDecoder::kDecodePixels_Mode) != SkImag
eDecoder::kFailure; | 175 SkImageDecoder::
kDecodePixels_Mode); |
| 174 decoder->setAllocator(NULL); | 176 decoder->setAllocator(NULL); |
| 175 if (!success) { | 177 if (SkImageDecoder::kFailure == decodeResult) { |
| 176 return false; | 178 return kInvalidInput; |
| 177 } | 179 } |
| 178 if (allocator.isReady()) { // Did not use pixels! | 180 if (allocator.isReady()) { // Did not use pixels! |
| 179 SkBitmap bm; | 181 SkBitmap bm; |
| 180 SkASSERT(bitmap.canCopyTo(info.colorType())); | 182 SkASSERT(bitmap.canCopyTo(info.colorType())); |
| 181 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); | 183 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); |
| 182 if (!copySuccess || allocator.isReady()) { | 184 if (!copySuccess || allocator.isReady()) { |
| 183 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); | 185 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
| 184 // Earlier we checked canCopyto(); we expect consistency. | 186 // Earlier we checked canCopyto(); we expect consistency. |
| 185 return false; | 187 return kInvalidConversion; |
| 186 } | 188 } |
| 187 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); | 189 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); |
| 188 } else { | 190 } else { |
| 189 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); | 191 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); |
| 190 } | 192 } |
| 191 | 193 |
| 192 if (kIndex_8_SkColorType == info.colorType()) { | 194 if (kIndex_8_SkColorType == info.colorType()) { |
| 193 if (kIndex_8_SkColorType != bitmap.colorType()) { | 195 if (kIndex_8_SkColorType != bitmap.colorType()) { |
| 194 return false; // they asked for Index8, but we didn't receive that
from decoder | 196 // they asked for Index8, but we didn't receive that from decoder |
| 197 return kInvalidConversion; |
| 195 } | 198 } |
| 196 SkColorTable* ctable = bitmap.getColorTable(); | 199 SkColorTable* ctable = bitmap.getColorTable(); |
| 197 if (NULL == ctable) { | 200 if (NULL == ctable) { |
| 198 return false; | 201 return kInvalidConversion; |
| 199 } | 202 } |
| 200 const int count = ctable->count(); | 203 const int count = ctable->count(); |
| 201 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); | 204 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); |
| 202 *ctableCount = count; | 205 *ctableCount = count; |
| 203 } | 206 } |
| 204 return true; | 207 if (SkImageDecoder::kPartialSuccess == decodeResult) { |
| 208 return kIncompleteInput; |
| 209 } |
| 210 return kSuccess; |
| 205 } | 211 } |
| 206 | 212 |
| 207 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], | 213 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], |
| 208 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { | 214 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { |
| 209 if (!fStream->rewind()) { | 215 if (!fStream->rewind()) { |
| 210 return false; | 216 return false; |
| 211 } | 217 } |
| 212 | 218 |
| 213 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 219 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 214 if (NULL == decoder.get()) { | 220 if (NULL == decoder.get()) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 290 |
| 285 SkImageGenerator* SkDecodingImageGenerator::Create( | 291 SkImageGenerator* SkDecodingImageGenerator::Create( |
| 286 SkStreamRewindable* stream, | 292 SkStreamRewindable* stream, |
| 287 const SkDecodingImageGenerator::Options& opts) { | 293 const SkDecodingImageGenerator::Options& opts) { |
| 288 SkASSERT(stream != NULL); | 294 SkASSERT(stream != NULL); |
| 289 if (stream == NULL) { | 295 if (stream == NULL) { |
| 290 return NULL; | 296 return NULL; |
| 291 } | 297 } |
| 292 return CreateDecodingImageGenerator(NULL, stream, opts); | 298 return CreateDecodingImageGenerator(NULL, stream, opts); |
| 293 } | 299 } |
| OLD | NEW |