blob: c223678769b710e5dfb5b25b224bf567ccfad3b7 [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)
Matt Sarett1a85ca52016-11-04 11:52:48 -040031 , fXformOnDecode(false)
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 }
msarett4ab9d5f2015-08-06 15:34:42 -070051
msarett5406d6f2015-08-31 06:55:13 -070052 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
53 if (kSuccess != result) {
54 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070055 }
msarettf724b992015-10-15 06:41:06 -070056 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070057 if (rows != dstInfo.height()) {
58 *rowsDecoded = rows;
59 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070060 }
msarett5406d6f2015-08-31 06:55:13 -070061 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070062}
63
64/*
65 * Process the color table for the bmp input
66 */
msarett34e0ec42016-04-22 16:27:24 -070067 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType,
68 int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070069 // Allocate memory for color table
70 uint32_t colorBytes = 0;
71 SkPMColor colorTable[256];
72 if (this->bitsPerPixel() <= 8) {
73 // Inform the caller of the number of colors
74 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070075 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070076 // We set the number of colors to maxColors in order to ensure
77 // safe memory accesses. Otherwise, an invalid pixel could
78 // access memory outside of our color table array.
79 *numColors = maxColors;
80 }
benjaminwagner886e5e42015-12-04 08:48:26 -080081 // Don't bother reading more than maxColors.
82 const uint32_t numColorsToRead =
83 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070084
85 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080086 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040087 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070088 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
89 SkCodecPrintf("Error: unable to read color table.\n");
90 return false;
91 }
92
Matt Sarett1a85ca52016-11-04 11:52:48 -040093 SkColorType packColorType = dstColorType;
94 SkAlphaType packAlphaType = dstAlphaType;
95 if (this->colorXform()) {
96 packColorType = kBGRA_8888_SkColorType;
97 packAlphaType = kUnpremul_SkAlphaType;
98 }
99
msarett4ab9d5f2015-08-06 15:34:42 -0700100 // Choose the proper packing function
Matt Sarett1a85ca52016-11-04 11:52:48 -0400101 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
102 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700103
104 // Fill in the color table
105 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800106 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700107 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
108 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
109 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
110 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800111 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700112 alpha = 0xFF;
113 } else {
114 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
115 }
116 colorTable[i] = packARGB(alpha, red, green, blue);
117 }
118
119 // To avoid segmentation faults on bad pixel data, fill the end of the
120 // color table with black. This is the same the behavior as the
121 // chromium decoder.
122 for (; i < maxColors; i++) {
123 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
124 }
125
Matt Sarett1a85ca52016-11-04 11:52:48 -0400126 if (this->colorXform() && !fXformOnDecode) {
Matt Sarett19aff5d2017-04-03 16:01:10 -0400127 SkColorSpaceXform::ColorFormat dstFormat = select_xform_format_ct(dstColorType);
Matt Sarett1a85ca52016-11-04 11:52:48 -0400128 SkColorSpaceXform::ColorFormat srcFormat = SkColorSpaceXform::kBGRA_8888_ColorFormat;
129 SkAlphaType xformAlphaType = select_xform_alpha(dstAlphaType,
130 this->getInfo().alphaType());
131 SkAssertResult(this->colorXform()->apply(dstFormat, colorTable, srcFormat, colorTable,
132 maxColors, xformAlphaType));
133 }
134
msarett4ab9d5f2015-08-06 15:34:42 -0700135 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700136 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700137 }
138
139 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
140 // begins. Pixel data always begins immediately after the color table.
141 if (!fInIco) {
142 // Check that we have not read past the pixel array offset
143 if(fOffset < colorBytes) {
144 // This may occur on OS 2.1 and other old versions where the color
145 // table defaults to max size, and the bmp tries to use a smaller
146 // color table. This is invalid, and our decision is to indicate
147 // an error, rather than try to guess the intended size of the
148 // color table.
149 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
150 return false;
151 }
152
153 // After reading the color table, skip to the start of the pixel array
154 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
155 SkCodecPrintf("Error: unable to skip to image data.\n");
156 return false;
157 }
158 }
159
160 // Return true on success
161 return true;
162}
163
msarettb30d6982016-02-15 10:18:45 -0800164void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700165 // In the case of bmp-in-icos, we will report BGRA to the client,
166 // since we may be required to apply an alpha mask after the decode.
167 // However, the swizzler needs to know the actual format of the bmp.
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400168 SkEncodedInfo encodedInfo = this->getEncodedInfo();
msarett3e375b02016-05-04 13:03:48 -0700169 if (fInIco) {
170 if (this->bitsPerPixel() <= 8) {
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400171 encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color,
172 encodedInfo.alpha(), this->bitsPerPixel());
msarett3e375b02016-05-04 13:03:48 -0700173 } else if (this->bitsPerPixel() == 24) {
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400174 encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kBGR_Color,
msarett3e375b02016-05-04 13:03:48 -0700175 SkEncodedInfo::kOpaque_Alpha, 8);
176 }
msarett4ab9d5f2015-08-06 15:34:42 -0700177 }
178
179 // Get a pointer to the color table if it exists
180 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
181
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400182 SkImageInfo swizzlerInfo = dstInfo;
183 SkCodec::Options swizzlerOptions = opts;
184 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500185 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400186 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
187 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
188 }
189
190 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
191 }
192
193
194 fSwizzler.reset(SkSwizzler::CreateSwizzler(encodedInfo, colorPtr, swizzlerInfo,
195 swizzlerOptions));
msarettb30d6982016-02-15 10:18:45 -0800196 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700197}
198
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400199SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700200 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
Matt Sarett1a85ca52016-11-04 11:52:48 -0400201 fXformOnDecode = false;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400202 if (this->colorXform()) {
Matt Sarett1a85ca52016-11-04 11:52:48 -0400203 fXformOnDecode = apply_xform_on_decode(dstInfo.colorType(), this->getEncodedInfo().color());
204 if (fXformOnDecode) {
205 this->resetXformBuffer(dstInfo.width());
206 }
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400207 }
208
msarett5406d6f2015-08-31 06:55:13 -0700209 // Create the color table if necessary and prepare the stream for decode
210 // Note that if it is non-NULL, inputColorCount will be modified
msarett34e0ec42016-04-22 16:27:24 -0700211 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) {
msarett5406d6f2015-08-31 06:55:13 -0700212 SkCodecPrintf("Error: could not create color table.\n");
213 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700214 }
msarett5406d6f2015-08-31 06:55:13 -0700215
216 // Copy the color table to the client if necessary
Hal Canary67b39de2016-11-07 11:47:44 -0500217 copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount);
msarett5406d6f2015-08-31 06:55:13 -0700218
msarettb30d6982016-02-15 10:18:45 -0800219 // Initialize a swizzler
220 this->initializeSwizzler(dstInfo, options);
msarett5406d6f2015-08-31 06:55:13 -0700221 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700222}
223
224/*
225 * Performs the bitmap decoding for standard input format
226 */
msarettbe8216a2015-12-04 08:00:50 -0800227int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
228 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700229 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700230 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700231 for (int y = 0; y < height; y++) {
232 // Read a row of the input
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400233 if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700234 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700235 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700236 }
237
238 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700239 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700240
241 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400242
Matt Sarett1a85ca52016-11-04 11:52:48 -0400243 if (fXformOnDecode) {
244 SkASSERT(this->colorXform());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400245 SkImageInfo xformInfo = dstInfo.makeWH(fSwizzler->swizzleWidth(), dstInfo.height());
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400246 fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400247 this->applyColorXform(xformInfo, dstRow, this->xformBuffer());
248 } else {
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400249 fSwizzler->swizzle(dstRow, this->srcBuffer());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400250 }
msarett4ab9d5f2015-08-06 15:34:42 -0700251 }
252
msarett1088db92016-03-22 08:58:35 -0700253 if (fInIco && fIsOpaque) {
msarettbe8216a2015-12-04 08:00:50 -0800254 const int startScanline = this->currScanline();
255 if (startScanline < 0) {
256 // We are not performing a scanline decode.
257 // Just decode the entire ICO mask and return.
258 decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes);
259 return height;
260 }
261
262 // In order to perform a scanline ICO decode, we must be able
263 // to skip ahead in the stream in order to apply the AND mask
264 // to the requested scanlines.
265 // We will do this by taking advantage of the fact that
266 // SkIcoCodec always uses a SkMemoryStream as its underlying
267 // representation of the stream.
268 const void* memoryBase = this->stream()->getMemoryBase();
269 SkASSERT(nullptr != memoryBase);
270 SkASSERT(this->stream()->hasLength());
271 SkASSERT(this->stream()->hasPosition());
272
273 const size_t length = this->stream()->getLength();
274 const size_t currPosition = this->stream()->getPosition();
275
276 // Calculate how many bytes we must skip to reach the AND mask.
277 const int remainingScanlines = this->getInfo().height() - startScanline - height;
msarett9b9497e2016-02-11 13:29:36 -0800278 const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() +
msarettbe8216a2015-12-04 08:00:50 -0800279 startScanline * fAndMaskRowBytes;
280 const size_t subStreamStartPosition = currPosition + bytesToSkip;
281 if (subStreamStartPosition >= length) {
282 // FIXME: How can we indicate that this decode was actually incomplete?
283 return height;
284 }
285
286 // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate
287 // the memory base into a stream in order to safely handle incomplete images
288 // without reading out of bounds memory.
289 const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase,
290 subStreamStartPosition);
291 const size_t subStreamLength = length - subStreamStartPosition;
292 // This call does not transfer ownership of the subStreamMemoryBase.
293 SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false);
294
295 // FIXME: If decodeIcoMask does not succeed, is there a way that we can
296 // indicate the decode was incomplete?
297 decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes);
298 }
299
msarette6dd0042015-10-09 11:07:34 -0700300 return height;
msarett5406d6f2015-08-31 06:55:13 -0700301}
scroggocc2feb12015-08-14 08:32:46 -0700302
msarettbe8216a2015-12-04 08:00:50 -0800303void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700304 void* dst, size_t dstRowBytes) {
305 // BMP in ICO have transparency, so this cannot be 565, and this mask
306 // prevents us from using kIndex8. The below code depends on the output
307 // being an SkPMColor.
msarett34e0ec42016-04-22 16:27:24 -0700308 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
Matt Sarett09a1c082017-02-01 15:34:22 -0800309 kBGRA_8888_SkColorType == dstInfo.colorType() ||
310 kRGBA_F16_SkColorType == dstInfo.colorType());
msarett4ab9d5f2015-08-06 15:34:42 -0700311
msarettbe8216a2015-12-04 08:00:50 -0800312 // If we are sampling, make sure that we only mask the sampled pixels.
313 // We do not need to worry about sampling in the y-dimension because that
314 // should be handled by SkSampledCodec.
315 const int sampleX = fSwizzler->sampleX();
316 const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX);
317 const int srcStartX = get_start_coord(sampleX);
318
msarett4ab9d5f2015-08-06 15:34:42 -0700319
msarett5406d6f2015-08-31 06:55:13 -0700320 SkPMColor* dstPtr = (SkPMColor*) dst;
321 for (int y = 0; y < dstInfo.height(); y++) {
322 // The srcBuffer will at least be large enough
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400323 if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700324 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800325 return;
msarett5406d6f2015-08-31 06:55:13 -0700326 }
msarett4ab9d5f2015-08-06 15:34:42 -0700327
Matt Sarett09a1c082017-02-01 15:34:22 -0800328 auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) {
329 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
330 uint64_t* dst64 = (uint64_t*) dstRow;
331 dst64[x] &= bit - 1;
332 } else {
333 uint32_t* dst32 = (uint32_t*) dstRow;
334 dst32[x] &= bit - 1;
335 }
336 };
337
msarett5406d6f2015-08-31 06:55:13 -0700338 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700339
Matt Sarett09a1c082017-02-01 15:34:22 -0800340 void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700341
msarettbe8216a2015-12-04 08:00:50 -0800342 int srcX = srcStartX;
343 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700344 int quotient;
345 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800346 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700347 uint32_t shift = 7 - modulus;
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400348 uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1;
Matt Sarett09a1c082017-02-01 15:34:22 -0800349 applyMask(dstRow, dstX, alphaBit);
msarettbe8216a2015-12-04 08:00:50 -0800350 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700351 }
352 }
msarett4ab9d5f2015-08-06 15:34:42 -0700353}
msarette6dd0042015-10-09 11:07:34 -0700354
msarettf7eb6fc2016-09-13 09:04:11 -0700355uint64_t SkBmpStandardCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
msarette6dd0042015-10-09 11:07:34 -0700356 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
357 if (colorPtr) {
msarettf7eb6fc2016-09-13 09:04:11 -0700358 return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(), colorPtr, 0,
Matt Sarett19aff5d2017-04-03 16:01:10 -0400359 this->colorXform(), false);
msarette6dd0042015-10-09 11:07:34 -0700360 }
msarettf7eb6fc2016-09-13 09:04:11 -0700361 return INHERITED::onGetFillValue(dstInfo);
msarette6dd0042015-10-09 11:07:34 -0700362}