| 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 ctable[], int* ctableCount) { |
| 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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 SkStreamRewindable* stream, | 276 SkStreamRewindable* stream, |
| 282 const SkDecodingImageGenerator::Options& opts) { | 277 const SkDecodingImageGenerator::Options& opts) { |
| 283 SkASSERT(stream != NULL); | 278 SkASSERT(stream != NULL); |
| 284 SkASSERT(stream->unique()); | 279 SkASSERT(stream->unique()); |
| 285 if ((stream == NULL) || !stream->unique()) { | 280 if ((stream == NULL) || !stream->unique()) { |
| 286 SkSafeUnref(stream); | 281 SkSafeUnref(stream); |
| 287 return NULL; | 282 return NULL; |
| 288 } | 283 } |
| 289 return CreateDecodingImageGenerator(NULL, stream, opts); | 284 return CreateDecodingImageGenerator(NULL, stream, opts); |
| 290 } | 285 } |
| OLD | NEW |