Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 static inline void setup_color_table(SkColorType colorType, | 24 static inline void setup_color_table(SkColorType colorType, |
| 25 SkPMColor* colorPtr, int* colorCount) { | 25 SkPMColor* colorPtr, int* colorCount) { |
| 26 if (kIndex_8_SkColorType == colorType) { | 26 if (kIndex_8_SkColorType == colorType) { |
| 27 colorPtr[0] = SK_ColorBLACK; | 27 colorPtr[0] = SK_ColorBLACK; |
| 28 colorPtr[1] = SK_ColorWHITE; | 28 colorPtr[1] = SK_ColorWHITE; |
| 29 *colorCount = 2; | 29 *colorCount = 2; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 static inline bool valid_color_type(SkColorType colorType, SkAlphaType alphaType ) { | |
| 34 switch (colorType) { | |
| 35 case kN32_SkColorType: | |
| 36 case kIndex_8_SkColorType: | |
| 37 case kGray_8_SkColorType: | |
|
msarett
2016/02/11 19:13:45
Should kGray_8 also require that the alphaType is
scroggo
2016/02/12 17:13:55
Not sure - this means that they have an inconsiste
| |
| 38 return true; | |
| 39 case kRGB_565_SkColorType: | |
| 40 return kOpaque_SkAlphaType == alphaType; | |
| 41 default: | |
| 42 return false; | |
| 43 } | |
| 44 } | |
| 45 | |
| 33 static bool read_byte(SkStream* stream, uint8_t* data) | 46 static bool read_byte(SkStream* stream, uint8_t* data) |
| 34 { | 47 { |
| 35 return stream->read(data, 1) == 1; | 48 return stream->read(data, 1) == 1; |
| 36 } | 49 } |
| 37 | 50 |
| 38 // http://en.wikipedia.org/wiki/Variable-length_quantity | 51 // http://en.wikipedia.org/wiki/Variable-length_quantity |
| 39 static bool read_mbf(SkStream* stream, uint64_t* value) { | 52 static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 40 uint64_t n = 0; | 53 uint64_t n = 0; |
| 41 uint8_t data; | 54 uint8_t data; |
| 42 const uint64_t kLimit = 0xFE00000000000000; | 55 const uint64_t kLimit = 0xFE00000000000000; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 } | 90 } |
| 78 return true; | 91 return true; |
| 79 } | 92 } |
| 80 | 93 |
| 81 bool SkWbmpCodec::onRewind() { | 94 bool SkWbmpCodec::onRewind() { |
| 82 return read_header(this->stream(), nullptr); | 95 return read_header(this->stream(), nullptr); |
| 83 } | 96 } |
| 84 | 97 |
| 85 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMC olor* ctable, | 98 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMC olor* ctable, |
| 86 const Options& opts) { | 99 const Options& opts) { |
| 87 // Create the swizzler based on the desired color type | |
| 88 switch (info.colorType()) { | |
| 89 case kIndex_8_SkColorType: | |
| 90 case kN32_SkColorType: | |
| 91 case kRGB_565_SkColorType: | |
| 92 case kGray_8_SkColorType: | |
| 93 break; | |
| 94 default: | |
| 95 return nullptr; | |
| 96 } | |
| 97 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts); | 100 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts); |
| 98 } | 101 } |
| 99 | 102 |
| 100 bool SkWbmpCodec::readRow(uint8_t* row) { | 103 bool SkWbmpCodec::readRow(uint8_t* row) { |
| 101 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; | 104 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; |
| 102 } | 105 } |
| 103 | 106 |
| 104 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) | 107 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) |
| 105 : INHERITED(info, stream) | 108 : INHERITED(info, stream) |
| 106 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width())) | 109 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width())) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 117 size_t rowBytes, | 120 size_t rowBytes, |
| 118 const Options& options, | 121 const Options& options, |
| 119 SkPMColor ctable[], | 122 SkPMColor ctable[], |
| 120 int* ctableCount, | 123 int* ctableCount, |
| 121 int* rowsDecoded) { | 124 int* rowsDecoded) { |
| 122 if (options.fSubset) { | 125 if (options.fSubset) { |
| 123 // Subsets are not supported. | 126 // Subsets are not supported. |
| 124 return kUnimplemented; | 127 return kUnimplemented; |
| 125 } | 128 } |
| 126 | 129 |
| 127 if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) { | 130 if (!valid_color_type(info.colorType(), info.alphaType()) || |
| 131 !valid_alpha(info.alphaType(), this->getInfo().alphaType())) { | |
| 128 return kInvalidConversion; | 132 return kInvalidConversion; |
| 129 } | 133 } |
| 130 | 134 |
| 131 // Prepare a color table if necessary | 135 // Prepare a color table if necessary |
| 132 setup_color_table(info.colorType(), ctable, ctableCount); | 136 setup_color_table(info.colorType(), ctable, ctableCount); |
| 133 | 137 |
| 134 // Initialize the swizzler | 138 // Initialize the swizzler |
| 135 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions)); | 139 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions)); |
| 136 if (nullptr == swizzler.get()) { | 140 SkASSERT(swizzler); |
| 137 return kInvalidConversion; | |
| 138 } | |
| 139 | 141 |
| 140 // Perform the decode | 142 // Perform the decode |
| 141 SkISize size = info.dimensions(); | 143 SkISize size = info.dimensions(); |
| 142 SkAutoTMalloc<uint8_t> src(fSrcRowBytes); | 144 SkAutoTMalloc<uint8_t> src(fSrcRowBytes); |
| 143 void* dstRow = dst; | 145 void* dstRow = dst; |
| 144 for (int y = 0; y < size.height(); ++y) { | 146 for (int y = 0; y < size.height(); ++y) { |
| 145 if (!this->readRow(src.get())) { | 147 if (!this->readRow(src.get())) { |
| 146 *rowsDecoded = y; | 148 *rowsDecoded = y; |
| 147 return kIncompleteInput; | 149 return kIncompleteInput; |
| 148 } | 150 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 return count; | 183 return count; |
| 182 } | 184 } |
| 183 | 185 |
| 184 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 186 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 185 const Options& options, SkPMColor inputColorTable[], int* inputColorCoun t) { | 187 const Options& options, SkPMColor inputColorTable[], int* inputColorCoun t) { |
| 186 if (options.fSubset) { | 188 if (options.fSubset) { |
| 187 // Subsets are not supported. | 189 // Subsets are not supported. |
| 188 return kUnimplemented; | 190 return kUnimplemented; |
| 189 } | 191 } |
| 190 | 192 |
| 191 if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) { | 193 if (!valid_color_type(dstInfo.colorType(), dstInfo.alphaType()) || |
| 194 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) { | |
| 192 return kInvalidConversion; | 195 return kInvalidConversion; |
| 193 } | 196 } |
| 194 | 197 |
| 195 // Fill in the color table | 198 // Fill in the color table |
| 196 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount); | 199 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount); |
| 197 | 200 |
| 198 // Copy the color table to a pointer that can be owned by the scanline decod er | 201 // Copy the color table to a pointer that can be owned by the scanline decod er |
| 199 if (kIndex_8_SkColorType == dstInfo.colorType()) { | 202 if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 200 fColorTable.reset(new SkColorTable(inputColorTable, 2)); | 203 fColorTable.reset(new SkColorTable(inputColorTable, 2)); |
| 201 } | 204 } |
| 202 | 205 |
| 203 // Initialize the swizzler | 206 // Initialize the swizzler |
| 204 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable. get()), options)); | 207 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable. get()), options)); |
| 205 if (nullptr == fSwizzler.get()) { | 208 SkASSERT(fSwizzler); |
| 206 return kInvalidConversion; | |
| 207 } | |
| 208 | 209 |
| 209 fSrcBuffer.reset(fSrcRowBytes); | 210 fSrcBuffer.reset(fSrcRowBytes); |
| 210 | 211 |
| 211 return kSuccess; | 212 return kSuccess; |
| 212 } | 213 } |
| OLD | NEW |