blob: 651ff83b8a6450712219d41f43cba712698ad480 [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 */
msarettc30c4182016-04-20 11:53:35 -070017SkBmpStandardCodec::SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info,
18 SkStream* stream, uint16_t bitsPerPixel, uint32_t numColors,
msarett4ab9d5f2015-08-06 15:34:42 -070019 uint32_t bytesPerColor, uint32_t offset,
msarettf4004f92016-02-11 10:49:31 -080020 SkCodec::SkScanlineOrder rowOrder,
21 bool isOpaque, bool inIco)
msarettc30c4182016-04-20 11:53:35 -070022 : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070023 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080024 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070025 , fBytesPerColor(bytesPerColor)
26 , fOffset(offset)
halcanary96fcdcc2015-08-27 07:41:13 -070027 , fSwizzler(nullptr)
msarett9b9497e2016-02-11 13:29:36 -080028 , fSrcBuffer(new uint8_t [this->srcRowBytes()])
msarettf4004f92016-02-11 10:49:31 -080029 , fIsOpaque(isOpaque)
msarett4ab9d5f2015-08-06 15:34:42 -070030 , fInIco(inIco)
msarettbe8216a2015-12-04 08:00:50 -080031 , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->getInfo().width(), 1)) : 0)
msarett4ab9d5f2015-08-06 15:34:42 -070032{}
33
34/*
35 * Initiates the bitmap decode
36 */
37SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
38 void* dst, size_t dstRowBytes,
39 const Options& opts,
40 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -070041 int* inputColorCount,
42 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070043 if (opts.fSubset) {
44 // Subsets are not supported.
45 return kUnimplemented;
46 }
47 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
48 SkCodecPrintf("Error: scaling not supported.\n");
49 return kInvalidScale;
50 }
51 if (!conversion_possible(dstInfo, this->getInfo())) {
52 SkCodecPrintf("Error: cannot convert input type to output type.\n");
53 return kInvalidConversion;
54 }
55
msarett5406d6f2015-08-31 06:55:13 -070056 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
57 if (kSuccess != result) {
58 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070059 }
msarettf724b992015-10-15 06:41:06 -070060 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070061 if (rows != dstInfo.height()) {
62 *rowsDecoded = rows;
63 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070064 }
msarett5406d6f2015-08-31 06:55:13 -070065 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070066}
67
68/*
69 * Process the color table for the bmp input
70 */
msarett34e0ec42016-04-22 16:27:24 -070071 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType,
72 int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070073 // 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 }
benjaminwagner886e5e42015-12-04 08:48:26 -080085 // Don't bother reading more than maxColors.
86 const uint32_t numColorsToRead =
87 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070088
89 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080090 colorBytes = numColorsToRead * fBytesPerColor;
halcanary385fe4d2015-08-26 13:07:48 -070091 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070092 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
93 SkCodecPrintf("Error: unable to read color table.\n");
94 return false;
95 }
96
97 // Choose the proper packing function
msarett34e0ec42016-04-22 16:27:24 -070098 bool isPremul = (kPremul_SkAlphaType == dstAlphaType) && !fIsOpaque;
99 PackColorProc packARGB = choose_pack_color_proc(isPremul, dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700100
101 // Fill in the color table
102 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800103 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700104 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
105 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
106 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
107 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800108 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700109 alpha = 0xFF;
110 } else {
111 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
112 }
113 colorTable[i] = packARGB(alpha, red, green, blue);
114 }
115
116 // To avoid segmentation faults on bad pixel data, fill the end of the
117 // color table with black. This is the same the behavior as the
118 // chromium decoder.
119 for (; i < maxColors; i++) {
120 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
121 }
122
123 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700124 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700125 }
126
127 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
128 // begins. Pixel data always begins immediately after the color table.
129 if (!fInIco) {
130 // Check that we have not read past the pixel array offset
131 if(fOffset < colorBytes) {
132 // This may occur on OS 2.1 and other old versions where the color
133 // table defaults to max size, and the bmp tries to use a smaller
134 // color table. This is invalid, and our decision is to indicate
135 // an error, rather than try to guess the intended size of the
136 // color table.
137 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
138 return false;
139 }
140
141 // After reading the color table, skip to the start of the pixel array
142 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
143 SkCodecPrintf("Error: unable to skip to image data.\n");
144 return false;
145 }
146 }
147
148 // Return true on success
149 return true;
150}
151
msarettb30d6982016-02-15 10:18:45 -0800152void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700153 // In the case of bmp-in-icos, we will report BGRA to the client,
154 // since we may be required to apply an alpha mask after the decode.
155 // However, the swizzler needs to know the actual format of the bmp.
msaretta45a6682016-04-22 13:18:37 -0700156 SkEncodedInfo swizzlerInfo = this->getEncodedInfo();
msarett3e375b02016-05-04 13:03:48 -0700157 if (fInIco) {
158 if (this->bitsPerPixel() <= 8) {
159 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color,
160 swizzlerInfo.alpha(), this->bitsPerPixel());
161 } else if (this->bitsPerPixel() == 24) {
162 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kBGR_Color,
163 SkEncodedInfo::kOpaque_Alpha, 8);
164 }
msarett4ab9d5f2015-08-06 15:34:42 -0700165 }
166
167 // Get a pointer to the color table if it exists
168 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
169
170 // Create swizzler
msaretta45a6682016-04-22 13:18:37 -0700171 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, colorPtr, dstInfo, opts));
msarettb30d6982016-02-15 10:18:45 -0800172 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700173}
174
msarett5406d6f2015-08-31 06:55:13 -0700175SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo,
176 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
177 // Create the color table if necessary and prepare the stream for decode
178 // Note that if it is non-NULL, inputColorCount will be modified
msarett34e0ec42016-04-22 16:27:24 -0700179 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) {
msarett5406d6f2015-08-31 06:55:13 -0700180 SkCodecPrintf("Error: could not create color table.\n");
181 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700182 }
msarett5406d6f2015-08-31 06:55:13 -0700183
184 // Copy the color table to the client if necessary
185 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
186
msarettb30d6982016-02-15 10:18:45 -0800187 // Initialize a swizzler
188 this->initializeSwizzler(dstInfo, options);
msarett5406d6f2015-08-31 06:55:13 -0700189 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700190}
191
192/*
193 * Performs the bitmap decoding for standard input format
194 */
msarettbe8216a2015-12-04 08:00:50 -0800195int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
196 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700197 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700198 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700199 for (int y = 0; y < height; y++) {
200 // Read a row of the input
msarett9b9497e2016-02-11 13:29:36 -0800201 if (this->stream()->read(fSrcBuffer.get(), this->srcRowBytes()) != this->srcRowBytes()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700202 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700203 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700204 }
205
206 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700207 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700208
209 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
210 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
211 }
212
msarett1088db92016-03-22 08:58:35 -0700213 if (fInIco && fIsOpaque) {
msarettbe8216a2015-12-04 08:00:50 -0800214 const int startScanline = this->currScanline();
215 if (startScanline < 0) {
216 // We are not performing a scanline decode.
217 // Just decode the entire ICO mask and return.
218 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
219 return height;
220 }
221
222 // In order to perform a scanline ICO decode, we must be able
223 // to skip ahead in the stream in order to apply the AND mask
224 // to the requested scanlines.
225 // We will do this by taking advantage of the fact that
226 // SkIcoCodec always uses a SkMemoryStream as its underlying
227 // representation of the stream.
228 const void* memoryBase = this->stream()->getMemoryBase();
229 SkASSERT(nullptr != memoryBase);
230 SkASSERT(this->stream()->hasLength());
231 SkASSERT(this->stream()->hasPosition());
232
233 const size_t length = this->stream()->getLength();
234 const size_t currPosition = this->stream()->getPosition();
235
236 // Calculate how many bytes we must skip to reach the AND mask.
237 const int remainingScanlines = this->getInfo().height() - startScanline - height;
msarett9b9497e2016-02-11 13:29:36 -0800238 const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() +
msarettbe8216a2015-12-04 08:00:50 -0800239 startScanline * fAndMaskRowBytes;
240 const size_t subStreamStartPosition = currPosition + bytesToSkip;
241 if (subStreamStartPosition >= length) {
242 // FIXME: How can we indicate that this decode was actually incomplete?
243 return height;
244 }
245
246 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
247 // the memory base into a stream in order to safely handle incomplete images
248 // without reading out of bounds memory.
249 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
250 subStreamStartPosition);
251 const size_t subStreamLength = length - subStreamStartPosition;
252 // This call does not transfer ownership of the subStreamMemoryBase.
253 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
254
255 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
256 // indicate the decode was incomplete?
257 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
258 }
259
msarette6dd0042015-10-09 11:07:34 -0700260 return height;
msarett5406d6f2015-08-31 06:55:13 -0700261}
scroggocc2feb12015-08-14 08:32:46 -0700262
msarettbe8216a2015-12-04 08:00:50 -0800263void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700264 void* dst, size_t dstRowBytes) {
265 // BMP in ICO have transparency, so this cannot be 565, and this mask
266 // prevents us from using kIndex8. The below code depends on the output
267 // being an SkPMColor.
msarett34e0ec42016-04-22 16:27:24 -0700268 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
269 kBGRA_8888_SkColorType == dstInfo.colorType());
msarett4ab9d5f2015-08-06 15:34:42 -0700270
msarettbe8216a2015-12-04 08:00:50 -0800271 // If we are sampling, make sure that we only mask the sampled pixels.
272 // We do not need to worry about sampling in the y-dimension because that
273 // should be handled by SkSampledCodec.
274 const int sampleX = fSwizzler->sampleX();
275 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX);
276 const int srcStartX = get_start_coord(sampleX);
277
msarett4ab9d5f2015-08-06 15:34:42 -0700278
msarett5406d6f2015-08-31 06:55:13 -0700279 SkPMColor* dstPtr = (SkPMColor*) dst;
280 for (int y = 0; y < dstInfo.height(); y++) {
281 // The srcBuffer will at least be large enough
msarettbe8216a2015-12-04 08:00:50 -0800282 if (stream->read(fSrcBuffer.get(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700283 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800284 return;
msarett5406d6f2015-08-31 06:55:13 -0700285 }
msarett4ab9d5f2015-08-06 15:34:42 -0700286
msarett5406d6f2015-08-31 06:55:13 -0700287 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700288
msarett5406d6f2015-08-31 06:55:13 -0700289 SkPMColor* dstRow =
290 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
291
msarettbe8216a2015-12-04 08:00:50 -0800292 int srcX = srcStartX;
293 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700294 int quotient;
295 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800296 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700297 uint32_t shift = 7 - modulus;
msarettbe8216a2015-12-04 08:00:50 -0800298 uint32_t alphaBit = (fSrcBuffer.get()[quotient] >> shift) & 0x1;
299 dstRow[dstX] &= alphaBit - 1;
300 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700301 }
302 }
msarett4ab9d5f2015-08-06 15:34:42 -0700303}
msarette6dd0042015-10-09 11:07:34 -0700304
scroggoc5560be2016-02-03 09:42:42 -0800305uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700306 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
307 if (colorPtr) {
308 return get_color_table_fill_value(colorType, colorPtr, 0);
309 }
scroggoc5560be2016-02-03 09:42:42 -0800310 return INHERITED::onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700311}