blob: 203e7da654568c0de1e8e33e427e96a41fd64ca4 [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)
benjaminwagner886e5e42015-12-04 08:48:26 -080023 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070024 , 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)
msarettbe8216a2015-12-04 08:00:50 -080030 , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->getInfo().width(), 1)) : 0)
msarett4ab9d5f2015-08-06 15:34:42 -070031{}
32
33/*
34 * Initiates the bitmap decode
35 */
36SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
37 void* dst, size_t dstRowBytes,
38 const Options& opts,
39 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -070040 int* inputColorCount,
41 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070042 if (opts.fSubset) {
43 // Subsets are not supported.
44 return kUnimplemented;
45 }
46 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
47 SkCodecPrintf("Error: scaling not supported.\n");
48 return kInvalidScale;
49 }
50 if (!conversion_possible(dstInfo, this->getInfo())) {
51 SkCodecPrintf("Error: cannot convert input type to output type.\n");
52 return kInvalidConversion;
53 }
54
msarett5406d6f2015-08-31 06:55:13 -070055 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
56 if (kSuccess != result) {
57 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070058 }
msarettf724b992015-10-15 06:41:06 -070059 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070060 if (rows != dstInfo.height()) {
61 *rowsDecoded = rows;
62 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070063 }
msarett5406d6f2015-08-31 06:55:13 -070064 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070065}
66
67/*
68 * Process the color table for the bmp input
69 */
msarettebf44082016-02-03 13:12:38 -080070 bool SkBmpStandardCodec::createColorTable(SkAlphaType dstAlphaType, int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070071 // 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 }
benjaminwagner886e5e42015-12-04 08:48:26 -080083 // Don't bother reading more than maxColors.
84 const uint32_t numColorsToRead =
85 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070086
87 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080088 colorBytes = numColorsToRead * fBytesPerColor;
halcanary385fe4d2015-08-26 13:07:48 -070089 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070090 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
91 SkCodecPrintf("Error: unable to read color table.\n");
92 return false;
93 }
94
95 // Choose the proper packing function
96 SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t);
msarettebf44082016-02-03 13:12:38 -080097 SkAlphaType encodedAlphaType = this->getInfo().alphaType();
98 if (kOpaque_SkAlphaType == encodedAlphaType || kUnpremul_SkAlphaType == dstAlphaType) {
99 packARGB = &SkPackARGB32NoCheck;
100 } else {
101 packARGB = &SkPremultiplyARGBInline;
msarett4ab9d5f2015-08-06 15:34:42 -0700102 }
103
104 // Fill in the color table
105 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800106 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700107 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
108 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
109 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
110 uint8_t alpha;
msarettebf44082016-02-03 13:12:38 -0800111 if (kOpaque_SkAlphaType == encodedAlphaType) {
msarett4ab9d5f2015-08-06 15:34:42 -0700112 alpha = 0xFF;
113 } else {
114 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
115 }
116 colorTable[i] = packARGB(alpha, red, green, blue);
117 }
118
119 // To avoid segmentation faults on bad pixel data, fill the end of the
120 // color table with black. This is the same the behavior as the
121 // chromium decoder.
122 for (; i < maxColors; i++) {
123 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
124 }
125
126 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700127 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700128 }
129
130 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
131 // begins. Pixel data always begins immediately after the color table.
132 if (!fInIco) {
133 // Check that we have not read past the pixel array offset
134 if(fOffset < colorBytes) {
135 // This may occur on OS 2.1 and other old versions where the color
136 // table defaults to max size, and the bmp tries to use a smaller
137 // color table. This is invalid, and our decision is to indicate
138 // an error, rather than try to guess the intended size of the
139 // color table.
140 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
141 return false;
142 }
143
144 // After reading the color table, skip to the start of the pixel array
145 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
146 SkCodecPrintf("Error: unable to skip to image data.\n");
147 return false;
148 }
149 }
150
151 // Return true on success
152 return true;
153}
154
msarettfdb47572015-10-13 12:50:14 -0700155bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700156 // Get swizzler configuration
157 SkSwizzler::SrcConfig config;
158 switch (this->bitsPerPixel()) {
159 case 1:
160 config = SkSwizzler::kIndex1;
161 break;
162 case 2:
163 config = SkSwizzler::kIndex2;
164 break;
165 case 4:
166 config = SkSwizzler::kIndex4;
167 break;
168 case 8:
169 config = SkSwizzler::kIndex;
170 break;
171 case 24:
172 config = SkSwizzler::kBGR;
173 break;
174 case 32:
scroggoc5560be2016-02-03 09:42:42 -0800175 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700176 config = SkSwizzler::kBGRX;
177 } else {
178 config = SkSwizzler::kBGRA;
179 }
180 break;
181 default:
182 SkASSERT(false);
183 return false;
184 }
185
186 // Get a pointer to the color table if it exists
187 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
188
189 // Create swizzler
msarettfdb47572015-10-13 12:50:14 -0700190 fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts));
msarett4ab9d5f2015-08-06 15:34:42 -0700191
halcanary96fcdcc2015-08-27 07:41:13 -0700192 if (nullptr == fSwizzler.get()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700193 return false;
194 }
195 return true;
196}
197
msarett5406d6f2015-08-31 06:55:13 -0700198SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
199 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
200 // Create the color table if necessary and prepare the stream for decode
201 // Note that if it is non-NULL, inputColorCount will be modified
202 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) {
203 SkCodecPrintf("Error: could not create color table.\n");
204 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700205 }
msarett5406d6f2015-08-31 06:55:13 -0700206
207 // Copy the color table to the client if necessary
208 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
209
210 // Initialize a swizzler if necessary
211 if (!this->initializeSwizzler(dstInfo, options)) {
212 SkCodecPrintf("Error: cannot initialize swizzler.\n");
213 return SkCodec::kInvalidConversion;
214 }
215 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700216}
217
218/*
219 * Performs the bitmap decoding for standard input format
220 */
msarettbe8216a2015-12-04 08:00:50 -0800221int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
222 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700223 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700224 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700225 for (int y = 0; y < height; y++) {
226 // Read a row of the input
msarett5406d6f2015-08-31 06:55:13 -0700227 if (this->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRowBytes) {
msarett4ab9d5f2015-08-06 15:34:42 -0700228 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700229 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700230 }
231
232 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700233 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700234
235 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
236 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
237 }
238
msarettbe8216a2015-12-04 08:00:50 -0800239 if (fInIco) {
240 const int startScanline = this->currScanline();
241 if (startScanline < 0) {
242 // We are not performing a scanline decode.
243 // Just decode the entire ICO mask and return.
244 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
245 return height;
246 }
247
248 // In order to perform a scanline ICO decode, we must be able
249 // to skip ahead in the stream in order to apply the AND mask
250 // to the requested scanlines.
251 // We will do this by taking advantage of the fact that
252 // SkIcoCodec always uses a SkMemoryStream as its underlying
253 // representation of the stream.
254 const void* memoryBase = this->stream()->getMemoryBase();
255 SkASSERT(nullptr != memoryBase);
256 SkASSERT(this->stream()->hasLength());
257 SkASSERT(this->stream()->hasPosition());
258
259 const size_t length = this->stream()->getLength();
260 const size_t currPosition = this->stream()->getPosition();
261
262 // Calculate how many bytes we must skip to reach the AND mask.
263 const int remainingScanlines = this->getInfo().height() - startScanline - height;
264 const size_t bytesToSkip = remainingScanlines * fSrcRowBytes +
265 startScanline * fAndMaskRowBytes;
266 const size_t subStreamStartPosition = currPosition + bytesToSkip;
267 if (subStreamStartPosition >= length) {
268 // FIXME: How can we indicate that this decode was actually incomplete?
269 return height;
270 }
271
272 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
273 // the memory base into a stream in order to safely handle incomplete images
274 // without reading out of bounds memory.
275 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
276 subStreamStartPosition);
277 const size_t subStreamLength = length - subStreamStartPosition;
278 // This call does not transfer ownership of the subStreamMemoryBase.
279 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
280
281 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
282 // indicate the decode was incomplete?
283 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
284 }
285
msarette6dd0042015-10-09 11:07:34 -0700286 return height;
msarett5406d6f2015-08-31 06:55:13 -0700287}
scroggocc2feb12015-08-14 08:32:46 -0700288
msarettbe8216a2015-12-04 08:00:50 -0800289void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700290 void* dst, size_t dstRowBytes) {
291 // BMP in ICO have transparency, so this cannot be 565, and this mask
292 // prevents us from using kIndex8. The below code depends on the output
293 // being an SkPMColor.
294 SkASSERT(dstInfo.colorType() == kN32_SkColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700295
msarettbe8216a2015-12-04 08:00:50 -0800296 // If we are sampling, make sure that we only mask the sampled pixels.
297 // We do not need to worry about sampling in the y-dimension because that
298 // should be handled by SkSampledCodec.
299 const int sampleX = fSwizzler->sampleX();
300 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX);
301 const int srcStartX = get_start_coord(sampleX);
302
msarett4ab9d5f2015-08-06 15:34:42 -0700303
msarett5406d6f2015-08-31 06:55:13 -0700304 SkPMColor* dstPtr = (SkPMColor*) dst;
305 for (int y = 0; y < dstInfo.height(); y++) {
306 // The srcBuffer will at least be large enough
msarettbe8216a2015-12-04 08:00:50 -0800307 if (stream->read(fSrcBuffer.get(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700308 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800309 return;
msarett5406d6f2015-08-31 06:55:13 -0700310 }
msarett4ab9d5f2015-08-06 15:34:42 -0700311
msarett5406d6f2015-08-31 06:55:13 -0700312 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700313
msarett5406d6f2015-08-31 06:55:13 -0700314 SkPMColor* dstRow =
315 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
316
msarettbe8216a2015-12-04 08:00:50 -0800317 int srcX = srcStartX;
318 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700319 int quotient;
320 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800321 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700322 uint32_t shift = 7 - modulus;
msarettbe8216a2015-12-04 08:00:50 -0800323 uint32_t alphaBit = (fSrcBuffer.get()[quotient] >> shift) & 0x1;
324 dstRow[dstX] &= alphaBit - 1;
325 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700326 }
327 }
msarett4ab9d5f2015-08-06 15:34:42 -0700328}
msarette6dd0042015-10-09 11:07:34 -0700329
scroggoc5560be2016-02-03 09:42:42 -0800330uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700331 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
332 if (colorPtr) {
333 return get_color_table_fill_value(colorType, colorPtr, 0);
334 }
scroggoc5560be2016-02-03 09:42:42 -0800335 return INHERITED::onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700336}