blob: dd71535b36bacbb5238a76e3b7024ea799a4b434 [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"
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 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040017SkBmpStandardCodec::SkBmpStandardCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
18 uint16_t bitsPerPixel, uint32_t numColors,
19 uint32_t bytesPerColor, uint32_t offset,
msarettf4004f92016-02-11 10:49:31 -080020 SkCodec::SkScanlineOrder rowOrder,
21 bool isOpaque, bool inIco)
Leon Scroggins III36f7e322018-08-27 11:55:46 -040022 : INHERITED(std::move(info), std::move(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,
msarette6dd0042015-10-09 11:07:34 -070039 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070040 if (opts.fSubset) {
41 // Subsets are not supported.
42 return kUnimplemented;
43 }
44 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
45 SkCodecPrintf("Error: scaling not supported.\n");
46 return kInvalidScale;
47 }
msarett4ab9d5f2015-08-06 15:34:42 -070048
Leon Scroggins571b30f2017-07-11 17:35:31 +000049 Result result = this->prepareToDecode(dstInfo, opts);
msarett5406d6f2015-08-31 06:55:13 -070050 if (kSuccess != result) {
51 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070052 }
msarettf724b992015-10-15 06:41:06 -070053 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070054 if (rows != dstInfo.height()) {
55 *rowsDecoded = rows;
56 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070057 }
msarett5406d6f2015-08-31 06:55:13 -070058 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070059}
60
61/*
62 * Process the color table for the bmp input
63 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000064 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType) {
msarett4ab9d5f2015-08-06 15:34:42 -070065 // Allocate memory for color table
66 uint32_t colorBytes = 0;
67 SkPMColor colorTable[256];
68 if (this->bitsPerPixel() <= 8) {
69 // Inform the caller of the number of colors
70 uint32_t maxColors = 1 << this->bitsPerPixel();
benjaminwagner886e5e42015-12-04 08:48:26 -080071 // Don't bother reading more than maxColors.
72 const uint32_t numColorsToRead =
73 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070074
75 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080076 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040077 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070078 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
79 SkCodecPrintf("Error: unable to read color table.\n");
80 return false;
81 }
82
Matt Sarett1a85ca52016-11-04 11:52:48 -040083 SkColorType packColorType = dstColorType;
84 SkAlphaType packAlphaType = dstAlphaType;
85 if (this->colorXform()) {
86 packColorType = kBGRA_8888_SkColorType;
87 packAlphaType = kUnpremul_SkAlphaType;
88 }
89
msarett4ab9d5f2015-08-06 15:34:42 -070090 // Choose the proper packing function
Matt Sarett1a85ca52016-11-04 11:52:48 -040091 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
92 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070093
94 // Fill in the color table
95 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -080096 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -070097 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
98 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
99 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
100 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800101 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700102 alpha = 0xFF;
103 } else {
104 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
105 }
106 colorTable[i] = packARGB(alpha, red, green, blue);
107 }
108
109 // To avoid segmentation faults on bad pixel data, fill the end of the
110 // color table with black. This is the same the behavior as the
111 // chromium decoder.
112 for (; i < maxColors; i++) {
113 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
114 }
115
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400116 if (this->colorXform() && !this->xformOnDecode()) {
117 this->applyColorXform(colorTable, colorTable, maxColors);
Matt Sarett1a85ca52016-11-04 11:52:48 -0400118 }
119
msarett4ab9d5f2015-08-06 15:34:42 -0700120 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700121 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700122 }
123
124 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
125 // begins. Pixel data always begins immediately after the color table.
126 if (!fInIco) {
127 // Check that we have not read past the pixel array offset
128 if(fOffset < colorBytes) {
129 // This may occur on OS 2.1 and other old versions where the color
130 // table defaults to max size, and the bmp tries to use a smaller
131 // color table. This is invalid, and our decision is to indicate
132 // an error, rather than try to guess the intended size of the
133 // color table.
134 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
135 return false;
136 }
137
138 // After reading the color table, skip to the start of the pixel array
139 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
140 SkCodecPrintf("Error: unable to skip to image data.\n");
141 return false;
142 }
143 }
144
145 // Return true on success
146 return true;
147}
148
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400149static SkEncodedInfo make_info(SkEncodedInfo::Color color,
150 SkEncodedInfo::Alpha alpha, int bitsPerPixel) {
151 // This is just used for the swizzler, which does not need the width or height.
152 return SkEncodedInfo::Make(0, 0, color, alpha, bitsPerPixel);
153}
154
155SkEncodedInfo SkBmpStandardCodec::swizzlerInfo() const {
156 const auto& info = this->getEncodedInfo();
157 if (fInIco) {
158 if (this->bitsPerPixel() <= 8) {
159 return make_info(SkEncodedInfo::kPalette_Color,
160 info.alpha(), this->bitsPerPixel());
161 }
162 if (this->bitsPerPixel() == 24) {
163 return make_info(SkEncodedInfo::kBGR_Color,
164 SkEncodedInfo::kOpaque_Alpha, 8);
165 }
166 }
167
168 return make_info(info.color(), info.alpha(), info.bitsPerComponent());
169}
170
msarettb30d6982016-02-15 10:18:45 -0800171void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700172 // In the case of bmp-in-icos, we will report BGRA to the client,
173 // since we may be required to apply an alpha mask after the decode.
174 // However, the swizzler needs to know the actual format of the bmp.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400175 SkEncodedInfo encodedInfo = this->swizzlerInfo();
msarett4ab9d5f2015-08-06 15:34:42 -0700176
177 // Get a pointer to the color table if it exists
178 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
179
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400180 SkImageInfo swizzlerInfo = dstInfo;
181 SkCodec::Options swizzlerOptions = opts;
182 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500183 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400184 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
185 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
186 }
187
188 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
189 }
190
191
192 fSwizzler.reset(SkSwizzler::CreateSwizzler(encodedInfo, colorPtr, swizzlerInfo,
193 swizzlerOptions));
msarettb30d6982016-02-15 10:18:45 -0800194 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700195}
196
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400197SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000198 const SkCodec::Options& options) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400199 if (this->xformOnDecode()) {
200 this->resetXformBuffer(dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400201 }
202
msarett5406d6f2015-08-31 06:55:13 -0700203 // Create the color table if necessary and prepare the stream for decode
204 // Note that if it is non-NULL, inputColorCount will be modified
Leon Scroggins571b30f2017-07-11 17:35:31 +0000205 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) {
msarett5406d6f2015-08-31 06:55:13 -0700206 SkCodecPrintf("Error: could not create color table.\n");
207 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700208 }
msarett5406d6f2015-08-31 06:55:13 -0700209
msarettb30d6982016-02-15 10:18:45 -0800210 // Initialize a swizzler
211 this->initializeSwizzler(dstInfo, options);
msarett5406d6f2015-08-31 06:55:13 -0700212 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700213}
214
215/*
216 * Performs the bitmap decoding for standard input format
217 */
msarettbe8216a2015-12-04 08:00:50 -0800218int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
219 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700220 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700221 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700222 for (int y = 0; y < height; y++) {
223 // Read a row of the input
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400224 if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700225 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700226 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700227 }
228
229 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700230 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700231
232 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400233
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400234 if (this->xformOnDecode()) {
Matt Sarett1a85ca52016-11-04 11:52:48 -0400235 SkASSERT(this->colorXform());
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400236 fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer());
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400237 this->applyColorXform(dstRow, this->xformBuffer(), fSwizzler->swizzleWidth());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400238 } else {
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400239 fSwizzler->swizzle(dstRow, this->srcBuffer());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400240 }
msarett4ab9d5f2015-08-06 15:34:42 -0700241 }
242
msarett1088db92016-03-22 08:58:35 -0700243 if (fInIco && fIsOpaque) {
msarettbe8216a2015-12-04 08:00:50 -0800244 const int startScanline = this->currScanline();
245 if (startScanline < 0) {
246 // We are not performing a scanline decode.
247 // Just decode the entire ICO mask and return.
248 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
249 return height;
250 }
251
252 // In order to perform a scanline ICO decode, we must be able
253 // to skip ahead in the stream in order to apply the AND mask
254 // to the requested scanlines.
255 // We will do this by taking advantage of the fact that
256 // SkIcoCodec always uses a SkMemoryStream as its underlying
257 // representation of the stream.
258 const void* memoryBase = this->stream()->getMemoryBase();
259 SkASSERT(nullptr != memoryBase);
260 SkASSERT(this->stream()->hasLength());
261 SkASSERT(this->stream()->hasPosition());
262
263 const size_t length = this->stream()->getLength();
264 const size_t currPosition = this->stream()->getPosition();
265
266 // Calculate how many bytes we must skip to reach the AND mask.
267 const int remainingScanlines = this->getInfo().height() - startScanline - height;
msarett9b9497e2016-02-11 13:29:36 -0800268 const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() +
msarettbe8216a2015-12-04 08:00:50 -0800269 startScanline * fAndMaskRowBytes;
270 const size_t subStreamStartPosition = currPosition + bytesToSkip;
271 if (subStreamStartPosition >= length) {
272 // FIXME: How can we indicate that this decode was actually incomplete?
273 return height;
274 }
275
276 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
277 // the memory base into a stream in order to safely handle incomplete images
278 // without reading out of bounds memory.
279 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
280 subStreamStartPosition);
281 const size_t subStreamLength = length - subStreamStartPosition;
282 // This call does not transfer ownership of the subStreamMemoryBase.
283 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
284
285 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
286 // indicate the decode was incomplete?
287 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
288 }
289
msarette6dd0042015-10-09 11:07:34 -0700290 return height;
msarett5406d6f2015-08-31 06:55:13 -0700291}
scroggocc2feb12015-08-14 08:32:46 -0700292
msarettbe8216a2015-12-04 08:00:50 -0800293void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700294 void* dst, size_t dstRowBytes) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000295 // BMP in ICO have transparency, so this cannot be 565. The below code depends
296 // on the output 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}