blob: 955760936000ffc0bd82102d6753b26fbc0729c7 [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,
msarette6dd0042015-10-09 11:07:34 -070039 int* inputColorCount,
40 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070041 if (opts.fSubset) {
42 // Subsets are not supported.
43 return kUnimplemented;
44 }
45 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
46 SkCodecPrintf("Error: scaling not supported.\n");
47 return kInvalidScale;
48 }
49 if (!conversion_possible(dstInfo, this->getInfo())) {
50 SkCodecPrintf("Error: cannot convert input type to output type.\n");
51 return kInvalidConversion;
52 }
53
msarett5406d6f2015-08-31 06:55:13 -070054 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
55 if (kSuccess != result) {
56 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070057 }
msarette6dd0042015-10-09 11:07:34 -070058 uint32_t rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
59 if (rows != dstInfo.height()) {
60 *rowsDecoded = rows;
61 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070062 }
msarett5406d6f2015-08-31 06:55:13 -070063 if (fInIco) {
64 return this->decodeIcoMask(dstInfo, dst, dstRowBytes);
65 }
66 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070067}
68
69/*
70 * Process the color table for the bmp input
71 */
72 bool SkBmpStandardCodec::createColorTable(SkAlphaType alphaType, int* numColors) {
73 // Allocate memory for color table
74 uint32_t colorBytes = 0;
75 SkPMColor colorTable[256];
76 if (this->bitsPerPixel() <= 8) {
77 // Inform the caller of the number of colors
78 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070079 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070080 // We set the number of colors to maxColors in order to ensure
81 // safe memory accesses. Otherwise, an invalid pixel could
82 // access memory outside of our color table array.
83 *numColors = maxColors;
84 }
85
86 // Read the color table from the stream
87 colorBytes = fNumColors * fBytesPerColor;
halcanary385fe4d2015-08-26 13:07:48 -070088 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070089 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
90 SkCodecPrintf("Error: unable to read color table.\n");
91 return false;
92 }
93
94 // Choose the proper packing function
95 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t);
96 switch (alphaType) {
97 case kOpaque_SkAlphaType:
98 case kUnpremul_SkAlphaType:
99 packARGB = &SkPackARGB32NoCheck;
100 break;
101 case kPremul_SkAlphaType:
102 packARGB = &SkPreMultiplyARGB;
103 break;
104 default:
105 // This should not be reached because conversion possible
106 // should fail if the alpha type is not one of the above
107 // values.
108 SkASSERT(false);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 packARGB = nullptr;
msarett4ab9d5f2015-08-06 15:34:42 -0700110 break;
111 }
112
113 // Fill in the color table
114 uint32_t i = 0;
115 for (; i < fNumColors; i++) {
116 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
117 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
118 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
119 uint8_t alpha;
120 if (kOpaque_SkAlphaType == alphaType) {
121 alpha = 0xFF;
122 } else {
123 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
124 }
125 colorTable[i] = packARGB(alpha, red, green, blue);
126 }
127
128 // To avoid segmentation faults on bad pixel data, fill the end of the
129 // color table with black. This is the same the behavior as the
130 // chromium decoder.
131 for (; i < maxColors; i++) {
132 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
133 }
134
135 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700136 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700137 }
138
139 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
140 // begins. Pixel data always begins immediately after the color table.
141 if (!fInIco) {
142 // Check that we have not read past the pixel array offset
143 if(fOffset < colorBytes) {
144 // This may occur on OS 2.1 and other old versions where the color
145 // table defaults to max size, and the bmp tries to use a smaller
146 // color table. This is invalid, and our decision is to indicate
147 // an error, rather than try to guess the intended size of the
148 // color table.
149 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
150 return false;
151 }
152
153 // After reading the color table, skip to the start of the pixel array
154 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
155 SkCodecPrintf("Error: unable to skip to image data.\n");
156 return false;
157 }
158 }
159
160 // Return true on success
161 return true;
162}
163
msarettfdb47572015-10-13 12:50:14 -0700164bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700165 // Get swizzler configuration
166 SkSwizzler::SrcConfig config;
167 switch (this->bitsPerPixel()) {
168 case 1:
169 config = SkSwizzler::kIndex1;
170 break;
171 case 2:
172 config = SkSwizzler::kIndex2;
173 break;
174 case 4:
175 config = SkSwizzler::kIndex4;
176 break;
177 case 8:
178 config = SkSwizzler::kIndex;
179 break;
180 case 24:
181 config = SkSwizzler::kBGR;
182 break;
183 case 32:
184 if (kOpaque_SkAlphaType == dstInfo.alphaType()) {
185 config = SkSwizzler::kBGRX;
186 } else {
187 config = SkSwizzler::kBGRA;
188 }
189 break;
190 default:
191 SkASSERT(false);
192 return false;
193 }
194
195 // Get a pointer to the color table if it exists
196 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
197
198 // Create swizzler
msarettfdb47572015-10-13 12:50:14 -0700199 fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts));
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 */
msarette6dd0042015-10-09 11:07:34 -0700230int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700231 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");
msarette6dd0042015-10-09 11:07:34 -0700239 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700240 }
241
242 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700243 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700244
245 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
246 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
247 }
248
msarett5406d6f2015-08-31 06:55:13 -0700249 // Finished decoding the entire image
msarette6dd0042015-10-09 11:07:34 -0700250 return height;
msarett5406d6f2015-08-31 06:55:13 -0700251}
scroggocc2feb12015-08-14 08:32:46 -0700252
msarett5406d6f2015-08-31 06:55:13 -0700253// TODO (msarett): This function will need to be modified in order to perform row by row decodes
254// when the Ico scanline decoder is implemented.
255SkCodec::Result SkBmpStandardCodec::decodeIcoMask(const SkImageInfo& dstInfo,
256 void* dst, size_t dstRowBytes) {
257 // BMP in ICO have transparency, so this cannot be 565, and this mask
258 // prevents us from using kIndex8. The below code depends on the output
259 // being an SkPMColor.
260 SkASSERT(dstInfo.colorType() == kN32_SkColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700261
msarett5406d6f2015-08-31 06:55:13 -0700262 // The AND mask is always 1 bit per pixel
263 const int width = this->getInfo().width();
264 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1));
msarett4ab9d5f2015-08-06 15:34:42 -0700265
msarett5406d6f2015-08-31 06:55:13 -0700266 SkPMColor* dstPtr = (SkPMColor*) dst;
267 for (int y = 0; y < dstInfo.height(); y++) {
268 // The srcBuffer will at least be large enough
269 if (stream()->read(fSrcBuffer.get(), rowBytes) != rowBytes) {
270 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
271 return kIncompleteInput;
272 }
msarett4ab9d5f2015-08-06 15:34:42 -0700273
msarett5406d6f2015-08-31 06:55:13 -0700274 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700275
msarett5406d6f2015-08-31 06:55:13 -0700276 SkPMColor* dstRow =
277 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
278
279 for (int x = 0; x < width; x++) {
280 int quotient;
281 int modulus;
282 SkTDivMod(x, 8, &quotient, &modulus);
283 uint32_t shift = 7 - modulus;
284 uint32_t alphaBit =
285 (fSrcBuffer.get()[quotient] >> shift) & 0x1;
286 dstRow[x] &= alphaBit - 1;
msarett4ab9d5f2015-08-06 15:34:42 -0700287 }
288 }
msarett4ab9d5f2015-08-06 15:34:42 -0700289 return kSuccess;
290}
msarette6dd0042015-10-09 11:07:34 -0700291
292uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const {
293 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
294 if (colorPtr) {
295 return get_color_table_fill_value(colorType, colorPtr, 0);
296 }
297 return INHERITED::onGetFillValue(colorType, alphaType);
298}