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" |
11 #include "SkStream.h" | 11 #include "SkStream.h" |
12 | 12 |
13 /* | 13 /* |
14 * Creates an instance of the decoder | 14 * Creates an instance of the decoder |
15 * Called only by NewFromStream | 15 * Called only by NewFromStream |
16 */ | 16 */ |
17 SkBmpStandardCodec::SkBmpStandardCodec(const SkImageInfo& info, SkStream* stream , | 17 SkBmpStandardCodec::SkBmpStandardCodec(const SkImageInfo& info, SkStream* stream , |
18 uint16_t bitsPerPixel, uint32_t numColors , | 18 uint16_t bitsPerPixel, uint32_t numColors , |
19 uint32_t bytesPerColor, uint32_t offset, | 19 uint32_t bytesPerColor, uint32_t offset, |
20 SkCodec::SkScanlineOrder rowOrder, bool i nIco) | 20 SkCodec::SkScanlineOrder rowOrder, bool i nIco) |
21 : INHERITED(info, stream, bitsPerPixel, rowOrder) | 21 : INHERITED(info, stream, bitsPerPixel, rowOrder) |
22 , fColorTable(nullptr) | 22 , fColorTable(nullptr) |
23 , fNumColors(this->computeNumColors(numColors)) | 23 , fNumColors(numColors) |
24 , fBytesPerColor(bytesPerColor) | 24 , fBytesPerColor(bytesPerColor) |
25 , fOffset(offset) | 25 , fOffset(offset) |
26 , fSwizzler(nullptr) | 26 , fSwizzler(nullptr) |
27 , fSrcRowBytes(SkAlign4(compute_row_bytes(this->getInfo().width(), this->bit sPerPixel()))) | 27 , fSrcRowBytes(SkAlign4(compute_row_bytes(this->getInfo().width(), this->bit sPerPixel()))) |
28 , fSrcBuffer(new uint8_t [fSrcRowBytes]) | 28 , fSrcBuffer(new uint8_t [fSrcRowBytes]) |
29 , fInIco(inIco) | 29 , fInIco(inIco) |
30 {} | 30 {} |
31 | 31 |
32 /* | 32 /* |
33 * Initiates the bitmap decode | 33 * Initiates the bitmap decode |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 SkPMColor colorTable[256]; | 75 SkPMColor colorTable[256]; |
76 if (this->bitsPerPixel() <= 8) { | 76 if (this->bitsPerPixel() <= 8) { |
77 // Inform the caller of the number of colors | 77 // Inform the caller of the number of colors |
78 uint32_t maxColors = 1 << this->bitsPerPixel(); | 78 uint32_t maxColors = 1 << this->bitsPerPixel(); |
79 if (nullptr != numColors) { | 79 if (nullptr != numColors) { |
80 // 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 |
81 // safe memory accesses. Otherwise, an invalid pixel could | 81 // safe memory accesses. Otherwise, an invalid pixel could |
82 // access memory outside of our color table array. | 82 // access memory outside of our color table array. |
83 *numColors = maxColors; | 83 *numColors = maxColors; |
84 } | 84 } |
85 // Don't bother reading more than maxColors. | |
86 uint32_t numColorsToRead = fNumColors == 0 ? maxColors : SkTMin(fNumColo rs, maxColors); | |
scroggo
2015/12/04 16:28:57
const?
dogben
2015/12/04 16:34:43
Done.
| |
85 | 87 |
86 // Read the color table from the stream | 88 // Read the color table from the stream |
87 colorBytes = fNumColors * fBytesPerColor; | 89 colorBytes = numColorsToRead * fBytesPerColor; |
88 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); | 90 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); |
89 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { | 91 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
90 SkCodecPrintf("Error: unable to read color table.\n"); | 92 SkCodecPrintf("Error: unable to read color table.\n"); |
91 return false; | 93 return false; |
92 } | 94 } |
93 | 95 |
94 // Choose the proper packing function | 96 // Choose the proper packing function |
95 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t); | 97 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t); |
96 switch (alphaType) { | 98 switch (alphaType) { |
97 case kOpaque_SkAlphaType: | 99 case kOpaque_SkAlphaType: |
98 case kUnpremul_SkAlphaType: | 100 case kUnpremul_SkAlphaType: |
99 packARGB = &SkPackARGB32NoCheck; | 101 packARGB = &SkPackARGB32NoCheck; |
100 break; | 102 break; |
101 case kPremul_SkAlphaType: | 103 case kPremul_SkAlphaType: |
102 packARGB = &SkPreMultiplyARGB; | 104 packARGB = &SkPreMultiplyARGB; |
103 break; | 105 break; |
104 default: | 106 default: |
105 // This should not be reached because conversion possible | 107 // This should not be reached because conversion possible |
106 // should fail if the alpha type is not one of the above | 108 // should fail if the alpha type is not one of the above |
107 // values. | 109 // values. |
108 SkASSERT(false); | 110 SkASSERT(false); |
109 packARGB = nullptr; | 111 packARGB = nullptr; |
110 break; | 112 break; |
111 } | 113 } |
112 | 114 |
113 // Fill in the color table | 115 // Fill in the color table |
114 uint32_t i = 0; | 116 uint32_t i = 0; |
115 for (; i < fNumColors; i++) { | 117 for (; i < numColorsToRead; i++) { |
116 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); | 118 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
117 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); | 119 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
118 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); | 120 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
119 uint8_t alpha; | 121 uint8_t alpha; |
120 if (kOpaque_SkAlphaType == alphaType) { | 122 if (kOpaque_SkAlphaType == alphaType) { |
121 alpha = 0xFF; | 123 alpha = 0xFF; |
122 } else { | 124 } else { |
123 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3); | 125 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3); |
124 } | 126 } |
125 colorTable[i] = packARGB(alpha, red, green, blue); | 127 colorTable[i] = packARGB(alpha, red, green, blue); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 return kSuccess; | 291 return kSuccess; |
290 } | 292 } |
291 | 293 |
292 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType a lphaType) const { | 294 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType a lphaType) const { |
293 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 295 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
294 if (colorPtr) { | 296 if (colorPtr) { |
295 return get_color_table_fill_value(colorType, colorPtr, 0); | 297 return get_color_table_fill_value(colorType, colorPtr, 0); |
296 } | 298 } |
297 return INHERITED::onGetFillValue(colorType, alphaType); | 299 return INHERITED::onGetFillValue(colorType, alphaType); |
298 } | 300 } |
OLD | NEW |