blob: bd4624e6cab64220e968f9e8dbfa76875c80a7b8 [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 "SkBmpRLECodec.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 */
Mike Reedede7bac2017-07-23 15:30:02 -040017SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info,
18 std::unique_ptr<SkStream> stream,
msarett5406d6f2015-08-31 06:55:13 -070019 uint16_t bitsPerPixel, uint32_t numColors,
20 uint32_t bytesPerColor, uint32_t offset,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050021 SkCodec::SkScanlineOrder rowOrder)
Mike Reedede7bac2017-07-23 15:30:02 -040022 : INHERITED(width, height, 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)
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -050027 , fBytesBuffered(0)
msarett5406d6f2015-08-31 06:55:13 -070028 , fCurrRLEByte(0)
29 , fSampleX(1)
30{}
msarett4ab9d5f2015-08-06 15:34:42 -070031
32/*
33 * Initiates the bitmap decode
34 */
35SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -070036 void* dst, size_t dstRowBytes,
37 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -070038 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070039 if (opts.fSubset) {
40 // Subsets are not supported.
41 return kUnimplemented;
42 }
msarett4ab9d5f2015-08-06 15:34:42 -070043
Leon Scroggins571b30f2017-07-11 17:35:31 +000044 Result result = this->prepareToDecode(dstInfo, opts);
msarett5406d6f2015-08-31 06:55:13 -070045 if (kSuccess != result) {
46 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070047 }
48
49 // Perform the decode
msarettf724b992015-10-15 06:41:06 -070050 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070051 if (rows != dstInfo.height()) {
52 // We set rowsDecoded equal to the height because the background has already
53 // been filled. RLE encodings sometimes skip pixels, so we always start by
54 // filling the background.
55 *rowsDecoded = dstInfo.height();
56 return kIncompleteInput;
57 }
58
59 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 SkBmpRLECodec::createColorTable(SkColorType dstColorType) {
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
84 // Fill in the color table
msarett34e0ec42016-04-22 16:27:24 -070085 PackColorProc packARGB = choose_pack_color_proc(false, dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070086 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -080087 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -070088 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
89 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
90 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
msarett34e0ec42016-04-22 16:27:24 -070091 colorTable[i] = packARGB(0xFF, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -070092 }
93
94 // To avoid segmentation faults on bad pixel data, fill the end of the
95 // color table with black. This is the same the behavior as the
96 // chromium decoder.
97 for (; i < maxColors; i++) {
98 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
99 }
100
101 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700102 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700103 }
104
105 // Check that we have not read past the pixel array offset
106 if(fOffset < colorBytes) {
107 // This may occur on OS 2.1 and other old versions where the color
108 // table defaults to max size, and the bmp tries to use a smaller
109 // color table. This is invalid, and our decision is to indicate
110 // an error, rather than try to guess the intended size of the
111 // color table.
112 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
113 return false;
114 }
115
116 // After reading the color table, skip to the start of the pixel array
117 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
118 SkCodecPrintf("Error: unable to skip to image data.\n");
119 return false;
120 }
121
122 // Return true on success
123 return true;
124}
125
126bool SkBmpRLECodec::initializeStreamBuffer() {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500127 fBytesBuffered = this->stream()->read(fStreamBuffer, kBufferSize);
128 if (fBytesBuffered == 0) {
msarett4ab9d5f2015-08-06 15:34:42 -0700129 SkCodecPrintf("Error: could not read RLE image data.\n");
130 return false;
131 }
msarett5406d6f2015-08-31 06:55:13 -0700132 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700133 return true;
134}
135
136/*
msarettd0375bc2015-08-12 08:08:56 -0700137 * @return the number of bytes remaining in the stream buffer after
138 * attempting to read more bytes from the stream
139 */
140size_t SkBmpRLECodec::checkForMoreData() {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500141 const size_t remainingBytes = fBytesBuffered - fCurrRLEByte;
142 uint8_t* buffer = fStreamBuffer;
msarettd0375bc2015-08-12 08:08:56 -0700143
144 // We will be reusing the same buffer, starting over from the beginning.
145 // Move any remaining bytes to the start of the buffer.
146 // We use memmove() instead of memcpy() because there is risk that the dst
147 // and src memory will overlap in corrupt images.
148 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
149
150 // Adjust the buffer ptr to the start of the unfilled data.
151 buffer += remainingBytes;
152
153 // Try to read additional bytes from the stream. There are fCurrRLEByte
154 // bytes of additional space remaining in the buffer, assuming that we
155 // have already copied remainingBytes to the start of the buffer.
156 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
157
158 // Update counters and return the number of bytes we currently have
159 // available. We are at the start of the buffer again.
160 fCurrRLEByte = 0;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500161 fBytesBuffered = remainingBytes + additionalBytes;
162 return fBytesBuffered;
msarettd0375bc2015-08-12 08:08:56 -0700163}
164
165/*
msarett4ab9d5f2015-08-06 15:34:42 -0700166 * Set an RLE pixel using the color table
167 */
168void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
169 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
170 uint8_t index) {
msarett9b9497e2016-02-11 13:29:36 -0800171 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700172 // Set the row
173 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700174
msarett5406d6f2015-08-31 06:55:13 -0700175 // Set the pixel based on destination color type
176 const int dstX = get_dst_coord(x, fSampleX);
177 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700178 case kRGBA_8888_SkColorType:
179 case kBGRA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700180 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
181 dstRow[dstX] = fColorTable->operator[](index);
182 break;
183 }
184 case kRGB_565_SkColorType: {
185 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
186 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
187 break;
188 }
189 default:
190 // This case should not be reached. We should catch an invalid
191 // color type when we check that the conversion is possible.
192 SkASSERT(false);
193 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700194 }
msarett4ab9d5f2015-08-06 15:34:42 -0700195 }
196}
197
198/*
199 * Set an RLE pixel from R, G, B values
200 */
201void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
202 const SkImageInfo& dstInfo, uint32_t x,
203 uint32_t y, uint8_t red, uint8_t green,
204 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800205 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700206 // Set the row
207 uint32_t row = this->getDstRow(y, dstInfo.height());
208
209 // Set the pixel based on destination color type
210 const int dstX = get_dst_coord(x, fSampleX);
211 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700212 case kRGBA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700213 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
msarett34e0ec42016-04-22 16:27:24 -0700214 dstRow[dstX] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
215 break;
216 }
217 case kBGRA_8888_SkColorType: {
218 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
219 dstRow[dstX] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700220 break;
221 }
222 case kRGB_565_SkColorType: {
223 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
224 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
225 break;
226 }
227 default:
228 // This case should not be reached. We should catch an invalid
229 // color type when we check that the conversion is possible.
230 SkASSERT(false);
231 break;
232 }
233 }
234}
235
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400236SkCodec::Result SkBmpRLECodec::onPrepareToDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000237 const SkCodec::Options& options) {
msarettfdb47572015-10-13 12:50:14 -0700238 // FIXME: Support subsets for scanline decodes.
239 if (options.fSubset) {
240 // Subsets are not supported.
241 return kUnimplemented;
242 }
243
scroggoe7fc14b2015-10-02 13:14:46 -0700244 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
245 // the sampler.
246 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800247 fLinesToSkip = 0;
248
Matt Sarett1a85ca52016-11-04 11:52:48 -0400249 SkColorType colorTableColorType = dstInfo.colorType();
250 if (this->colorXform()) {
251 // Just set a known colorType for the colorTable. No need to actually transform
Leon Scroggins571b30f2017-07-11 17:35:31 +0000252 // the colors in the colorTable.
Matt Sarett1a85ca52016-11-04 11:52:48 -0400253 colorTableColorType = kBGRA_8888_SkColorType;
254 }
255
msarett5406d6f2015-08-31 06:55:13 -0700256 // Create the color table if necessary and prepare the stream for decode
257 // Note that if it is non-NULL, inputColorCount will be modified
Leon Scroggins571b30f2017-07-11 17:35:31 +0000258 if (!this->createColorTable(colorTableColorType)) {
msarett5406d6f2015-08-31 06:55:13 -0700259 SkCodecPrintf("Error: could not create color table.\n");
260 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700261 }
262
msarett5406d6f2015-08-31 06:55:13 -0700263 // Initialize a buffer for encoded RLE data
264 if (!this->initializeStreamBuffer()) {
265 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
msarett2a98bac2016-02-17 14:06:46 -0800266 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700267 }
msarett5406d6f2015-08-31 06:55:13 -0700268
msarett5406d6f2015-08-31 06:55:13 -0700269 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700270}
271
272/*
273 * Performs the bitmap decoding for RLE input format
274 * RLE decoding is performed all at once, rather than a one row at a time
275 */
msarette6dd0042015-10-09 11:07:34 -0700276int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes,
277 const Options& opts) {
Matt Sarett61935682016-11-03 17:27:12 +0000278 const int width = this->getInfo().width();
279 int height = info.height();
280
281 // Account for sampling.
282 SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height);
283
284 // Set the background as transparent. Then, if the RLE code skips pixels,
285 // the skipped pixels will be transparent.
Matt Sarett61935682016-11-03 17:27:12 +0000286 if (dst) {
287 SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized);
288 }
289
290 // Adjust the height and the dst if the previous call to decodeRows() left us
291 // with lines that need to be skipped.
292 if (height > fLinesToSkip) {
293 height -= fLinesToSkip;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400294 if (dst) {
295 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
296 }
Matt Sarett61935682016-11-03 17:27:12 +0000297 fLinesToSkip = 0;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400298
299 dstInfo = dstInfo.makeWH(dstInfo.width(), height);
Matt Sarett61935682016-11-03 17:27:12 +0000300 } else {
301 fLinesToSkip -= height;
302 return height;
303 }
304
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400305 void* decodeDst = dst;
306 size_t decodeRowBytes = dstRowBytes;
307 SkImageInfo decodeInfo = dstInfo;
308 if (decodeDst) {
309 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500310 decodeInfo = decodeInfo.makeColorType(kXformSrcColorType);
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400311 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
312 int count = height * dstInfo.width();
313 this->resetXformBuffer(count);
314 sk_bzero(this->xformBuffer(), count * sizeof(uint32_t));
315 decodeDst = this->xformBuffer();
316 decodeRowBytes = dstInfo.width() * sizeof(uint32_t);
317 }
318 }
319 }
320
321 int decodedHeight = this->decodeRLE(decodeInfo, decodeDst, decodeRowBytes);
322 if (this->colorXform() && decodeDst) {
323 for (int y = 0; y < decodedHeight; y++) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400324 this->applyColorXform(dst, decodeDst, dstInfo.width());
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400325 decodeDst = SkTAddOffset<void>(decodeDst, decodeRowBytes);
326 dst = SkTAddOffset<void>(dst, dstRowBytes);
327 }
328 }
329
330 return decodedHeight;
331}
332
333int SkBmpRLECodec::decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes) {
334 // Use the original width to count the number of pixels in each row.
335 const int width = this->getInfo().width();
336
337 // This tells us the number of rows that we are meant to decode.
338 const int height = dstInfo.height();
339
340 // Set RLE flags
341 static const uint8_t RLE_ESCAPE = 0;
342 static const uint8_t RLE_EOL = 0;
343 static const uint8_t RLE_EOF = 1;
344 static const uint8_t RLE_DELTA = 2;
345
msarett4946b942016-02-11 08:41:01 -0800346 // Destination parameters
347 int x = 0;
348 int y = 0;
349
msarett4ab9d5f2015-08-06 15:34:42 -0700350 while (true) {
351 // If we have reached a row that is beyond the requested height, we have
352 // succeeded.
353 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700354 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700355 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700356 // would require us to stop before decoding the full height.
357 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700358 }
359
360 // Every entry takes at least two bytes
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500361 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700362 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700363 return y;
msarettd0375bc2015-08-12 08:08:56 -0700364 }
msarett4ab9d5f2015-08-06 15:34:42 -0700365 }
366
367 // Read the next two bytes. These bytes have different meanings
368 // depending on their values. In the first interpretation, the first
369 // byte is an escape flag and the second byte indicates what special
370 // task to perform.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500371 const uint8_t flag = fStreamBuffer[fCurrRLEByte++];
372 const uint8_t task = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700373
374 // Perform decoding
375 if (RLE_ESCAPE == flag) {
376 switch (task) {
377 case RLE_EOL:
378 x = 0;
379 y++;
380 break;
381 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800382 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700383 case RLE_DELTA: {
384 // Two bytes are needed to specify delta
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500385 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700386 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700387 return y;
msarettd0375bc2015-08-12 08:08:56 -0700388 }
msarett4ab9d5f2015-08-06 15:34:42 -0700389 }
390 // Modify x and y
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500391 const uint8_t dx = fStreamBuffer[fCurrRLEByte++];
392 const uint8_t dy = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700393 x += dx;
394 y += dy;
msarett4946b942016-02-11 08:41:01 -0800395 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700396 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700397 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800398 } else if (y > height) {
399 fLinesToSkip = y - height;
400 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700401 }
402 break;
403 }
404 default: {
405 // If task does not match any of the above signals, it
406 // indicates that we have a sequence of non-RLE pixels.
407 // Furthermore, the value of task is equal to the number
408 // of pixels to interpret.
409 uint8_t numPixels = task;
410 const size_t rowBytes = compute_row_bytes(numPixels,
411 this->bitsPerPixel());
412 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700413 // image.
414 if (x + numPixels > width) {
415 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700416 return y;
msarettd0375bc2015-08-12 08:08:56 -0700417 }
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500418
msarettd0375bc2015-08-12 08:08:56 -0700419 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700420 // remaining in the stream to set numPixels.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500421
422 // At most, alignedRowBytes can be 255 (max uint8_t) *
423 // 3 (max bytes per pixel) + 1 (aligned) = 766. If
424 // fStreamBuffer was smaller than this,
425 // checkForMoreData would never succeed for some bmps.
426 static_assert(255 * 3 + 1 < kBufferSize,
427 "kBufferSize needs to be larger!");
428 const size_t alignedRowBytes = SkAlign2(rowBytes);
429 if ((int) fBytesBuffered - fCurrRLEByte < alignedRowBytes) {
430 SkASSERT(alignedRowBytes < kBufferSize);
431 if (this->checkForMoreData() < alignedRowBytes) {
msarette6dd0042015-10-09 11:07:34 -0700432 return y;
msarettd0375bc2015-08-12 08:08:56 -0700433 }
msarett4ab9d5f2015-08-06 15:34:42 -0700434 }
435 // Set numPixels number of pixels
436 while (numPixels > 0) {
437 switch(this->bitsPerPixel()) {
438 case 4: {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500439 SkASSERT(fCurrRLEByte < fBytesBuffered);
440 uint8_t val = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700441 setPixel(dst, dstRowBytes, dstInfo, x++,
442 y, val >> 4);
443 numPixels--;
444 if (numPixels != 0) {
445 setPixel(dst, dstRowBytes, dstInfo,
446 x++, y, val & 0xF);
447 numPixels--;
448 }
449 break;
450 }
451 case 8:
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500452 SkASSERT(fCurrRLEByte < fBytesBuffered);
msarett4ab9d5f2015-08-06 15:34:42 -0700453 setPixel(dst, dstRowBytes, dstInfo, x++,
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500454 y, fStreamBuffer[fCurrRLEByte++]);
msarett4ab9d5f2015-08-06 15:34:42 -0700455 numPixels--;
456 break;
457 case 24: {
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500458 SkASSERT(fCurrRLEByte + 2 < fBytesBuffered);
459 uint8_t blue = fStreamBuffer[fCurrRLEByte++];
460 uint8_t green = fStreamBuffer[fCurrRLEByte++];
461 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700462 setRGBPixel(dst, dstRowBytes, dstInfo,
463 x++, y, red, green, blue);
464 numPixels--;
msarettd567a812016-10-05 07:19:27 -0700465 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700466 }
467 default:
468 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700469 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700470 }
471 }
472 // Skip a byte if necessary to maintain alignment
473 if (!SkIsAlign2(rowBytes)) {
474 fCurrRLEByte++;
475 }
476 break;
477 }
478 }
479 } else {
480 // If the first byte read is not a flag, it indicates the number of
481 // pixels to set in RLE mode.
482 const uint8_t numPixels = flag;
483 const int endX = SkTMin<int>(x + numPixels, width);
484
485 if (24 == this->bitsPerPixel()) {
486 // In RLE24, the second byte read is part of the pixel color.
487 // There are two more required bytes to finish encoding the
488 // color.
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500489 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700490 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700491 return y;
msarettd0375bc2015-08-12 08:08:56 -0700492 }
msarett4ab9d5f2015-08-06 15:34:42 -0700493 }
494
495 // Fill the pixels up to endX with the specified color
496 uint8_t blue = task;
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -0500497 uint8_t green = fStreamBuffer[fCurrRLEByte++];
498 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700499 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700500 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700501 }
502 } else {
503 // In RLE8 or RLE4, the second byte read gives the index in the
504 // color table to look up the pixel color.
505 // RLE8 has one color index that gets repeated
506 // RLE4 has two color indexes in the upper and lower 4 bits of
507 // the bytes, which are alternated
508 uint8_t indices[2] = { task, task };
509 if (4 == this->bitsPerPixel()) {
510 indices[0] >>= 4;
511 indices[1] &= 0xf;
512 }
513
514 // Set the indicated number of pixels
515 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700516 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700517 which = !which;
518 }
519 }
520 }
521 }
522}
scroggoe7fc14b2015-10-02 13:14:46 -0700523
msarett9b9497e2016-02-11 13:29:36 -0800524bool SkBmpRLECodec::skipRows(int count) {
525 const SkImageInfo rowInfo = SkImageInfo::Make(this->getInfo().width(), count, kN32_SkColorType,
526 kUnpremul_SkAlphaType);
527
528 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
529}
530
msarette6dd0042015-10-09 11:07:34 -0700531// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
532// Or it should do all sampling natively.
533// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700534class SkBmpRLESampler : public SkSampler {
535public:
536 SkBmpRLESampler(SkBmpRLECodec* codec)
537 : fCodec(codec)
538 {
539 SkASSERT(fCodec);
540 }
541
542private:
msarette6dd0042015-10-09 11:07:34 -0700543 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700544 return fCodec->setSampleX(sampleX);
545 }
546
547 // Unowned pointer. fCodec will delete this class in its destructor.
548 SkBmpRLECodec* fCodec;
549};
550
msarett17315c22016-02-11 10:35:48 -0800551SkSampler* SkBmpRLECodec::getSampler(bool /*createIfNecessary*/) {
552 // We will always create an SkBmpRLESampler if one is requested.
553 // This allows clients to always use the SkBmpRLESampler's
554 // version of fill(), which does nothing since RLE decodes have
555 // already filled pixel memory. This seems fine, since creating
556 // an SkBmpRLESampler is pretty inexpensive.
557 if (!fSampler) {
scroggoe7fc14b2015-10-02 13:14:46 -0700558 fSampler.reset(new SkBmpRLESampler(this));
559 }
560
Ben Wagner145dbcd2016-11-03 14:40:50 -0400561 return fSampler.get();
scroggoe7fc14b2015-10-02 13:14:46 -0700562}
563
msarette6dd0042015-10-09 11:07:34 -0700564int SkBmpRLECodec::setSampleX(int sampleX){
scroggoe7fc14b2015-10-02 13:14:46 -0700565 fSampleX = sampleX;
566 return get_scaled_dimension(this->getInfo().width(), sampleX);
567}