blob: 85b40778b6c1c8bc1ff38d4347a5592034b62b6c [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 */
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 }
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);
97 switch (alphaType) {
98 case kOpaque_SkAlphaType:
99 case kUnpremul_SkAlphaType:
100 packARGB = &SkPackARGB32NoCheck;
101 break;
102 case kPremul_SkAlphaType:
103 packARGB = &SkPreMultiplyARGB;
104 break;
105 default:
106 // This should not be reached because conversion possible
107 // should fail if the alpha type is not one of the above
108 // values.
109 SkASSERT(false);
halcanary96fcdcc2015-08-27 07:41:13 -0700110 packARGB = nullptr;
msarett4ab9d5f2015-08-06 15:34:42 -0700111 break;
112 }
113
114 // Fill in the color table
115 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800116 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700117 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
118 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
119 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
120 uint8_t alpha;
121 if (kOpaque_SkAlphaType == alphaType) {
122 alpha = 0xFF;
123 } else {
124 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
125 }
126 colorTable[i] = packARGB(alpha, red, green, blue);
127 }
128
129 // To avoid segmentation faults on bad pixel data, fill the end of the
130 // color table with black. This is the same the behavior as the
131 // chromium decoder.
132 for (; i < maxColors; i++) {
133 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
134 }
135
136 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700137 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700138 }
139
140 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
141 // begins. Pixel data always begins immediately after the color table.
142 if (!fInIco) {
143 // Check that we have not read past the pixel array offset
144 if(fOffset < colorBytes) {
145 // This may occur on OS 2.1 and other old versions where the color
146 // table defaults to max size, and the bmp tries to use a smaller
147 // color table. This is invalid, and our decision is to indicate
148 // an error, rather than try to guess the intended size of the
149 // color table.
150 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
151 return false;
152 }
153
154 // After reading the color table, skip to the start of the pixel array
155 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
156 SkCodecPrintf("Error: unable to skip to image data.\n");
157 return false;
158 }
159 }
160
161 // Return true on success
162 return true;
163}
164
msarettfdb47572015-10-13 12:50:14 -0700165bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, 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
msarettfdb47572015-10-13 12:50:14 -0700200 fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts));
msarett4ab9d5f2015-08-06 15:34:42 -0700201
halcanary96fcdcc2015-08-27 07:41:13 -0700202 if (nullptr == fSwizzler.get()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700203 return false;
204 }
205 return true;
206}
207
msarett5406d6f2015-08-31 06:55:13 -0700208SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
209 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
210 // Create the color table if necessary and prepare the stream for decode
211 // Note that if it is non-NULL, inputColorCount will be modified
212 if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) {
213 SkCodecPrintf("Error: could not create color table.\n");
214 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700215 }
msarett5406d6f2015-08-31 06:55:13 -0700216
217 // Copy the color table to the client if necessary
218 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
219
220 // Initialize a swizzler if necessary
221 if (!this->initializeSwizzler(dstInfo, options)) {
222 SkCodecPrintf("Error: cannot initialize swizzler.\n");
223 return SkCodec::kInvalidConversion;
224 }
225 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700226}
227
228/*
229 * Performs the bitmap decoding for standard input format
230 */
msarettbe8216a2015-12-04 08:00:50 -0800231int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, 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
msarettbe8216a2015-12-04 08:00:50 -0800249 if (fInIco) {
250 const int startScanline = this->currScanline();
251 if (startScanline < 0) {
252 // We are not performing a scanline decode.
253 // Just decode the entire ICO mask and return.
254 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
255 return height;
256 }
257
258 // In order to perform a scanline ICO decode, we must be able
259 // to skip ahead in the stream in order to apply the AND mask
260 // to the requested scanlines.
261 // We will do this by taking advantage of the fact that
262 // SkIcoCodec always uses a SkMemoryStream as its underlying
263 // representation of the stream.
264 const void* memoryBase = this->stream()->getMemoryBase();
265 SkASSERT(nullptr != memoryBase);
266 SkASSERT(this->stream()->hasLength());
267 SkASSERT(this->stream()->hasPosition());
268
269 const size_t length = this->stream()->getLength();
270 const size_t currPosition = this->stream()->getPosition();
271
272 // Calculate how many bytes we must skip to reach the AND mask.
273 const int remainingScanlines = this->getInfo().height() - startScanline - height;
274 const size_t bytesToSkip = remainingScanlines * fSrcRowBytes +
275 startScanline * fAndMaskRowBytes;
276 const size_t subStreamStartPosition = currPosition + bytesToSkip;
277 if (subStreamStartPosition >= length) {
278 // FIXME: How can we indicate that this decode was actually incomplete?
279 return height;
280 }
281
282 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
283 // the memory base into a stream in order to safely handle incomplete images
284 // without reading out of bounds memory.
285 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
286 subStreamStartPosition);
287 const size_t subStreamLength = length - subStreamStartPosition;
288 // This call does not transfer ownership of the subStreamMemoryBase.
289 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
290
291 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
292 // indicate the decode was incomplete?
293 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
294 }
295
msarette6dd0042015-10-09 11:07:34 -0700296 return height;
msarett5406d6f2015-08-31 06:55:13 -0700297}
scroggocc2feb12015-08-14 08:32:46 -0700298
msarettbe8216a2015-12-04 08:00:50 -0800299void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700300 void* dst, size_t dstRowBytes) {
301 // BMP in ICO have transparency, so this cannot be 565, and this mask
302 // prevents us from using kIndex8. The below code depends on the output
303 // being an SkPMColor.
304 SkASSERT(dstInfo.colorType() == kN32_SkColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700305
msarettbe8216a2015-12-04 08:00:50 -0800306 // If we are sampling, make sure that we only mask the sampled pixels.
307 // We do not need to worry about sampling in the y-dimension because that
308 // should be handled by SkSampledCodec.
309 const int sampleX = fSwizzler->sampleX();
310 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX);
311 const int srcStartX = get_start_coord(sampleX);
312
msarett4ab9d5f2015-08-06 15:34:42 -0700313
msarett5406d6f2015-08-31 06:55:13 -0700314 SkPMColor* dstPtr = (SkPMColor*) dst;
315 for (int y = 0; y < dstInfo.height(); y++) {
316 // The srcBuffer will at least be large enough
msarettbe8216a2015-12-04 08:00:50 -0800317 if (stream->read(fSrcBuffer.get(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700318 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800319 return;
msarett5406d6f2015-08-31 06:55:13 -0700320 }
msarett4ab9d5f2015-08-06 15:34:42 -0700321
msarett5406d6f2015-08-31 06:55:13 -0700322 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700323
msarett5406d6f2015-08-31 06:55:13 -0700324 SkPMColor* dstRow =
325 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
326
msarettbe8216a2015-12-04 08:00:50 -0800327 int srcX = srcStartX;
328 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700329 int quotient;
330 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800331 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700332 uint32_t shift = 7 - modulus;
msarettbe8216a2015-12-04 08:00:50 -0800333 uint32_t alphaBit = (fSrcBuffer.get()[quotient] >> shift) & 0x1;
334 dstRow[dstX] &= alphaBit - 1;
335 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700336 }
337 }
msarett4ab9d5f2015-08-06 15:34:42 -0700338}
msarette6dd0042015-10-09 11:07:34 -0700339
340uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const {
341 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
342 if (colorPtr) {
343 return get_color_table_fill_value(colorType, colorPtr, 0);
344 }
345 return INHERITED::onGetFillValue(colorType, alphaType);
346}