blob: 938fe8c7882b5565dc3951ea39c1471315d2abae [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
164bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo,
165 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700166 // Get swizzler configuration
167 SkSwizzler::SrcConfig config;
168 switch (this->bitsPerPixel()) {
169 case 1:
170 config = SkSwizzler::kIndex1;
171 break;
172 case 2:
173 config = SkSwizzler::kIndex2;
174 break;
175 case 4:
176 config = SkSwizzler::kIndex4;
177 break;
178 case 8:
179 config = SkSwizzler::kIndex;
180 break;
181 case 24:
182 config = SkSwizzler::kBGR;
183 break;
184 case 32:
185 if (kOpaque_SkAlphaType == dstInfo.alphaType()) {
186 config = SkSwizzler::kBGRX;
187 } else {
188 config = SkSwizzler::kBGRA;
189 }
190 break;
191 default:
192 SkASSERT(false);
193 return false;
194 }
195
196 // Get a pointer to the color table if it exists
197 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
198
199 // Create swizzler
200 fSwizzler.reset(SkSwizzler::CreateSwizzler(config,
scroggoe7fc14b2015-10-02 13:14:46 -0700201 colorPtr, dstInfo, opts.fZeroInitialized));
msarett4ab9d5f2015-08-06 15:34:42 -0700202
halcanary96fcdcc2015-08-27 07:41:13 -0700203 if (nullptr == fSwizzler.get()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700204 return false;
205 }
206 return true;
207}
208
msarett5406d6f2015-08-31 06:55:13 -0700209SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
210 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
211 // Create the color table if necessary and prepare the stream for decode
212 // Note that if it is non-NULL, inputColorCount will be modified
213 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) {
214 SkCodecPrintf("Error: could not create color table.\n");
215 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700216 }
msarett5406d6f2015-08-31 06:55:13 -0700217
218 // Copy the color table to the client if necessary
219 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
220
221 // Initialize a swizzler if necessary
222 if (!this->initializeSwizzler(dstInfo, options)) {
223 SkCodecPrintf("Error: cannot initialize swizzler.\n");
224 return SkCodec::kInvalidConversion;
225 }
226 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700227}
228
229/*
230 * Performs the bitmap decoding for standard input format
231 */
msarette6dd0042015-10-09 11:07:34 -0700232int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700233 void* dst, size_t dstRowBytes,
234 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700235 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700236 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700237 for (int y = 0; y < height; y++) {
238 // Read a row of the input
msarett5406d6f2015-08-31 06:55:13 -0700239 if (this->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRowBytes) {
msarett4ab9d5f2015-08-06 15:34:42 -0700240 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700241 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700242 }
243
244 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700245 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700246
247 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
248 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
249 }
250
msarett5406d6f2015-08-31 06:55:13 -0700251 // Finished decoding the entire image
msarette6dd0042015-10-09 11:07:34 -0700252 return height;
msarett5406d6f2015-08-31 06:55:13 -0700253}
scroggocc2feb12015-08-14 08:32:46 -0700254
msarett5406d6f2015-08-31 06:55:13 -0700255// TODO (msarett): This function will need to be modified in order to perform row by row decodes
256// when the Ico scanline decoder is implemented.
257SkCodec::Result SkBmpStandardCodec::decodeIcoMask(const SkImageInfo& dstInfo,
258 void* dst, size_t dstRowBytes) {
259 // BMP in ICO have transparency, so this cannot be 565, and this mask
260 // prevents us from using kIndex8. The below code depends on the output
261 // being an SkPMColor.
262 SkASSERT(dstInfo.colorType() == kN32_SkColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700263
msarett5406d6f2015-08-31 06:55:13 -0700264 // The AND mask is always 1 bit per pixel
265 const int width = this->getInfo().width();
266 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1));
msarett4ab9d5f2015-08-06 15:34:42 -0700267
msarett5406d6f2015-08-31 06:55:13 -0700268 SkPMColor* dstPtr = (SkPMColor*) dst;
269 for (int y = 0; y < dstInfo.height(); y++) {
270 // The srcBuffer will at least be large enough
271 if (stream()->read(fSrcBuffer.get(), rowBytes) != rowBytes) {
272 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
273 return kIncompleteInput;
274 }
msarett4ab9d5f2015-08-06 15:34:42 -0700275
msarett5406d6f2015-08-31 06:55:13 -0700276 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700277
msarett5406d6f2015-08-31 06:55:13 -0700278 SkPMColor* dstRow =
279 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
280
281 for (int x = 0; x < width; x++) {
282 int quotient;
283 int modulus;
284 SkTDivMod(x, 8, &quotient, &modulus);
285 uint32_t shift = 7 - modulus;
286 uint32_t alphaBit =
287 (fSrcBuffer.get()[quotient] >> shift) & 0x1;
288 dstRow[x] &= alphaBit - 1;
msarett4ab9d5f2015-08-06 15:34:42 -0700289 }
290 }
msarett4ab9d5f2015-08-06 15:34:42 -0700291 return kSuccess;
292}
msarette6dd0042015-10-09 11:07:34 -0700293
294uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const {
295 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
296 if (colorPtr) {
297 return get_color_table_fill_value(colorType, colorPtr, 0);
298 }
299 return INHERITED::onGetFillValue(colorType, alphaType);
300}