blob: 1f7593ed42599d67faba282a154debf447513843 [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"
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
Hal Canary2d404902018-09-06 11:20:23 -040011#include "SkMathPriv.h"
msarett4ab9d5f2015-08-06 15:34:42 -070012#include "SkStream.h"
13
14/*
msarett4ab9d5f2015-08-06 15:34:42 -070015 * Creates an instance of the decoder
16 * Called only by NewFromStream
17 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040018SkBmpStandardCodec::SkBmpStandardCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
19 uint16_t bitsPerPixel, uint32_t numColors,
20 uint32_t bytesPerColor, uint32_t offset,
msarettf4004f92016-02-11 10:49:31 -080021 SkCodec::SkScanlineOrder rowOrder,
22 bool isOpaque, bool inIco)
Leon Scroggins III36f7e322018-08-27 11:55:46 -040023 : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070024 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080025 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070026 , fBytesPerColor(bytesPerColor)
27 , fOffset(offset)
halcanary96fcdcc2015-08-27 07:41:13 -070028 , fSwizzler(nullptr)
msarettf4004f92016-02-11 10:49:31 -080029 , fIsOpaque(isOpaque)
msarett4ab9d5f2015-08-06 15:34:42 -070030 , fInIco(inIco)
Leon Scroggins III712476e2018-10-03 15:47:00 -040031 , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->dimensions().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,
msarette6dd0042015-10-09 11:07:34 -070040 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070041 if (opts.fSubset) {
42 // Subsets are not supported.
43 return kUnimplemented;
44 }
Leon Scroggins III712476e2018-10-03 15:47:00 -040045 if (dstInfo.dimensions() != this->dimensions()) {
msarett4ab9d5f2015-08-06 15:34:42 -070046 SkCodecPrintf("Error: scaling not supported.\n");
47 return kInvalidScale;
48 }
msarett4ab9d5f2015-08-06 15:34:42 -070049
Leon Scroggins571b30f2017-07-11 17:35:31 +000050 Result result = this->prepareToDecode(dstInfo, opts);
msarett5406d6f2015-08-31 06:55:13 -070051 if (kSuccess != result) {
52 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070053 }
msarettf724b992015-10-15 06:41:06 -070054 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070055 if (rows != dstInfo.height()) {
56 *rowsDecoded = rows;
57 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070058 }
msarett5406d6f2015-08-31 06:55:13 -070059 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070060}
61
62/*
63 * Process the color table for the bmp input
64 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000065 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType) {
msarett4ab9d5f2015-08-06 15:34:42 -070066 // Allocate memory for color table
67 uint32_t colorBytes = 0;
68 SkPMColor colorTable[256];
69 if (this->bitsPerPixel() <= 8) {
70 // Inform the caller of the number of colors
71 uint32_t maxColors = 1 << this->bitsPerPixel();
benjaminwagner886e5e42015-12-04 08:48:26 -080072 // Don't bother reading more than maxColors.
73 const uint32_t numColorsToRead =
74 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070075
76 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080077 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040078 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070079 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
80 SkCodecPrintf("Error: unable to read color table.\n");
81 return false;
82 }
83
Matt Sarett1a85ca52016-11-04 11:52:48 -040084 SkColorType packColorType = dstColorType;
85 SkAlphaType packAlphaType = dstAlphaType;
86 if (this->colorXform()) {
87 packColorType = kBGRA_8888_SkColorType;
88 packAlphaType = kUnpremul_SkAlphaType;
89 }
90
msarett4ab9d5f2015-08-06 15:34:42 -070091 // Choose the proper packing function
Matt Sarett1a85ca52016-11-04 11:52:48 -040092 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
93 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070094
95 // Fill in the color table
96 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -080097 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -070098 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
99 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
100 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
101 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800102 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700103 alpha = 0xFF;
104 } else {
105 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
106 }
107 colorTable[i] = packARGB(alpha, red, green, blue);
108 }
109
110 // To avoid segmentation faults on bad pixel data, fill the end of the
111 // color table with black. This is the same the behavior as the
112 // chromium decoder.
113 for (; i < maxColors; i++) {
114 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
115 }
116
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400117 if (this->colorXform() && !this->xformOnDecode()) {
118 this->applyColorXform(colorTable, colorTable, maxColors);
Matt Sarett1a85ca52016-11-04 11:52:48 -0400119 }
120
msarett4ab9d5f2015-08-06 15:34:42 -0700121 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700122 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700123 }
124
125 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
126 // begins. Pixel data always begins immediately after the color table.
127 if (!fInIco) {
128 // Check that we have not read past the pixel array offset
129 if(fOffset < colorBytes) {
130 // This may occur on OS 2.1 and other old versions where the color
131 // table defaults to max size, and the bmp tries to use a smaller
132 // color table. This is invalid, and our decision is to indicate
133 // an error, rather than try to guess the intended size of the
134 // color table.
135 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
136 return false;
137 }
138
139 // After reading the color table, skip to the start of the pixel array
140 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
141 SkCodecPrintf("Error: unable to skip to image data.\n");
142 return false;
143 }
144 }
145
146 // Return true on success
147 return true;
148}
149
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400150static SkEncodedInfo make_info(SkEncodedInfo::Color color,
151 SkEncodedInfo::Alpha alpha, int bitsPerPixel) {
152 // This is just used for the swizzler, which does not need the width or height.
153 return SkEncodedInfo::Make(0, 0, color, alpha, bitsPerPixel);
154}
155
156SkEncodedInfo SkBmpStandardCodec::swizzlerInfo() const {
157 const auto& info = this->getEncodedInfo();
158 if (fInIco) {
159 if (this->bitsPerPixel() <= 8) {
160 return make_info(SkEncodedInfo::kPalette_Color,
161 info.alpha(), this->bitsPerPixel());
162 }
163 if (this->bitsPerPixel() == 24) {
164 return make_info(SkEncodedInfo::kBGR_Color,
165 SkEncodedInfo::kOpaque_Alpha, 8);
166 }
167 }
168
169 return make_info(info.color(), info.alpha(), info.bitsPerComponent());
170}
171
msarettb30d6982016-02-15 10:18:45 -0800172void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700173 // In the case of bmp-in-icos, we will report BGRA to the client,
174 // since we may be required to apply an alpha mask after the decode.
175 // However, the swizzler needs to know the actual format of the bmp.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400176 SkEncodedInfo encodedInfo = this->swizzlerInfo();
msarett4ab9d5f2015-08-06 15:34:42 -0700177
178 // Get a pointer to the color table if it exists
179 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
180
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400181 SkImageInfo swizzlerInfo = dstInfo;
182 SkCodec::Options swizzlerOptions = opts;
183 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500184 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400185 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
186 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
187 }
188
189 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
190 }
191
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400192 fSwizzler = SkSwizzler::Make(encodedInfo, colorPtr, swizzlerInfo, swizzlerOptions);
msarettb30d6982016-02-15 10:18:45 -0800193 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700194}
195
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400196SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000197 const SkCodec::Options& options) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400198 if (this->xformOnDecode()) {
199 this->resetXformBuffer(dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400200 }
201
msarett5406d6f2015-08-31 06:55:13 -0700202 // Create the color table if necessary and prepare the stream for decode
203 // Note that if it is non-NULL, inputColorCount will be modified
Leon Scroggins571b30f2017-07-11 17:35:31 +0000204 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) {
msarett5406d6f2015-08-31 06:55:13 -0700205 SkCodecPrintf("Error: could not create color table.\n");
206 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700207 }
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.
Leon Scroggins III712476e2018-10-03 15:47:00 -0400266 const int remainingScanlines = this->dimensions().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) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000294 // BMP in ICO have transparency, so this cannot be 565. The below code depends
295 // on the output being an SkPMColor.
msarett34e0ec42016-04-22 16:27:24 -0700296 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
Matt Sarett09a1c082017-02-01 15:34:22 -0800297 kBGRA_8888_SkColorType == dstInfo.colorType() ||
298 kRGBA_F16_SkColorType == dstInfo.colorType());
msarett4ab9d5f2015-08-06 15:34:42 -0700299
msarettbe8216a2015-12-04 08:00:50 -0800300 // If we are sampling, make sure that we only mask the sampled pixels.
301 // We do not need to worry about sampling in the y-dimension because that
302 // should be handled by SkSampledCodec.
303 const int sampleX = fSwizzler->sampleX();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400304 const int sampledWidth = get_scaled_dimension(this->dimensions().width(), sampleX);
msarettbe8216a2015-12-04 08:00:50 -0800305 const int srcStartX = get_start_coord(sampleX);
306
msarett4ab9d5f2015-08-06 15:34:42 -0700307
msarett5406d6f2015-08-31 06:55:13 -0700308 SkPMColor* dstPtr = (SkPMColor*) dst;
309 for (int y = 0; y < dstInfo.height(); y++) {
310 // The srcBuffer will at least be large enough
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400311 if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700312 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800313 return;
msarett5406d6f2015-08-31 06:55:13 -0700314 }
msarett4ab9d5f2015-08-06 15:34:42 -0700315
Matt Sarett09a1c082017-02-01 15:34:22 -0800316 auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) {
317 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
318 uint64_t* dst64 = (uint64_t*) dstRow;
319 dst64[x] &= bit - 1;
320 } else {
321 uint32_t* dst32 = (uint32_t*) dstRow;
322 dst32[x] &= bit - 1;
323 }
324 };
325
msarett5406d6f2015-08-31 06:55:13 -0700326 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700327
Matt Sarett09a1c082017-02-01 15:34:22 -0800328 void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700329
msarettbe8216a2015-12-04 08:00:50 -0800330 int srcX = srcStartX;
331 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700332 int quotient;
333 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800334 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700335 uint32_t shift = 7 - modulus;
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400336 uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1;
Matt Sarett09a1c082017-02-01 15:34:22 -0800337 applyMask(dstRow, dstX, alphaBit);
msarettbe8216a2015-12-04 08:00:50 -0800338 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700339 }
340 }
msarett4ab9d5f2015-08-06 15:34:42 -0700341}