blob: 2b59485b298ea9e93fce705f41f27f4c9d5b141e [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"
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 */
msarett5406d6f2015-08-31 06:55:13 -070017SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream,
18 uint16_t bitsPerPixel, uint32_t numColors,
19 uint32_t bytesPerColor, uint32_t offset,
Leon Scroggins IIIc45df642017-01-18 12:39:07 -050020 SkCodec::SkScanlineOrder rowOrder)
msarett4ab9d5f2015-08-06 15:34:42 -070021 : INHERITED(info, stream, bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070022 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080023 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070024 , fBytesPerColor(bytesPerColor)
25 , fOffset(offset)
Leon Scroggins IIIc45df642017-01-18 12:39:07 -050026 , fBytesBuffered(0)
msarett5406d6f2015-08-31 06:55:13 -070027 , fCurrRLEByte(0)
28 , fSampleX(1)
29{}
msarett4ab9d5f2015-08-06 15:34:42 -070030
31/*
32 * Initiates the bitmap decode
33 */
34SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -070035 void* dst, size_t dstRowBytes,
36 const Options& opts,
37 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -070038 int* inputColorCount,
39 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070040 if (opts.fSubset) {
41 // Subsets are not supported.
42 return kUnimplemented;
43 }
msarett4ab9d5f2015-08-06 15:34:42 -070044 if (!conversion_possible(dstInfo, this->getInfo())) {
45 SkCodecPrintf("Error: cannot convert input type to output type.\n");
46 return kInvalidConversion;
47 }
48
msarett5406d6f2015-08-31 06:55:13 -070049 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
50 if (kSuccess != result) {
51 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070052 }
53
54 // Perform the decode
msarettf724b992015-10-15 06:41:06 -070055 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070056 if (rows != dstInfo.height()) {
57 // We set rowsDecoded equal to the height because the background has already
58 // been filled. RLE encodings sometimes skip pixels, so we always start by
59 // filling the background.
60 *rowsDecoded = dstInfo.height();
61 return kIncompleteInput;
62 }
63
64 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070065}
66
67/*
68 * Process the color table for the bmp input
69 */
70 bool SkBmpRLECodec::createColorTable(int* numColors) {
71 // Allocate memory for color table
72 uint32_t colorBytes = 0;
73 SkPMColor colorTable[256];
74 if (this->bitsPerPixel() <= 8) {
75 // Inform the caller of the number of colors
76 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070077 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070078 // We set the number of colors to maxColors in order to ensure
79 // safe memory accesses. Otherwise, an invalid pixel could
80 // access memory outside of our color table array.
81 *numColors = maxColors;
82 }
benjaminwagner886e5e42015-12-04 08:48:26 -080083 // Don't bother reading more than maxColors.
84 const uint32_t numColorsToRead =
85 fNumColors == 0 ? maxColors : SkTMin(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;
halcanary385fe4d2015-08-26 13:07:48 -070089 SkAutoTDeleteArray<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
95 // Fill in the color table
96 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -080097 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -070098 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
99 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
100 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
101 colorTable[i] = SkPackARGB32NoCheck(0xFF, red, green, blue);
102 }
103
104 // To avoid segmentation faults on bad pixel data, fill the end of the
105 // color table with black. This is the same the behavior as the
106 // chromium decoder.
107 for (; i < maxColors; i++) {
108 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
109 }
110
111 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700112 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700113 }
114
115 // Check that we have not read past the pixel array offset
116 if(fOffset < colorBytes) {
117 // This may occur on OS 2.1 and other old versions where the color
118 // table defaults to max size, and the bmp tries to use a smaller
119 // color table. This is invalid, and our decision is to indicate
120 // an error, rather than try to guess the intended size of the
121 // color table.
122 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
123 return false;
124 }
125
126 // After reading the color table, skip to the start of the pixel array
127 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
128 SkCodecPrintf("Error: unable to skip to image data.\n");
129 return false;
130 }
131
132 // Return true on success
133 return true;
134}
135
136bool SkBmpRLECodec::initializeStreamBuffer() {
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500137 fBytesBuffered = this->stream()->read(fStreamBuffer, kBufferSize);
138 if (fBytesBuffered == 0) {
msarett4ab9d5f2015-08-06 15:34:42 -0700139 SkCodecPrintf("Error: could not read RLE image data.\n");
140 return false;
141 }
msarett5406d6f2015-08-31 06:55:13 -0700142 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700143 return true;
144}
145
146/*
msarettd0375bc2015-08-12 08:08:56 -0700147 * @return the number of bytes remaining in the stream buffer after
148 * attempting to read more bytes from the stream
149 */
150size_t SkBmpRLECodec::checkForMoreData() {
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500151 const size_t remainingBytes = fBytesBuffered - fCurrRLEByte;
152 uint8_t* buffer = fStreamBuffer;
msarettd0375bc2015-08-12 08:08:56 -0700153
154 // We will be reusing the same buffer, starting over from the beginning.
155 // Move any remaining bytes to the start of the buffer.
156 // We use memmove() instead of memcpy() because there is risk that the dst
157 // and src memory will overlap in corrupt images.
158 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
159
160 // Adjust the buffer ptr to the start of the unfilled data.
161 buffer += remainingBytes;
162
163 // Try to read additional bytes from the stream. There are fCurrRLEByte
164 // bytes of additional space remaining in the buffer, assuming that we
165 // have already copied remainingBytes to the start of the buffer.
166 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
167
168 // Update counters and return the number of bytes we currently have
169 // available. We are at the start of the buffer again.
170 fCurrRLEByte = 0;
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500171 fBytesBuffered = remainingBytes + additionalBytes;
172 return fBytesBuffered;
msarettd0375bc2015-08-12 08:08:56 -0700173}
174
175/*
msarett4ab9d5f2015-08-06 15:34:42 -0700176 * Set an RLE pixel using the color table
177 */
178void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
179 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
180 uint8_t index) {
msarett9b9497e2016-02-11 13:29:36 -0800181 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700182 // Set the row
183 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700184
msarett5406d6f2015-08-31 06:55:13 -0700185 // Set the pixel based on destination color type
186 const int dstX = get_dst_coord(x, fSampleX);
187 switch (dstInfo.colorType()) {
188 case kN32_SkColorType: {
189 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
190 dstRow[dstX] = fColorTable->operator[](index);
191 break;
192 }
193 case kRGB_565_SkColorType: {
194 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
195 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
196 break;
197 }
198 default:
199 // This case should not be reached. We should catch an invalid
200 // color type when we check that the conversion is possible.
201 SkASSERT(false);
202 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700203 }
msarett4ab9d5f2015-08-06 15:34:42 -0700204 }
205}
206
207/*
208 * Set an RLE pixel from R, G, B values
209 */
210void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
211 const SkImageInfo& dstInfo, uint32_t x,
212 uint32_t y, uint8_t red, uint8_t green,
213 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800214 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700215 // Set the row
216 uint32_t row = this->getDstRow(y, dstInfo.height());
217
218 // Set the pixel based on destination color type
219 const int dstX = get_dst_coord(x, fSampleX);
220 switch (dstInfo.colorType()) {
221 case kN32_SkColorType: {
222 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
223 dstRow[dstX] = SkPackARGB32NoCheck(0xFF, red, green, blue);
224 break;
225 }
226 case kRGB_565_SkColorType: {
227 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
228 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
229 break;
230 }
231 default:
232 // This case should not be reached. We should catch an invalid
233 // color type when we check that the conversion is possible.
234 SkASSERT(false);
235 break;
236 }
237 }
238}
239
240SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo,
241 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
msarettfdb47572015-10-13 12:50:14 -0700242 // FIXME: Support subsets for scanline decodes.
243 if (options.fSubset) {
244 // Subsets are not supported.
245 return kUnimplemented;
246 }
247
scroggoe7fc14b2015-10-02 13:14:46 -0700248 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
249 // the sampler.
250 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800251 fLinesToSkip = 0;
252
msarett5406d6f2015-08-31 06:55:13 -0700253 // Create the color table if necessary and prepare the stream for decode
254 // Note that if it is non-NULL, inputColorCount will be modified
255 if (!this->createColorTable(inputColorCount)) {
256 SkCodecPrintf("Error: could not create color table.\n");
257 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700258 }
259
msarett5406d6f2015-08-31 06:55:13 -0700260 // Copy the color table to the client if necessary
261 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
262
263 // 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) {
msarett4ab9d5f2015-08-06 15:34:42 -0700278 // Set RLE flags
279 static const uint8_t RLE_ESCAPE = 0;
280 static const uint8_t RLE_EOL = 0;
281 static const uint8_t RLE_EOF = 1;
282 static const uint8_t RLE_DELTA = 2;
283
msarett5406d6f2015-08-31 06:55:13 -0700284 const int width = this->getInfo().width();
msarett4946b942016-02-11 08:41:01 -0800285 int height = info.height();
scroggoe7fc14b2015-10-02 13:14:46 -0700286
287 // Account for sampling.
288 SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height);
msarett4ab9d5f2015-08-06 15:34:42 -0700289
msarett4ab9d5f2015-08-06 15:34:42 -0700290 // Set the background as transparent. Then, if the RLE code skips pixels,
291 // the skipped pixels will be transparent.
292 // Because of the need for transparent pixels, kN32 is the only color
293 // type that makes sense for the destination format.
294 SkASSERT(kN32_SkColorType == dstInfo.colorType());
msarett9b9497e2016-02-11 13:29:36 -0800295 if (dst) {
296 SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized);
297 }
msarett4ab9d5f2015-08-06 15:34:42 -0700298
msarett4946b942016-02-11 08:41:01 -0800299 // Adjust the height and the dst if the previous call to decodeRows() left us
300 // with lines that need to be skipped.
301 if (height > fLinesToSkip) {
302 height -= fLinesToSkip;
303 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
304 fLinesToSkip = 0;
305 } else {
306 fLinesToSkip -= height;
307 return height;
308 }
309
310 // Destination parameters
311 int x = 0;
312 int y = 0;
313
msarett4ab9d5f2015-08-06 15:34:42 -0700314 while (true) {
315 // If we have reached a row that is beyond the requested height, we have
316 // succeeded.
317 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700318 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700319 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700320 // would require us to stop before decoding the full height.
321 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700322 }
323
324 // Every entry takes at least two bytes
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500325 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700326 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700327 return y;
msarettd0375bc2015-08-12 08:08:56 -0700328 }
msarett4ab9d5f2015-08-06 15:34:42 -0700329 }
330
331 // Read the next two bytes. These bytes have different meanings
332 // depending on their values. In the first interpretation, the first
333 // byte is an escape flag and the second byte indicates what special
334 // task to perform.
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500335 const uint8_t flag = fStreamBuffer[fCurrRLEByte++];
336 const uint8_t task = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700337
338 // Perform decoding
339 if (RLE_ESCAPE == flag) {
340 switch (task) {
341 case RLE_EOL:
342 x = 0;
343 y++;
344 break;
345 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800346 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700347 case RLE_DELTA: {
348 // Two bytes are needed to specify delta
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500349 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700350 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700351 return y;
msarettd0375bc2015-08-12 08:08:56 -0700352 }
msarett4ab9d5f2015-08-06 15:34:42 -0700353 }
354 // Modify x and y
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500355 const uint8_t dx = fStreamBuffer[fCurrRLEByte++];
356 const uint8_t dy = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700357 x += dx;
358 y += dy;
msarett4946b942016-02-11 08:41:01 -0800359 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700360 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700361 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800362 } else if (y > height) {
363 fLinesToSkip = y - height;
364 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700365 }
366 break;
367 }
368 default: {
369 // If task does not match any of the above signals, it
370 // indicates that we have a sequence of non-RLE pixels.
371 // Furthermore, the value of task is equal to the number
372 // of pixels to interpret.
373 uint8_t numPixels = task;
374 const size_t rowBytes = compute_row_bytes(numPixels,
375 this->bitsPerPixel());
376 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700377 // image.
378 if (x + numPixels > width) {
379 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700380 return y;
msarettd0375bc2015-08-12 08:08:56 -0700381 }
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500382
msarettd0375bc2015-08-12 08:08:56 -0700383 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700384 // remaining in the stream to set numPixels.
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500385
386 // At most, alignedRowBytes can be 255 (max uint8_t) *
387 // 3 (max bytes per pixel) + 1 (aligned) = 766. If
388 // fStreamBuffer was smaller than this,
389 // checkForMoreData would never succeed for some bmps.
390 static_assert(255 * 3 + 1 < kBufferSize,
391 "kBufferSize needs to be larger!");
392 const size_t alignedRowBytes = SkAlign2(rowBytes);
393 if ((int) fBytesBuffered - fCurrRLEByte < alignedRowBytes) {
394 SkASSERT(alignedRowBytes < kBufferSize);
395 if (this->checkForMoreData() < alignedRowBytes) {
msarette6dd0042015-10-09 11:07:34 -0700396 return y;
msarettd0375bc2015-08-12 08:08:56 -0700397 }
msarett4ab9d5f2015-08-06 15:34:42 -0700398 }
399 // Set numPixels number of pixels
400 while (numPixels > 0) {
401 switch(this->bitsPerPixel()) {
402 case 4: {
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500403 SkASSERT(fCurrRLEByte < fBytesBuffered);
404 uint8_t val = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700405 setPixel(dst, dstRowBytes, dstInfo, x++,
406 y, val >> 4);
407 numPixels--;
408 if (numPixels != 0) {
409 setPixel(dst, dstRowBytes, dstInfo,
410 x++, y, val & 0xF);
411 numPixels--;
412 }
413 break;
414 }
415 case 8:
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500416 SkASSERT(fCurrRLEByte < fBytesBuffered);
msarett4ab9d5f2015-08-06 15:34:42 -0700417 setPixel(dst, dstRowBytes, dstInfo, x++,
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500418 y, fStreamBuffer[fCurrRLEByte++]);
msarett4ab9d5f2015-08-06 15:34:42 -0700419 numPixels--;
420 break;
421 case 24: {
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500422 SkASSERT(fCurrRLEByte + 2 < fBytesBuffered);
423 uint8_t blue = fStreamBuffer[fCurrRLEByte++];
424 uint8_t green = fStreamBuffer[fCurrRLEByte++];
425 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700426 setRGBPixel(dst, dstRowBytes, dstInfo,
427 x++, y, red, green, blue);
428 numPixels--;
429 }
430 default:
431 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700432 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700433 }
434 }
435 // Skip a byte if necessary to maintain alignment
436 if (!SkIsAlign2(rowBytes)) {
437 fCurrRLEByte++;
438 }
439 break;
440 }
441 }
442 } else {
443 // If the first byte read is not a flag, it indicates the number of
444 // pixels to set in RLE mode.
445 const uint8_t numPixels = flag;
446 const int endX = SkTMin<int>(x + numPixels, width);
447
448 if (24 == this->bitsPerPixel()) {
449 // In RLE24, the second byte read is part of the pixel color.
450 // There are two more required bytes to finish encoding the
451 // color.
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500452 if ((int) fBytesBuffered - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700453 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700454 return y;
msarettd0375bc2015-08-12 08:08:56 -0700455 }
msarett4ab9d5f2015-08-06 15:34:42 -0700456 }
457
458 // Fill the pixels up to endX with the specified color
459 uint8_t blue = task;
Leon Scroggins IIIc45df642017-01-18 12:39:07 -0500460 uint8_t green = fStreamBuffer[fCurrRLEByte++];
461 uint8_t red = fStreamBuffer[fCurrRLEByte++];
msarett4ab9d5f2015-08-06 15:34:42 -0700462 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700463 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700464 }
465 } else {
466 // In RLE8 or RLE4, the second byte read gives the index in the
467 // color table to look up the pixel color.
468 // RLE8 has one color index that gets repeated
469 // RLE4 has two color indexes in the upper and lower 4 bits of
470 // the bytes, which are alternated
471 uint8_t indices[2] = { task, task };
472 if (4 == this->bitsPerPixel()) {
473 indices[0] >>= 4;
474 indices[1] &= 0xf;
475 }
476
477 // Set the indicated number of pixels
478 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700479 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700480 which = !which;
481 }
482 }
483 }
484 }
485}
scroggoe7fc14b2015-10-02 13:14:46 -0700486
msarett9b9497e2016-02-11 13:29:36 -0800487bool SkBmpRLECodec::skipRows(int count) {
488 const SkImageInfo rowInfo = SkImageInfo::Make(this->getInfo().width(), count, kN32_SkColorType,
489 kUnpremul_SkAlphaType);
490
491 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
492}
493
msarette6dd0042015-10-09 11:07:34 -0700494// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
495// Or it should do all sampling natively.
496// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700497class SkBmpRLESampler : public SkSampler {
498public:
499 SkBmpRLESampler(SkBmpRLECodec* codec)
500 : fCodec(codec)
501 {
502 SkASSERT(fCodec);
503 }
504
505private:
msarette6dd0042015-10-09 11:07:34 -0700506 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700507 return fCodec->setSampleX(sampleX);
508 }
509
510 // Unowned pointer. fCodec will delete this class in its destructor.
511 SkBmpRLECodec* fCodec;
512};
513
msarett17315c22016-02-11 10:35:48 -0800514SkSampler* SkBmpRLECodec::getSampler(bool /*createIfNecessary*/) {
515 // We will always create an SkBmpRLESampler if one is requested.
516 // This allows clients to always use the SkBmpRLESampler's
517 // version of fill(), which does nothing since RLE decodes have
518 // already filled pixel memory. This seems fine, since creating
519 // an SkBmpRLESampler is pretty inexpensive.
520 if (!fSampler) {
scroggoe7fc14b2015-10-02 13:14:46 -0700521 fSampler.reset(new SkBmpRLESampler(this));
522 }
523
524 return fSampler;
525}
526
msarette6dd0042015-10-09 11:07:34 -0700527int SkBmpRLECodec::setSampleX(int sampleX){
scroggoe7fc14b2015-10-02 13:14:46 -0700528 fSampleX = sampleX;
529 return get_scaled_dimension(this->getInfo().width(), sampleX);
530}