blob: 5e65ebbe53d71cda3bf25b46164f96b8157ef34e [file] [log] [blame]
msarett4ab9d5f2015-08-06 15:34:42 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBmpStandardCodec.h"
9#include "SkCodecPriv.h"
10#include "SkColorPriv.h"
msarett4ab9d5f2015-08-06 15:34:42 -070011#include "SkStream.h"
12
13/*
msarett4ab9d5f2015-08-06 15:34:42 -070014 * Creates an instance of the decoder
15 * Called only by NewFromStream
16 */
17SkBmpStandardCodec::SkBmpStandardCodec(const SkImageInfo& info, SkStream* stream,
18 uint16_t bitsPerPixel, uint32_t numColors,
19 uint32_t bytesPerColor, uint32_t offset,
scroggo46c57472015-09-30 08:57:13 -070020 SkCodec::SkScanlineOrder rowOrder, bool inIco)
msarett4ab9d5f2015-08-06 15:34:42 -070021 : INHERITED(info, stream, bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070022 , fColorTable(nullptr)
msarett4ab9d5f2015-08-06 15:34:42 -070023 , fNumColors(this->computeNumColors(numColors))
24 , fBytesPerColor(bytesPerColor)
25 , fOffset(offset)
halcanary96fcdcc2015-08-27 07:41:13 -070026 , fSwizzler(nullptr)
msarett5406d6f2015-08-31 06:55:13 -070027 , fSrcRowBytes(SkAlign4(compute_row_bytes(this->getInfo().width(), this->bitsPerPixel())))
28 , fSrcBuffer(new uint8_t [fSrcRowBytes])
msarett4ab9d5f2015-08-06 15:34:42 -070029 , fInIco(inIco)
30{}
31
32/*
33 * Initiates the bitmap decode
34 */
35SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
36 void* dst, size_t dstRowBytes,
37 const Options& opts,
38 SkPMColor* inputColorPtr,
39 int* inputColorCount) {
msarett4ab9d5f2015-08-06 15:34:42 -070040 if (opts.fSubset) {
41 // Subsets are not supported.
42 return kUnimplemented;
43 }
44 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
45 SkCodecPrintf("Error: scaling not supported.\n");
46 return kInvalidScale;
47 }
48 if (!conversion_possible(dstInfo, this->getInfo())) {
49 SkCodecPrintf("Error: cannot convert input type to output type.\n");
50 return kInvalidConversion;
51 }
52
msarett5406d6f2015-08-31 06:55:13 -070053 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
54 if (kSuccess != result) {
55 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070056 }
msarett5406d6f2015-08-31 06:55:13 -070057 result = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
58 if (kSuccess != result) {
59 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070060 }
msarett5406d6f2015-08-31 06:55:13 -070061 if (fInIco) {
62 return this->decodeIcoMask(dstInfo, dst, dstRowBytes);
63 }
64 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070065}
66
67/*
68 * Process the color table for the bmp input
69 */
70 bool SkBmpStandardCodec::createColorTable(SkAlphaType alphaType, int* numColors) {
71 // Allocate memory for color table
72 uint32_t colorBytes = 0;
73 SkPMColor colorTable[256];
74 if (this->bitsPerPixel() <= 8) {
75 // Inform the caller of the number of colors
76 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070077 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070078 // We set the number of colors to maxColors in order to ensure
79 // safe memory accesses. Otherwise, an invalid pixel could
80 // access memory outside of our color table array.
81 *numColors = maxColors;
82 }
83
84 // Read the color table from the stream
85 colorBytes = fNumColors * fBytesPerColor;
halcanary385fe4d2015-08-26 13:07:48 -070086 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070087 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
88 SkCodecPrintf("Error: unable to read color table.\n");
89 return false;
90 }
91
92 // Choose the proper packing function
93 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t);
94 switch (alphaType) {
95 case kOpaque_SkAlphaType:
96 case kUnpremul_SkAlphaType:
97 packARGB = &SkPackARGB32NoCheck;
98 break;
99 case kPremul_SkAlphaType:
100 packARGB = &SkPreMultiplyARGB;
101 break;
102 default:
103 // This should not be reached because conversion possible
104 // should fail if the alpha type is not one of the above
105 // values.
106 SkASSERT(false);
halcanary96fcdcc2015-08-27 07:41:13 -0700107 packARGB = nullptr;
msarett4ab9d5f2015-08-06 15:34:42 -0700108 break;
109 }
110
111 // Fill in the color table
112 uint32_t i = 0;
113 for (; i < fNumColors; i++) {
114 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
115 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
116 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
117 uint8_t alpha;
118 if (kOpaque_SkAlphaType == alphaType) {
119 alpha = 0xFF;
120 } else {
121 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
122 }
123 colorTable[i] = packARGB(alpha, red, green, blue);
124 }
125
126 // To avoid segmentation faults on bad pixel data, fill the end of the
127 // color table with black. This is the same the behavior as the
128 // chromium decoder.
129 for (; i < maxColors; i++) {
130 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
131 }
132
133 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700134 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700135 }
136
137 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
138 // begins. Pixel data always begins immediately after the color table.
139 if (!fInIco) {
140 // Check that we have not read past the pixel array offset
141 if(fOffset < colorBytes) {
142 // This may occur on OS 2.1 and other old versions where the color
143 // table defaults to max size, and the bmp tries to use a smaller
144 // color table. This is invalid, and our decision is to indicate
145 // an error, rather than try to guess the intended size of the
146 // color table.
147 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
148 return false;
149 }
150
151 // After reading the color table, skip to the start of the pixel array
152 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
153 SkCodecPrintf("Error: unable to skip to image data.\n");
154 return false;
155 }
156 }
157
158 // Return true on success
159 return true;
160}
161
162bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo,
163 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700164 // Get swizzler configuration
165 SkSwizzler::SrcConfig config;
166 switch (this->bitsPerPixel()) {
167 case 1:
168 config = SkSwizzler::kIndex1;
169 break;
170 case 2:
171 config = SkSwizzler::kIndex2;
172 break;
173 case 4:
174 config = SkSwizzler::kIndex4;
175 break;
176 case 8:
177 config = SkSwizzler::kIndex;
178 break;
179 case 24:
180 config = SkSwizzler::kBGR;
181 break;
182 case 32:
183 if (kOpaque_SkAlphaType == dstInfo.alphaType()) {
184 config = SkSwizzler::kBGRX;
185 } else {
186 config = SkSwizzler::kBGRA;
187 }
188 break;
189 default:
190 SkASSERT(false);
191 return false;
192 }
193
194 // Get a pointer to the color table if it exists
195 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
196
197 // Create swizzler
198 fSwizzler.reset(SkSwizzler::CreateSwizzler(config,
emmaleer8f4ba762015-08-14 07:44:46 -0700199 colorPtr, dstInfo, opts.fZeroInitialized, this->getInfo()));
msarett4ab9d5f2015-08-06 15:34:42 -0700200
halcanary96fcdcc2015-08-27 07:41:13 -0700201 if (nullptr == fSwizzler.get()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700202 return false;
203 }
204 return true;
205}
206
msarett5406d6f2015-08-31 06:55:13 -0700207SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
208 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
209 // Create the color table if necessary and prepare the stream for decode
210 // Note that if it is non-NULL, inputColorCount will be modified
211 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) {
212 SkCodecPrintf("Error: could not create color table.\n");
213 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700214 }
msarett5406d6f2015-08-31 06:55:13 -0700215
216 // Copy the color table to the client if necessary
217 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
218
219 // Initialize a swizzler if necessary
220 if (!this->initializeSwizzler(dstInfo, options)) {
221 SkCodecPrintf("Error: cannot initialize swizzler.\n");
222 return SkCodec::kInvalidConversion;
223 }
224 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700225}
226
227/*
228 * Performs the bitmap decoding for standard input format
229 */
msarett5406d6f2015-08-31 06:55:13 -0700230SkCodec::Result SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo,
231 void* dst, size_t dstRowBytes,
232 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700233 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700234 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700235 for (int y = 0; y < height; y++) {
236 // Read a row of the input
msarett5406d6f2015-08-31 06:55:13 -0700237 if (this->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRowBytes) {
msarett4ab9d5f2015-08-06 15:34:42 -0700238 SkCodecPrintf("Warning: incomplete input stream.\n");
239 // Fill the destination image on failure
msarett5406d6f2015-08-31 06:55:13 -0700240 void* dstStart = this->getDstStartRow(dst, dstRowBytes, y);
241 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
242 uint32_t fillColorOrIndex = get_fill_color_or_index(dstInfo.alphaType());
243 SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, dstInfo.height() - y,
244 fillColorOrIndex, colorPtr, opts.fZeroInitialized);
msarett4ab9d5f2015-08-06 15:34:42 -0700245 return kIncompleteInput;
246 }
247
248 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700249 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700250
251 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
252 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
253 }
254
msarett5406d6f2015-08-31 06:55:13 -0700255 // Finished decoding the entire image
256 return kSuccess;
257}
scroggocc2feb12015-08-14 08:32:46 -0700258
msarett5406d6f2015-08-31 06:55:13 -0700259// TODO (msarett): This function will need to be modified in order to perform row by row decodes
260// when the Ico scanline decoder is implemented.
261SkCodec::Result SkBmpStandardCodec::decodeIcoMask(const SkImageInfo& dstInfo,
262 void* dst, size_t dstRowBytes) {
263 // 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
265 // being an SkPMColor.
266 SkASSERT(dstInfo.colorType() == kN32_SkColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700267
msarett5406d6f2015-08-31 06:55:13 -0700268 // The AND mask is always 1 bit per pixel
269 const int width = this->getInfo().width();
270 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1));
msarett4ab9d5f2015-08-06 15:34:42 -0700271
msarett5406d6f2015-08-31 06:55:13 -0700272 SkPMColor* dstPtr = (SkPMColor*) dst;
273 for (int y = 0; y < dstInfo.height(); y++) {
274 // The srcBuffer will at least be large enough
275 if (stream()->read(fSrcBuffer.get(), rowBytes) != rowBytes) {
276 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
277 return kIncompleteInput;
278 }
msarett4ab9d5f2015-08-06 15:34:42 -0700279
msarett5406d6f2015-08-31 06:55:13 -0700280 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700281
msarett5406d6f2015-08-31 06:55:13 -0700282 SkPMColor* dstRow =
283 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
284
285 for (int x = 0; x < width; x++) {
286 int quotient;
287 int modulus;
288 SkTDivMod(x, 8, &quotient, &modulus);
289 uint32_t shift = 7 - modulus;
290 uint32_t alphaBit =
291 (fSrcBuffer.get()[quotient] >> shift) & 0x1;
292 dstRow[x] &= alphaBit - 1;
msarett4ab9d5f2015-08-06 15:34:42 -0700293 }
294 }
msarett4ab9d5f2015-08-06 15:34:42 -0700295 return kSuccess;
296}