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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 164 |
168 // Create swizzler | 165 // Create swizzler |
169 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, colorPtr, dstInfo,
opts)); | 166 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, colorPtr, dstInfo,
opts)); |
170 SkASSERT(fSwizzler); | 167 SkASSERT(fSwizzler); |
171 } | 168 } |
172 | 169 |
173 SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo, | 170 SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo, |
174 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { | 171 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo
lorCount) { |
175 // Create the color table if necessary and prepare the stream for decode | 172 // Create the color table if necessary and prepare the stream for decode |
176 // Note that if it is non-NULL, inputColorCount will be modified | 173 // Note that if it is non-NULL, inputColorCount will be modified |
177 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) { | 174 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputC
olorCount)) { |
178 SkCodecPrintf("Error: could not create color table.\n"); | 175 SkCodecPrintf("Error: could not create color table.\n"); |
179 return SkCodec::kInvalidInput; | 176 return SkCodec::kInvalidInput; |
180 } | 177 } |
181 | 178 |
182 // Copy the color table to the client if necessary | 179 // Copy the color table to the client if necessary |
183 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; | 180 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount)
; |
184 | 181 |
185 // Initialize a swizzler | 182 // Initialize a swizzler |
186 this->initializeSwizzler(dstInfo, options); | 183 this->initializeSwizzler(dstInfo, options); |
187 return SkCodec::kSuccess; | 184 return SkCodec::kSuccess; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 253 } |
257 | 254 |
258 return height; | 255 return height; |
259 } | 256 } |
260 | 257 |
261 void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstI
nfo, | 258 void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstI
nfo, |
262 void* dst, size_t dstRowBytes) { | 259 void* dst, size_t dstRowBytes) { |
263 // BMP in ICO have transparency, so this cannot be 565, and this mask | 260 // BMP in ICO have transparency, so this cannot be 565, and this mask |
264 // prevents us from using kIndex8. The below code depends on the output | 261 // prevents us from using kIndex8. The below code depends on the output |
265 // being an SkPMColor. | 262 // being an SkPMColor. |
266 SkASSERT(dstInfo.colorType() == kN32_SkColorType); | 263 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() || |
| 264 kBGRA_8888_SkColorType == dstInfo.colorType()); |
267 | 265 |
268 // If we are sampling, make sure that we only mask the sampled pixels. | 266 // If we are sampling, make sure that we only mask the sampled pixels. |
269 // We do not need to worry about sampling in the y-dimension because that | 267 // We do not need to worry about sampling in the y-dimension because that |
270 // should be handled by SkSampledCodec. | 268 // should be handled by SkSampledCodec. |
271 const int sampleX = fSwizzler->sampleX(); | 269 const int sampleX = fSwizzler->sampleX(); |
272 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampl
eX); | 270 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampl
eX); |
273 const int srcStartX = get_start_coord(sampleX); | 271 const int srcStartX = get_start_coord(sampleX); |
274 | 272 |
275 | 273 |
276 SkPMColor* dstPtr = (SkPMColor*) dst; | 274 SkPMColor* dstPtr = (SkPMColor*) dst; |
(...skipping 22 matching lines...) Expand all Loading... |
299 } | 297 } |
300 } | 298 } |
301 | 299 |
302 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const { | 300 uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const { |
303 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 301 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
304 if (colorPtr) { | 302 if (colorPtr) { |
305 return get_color_table_fill_value(colorType, colorPtr, 0); | 303 return get_color_table_fill_value(colorType, colorPtr, 0); |
306 } | 304 } |
307 return INHERITED::onGetFillValue(colorType); | 305 return INHERITED::onGetFillValue(colorType); |
308 } | 306 } |
OLD | NEW |