| 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 "SkBmpStandardCodec.h" | 8 #include "SkBmpStandardCodec.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 if (rows != dstInfo.height()) { | 61 if (rows != dstInfo.height()) { |
| 62 *rowsDecoded = rows; | 62 *rowsDecoded = rows; |
| 63 return kIncompleteInput; | 63 return kIncompleteInput; |
| 64 } | 64 } |
| 65 return kSuccess; | 65 return kSuccess; |
| 66 } | 66 } |
| 67 | 67 |
| 68 /* | 68 /* |
| 69 * Process the color table for the bmp input | 69 * Process the color table for the bmp input |
| 70 */ | 70 */ |
| 71 bool SkBmpStandardCodec::createColorTable(SkAlphaType dstAlphaType, int* numCol
ors) { | 71 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType
dstAlphaType, |
| 72 int* numColors) { |
| 72 // Allocate memory for color table | 73 // Allocate memory for color table |
| 73 uint32_t colorBytes = 0; | 74 uint32_t colorBytes = 0; |
| 74 SkPMColor colorTable[256]; | 75 SkPMColor colorTable[256]; |
| 75 if (this->bitsPerPixel() <= 8) { | 76 if (this->bitsPerPixel() <= 8) { |
| 76 // Inform the caller of the number of colors | 77 // Inform the caller of the number of colors |
| 77 uint32_t maxColors = 1 << this->bitsPerPixel(); | 78 uint32_t maxColors = 1 << this->bitsPerPixel(); |
| 78 if (nullptr != numColors) { | 79 if (nullptr != numColors) { |
| 79 // We set the number of colors to maxColors in order to ensure | 80 // We set the number of colors to maxColors in order to ensure |
| 80 // safe memory accesses. Otherwise, an invalid pixel could | 81 // safe memory accesses. Otherwise, an invalid pixel could |
| 81 // access memory outside of our color table array. | 82 // access memory outside of our color table array. |
| 82 *numColors = maxColors; | 83 *numColors = maxColors; |
| 83 } | 84 } |
| 84 // Don't bother reading more than maxColors. | 85 // Don't bother reading more than maxColors. |
| 85 const uint32_t numColorsToRead = | 86 const uint32_t numColorsToRead = |
| 86 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); | 87 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); |
| 87 | 88 |
| 88 // Read the color table from the stream | 89 // Read the color table from the stream |
| 89 colorBytes = numColorsToRead * fBytesPerColor; | 90 colorBytes = numColorsToRead * fBytesPerColor; |
| 90 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); | 91 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); |
| 91 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { | 92 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
| 92 SkCodecPrintf("Error: unable to read color table.\n"); | 93 SkCodecPrintf("Error: unable to read color table.\n"); |
| 93 return false; | 94 return false; |
| 94 } | 95 } |
| 95 | 96 |
| 96 // Choose the proper packing function | 97 // Choose the proper packing function |
| 97 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t); | 98 bool isPremul = (kPremul_SkAlphaType == dstAlphaType) && !fIsOpaque; |
| 98 if (fIsOpaque || kUnpremul_SkAlphaType == dstAlphaType) { | 99 PackColorProc packARGB = choose_pack_color_proc(isPremul, dstColorType); |
| 99 packARGB = &SkPackARGB32NoCheck; | |
| 100 } else { | |
| 101 packARGB = &SkPremultiplyARGBInline; | |
| 102 } | |
| 103 | 100 |
| 104 // Fill in the color table | 101 // Fill in the color table |
| 105 uint32_t i = 0; | 102 uint32_t i = 0; |
| 106 for (; i < numColorsToRead; i++) { | 103 for (; i < numColorsToRead; i++) { |
| 107 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); | 104 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 108 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); | 105 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 109 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); | 106 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 110 uint8_t alpha; | 107 uint8_t alpha; |
| 111 if (fIsOpaque) { | 108 if (fIsOpaque) { |
| 112 alpha = 0xFF; | 109 alpha = 0xFF; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 SkCodecPrintf("Error: unable to skip to image data.\n"); | 143 SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 147 return false; | 144 return false; |
| 148 } | 145 } |
| 149 } | 146 } |
| 150 | 147 |
| 151 // Return true on success | 148 // Return true on success |
| 152 return true; | 149 return true; |
| 153 } | 150 } |
| 154 | 151 |
| 155 void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Op
tions& opts) { | 152 void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Op
tions& opts) { |
| 156 // Get swizzler configuration | |
| 157 SkSwizzler::SrcConfig config = SkSwizzler::kUnknown; | |
| 158 switch (this->bitsPerPixel()) { | |
| 159 case 1: | |
| 160 config = SkSwizzler::kIndex1; | |
| 161 break; | |
| 162 case 2: | |
| 163 config = SkSwizzler::kIndex2; | |
| 164 break; | |
| 165 case 4: | |
| 166 config = SkSwizzler::kIndex4; | |
| 167 break; | |
| 168 case 8: | |
| 169 config = SkSwizzler::kIndex; | |
| 170 break; | |
| 171 case 24: | |
| 172 config = SkSwizzler::kBGR; | |
| 173 break; | |
| 174 case 32: | |
| 175 if (fIsOpaque) { | |
| 176 config = SkSwizzler::kBGRX; | |
| 177 } else { | |
| 178 config = SkSwizzler::kBGRA; | |
| 179 } | |
| 180 break; | |
| 181 default: | |
| 182 SkASSERT(false); | |
| 183 } | |
| 184 | |
| 185 // Get a pointer to the color table if it exists | 153 // Get a pointer to the color table if it exists |
| 186 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 154 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 187 | 155 |
| 188 // Create swizzler | 156 // Create swizzler |
| 189 fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts))
; | 157 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colorPtr,
dstInfo, opts)); |
| 190 SkASSERT(fSwizzler); | 158 SkASSERT(fSwizzler); |
| 191 } | 159 } |
| 192 | 160 |
| 193 SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo, | 161 SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 194 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { | 162 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { |
| 195 // Create the color table if necessary and prepare the stream for decode | 163 // Create the color table if necessary and prepare the stream for decode |
| 196 // Note that if it is non-NULL, inputColorCount will be modified | 164 // Note that if it is non-NULL, inputColorCount will be modified |
| 197 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) { | 165 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputC
olorCount)) { |
| 198 SkCodecPrintf("Error: could not create color table.\n"); | 166 SkCodecPrintf("Error: could not create color table.\n"); |
| 199 return SkCodec::kInvalidInput; | 167 return SkCodec::kInvalidInput; |
| 200 } | 168 } |
| 201 | 169 |
| 202 // Copy the color table to the client if necessary | 170 // Copy the color table to the client if necessary |
| 203 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; | 171 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; |
| 204 | 172 |
| 205 // Initialize a swizzler | 173 // Initialize a swizzler |
| 206 this->initializeSwizzler(dstInfo, options); | 174 this->initializeSwizzler(dstInfo, options); |
| 207 return SkCodec::kSuccess; | 175 return SkCodec::kSuccess; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } | 287 } |
| 320 } | 288 } |
| 321 | 289 |
| 322 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const { | 290 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const { |
| 323 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 291 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 324 if (colorPtr) { | 292 if (colorPtr) { |
| 325 return get_color_table_fill_value(colorType, colorPtr, 0); | 293 return get_color_table_fill_value(colorType, colorPtr, 0); |
| 326 } | 294 } |
| 327 return INHERITED::onGetFillValue(colorType); | 295 return INHERITED::onGetFillValue(colorType); |
| 328 } | 296 } |
| OLD | NEW |