blob: 959e75ba5b3d15cc23e23fc438b9bae8b5e12263 [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)
msarettf4004f92016-02-11 10:49:31 -080028 , fIsOpaque(isOpaque)
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 }
msarett4ab9d5f2015-08-06 15:34:42 -070050
msarett5406d6f2015-08-31 06:55:13 -070051 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
52 if (kSuccess != result) {
53 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070054 }
msarettf724b992015-10-15 06:41:06 -070055 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070056 if (rows != dstInfo.height()) {
57 *rowsDecoded = rows;
58 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070059 }
msarett5406d6f2015-08-31 06:55:13 -070060 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070061}
62
63/*
64 * Process the color table for the bmp input
65 */
msarett34e0ec42016-04-22 16:27:24 -070066 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType,
67 int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070068 // Allocate memory for color table
69 uint32_t colorBytes = 0;
70 SkPMColor colorTable[256];
71 if (this->bitsPerPixel() <= 8) {
72 // Inform the caller of the number of colors
73 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070074 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070075 // We set the number of colors to maxColors in order to ensure
76 // safe memory accesses. Otherwise, an invalid pixel could
77 // access memory outside of our color table array.
78 *numColors = maxColors;
79 }
benjaminwagner886e5e42015-12-04 08:48:26 -080080 // Don't bother reading more than maxColors.
81 const uint32_t numColorsToRead =
82 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070083
84 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080085 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040086 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070087 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
88 SkCodecPrintf("Error: unable to read color table.\n");
89 return false;
90 }
91
Matt Sarett1a85ca52016-11-04 11:52:48 -040092 SkColorType packColorType = dstColorType;
93 SkAlphaType packAlphaType = dstAlphaType;
94 if (this->colorXform()) {
95 packColorType = kBGRA_8888_SkColorType;
96 packAlphaType = kUnpremul_SkAlphaType;
97 }
98
msarett4ab9d5f2015-08-06 15:34:42 -070099 // Choose the proper packing function
Matt Sarett1a85ca52016-11-04 11:52:48 -0400100 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
101 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700102
103 // Fill in the color table
104 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800105 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700106 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
107 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
108 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
109 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800110 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700111 alpha = 0xFF;
112 } else {
113 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
114 }
115 colorTable[i] = packARGB(alpha, red, green, blue);
116 }
117
118 // To avoid segmentation faults on bad pixel data, fill the end of the
119 // color table with black. This is the same the behavior as the
120 // chromium decoder.
121 for (; i < maxColors; i++) {
122 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
123 }
124
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400125 if (this->colorXform() && !this->xformOnDecode()) {
126 this->applyColorXform(colorTable, colorTable, maxColors);
Matt Sarett1a85ca52016-11-04 11:52:48 -0400127 }
128
msarett4ab9d5f2015-08-06 15:34:42 -0700129 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700130 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700131 }
132
133 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
134 // begins. Pixel data always begins immediately after the color table.
135 if (!fInIco) {
136 // Check that we have not read past the pixel array offset
137 if(fOffset < colorBytes) {
138 // This may occur on OS 2.1 and other old versions where the color
139 // table defaults to max size, and the bmp tries to use a smaller
140 // color table. This is invalid, and our decision is to indicate
141 // an error, rather than try to guess the intended size of the
142 // color table.
143 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
144 return false;
145 }
146
147 // After reading the color table, skip to the start of the pixel array
148 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
149 SkCodecPrintf("Error: unable to skip to image data.\n");
150 return false;
151 }
152 }
153
154 // Return true on success
155 return true;
156}
157
msarettb30d6982016-02-15 10:18:45 -0800158void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700159 // In the case of bmp-in-icos, we will report BGRA to the client,
160 // since we may be required to apply an alpha mask after the decode.
161 // However, the swizzler needs to know the actual format of the bmp.
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400162 SkEncodedInfo encodedInfo = this->getEncodedInfo();
msarett3e375b02016-05-04 13:03:48 -0700163 if (fInIco) {
164 if (this->bitsPerPixel() <= 8) {
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400165 encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color,
166 encodedInfo.alpha(), this->bitsPerPixel());
msarett3e375b02016-05-04 13:03:48 -0700167 } else if (this->bitsPerPixel() == 24) {
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400168 encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kBGR_Color,
msarett3e375b02016-05-04 13:03:48 -0700169 SkEncodedInfo::kOpaque_Alpha, 8);
170 }
msarett4ab9d5f2015-08-06 15:34:42 -0700171 }
172
173 // Get a pointer to the color table if it exists
174 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
175
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400176 SkImageInfo swizzlerInfo = dstInfo;
177 SkCodec::Options swizzlerOptions = opts;
178 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500179 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400180 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
181 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
182 }
183
184 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
185 }
186
187
188 fSwizzler.reset(SkSwizzler::CreateSwizzler(encodedInfo, colorPtr, swizzlerInfo,
189 swizzlerOptions));
msarettb30d6982016-02-15 10:18:45 -0800190 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700191}
192
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400193SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700194 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400195 if (this->xformOnDecode()) {
196 this->resetXformBuffer(dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400197 }
198
msarett5406d6f2015-08-31 06:55:13 -0700199 // Create the color table if necessary and prepare the stream for decode
200 // Note that if it is non-NULL, inputColorCount will be modified
msarett34e0ec42016-04-22 16:27:24 -0700201 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) {
msarett5406d6f2015-08-31 06:55:13 -0700202 SkCodecPrintf("Error: could not create color table.\n");
203 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700204 }
msarett5406d6f2015-08-31 06:55:13 -0700205
206 // Copy the color table to the client if necessary
Hal Canary67b39de2016-11-07 11:47:44 -0500207 copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount);
msarett5406d6f2015-08-31 06:55:13 -0700208
msarettb30d6982016-02-15 10:18:45 -0800209 // Initialize a swizzler
210 this->initializeSwizzler(dstInfo, options);
msarett5406d6f2015-08-31 06:55:13 -0700211 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700212}
213
214/*
215 * Performs the bitmap decoding for standard input format
216 */
msarettbe8216a2015-12-04 08:00:50 -0800217int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
218 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700219 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700220 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700221 for (int y = 0; y < height; y++) {
222 // Read a row of the input
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400223 if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700224 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700225 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700226 }
227
228 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700229 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700230
231 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400232
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400233 if (this->xformOnDecode()) {
Matt Sarett1a85ca52016-11-04 11:52:48 -0400234 SkASSERT(this->colorXform());
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400235 fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer());
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400236 this->applyColorXform(dstRow, this->xformBuffer(), fSwizzler->swizzleWidth());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400237 } else {
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400238 fSwizzler->swizzle(dstRow, this->srcBuffer());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400239 }
msarett4ab9d5f2015-08-06 15:34:42 -0700240 }
241
msarett1088db92016-03-22 08:58:35 -0700242 if (fInIco && fIsOpaque) {
msarettbe8216a2015-12-04 08:00:50 -0800243 const int startScanline = this->currScanline();
244 if (startScanline < 0) {
245 // We are not performing a scanline decode.
246 // Just decode the entire ICO mask and return.
247 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
248 return height;
249 }
250
251 // In order to perform a scanline ICO decode, we must be able
252 // to skip ahead in the stream in order to apply the AND mask
253 // to the requested scanlines.
254 // We will do this by taking advantage of the fact that
255 // SkIcoCodec always uses a SkMemoryStream as its underlying
256 // representation of the stream.
257 const void* memoryBase = this->stream()->getMemoryBase();
258 SkASSERT(nullptr != memoryBase);
259 SkASSERT(this->stream()->hasLength());
260 SkASSERT(this->stream()->hasPosition());
261
262 const size_t length = this->stream()->getLength();
263 const size_t currPosition = this->stream()->getPosition();
264
265 // Calculate how many bytes we must skip to reach the AND mask.
266 const int remainingScanlines = this->getInfo().height() - startScanline - height;
msarett9b9497e2016-02-11 13:29:36 -0800267 const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() +
msarettbe8216a2015-12-04 08:00:50 -0800268 startScanline * fAndMaskRowBytes;
269 const size_t subStreamStartPosition = currPosition + bytesToSkip;
270 if (subStreamStartPosition >= length) {
271 // FIXME: How can we indicate that this decode was actually incomplete?
272 return height;
273 }
274
275 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
276 // the memory base into a stream in order to safely handle incomplete images
277 // without reading out of bounds memory.
278 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
279 subStreamStartPosition);
280 const size_t subStreamLength = length - subStreamStartPosition;
281 // This call does not transfer ownership of the subStreamMemoryBase.
282 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
283
284 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
285 // indicate the decode was incomplete?
286 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
287 }
288
msarette6dd0042015-10-09 11:07:34 -0700289 return height;
msarett5406d6f2015-08-31 06:55:13 -0700290}
scroggocc2feb12015-08-14 08:32:46 -0700291
msarettbe8216a2015-12-04 08:00:50 -0800292void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700293 void* dst, size_t dstRowBytes) {
294 // BMP in ICO have transparency, so this cannot be 565, and this mask
295 // prevents us from using kIndex8. The below code depends on the output
296 // being an SkPMColor.
msarett34e0ec42016-04-22 16:27:24 -0700297 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
Matt Sarett09a1c082017-02-01 15:34:22 -0800298 kBGRA_8888_SkColorType == dstInfo.colorType() ||
299 kRGBA_F16_SkColorType == dstInfo.colorType());
msarett4ab9d5f2015-08-06 15:34:42 -0700300
msarettbe8216a2015-12-04 08:00:50 -0800301 // If we are sampling, make sure that we only mask the sampled pixels.
302 // We do not need to worry about sampling in the y-dimension because that
303 // should be handled by SkSampledCodec.
304 const int sampleX = fSwizzler->sampleX();
305 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX);
306 const int srcStartX = get_start_coord(sampleX);
307
msarett4ab9d5f2015-08-06 15:34:42 -0700308
msarett5406d6f2015-08-31 06:55:13 -0700309 SkPMColor* dstPtr = (SkPMColor*) dst;
310 for (int y = 0; y < dstInfo.height(); y++) {
311 // The srcBuffer will at least be large enough
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400312 if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700313 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800314 return;
msarett5406d6f2015-08-31 06:55:13 -0700315 }
msarett4ab9d5f2015-08-06 15:34:42 -0700316
Matt Sarett09a1c082017-02-01 15:34:22 -0800317 auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) {
318 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
319 uint64_t* dst64 = (uint64_t*) dstRow;
320 dst64[x] &= bit - 1;
321 } else {
322 uint32_t* dst32 = (uint32_t*) dstRow;
323 dst32[x] &= bit - 1;
324 }
325 };
326
msarett5406d6f2015-08-31 06:55:13 -0700327 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700328
Matt Sarett09a1c082017-02-01 15:34:22 -0800329 void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700330
msarettbe8216a2015-12-04 08:00:50 -0800331 int srcX = srcStartX;
332 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700333 int quotient;
334 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800335 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700336 uint32_t shift = 7 - modulus;
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400337 uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1;
Matt Sarett09a1c082017-02-01 15:34:22 -0800338 applyMask(dstRow, dstX, alphaBit);
msarettbe8216a2015-12-04 08:00:50 -0800339 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700340 }
341 }
msarett4ab9d5f2015-08-06 15:34:42 -0700342}
msarette6dd0042015-10-09 11:07:34 -0700343
msarettf7eb6fc2016-09-13 09:04:11 -0700344uint64_t SkBmpStandardCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
msarette6dd0042015-10-09 11:07:34 -0700345 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
346 if (colorPtr) {
msarettf7eb6fc2016-09-13 09:04:11 -0700347 return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(), colorPtr, 0,
Matt Sarett19aff5d2017-04-03 16:01:10 -0400348 this->colorXform(), false);
msarette6dd0042015-10-09 11:07:34 -0700349 }
msarettf7eb6fc2016-09-13 09:04:11 -0700350 return INHERITED::onGetFillValue(dstInfo);
msarette6dd0042015-10-09 11:07:34 -0700351}