blob: 08921203eb2149eb47b375633557ff9e40698fd3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/codec/SkBmpStandardCodec.h"
Kevin Lubick3296f7b2022-08-11 08:17:29 -04009
Kevin Lubick0c437ea2022-10-26 07:39:24 -040010#include "include/core/SkAlphaType.h"
Kevin Lubick3296f7b2022-08-11 08:17:29 -040011#include "include/core/SkColor.h"
12#include "include/core/SkColorPriv.h"
Kevin Lubick0c437ea2022-10-26 07:39:24 -040013#include "include/core/SkColorType.h"
Kevin Lubick3296f7b2022-08-11 08:17:29 -040014#include "include/core/SkImageInfo.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkStream.h"
Kevin Lubickffd084f2023-01-04 14:51:12 -050017#include "include/private/base/SkAlign.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050018#include "include/private/base/SkTemplates.h"
Kevin Lubick1b3aa8b2023-01-19 14:03:31 -050019#include "src/base/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/codec/SkCodecPriv.h"
msarett4ab9d5f2015-08-06 15:34:42 -070021
Kevin Lubick3296f7b2022-08-11 08:17:29 -040022#include <algorithm>
23#include <utility>
24
msarett4ab9d5f2015-08-06 15:34:42 -070025/*
msarett4ab9d5f2015-08-06 15:34:42 -070026 * Creates an instance of the decoder
27 * Called only by NewFromStream
28 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040029SkBmpStandardCodec::SkBmpStandardCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
30 uint16_t bitsPerPixel, uint32_t numColors,
31 uint32_t bytesPerColor, uint32_t offset,
msarettf4004f92016-02-11 10:49:31 -080032 SkCodec::SkScanlineOrder rowOrder,
33 bool isOpaque, bool inIco)
Leon Scroggins III36f7e322018-08-27 11:55:46 -040034 : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070035 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080036 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070037 , fBytesPerColor(bytesPerColor)
38 , fOffset(offset)
halcanary96fcdcc2015-08-27 07:41:13 -070039 , fSwizzler(nullptr)
msarettf4004f92016-02-11 10:49:31 -080040 , fIsOpaque(isOpaque)
msarett4ab9d5f2015-08-06 15:34:42 -070041 , fInIco(inIco)
Leon Scroggins III712476e2018-10-03 15:47:00 -040042 , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->dimensions().width(), 1)) : 0)
msarett4ab9d5f2015-08-06 15:34:42 -070043{}
44
45/*
46 * Initiates the bitmap decode
47 */
48SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo,
49 void* dst, size_t dstRowBytes,
50 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -070051 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070052 if (opts.fSubset) {
53 // Subsets are not supported.
54 return kUnimplemented;
55 }
Leon Scroggins III712476e2018-10-03 15:47:00 -040056 if (dstInfo.dimensions() != this->dimensions()) {
msarett4ab9d5f2015-08-06 15:34:42 -070057 SkCodecPrintf("Error: scaling not supported.\n");
58 return kInvalidScale;
59 }
msarett4ab9d5f2015-08-06 15:34:42 -070060
Leon Scroggins571b30f2017-07-11 17:35:31 +000061 Result result = this->prepareToDecode(dstInfo, opts);
msarett5406d6f2015-08-31 06:55:13 -070062 if (kSuccess != result) {
63 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070064 }
msarettf724b992015-10-15 06:41:06 -070065 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070066 if (rows != dstInfo.height()) {
67 *rowsDecoded = rows;
68 return kIncompleteInput;
msarett4ab9d5f2015-08-06 15:34:42 -070069 }
msarett5406d6f2015-08-31 06:55:13 -070070 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070071}
72
73/*
74 * Process the color table for the bmp input
75 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000076 bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType) {
msarett4ab9d5f2015-08-06 15:34:42 -070077 // Allocate memory for color table
78 uint32_t colorBytes = 0;
79 SkPMColor colorTable[256];
80 if (this->bitsPerPixel() <= 8) {
81 // Inform the caller of the number of colors
82 uint32_t maxColors = 1 << this->bitsPerPixel();
benjaminwagner886e5e42015-12-04 08:48:26 -080083 // Don't bother reading more than maxColors.
84 const uint32_t numColorsToRead =
Brian Osman788b9162020-02-07 10:36:46 -050085 fNumColors == 0 ? maxColors : std::min(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070086
87 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080088 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040089 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070090 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
91 SkCodecPrintf("Error: unable to read color table.\n");
92 return false;
93 }
94
Matt Sarett1a85ca52016-11-04 11:52:48 -040095 SkColorType packColorType = dstColorType;
96 SkAlphaType packAlphaType = dstAlphaType;
97 if (this->colorXform()) {
98 packColorType = kBGRA_8888_SkColorType;
99 packAlphaType = kUnpremul_SkAlphaType;
100 }
101
msarett4ab9d5f2015-08-06 15:34:42 -0700102 // Choose the proper packing function
Matt Sarett1a85ca52016-11-04 11:52:48 -0400103 bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque;
104 PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700105
106 // Fill in the color table
107 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800108 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700109 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
110 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
111 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
112 uint8_t alpha;
msarettf4004f92016-02-11 10:49:31 -0800113 if (fIsOpaque) {
msarett4ab9d5f2015-08-06 15:34:42 -0700114 alpha = 0xFF;
115 } else {
116 alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3);
117 }
118 colorTable[i] = packARGB(alpha, red, green, blue);
119 }
120
121 // To avoid segmentation faults on bad pixel data, fill the end of the
122 // color table with black. This is the same the behavior as the
123 // chromium decoder.
124 for (; i < maxColors; i++) {
125 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
126 }
127
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400128 if (this->colorXform() && !this->xformOnDecode()) {
129 this->applyColorXform(colorTable, colorTable, maxColors);
Matt Sarett1a85ca52016-11-04 11:52:48 -0400130 }
131
msarett4ab9d5f2015-08-06 15:34:42 -0700132 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700133 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700134 }
135
136 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
137 // begins. Pixel data always begins immediately after the color table.
138 if (!fInIco) {
139 // Check that we have not read past the pixel array offset
140 if(fOffset < colorBytes) {
141 // This may occur on OS 2.1 and other old versions where the color
142 // table defaults to max size, and the bmp tries to use a smaller
143 // color table. This is invalid, and our decision is to indicate
144 // an error, rather than try to guess the intended size of the
145 // color table.
146 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
147 return false;
148 }
149
150 // After reading the color table, skip to the start of the pixel array
151 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
152 SkCodecPrintf("Error: unable to skip to image data.\n");
153 return false;
154 }
155 }
156
157 // Return true on success
158 return true;
159}
160
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400161static SkEncodedInfo make_info(SkEncodedInfo::Color color,
162 SkEncodedInfo::Alpha alpha, int bitsPerPixel) {
163 // This is just used for the swizzler, which does not need the width or height.
164 return SkEncodedInfo::Make(0, 0, color, alpha, bitsPerPixel);
165}
166
167SkEncodedInfo SkBmpStandardCodec::swizzlerInfo() const {
168 const auto& info = this->getEncodedInfo();
169 if (fInIco) {
170 if (this->bitsPerPixel() <= 8) {
171 return make_info(SkEncodedInfo::kPalette_Color,
172 info.alpha(), this->bitsPerPixel());
173 }
174 if (this->bitsPerPixel() == 24) {
175 return make_info(SkEncodedInfo::kBGR_Color,
176 SkEncodedInfo::kOpaque_Alpha, 8);
177 }
178 }
179
180 return make_info(info.color(), info.alpha(), info.bitsPerComponent());
181}
182
msarettb30d6982016-02-15 10:18:45 -0800183void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett3e375b02016-05-04 13:03:48 -0700184 // In the case of bmp-in-icos, we will report BGRA to the client,
185 // since we may be required to apply an alpha mask after the decode.
186 // However, the swizzler needs to know the actual format of the bmp.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400187 SkEncodedInfo encodedInfo = this->swizzlerInfo();
msarett4ab9d5f2015-08-06 15:34:42 -0700188
189 // Get a pointer to the color table if it exists
190 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
191
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400192 SkImageInfo swizzlerInfo = dstInfo;
193 SkCodec::Options swizzlerOptions = opts;
194 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500195 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400196 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
197 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
198 }
199
200 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
201 }
202
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400203 fSwizzler = SkSwizzler::Make(encodedInfo, colorPtr, swizzlerInfo, swizzlerOptions);
msarettb30d6982016-02-15 10:18:45 -0800204 SkASSERT(fSwizzler);
msarett4ab9d5f2015-08-06 15:34:42 -0700205}
206
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400207SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000208 const SkCodec::Options& options) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400209 if (this->xformOnDecode()) {
210 this->resetXformBuffer(dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400211 }
212
msarett5406d6f2015-08-31 06:55:13 -0700213 // Create the color table if necessary and prepare the stream for decode
214 // Note that if it is non-NULL, inputColorCount will be modified
Leon Scroggins571b30f2017-07-11 17:35:31 +0000215 if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) {
msarett5406d6f2015-08-31 06:55:13 -0700216 SkCodecPrintf("Error: could not create color table.\n");
217 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700218 }
msarett5406d6f2015-08-31 06:55:13 -0700219
msarettb30d6982016-02-15 10:18:45 -0800220 // Initialize a swizzler
221 this->initializeSwizzler(dstInfo, options);
msarett5406d6f2015-08-31 06:55:13 -0700222 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700223}
224
225/*
226 * Performs the bitmap decoding for standard input format
227 */
msarettbe8216a2015-12-04 08:00:50 -0800228int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
229 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700230 // Iterate over rows of the image
msarett5406d6f2015-08-31 06:55:13 -0700231 const int height = dstInfo.height();
msarett4ab9d5f2015-08-06 15:34:42 -0700232 for (int y = 0; y < height; y++) {
233 // Read a row of the input
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400234 if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) {
msarett4ab9d5f2015-08-06 15:34:42 -0700235 SkCodecPrintf("Warning: incomplete input stream.\n");
msarette6dd0042015-10-09 11:07:34 -0700236 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700237 }
238
239 // Decode the row in destination format
msarett5406d6f2015-08-31 06:55:13 -0700240 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700241
242 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400243
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400244 if (this->xformOnDecode()) {
Matt Sarett1a85ca52016-11-04 11:52:48 -0400245 SkASSERT(this->colorXform());
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400246 fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer());
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400247 this->applyColorXform(dstRow, this->xformBuffer(), fSwizzler->swizzleWidth());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400248 } 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.
Leon Scroggins III712476e2018-10-03 15:47:00 -0400277 const int remainingScanlines = this->dimensions().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) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000305 // BMP in ICO have transparency, so this cannot be 565. The below code depends
306 // on the output being an SkPMColor.
msarett34e0ec42016-04-22 16:27:24 -0700307 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
Matt Sarett09a1c082017-02-01 15:34:22 -0800308 kBGRA_8888_SkColorType == dstInfo.colorType() ||
309 kRGBA_F16_SkColorType == dstInfo.colorType());
msarett4ab9d5f2015-08-06 15:34:42 -0700310
msarettbe8216a2015-12-04 08:00:50 -0800311 // If we are sampling, make sure that we only mask the sampled pixels.
312 // We do not need to worry about sampling in the y-dimension because that
313 // should be handled by SkSampledCodec.
314 const int sampleX = fSwizzler->sampleX();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400315 const int sampledWidth = get_scaled_dimension(this->dimensions().width(), sampleX);
msarettbe8216a2015-12-04 08:00:50 -0800316 const int srcStartX = get_start_coord(sampleX);
317
msarett4ab9d5f2015-08-06 15:34:42 -0700318
msarett5406d6f2015-08-31 06:55:13 -0700319 SkPMColor* dstPtr = (SkPMColor*) dst;
320 for (int y = 0; y < dstInfo.height(); y++) {
321 // The srcBuffer will at least be large enough
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400322 if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) {
msarett5406d6f2015-08-31 06:55:13 -0700323 SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n");
msarettbe8216a2015-12-04 08:00:50 -0800324 return;
msarett5406d6f2015-08-31 06:55:13 -0700325 }
msarett4ab9d5f2015-08-06 15:34:42 -0700326
Matt Sarett09a1c082017-02-01 15:34:22 -0800327 auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) {
328 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
329 uint64_t* dst64 = (uint64_t*) dstRow;
330 dst64[x] &= bit - 1;
331 } else {
332 uint32_t* dst32 = (uint32_t*) dstRow;
333 dst32[x] &= bit - 1;
334 }
335 };
336
msarett5406d6f2015-08-31 06:55:13 -0700337 int row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700338
Matt Sarett09a1c082017-02-01 15:34:22 -0800339 void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700340
msarettbe8216a2015-12-04 08:00:50 -0800341 int srcX = srcStartX;
342 for (int dstX = 0; dstX < sampledWidth; dstX++) {
msarett5406d6f2015-08-31 06:55:13 -0700343 int quotient;
344 int modulus;
msarettbe8216a2015-12-04 08:00:50 -0800345 SkTDivMod(srcX, 8, &quotient, &modulus);
msarett5406d6f2015-08-31 06:55:13 -0700346 uint32_t shift = 7 - modulus;
Leon Scroggins IIId81fed92017-06-01 13:42:28 -0400347 uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1;
Matt Sarett09a1c082017-02-01 15:34:22 -0800348 applyMask(dstRow, dstX, alphaBit);
msarettbe8216a2015-12-04 08:00:50 -0800349 srcX += sampleX;
msarett4ab9d5f2015-08-06 15:34:42 -0700350 }
351 }
msarett4ab9d5f2015-08-06 15:34:42 -0700352}