blob: 2b40647e8ecd81139e9fbed76153623382f33883 [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
John Stilesfbd050b2020-08-03 13:21:46 -04008#include "src/codec/SkBmpRLECodec.h"
9
10#include <memory>
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkStream.h"
13#include "include/private/SkColorData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/codec/SkCodecPriv.h"
msarett4ab9d5f2015-08-06 15:34:42 -070015
16/*
msarett4ab9d5f2015-08-06 15:34:42 -070017 * Creates an instance of the decoder
18 * Called only by NewFromStream
19 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040020SkBmpRLECodec::SkBmpRLECodec(SkEncodedInfo&& info,
Mike Reedede7bac2017-07-23 15:30:02 -040021 std::unique_ptr<SkStream> stream,
msarett5406d6f2015-08-31 06:55:13 -070022 uint16_t bitsPerPixel, uint32_t numColors,
23 uint32_t bytesPerColor, uint32_t offset,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050024 SkCodec::SkScanlineOrder rowOrder)
Leon Scroggins III36f7e322018-08-27 11:55:46 -040025 : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070026 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080027 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070028 , fBytesPerColor(bytesPerColor)
29 , fOffset(offset)
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050030 , fBytesBuffered(0)
msarett5406d6f2015-08-31 06:55:13 -070031 , fCurrRLEByte(0)
32 , fSampleX(1)
33{}
msarett4ab9d5f2015-08-06 15:34:42 -070034
35/*
36 * Initiates the bitmap decode
37 */
38SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -070039 void* dst, size_t dstRowBytes,
40 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -070041 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070042 if (opts.fSubset) {
43 // Subsets are not supported.
44 return kUnimplemented;
45 }
msarett4ab9d5f2015-08-06 15:34:42 -070046
Leon Scroggins571b30f2017-07-11 17:35:31 +000047 Result result = this->prepareToDecode(dstInfo, opts);
msarett5406d6f2015-08-31 06:55:13 -070048 if (kSuccess != result) {
49 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070050 }
51
52 // Perform the decode
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 // We set rowsDecoded equal to the height because the background has already
56 // been filled. RLE encodings sometimes skip pixels, so we always start by
57 // filling the background.
58 *rowsDecoded = dstInfo.height();
59 return kIncompleteInput;
60 }
61
62 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070063}
64
65/*
66 * Process the color table for the bmp input
67 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000068 bool SkBmpRLECodec::createColorTable(SkColorType dstColorType) {
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();
benjaminwagner886e5e42015-12-04 08:48:26 -080075 // Don't bother reading more than maxColors.
76 const uint32_t numColorsToRead =
Brian Osman788b9162020-02-07 10:36:46 -050077 fNumColors == 0 ? maxColors : std::min(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070078
79 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080080 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040081 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070082 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
83 SkCodecPrintf("Error: unable to read color table.\n");
84 return false;
85 }
86
87 // Fill in the color table
msarett34e0ec42016-04-22 16:27:24 -070088 PackColorProc packARGB = choose_pack_color_proc(false, dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070089 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -080090 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -070091 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
92 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
93 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
msarett34e0ec42016-04-22 16:27:24 -070094 colorTable[i] = packARGB(0xFF, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -070095 }
96
97 // To avoid segmentation faults on bad pixel data, fill the end of the
98 // color table with black. This is the same the behavior as the
99 // chromium decoder.
100 for (; i < maxColors; i++) {
101 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
102 }
103
104 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700105 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700106 }
107
108 // Check that we have not read past the pixel array offset
109 if(fOffset < colorBytes) {
110 // This may occur on OS 2.1 and other old versions where the color
111 // table defaults to max size, and the bmp tries to use a smaller
112 // color table. This is invalid, and our decision is to indicate
113 // an error, rather than try to guess the intended size of the
114 // color table.
115 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
116 return false;
117 }
118
119 // After reading the color table, skip to the start of the pixel array
120 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
121 SkCodecPrintf("Error: unable to skip to image data.\n");
122 return false;
123 }
124
125 // Return true on success
126 return true;
127}
128
129bool SkBmpRLECodec::initializeStreamBuffer() {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500130 fBytesBuffered = this->stream()->read(fStreamBuffer, kBufferSize);
131 if (fBytesBuffered == 0) {
msarett4ab9d5f2015-08-06 15:34:42 -0700132 SkCodecPrintf("Error: could not read RLE image data.\n");
133 return false;
134 }
msarett5406d6f2015-08-31 06:55:13 -0700135 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700136 return true;
137}
138
139/*
msarettd0375bc2015-08-12 08:08:56 -0700140 * @return the number of bytes remaining in the stream buffer after
141 * attempting to read more bytes from the stream
142 */
143size_t SkBmpRLECodec::checkForMoreData() {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500144 const size_t remainingBytes = fBytesBuffered - fCurrRLEByte;
145 uint8_t* buffer = fStreamBuffer;
msarettd0375bc2015-08-12 08:08:56 -0700146
147 // We will be reusing the same buffer, starting over from the beginning.
148 // Move any remaining bytes to the start of the buffer.
149 // We use memmove() instead of memcpy() because there is risk that the dst
150 // and src memory will overlap in corrupt images.
151 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
152
153 // Adjust the buffer ptr to the start of the unfilled data.
154 buffer += remainingBytes;
155
156 // Try to read additional bytes from the stream. There are fCurrRLEByte
157 // bytes of additional space remaining in the buffer, assuming that we
158 // have already copied remainingBytes to the start of the buffer.
159 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
160
161 // Update counters and return the number of bytes we currently have
162 // available. We are at the start of the buffer again.
163 fCurrRLEByte = 0;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500164 fBytesBuffered = remainingBytes + additionalBytes;
165 return fBytesBuffered;
msarettd0375bc2015-08-12 08:08:56 -0700166}
167
168/*
msarett4ab9d5f2015-08-06 15:34:42 -0700169 * Set an RLE pixel using the color table
170 */
171void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
172 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
173 uint8_t index) {
msarett9b9497e2016-02-11 13:29:36 -0800174 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700175 // Set the row
176 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700177
msarett5406d6f2015-08-31 06:55:13 -0700178 // Set the pixel based on destination color type
179 const int dstX = get_dst_coord(x, fSampleX);
180 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700181 case kRGBA_8888_SkColorType:
182 case kBGRA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700183 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
184 dstRow[dstX] = fColorTable->operator[](index);
185 break;
186 }
187 case kRGB_565_SkColorType: {
188 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
189 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
190 break;
191 }
192 default:
193 // This case should not be reached. We should catch an invalid
194 // color type when we check that the conversion is possible.
195 SkASSERT(false);
196 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700197 }
msarett4ab9d5f2015-08-06 15:34:42 -0700198 }
199}
200
201/*
202 * Set an RLE pixel from R, G, B values
203 */
204void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
205 const SkImageInfo& dstInfo, uint32_t x,
206 uint32_t y, uint8_t red, uint8_t green,
207 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800208 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700209 // Set the row
210 uint32_t row = this->getDstRow(y, dstInfo.height());
211
212 // Set the pixel based on destination color type
213 const int dstX = get_dst_coord(x, fSampleX);
214 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700215 case kRGBA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700216 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
msarett34e0ec42016-04-22 16:27:24 -0700217 dstRow[dstX] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
218 break;
219 }
220 case kBGRA_8888_SkColorType: {
221 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
222 dstRow[dstX] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700223 break;
224 }
225 case kRGB_565_SkColorType: {
226 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
227 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
228 break;
229 }
230 default:
231 // This case should not be reached. We should catch an invalid
232 // color type when we check that the conversion is possible.
233 SkASSERT(false);
234 break;
235 }
236 }
237}
238
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400239SkCodec::Result SkBmpRLECodec::onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000240 const SkCodec::Options& options) {
msarettfdb47572015-10-13 12:50:14 -0700241 // FIXME: Support subsets for scanline decodes.
242 if (options.fSubset) {
243 // Subsets are not supported.
244 return kUnimplemented;
245 }
246
scroggoe7fc14b2015-10-02 13:14:46 -0700247 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
248 // the sampler.
249 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800250 fLinesToSkip = 0;
251
Matt Sarett1a85ca52016-11-04 11:52:48 -0400252 SkColorType colorTableColorType = dstInfo.colorType();
253 if (this->colorXform()) {
254 // Just set a known colorType for the colorTable. No need to actually transform
Leon Scroggins571b30f2017-07-11 17:35:31 +0000255 // the colors in the colorTable.
Matt Sarett1a85ca52016-11-04 11:52:48 -0400256 colorTableColorType = kBGRA_8888_SkColorType;
257 }
258
msarett5406d6f2015-08-31 06:55:13 -0700259 // Create the color table if necessary and prepare the stream for decode
260 // Note that if it is non-NULL, inputColorCount will be modified
Leon Scroggins571b30f2017-07-11 17:35:31 +0000261 if (!this->createColorTable(colorTableColorType)) {
msarett5406d6f2015-08-31 06:55:13 -0700262 SkCodecPrintf("Error: could not create color table.\n");
263 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700264 }
265
msarett5406d6f2015-08-31 06:55:13 -0700266 // Initialize a buffer for encoded RLE data
267 if (!this->initializeStreamBuffer()) {
268 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
msarett2a98bac2016-02-17 14:06:46 -0800269 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700270 }
msarett5406d6f2015-08-31 06:55:13 -0700271
msarett5406d6f2015-08-31 06:55:13 -0700272 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700273}
274
275/*
276 * Performs the bitmap decoding for RLE input format
277 * RLE decoding is performed all at once, rather than a one row at a time
278 */
msarette6dd0042015-10-09 11:07:34 -0700279int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes,
280 const Options& opts) {
Matt Sarett61935682016-11-03 17:27:12 +0000281 int height = info.height();
282
283 // Account for sampling.
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400284 SkImageInfo dstInfo = info.makeWH(this->fillWidth(), height);
Matt Sarett61935682016-11-03 17:27:12 +0000285
286 // Set the background as transparent. Then, if the RLE code skips pixels,
287 // the skipped pixels will be transparent.
Matt Sarett61935682016-11-03 17:27:12 +0000288 if (dst) {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400289 SkSampler::Fill(dstInfo, dst, dstRowBytes, opts.fZeroInitialized);
Matt Sarett61935682016-11-03 17:27:12 +0000290 }
291
292 // Adjust the height and the dst if the previous call to decodeRows() left us
293 // with lines that need to be skipped.
294 if (height > fLinesToSkip) {
295 height -= fLinesToSkip;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400296 if (dst) {
297 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
298 }
Matt Sarett61935682016-11-03 17:27:12 +0000299 fLinesToSkip = 0;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400300
301 dstInfo = dstInfo.makeWH(dstInfo.width(), height);
Matt Sarett61935682016-11-03 17:27:12 +0000302 } else {
303 fLinesToSkip -= height;
304 return height;
305 }
306
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400307 void* decodeDst = dst;
308 size_t decodeRowBytes = dstRowBytes;
309 SkImageInfo decodeInfo = dstInfo;
310 if (decodeDst) {
311 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500312 decodeInfo = decodeInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400313 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
314 int count = height * dstInfo.width();
315 this->resetXformBuffer(count);
316 sk_bzero(this->xformBuffer(), count * sizeof(uint32_t));
317 decodeDst = this->xformBuffer();
318 decodeRowBytes = dstInfo.width() * sizeof(uint32_t);
319 }
320 }
321 }
322
323 int decodedHeight = this->decodeRLE(decodeInfo, decodeDst, decodeRowBytes);
324 if (this->colorXform() && decodeDst) {
325 for (int y = 0; y < decodedHeight; y++) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400326 this->applyColorXform(dst, decodeDst, dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400327 decodeDst = SkTAddOffset<void>(decodeDst, decodeRowBytes);
328 dst = SkTAddOffset<void>(dst, dstRowBytes);
329 }
330 }
331
332 return decodedHeight;
333}
334
335int SkBmpRLECodec::decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes) {
336 // Use the original width to count the number of pixels in each row.
Leon Scroggins III712476e2018-10-03 15:47:00 -0400337 const int width = this->dimensions().width();
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400338
339 // This tells us the number of rows that we are meant to decode.
340 const int height = dstInfo.height();
341
342 // Set RLE flags
Leon Scroggins III862c1962017-10-02 16:28:49 -0400343 constexpr uint8_t RLE_ESCAPE = 0;
344 constexpr uint8_t RLE_EOL = 0;
345 constexpr uint8_t RLE_EOF = 1;
346 constexpr uint8_t RLE_DELTA = 2;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400347
msarett4946b942016-02-11 08:41:01 -0800348 // Destination parameters
349 int x = 0;
350 int y = 0;
351
msarett4ab9d5f2015-08-06 15:34:42 -0700352 while (true) {
353 // If we have reached a row that is beyond the requested height, we have
354 // succeeded.
355 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700356 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700357 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700358 // would require us to stop before decoding the full height.
359 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700360 }
361
362 // Every entry takes at least two bytes
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500363 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700364 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700365 return y;
msarettd0375bc2015-08-12 08:08:56 -0700366 }
msarett4ab9d5f2015-08-06 15:34:42 -0700367 }
368
369 // Read the next two bytes. These bytes have different meanings
370 // depending on their values. In the first interpretation, the first
371 // byte is an escape flag and the second byte indicates what special
372 // task to perform.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500373 const uint8_t flag = fStreamBuffer[fCurrRLEByte++];
374 const uint8_t task = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700375
376 // Perform decoding
377 if (RLE_ESCAPE == flag) {
378 switch (task) {
379 case RLE_EOL:
380 x = 0;
381 y++;
382 break;
383 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800384 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700385 case RLE_DELTA: {
386 // Two bytes are needed to specify delta
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500387 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700388 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700389 return y;
msarettd0375bc2015-08-12 08:08:56 -0700390 }
msarett4ab9d5f2015-08-06 15:34:42 -0700391 }
392 // Modify x and y
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500393 const uint8_t dx = fStreamBuffer[fCurrRLEByte++];
394 const uint8_t dy = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700395 x += dx;
396 y += dy;
msarett4946b942016-02-11 08:41:01 -0800397 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700398 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700399 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800400 } else if (y > height) {
401 fLinesToSkip = y - height;
402 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700403 }
404 break;
405 }
406 default: {
407 // If task does not match any of the above signals, it
408 // indicates that we have a sequence of non-RLE pixels.
409 // Furthermore, the value of task is equal to the number
410 // of pixels to interpret.
411 uint8_t numPixels = task;
412 const size_t rowBytes = compute_row_bytes(numPixels,
413 this->bitsPerPixel());
414 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700415 // image.
416 if (x + numPixels > width) {
417 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700418 return y;
msarettd0375bc2015-08-12 08:08:56 -0700419 }
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500420
msarettd0375bc2015-08-12 08:08:56 -0700421 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700422 // remaining in the stream to set numPixels.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500423
424 // At most, alignedRowBytes can be 255 (max uint8_t) *
425 // 3 (max bytes per pixel) + 1 (aligned) = 766. If
426 // fStreamBuffer was smaller than this,
427 // checkForMoreData would never succeed for some bmps.
428 static_assert(255 * 3 + 1 < kBufferSize,
429 "kBufferSize needs to be larger!");
430 const size_t alignedRowBytes = SkAlign2(rowBytes);
431 if ((int) fBytesBuffered - fCurrRLEByte < alignedRowBytes) {
432 SkASSERT(alignedRowBytes < kBufferSize);
433 if (this->checkForMoreData() < alignedRowBytes) {
msarette6dd0042015-10-09 11:07:34 -0700434 return y;
msarettd0375bc2015-08-12 08:08:56 -0700435 }
msarett4ab9d5f2015-08-06 15:34:42 -0700436 }
437 // Set numPixels number of pixels
438 while (numPixels > 0) {
439 switch(this->bitsPerPixel()) {
440 case 4: {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500441 SkASSERT(fCurrRLEByte < fBytesBuffered);
442 uint8_t val = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700443 setPixel(dst, dstRowBytes, dstInfo, x++,
444 y, val >> 4);
445 numPixels--;
446 if (numPixels != 0) {
447 setPixel(dst, dstRowBytes, dstInfo,
448 x++, y, val & 0xF);
449 numPixels--;
450 }
451 break;
452 }
453 case 8:
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500454 SkASSERT(fCurrRLEByte < fBytesBuffered);
msarett4ab9d5f2015-08-06 15:34:42 -0700455 setPixel(dst, dstRowBytes, dstInfo, x++,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500456 y, fStreamBuffer[fCurrRLEByte++]);
msarett4ab9d5f2015-08-06 15:34:42 -0700457 numPixels--;
458 break;
459 case 24: {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500460 SkASSERT(fCurrRLEByte + 2 < fBytesBuffered);
461 uint8_t blue = fStreamBuffer[fCurrRLEByte++];
462 uint8_t green = fStreamBuffer[fCurrRLEByte++];
463 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700464 setRGBPixel(dst, dstRowBytes, dstInfo,
465 x++, y, red, green, blue);
466 numPixels--;
msarettd567a812016-10-05 07:19:27 -0700467 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700468 }
469 default:
470 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700471 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700472 }
473 }
474 // Skip a byte if necessary to maintain alignment
475 if (!SkIsAlign2(rowBytes)) {
476 fCurrRLEByte++;
477 }
478 break;
479 }
480 }
481 } else {
482 // If the first byte read is not a flag, it indicates the number of
483 // pixels to set in RLE mode.
484 const uint8_t numPixels = flag;
Brian Osman788b9162020-02-07 10:36:46 -0500485 const int endX = std::min<int>(x + numPixels, width);
msarett4ab9d5f2015-08-06 15:34:42 -0700486
487 if (24 == this->bitsPerPixel()) {
488 // In RLE24, the second byte read is part of the pixel color.
489 // There are two more required bytes to finish encoding the
490 // color.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500491 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700492 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700493 return y;
msarettd0375bc2015-08-12 08:08:56 -0700494 }
msarett4ab9d5f2015-08-06 15:34:42 -0700495 }
496
497 // Fill the pixels up to endX with the specified color
498 uint8_t blue = task;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500499 uint8_t green = fStreamBuffer[fCurrRLEByte++];
500 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700501 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700502 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700503 }
504 } else {
505 // In RLE8 or RLE4, the second byte read gives the index in the
506 // color table to look up the pixel color.
507 // RLE8 has one color index that gets repeated
508 // RLE4 has two color indexes in the upper and lower 4 bits of
509 // the bytes, which are alternated
510 uint8_t indices[2] = { task, task };
511 if (4 == this->bitsPerPixel()) {
512 indices[0] >>= 4;
513 indices[1] &= 0xf;
514 }
515
516 // Set the indicated number of pixels
517 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700518 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700519 which = !which;
520 }
521 }
522 }
523 }
524}
scroggoe7fc14b2015-10-02 13:14:46 -0700525
msarett9b9497e2016-02-11 13:29:36 -0800526bool SkBmpRLECodec::skipRows(int count) {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400527 const SkImageInfo rowInfo = SkImageInfo::Make(this->dimensions().width(), count,
528 kN32_SkColorType, kUnpremul_SkAlphaType);
msarett9b9497e2016-02-11 13:29:36 -0800529 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
530}
531
msarette6dd0042015-10-09 11:07:34 -0700532// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
533// Or it should do all sampling natively.
534// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700535class SkBmpRLESampler : public SkSampler {
536public:
537 SkBmpRLESampler(SkBmpRLECodec* codec)
538 : fCodec(codec)
539 {
540 SkASSERT(fCodec);
541 }
542
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400543 int fillWidth() const override {
544 return fCodec->fillWidth();
545 }
546
scroggoe7fc14b2015-10-02 13:14:46 -0700547private:
msarette6dd0042015-10-09 11:07:34 -0700548 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700549 return fCodec->setSampleX(sampleX);
550 }
551
552 // Unowned pointer. fCodec will delete this class in its destructor.
553 SkBmpRLECodec* fCodec;
554};
555
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400556SkSampler* SkBmpRLECodec::getSampler(bool createIfNecessary) {
557 if (!fSampler && createIfNecessary) {
John Stilesfbd050b2020-08-03 13:21:46 -0400558 fSampler = std::make_unique<SkBmpRLESampler>(this);
scroggoe7fc14b2015-10-02 13:14:46 -0700559 }
560
Ben Wagner145dbcd2016-11-03 14:40:50 -0400561 return fSampler.get();
scroggoe7fc14b2015-10-02 13:14:46 -0700562}
563
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400564int SkBmpRLECodec::setSampleX(int sampleX) {
scroggoe7fc14b2015-10-02 13:14:46 -0700565 fSampleX = sampleX;
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400566 return this->fillWidth();
567}
568
569int SkBmpRLECodec::fillWidth() const {
570 return get_scaled_dimension(this->dimensions().width(), fSampleX);
scroggoe7fc14b2015-10-02 13:14:46 -0700571}