| 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 virtual SkData* refEncodedData() SK_OVERRIDE; | |
| 27 // This implementaion of getInfo() always returns true. | |
| 28 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; | |
| 29 virtual bool getPixels(const SkImageInfo& info, | |
| 30 void* pixels, | |
| 31 size_t rowBytes) SK_OVERRIDE; | |
| 32 | 26 |
| 33 SkData* fData; | 27 SkData* fData; |
| 34 SkStreamRewindable* fStream; | 28 SkStreamRewindable* fStream; |
| 35 const SkImageInfo fInfo; | 29 const SkImageInfo fInfo; |
| 36 const int fSampleSize; | 30 const int fSampleSize; |
| 37 const bool fDitherImage; | 31 const bool fDitherImage; |
| 38 | 32 |
| 39 DecodingImageGenerator(SkData* data, | 33 DecodingImageGenerator(SkData* data, |
| 40 SkStreamRewindable* stream, | 34 SkStreamRewindable* stream, |
| 41 const SkImageInfo& info, | 35 const SkImageInfo& info, |
| 42 int sampleSize, | 36 int sampleSize, |
| 43 bool ditherImage); | 37 bool ditherImage); |
| 38 |
| 39 protected: |
| 40 virtual SkData* onRefEncodedData() SK_OVERRIDE; |
| 41 // This implementaion of getInfo() always returns true. |
| 42 virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE; |
| 43 virtual bool onGetPixels(const SkImageInfo& info, |
| 44 void* pixels, size_t rowBytes, |
| 45 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE; |
| 46 |
| 47 private: |
| 44 typedef SkImageGenerator INHERITED; | 48 typedef SkImageGenerator INHERITED; |
| 45 }; | 49 }; |
| 46 | 50 |
| 47 /** | 51 /** |
| 48 * Special allocator used by getPixels(). Uses preallocated memory | 52 * Special allocator used by getPixels(). Uses preallocated memory |
| 49 * provided if possible, else fall-back on the default allocator | 53 * provided if possible, else fall-back on the default allocator |
| 50 */ | 54 */ |
| 51 class TargetAllocator : public SkBitmap::Allocator { | 55 class TargetAllocator : public SkBitmap::Allocator { |
| 52 public: | 56 public: |
| 53 TargetAllocator(const SkImageInfo& info, | 57 TargetAllocator(const SkImageInfo& info, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 { | 120 { |
| 117 SkASSERT(stream != NULL); | 121 SkASSERT(stream != NULL); |
| 118 SkSafeRef(fData); // may be NULL. | 122 SkSafeRef(fData); // may be NULL. |
| 119 } | 123 } |
| 120 | 124 |
| 121 DecodingImageGenerator::~DecodingImageGenerator() { | 125 DecodingImageGenerator::~DecodingImageGenerator() { |
| 122 SkSafeUnref(fData); | 126 SkSafeUnref(fData); |
| 123 fStream->unref(); | 127 fStream->unref(); |
| 124 } | 128 } |
| 125 | 129 |
| 126 bool DecodingImageGenerator::getInfo(SkImageInfo* info) { | 130 bool DecodingImageGenerator::onGetInfo(SkImageInfo* info) { |
| 127 if (info != NULL) { | 131 *info = fInfo; |
| 128 *info = fInfo; | |
| 129 } | |
| 130 return true; | 132 return true; |
| 131 } | 133 } |
| 132 | 134 |
| 133 SkData* DecodingImageGenerator::refEncodedData() { | 135 SkData* DecodingImageGenerator::onRefEncodedData() { |
| 134 // This functionality is used in `gm --serialize` | 136 // This functionality is used in `gm --serialize` |
| 135 // Does not encode options. | 137 // Does not encode options. |
| 136 if (fData != NULL) { | 138 if (fData != NULL) { |
| 137 return SkSafeRef(fData); | 139 return SkSafeRef(fData); |
| 138 } | 140 } |
| 139 // TODO(halcanary): SkStreamRewindable needs a refData() function | 141 // TODO(halcanary): SkStreamRewindable needs a refData() function |
| 140 // which returns a cheap copy of the underlying data. | 142 // which returns a cheap copy of the underlying data. |
| 141 if (!fStream->rewind()) { | 143 if (!fStream->rewind()) { |
| 142 return NULL; | 144 return NULL; |
| 143 } | 145 } |
| 144 size_t length = fStream->getLength(); | 146 size_t length = fStream->getLength(); |
| 145 if (0 == length) { | 147 if (0 == length) { |
| 146 return NULL; | 148 return NULL; |
| 147 } | 149 } |
| 148 void* buffer = sk_malloc_flags(length, 0); | 150 void* buffer = sk_malloc_flags(length, 0); |
| 149 SkCheckResult(fStream->read(buffer, length), length); | 151 SkCheckResult(fStream->read(buffer, length), length); |
| 150 fData = SkData::NewFromMalloc(buffer, length); | 152 fData = SkData::NewFromMalloc(buffer, length); |
| 151 return SkSafeRef(fData); | 153 return SkSafeRef(fData); |
| 152 } | 154 } |
| 153 | 155 |
| 154 bool DecodingImageGenerator::getPixels(const SkImageInfo& info, | 156 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, |
| 155 void* pixels, | 157 void* pixels, size_t rowBytes, |
| 156 size_t rowBytes) { | 158 SkPMColor ctableEntries[], int* ctableC
ount) { |
| 157 if (NULL == pixels) { | |
| 158 return false; | |
| 159 } | |
| 160 if (fInfo != info) { | 159 if (fInfo != info) { |
| 161 // The caller has specified a different info. This is an | 160 // The caller has specified a different info. This is an |
| 162 // error for this kind of SkImageGenerator. Use the Options | 161 // error for this kind of SkImageGenerator. Use the Options |
| 163 // to change the settings. | 162 // to change the settings. |
| 164 return false; | 163 return false; |
| 165 } | 164 } |
| 166 if (info.minRowBytes() > rowBytes) { | |
| 167 // The caller has specified a bad rowBytes. | |
| 168 return false; | |
| 169 } | |
| 170 | 165 |
| 171 SkAssertResult(fStream->rewind()); | 166 SkAssertResult(fStream->rewind()); |
| 172 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 167 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 173 if (NULL == decoder.get()) { | 168 if (NULL == decoder.get()) { |
| 174 return false; | 169 return false; |
| 175 } | 170 } |
| 176 decoder->setDitherImage(fDitherImage); | 171 decoder->setDitherImage(fDitherImage); |
| 177 decoder->setSampleSize(fSampleSize); | 172 decoder->setSampleSize(fSampleSize); |
| 178 decoder->setRequireUnpremultipliedColors( | 173 decoder->setRequireUnpremultipliedColors( |
| 179 info.fAlphaType == kUnpremul_SkAlphaType); | 174 info.fAlphaType == kUnpremul_SkAlphaType); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 195 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); | 190 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); |
| 196 if (!copySuccess || allocator.isReady()) { | 191 if (!copySuccess || allocator.isReady()) { |
| 197 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); | 192 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
| 198 // Earlier we checked canCopyto(); we expect consistency. | 193 // Earlier we checked canCopyto(); we expect consistency. |
| 199 return false; | 194 return false; |
| 200 } | 195 } |
| 201 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); | 196 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); |
| 202 } else { | 197 } else { |
| 203 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); | 198 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); |
| 204 } | 199 } |
| 200 |
| 201 if (kIndex_8_SkColorType == info.colorType()) { |
| 202 if (kIndex_8_SkColorType != bitmap.colorType()) { |
| 203 return false; // they asked for Index8, but we didn't receive that
from decoder |
| 204 } |
| 205 SkColorTable* ctable = bitmap.getColorTable(); |
| 206 if (NULL == ctable) { |
| 207 return false; |
| 208 } |
| 209 const int count = ctable->count(); |
| 210 memcpy(ctableEntries, ctable->lockColors(), count * sizeof(SkPMColor)); |
| 211 ctable->unlockColors(); |
| 212 *ctableCount = count; |
| 213 } |
| 205 return true; | 214 return true; |
| 206 } | 215 } |
| 207 | 216 |
| 208 // A contructor-type function that returns NULL on failure. This | 217 // A contructor-type function that returns NULL on failure. This |
| 209 // prevents the returned SkImageGenerator from ever being in a bad | 218 // prevents the returned SkImageGenerator from ever being in a bad |
| 210 // state. Called by both Create() functions | 219 // state. Called by both Create() functions |
| 211 SkImageGenerator* CreateDecodingImageGenerator( | 220 SkImageGenerator* CreateDecodingImageGenerator( |
| 212 SkData* data, | 221 SkData* data, |
| 213 SkStreamRewindable* stream, | 222 SkStreamRewindable* stream, |
| 214 const SkDecodingImageGenerator::Options& opts) { | 223 const SkDecodingImageGenerator::Options& opts) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 SkStreamRewindable* stream, | 290 SkStreamRewindable* stream, |
| 282 const SkDecodingImageGenerator::Options& opts) { | 291 const SkDecodingImageGenerator::Options& opts) { |
| 283 SkASSERT(stream != NULL); | 292 SkASSERT(stream != NULL); |
| 284 SkASSERT(stream->unique()); | 293 SkASSERT(stream->unique()); |
| 285 if ((stream == NULL) || !stream->unique()) { | 294 if ((stream == NULL) || !stream->unique()) { |
| 286 SkSafeUnref(stream); | 295 SkSafeUnref(stream); |
| 287 return NULL; | 296 return NULL; |
| 288 } | 297 } |
| 289 return CreateDecodingImageGenerator(NULL, stream, opts); | 298 return CreateDecodingImageGenerator(NULL, stream, opts); |
| 290 } | 299 } |
| OLD | NEW |