blob: 793bbfd2609e3bf139ec79795f7087b8d79f330d [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,
scroggo46c57472015-09-30 08:57:13 -070020 SkCodec::SkScanlineOrder rowOrder,
halcanary385fe4d2015-08-26 13:07:48 -070021 size_t RLEBytes)
msarett4ab9d5f2015-08-06 15:34:42 -070022 : INHERITED(info, stream, bitsPerPixel, rowOrder)
halcanary96fcdcc2015-08-27 07:41:13 -070023 , fColorTable(nullptr)
benjaminwagner886e5e42015-12-04 08:48:26 -080024 , fNumColors(numColors)
msarett4ab9d5f2015-08-06 15:34:42 -070025 , fBytesPerColor(bytesPerColor)
26 , fOffset(offset)
halcanary385fe4d2015-08-26 13:07:48 -070027 , fStreamBuffer(new uint8_t[RLEBytes])
msarett4ab9d5f2015-08-06 15:34:42 -070028 , fRLEBytes(RLEBytes)
msarett4946b942016-02-11 08:41:01 -080029 , fOrigRLEBytes(RLEBytes)
msarett5406d6f2015-08-31 06:55:13 -070030 , fCurrRLEByte(0)
31 , fSampleX(1)
32{}
msarett4ab9d5f2015-08-06 15:34:42 -070033
34/*
35 * Initiates the bitmap decode
36 */
37SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -070038 void* dst, size_t dstRowBytes,
39 const Options& opts,
40 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -070041 int* inputColorCount,
42 int* rowsDecoded) {
msarett4ab9d5f2015-08-06 15:34:42 -070043 if (opts.fSubset) {
44 // Subsets are not supported.
45 return kUnimplemented;
46 }
msarett4ab9d5f2015-08-06 15:34:42 -070047 if (!conversion_possible(dstInfo, this->getInfo())) {
48 SkCodecPrintf("Error: cannot convert input type to output type.\n");
49 return kInvalidConversion;
50 }
51
msarett5406d6f2015-08-31 06:55:13 -070052 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
53 if (kSuccess != result) {
54 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070055 }
56
57 // Perform the decode
msarettf724b992015-10-15 06:41:06 -070058 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070059 if (rows != dstInfo.height()) {
60 // We set rowsDecoded equal to the height because the background has already
61 // been filled. RLE encodings sometimes skip pixels, so we always start by
62 // filling the background.
63 *rowsDecoded = dstInfo.height();
64 return kIncompleteInput;
65 }
66
67 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070068}
69
70/*
71 * Process the color table for the bmp input
72 */
73 bool SkBmpRLECodec::createColorTable(int* numColors) {
74 // Allocate memory for color table
75 uint32_t colorBytes = 0;
76 SkPMColor colorTable[256];
77 if (this->bitsPerPixel() <= 8) {
78 // Inform the caller of the number of colors
79 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070080 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070081 // We set the number of colors to maxColors in order to ensure
82 // safe memory accesses. Otherwise, an invalid pixel could
83 // access memory outside of our color table array.
84 *numColors = maxColors;
85 }
benjaminwagner886e5e42015-12-04 08:48:26 -080086 // Don't bother reading more than maxColors.
87 const uint32_t numColorsToRead =
88 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070089
90 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080091 colorBytes = numColorsToRead * fBytesPerColor;
halcanary385fe4d2015-08-26 13:07:48 -070092 SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070093 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
94 SkCodecPrintf("Error: unable to read color table.\n");
95 return false;
96 }
97
98 // Fill in the color table
99 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800100 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700101 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
102 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
103 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
104 colorTable[i] = SkPackARGB32NoCheck(0xFF, red, green, blue);
105 }
106
107 // To avoid segmentation faults on bad pixel data, fill the end of the
108 // color table with black. This is the same the behavior as the
109 // chromium decoder.
110 for (; i < maxColors; i++) {
111 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
112 }
113
114 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700115 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700116 }
117
118 // Check that we have not read past the pixel array offset
119 if(fOffset < colorBytes) {
120 // This may occur on OS 2.1 and other old versions where the color
121 // table defaults to max size, and the bmp tries to use a smaller
122 // color table. This is invalid, and our decision is to indicate
123 // an error, rather than try to guess the intended size of the
124 // color table.
125 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
126 return false;
127 }
128
129 // After reading the color table, skip to the start of the pixel array
130 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
131 SkCodecPrintf("Error: unable to skip to image data.\n");
132 return false;
133 }
134
135 // Return true on success
136 return true;
137}
138
139bool SkBmpRLECodec::initializeStreamBuffer() {
140 // Setup a buffer to contain the full input stream
msarett5406d6f2015-08-31 06:55:13 -0700141 // TODO (msarett): I'm not sure it is smart or optimal to trust fRLEBytes (read from header)
142 // as the size of our buffer. First of all, the decode fails if fRLEBytes is
143 // corrupt (negative, zero, or small) when we might be able to decode
144 // successfully with a fixed size buffer. Additionally, we would save memory
145 // using a fixed size buffer if the RLE encoding is large. On the other hand,
146 // we may also waste memory with a fixed size buffer. And determining a
147 // minimum size for our buffer would depend on the image width (so it's not
148 // really "fixed" size), and we may end up allocating a buffer that is
149 // generally larger than the average encoded size anyway.
msarett4ab9d5f2015-08-06 15:34:42 -0700150 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes);
151 if (totalBytes < fRLEBytes) {
152 fRLEBytes = totalBytes;
153 SkCodecPrintf("Warning: incomplete RLE file.\n");
154 }
155 if (fRLEBytes == 0) {
156 SkCodecPrintf("Error: could not read RLE image data.\n");
157 return false;
158 }
msarett5406d6f2015-08-31 06:55:13 -0700159 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700160 return true;
161}
162
163/*
msarettd0375bc2015-08-12 08:08:56 -0700164 * Before signalling kIncompleteInput, we should attempt to load the
165 * stream buffer with additional data.
166 *
167 * @return the number of bytes remaining in the stream buffer after
168 * attempting to read more bytes from the stream
169 */
170size_t SkBmpRLECodec::checkForMoreData() {
171 const size_t remainingBytes = fRLEBytes - fCurrRLEByte;
172 uint8_t* buffer = fStreamBuffer.get();
173
174 // We will be reusing the same buffer, starting over from the beginning.
175 // Move any remaining bytes to the start of the buffer.
176 // We use memmove() instead of memcpy() because there is risk that the dst
177 // and src memory will overlap in corrupt images.
178 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
179
180 // Adjust the buffer ptr to the start of the unfilled data.
181 buffer += remainingBytes;
182
183 // Try to read additional bytes from the stream. There are fCurrRLEByte
184 // bytes of additional space remaining in the buffer, assuming that we
185 // have already copied remainingBytes to the start of the buffer.
186 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
187
188 // Update counters and return the number of bytes we currently have
189 // available. We are at the start of the buffer again.
190 fCurrRLEByte = 0;
191 // If we were unable to fill the buffer, fRLEBytes is no longer equal to
192 // the size of the buffer. There will be unused space at the end. This
193 // should be fine, given that there are no more bytes in the stream.
194 fRLEBytes = remainingBytes + additionalBytes;
195 return fRLEBytes;
196}
197
198/*
msarett4ab9d5f2015-08-06 15:34:42 -0700199 * Set an RLE pixel using the color table
200 */
201void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
202 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
203 uint8_t index) {
msarett9b9497e2016-02-11 13:29:36 -0800204 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700205 // Set the row
206 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700207
msarett5406d6f2015-08-31 06:55:13 -0700208 // Set the pixel based on destination color type
209 const int dstX = get_dst_coord(x, fSampleX);
210 switch (dstInfo.colorType()) {
211 case kN32_SkColorType: {
212 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
213 dstRow[dstX] = fColorTable->operator[](index);
214 break;
215 }
216 case kRGB_565_SkColorType: {
217 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
218 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
219 break;
220 }
221 default:
222 // This case should not be reached. We should catch an invalid
223 // color type when we check that the conversion is possible.
224 SkASSERT(false);
225 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700226 }
msarett4ab9d5f2015-08-06 15:34:42 -0700227 }
228}
229
230/*
231 * Set an RLE pixel from R, G, B values
232 */
233void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
234 const SkImageInfo& dstInfo, uint32_t x,
235 uint32_t y, uint8_t red, uint8_t green,
236 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800237 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700238 // Set the row
239 uint32_t row = this->getDstRow(y, dstInfo.height());
240
241 // Set the pixel based on destination color type
242 const int dstX = get_dst_coord(x, fSampleX);
243 switch (dstInfo.colorType()) {
244 case kN32_SkColorType: {
245 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
246 dstRow[dstX] = SkPackARGB32NoCheck(0xFF, red, green, blue);
247 break;
248 }
249 case kRGB_565_SkColorType: {
250 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
251 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
252 break;
253 }
254 default:
255 // This case should not be reached. We should catch an invalid
256 // color type when we check that the conversion is possible.
257 SkASSERT(false);
258 break;
259 }
260 }
261}
262
263SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo,
264 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
msarettfdb47572015-10-13 12:50:14 -0700265 // FIXME: Support subsets for scanline decodes.
266 if (options.fSubset) {
267 // Subsets are not supported.
268 return kUnimplemented;
269 }
270
scroggoe7fc14b2015-10-02 13:14:46 -0700271 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
272 // the sampler.
273 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800274 fLinesToSkip = 0;
275
msarett5406d6f2015-08-31 06:55:13 -0700276 // Create the color table if necessary and prepare the stream for decode
277 // Note that if it is non-NULL, inputColorCount will be modified
278 if (!this->createColorTable(inputColorCount)) {
279 SkCodecPrintf("Error: could not create color table.\n");
280 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700281 }
282
msarett5406d6f2015-08-31 06:55:13 -0700283 // Copy the color table to the client if necessary
284 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
285
286 // Initialize a buffer for encoded RLE data
msarett4946b942016-02-11 08:41:01 -0800287 fRLEBytes = fOrigRLEBytes;
msarett5406d6f2015-08-31 06:55:13 -0700288 if (!this->initializeStreamBuffer()) {
289 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
msarett2a98bac2016-02-17 14:06:46 -0800290 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700291 }
msarett5406d6f2015-08-31 06:55:13 -0700292
msarett5406d6f2015-08-31 06:55:13 -0700293 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700294}
295
296/*
297 * Performs the bitmap decoding for RLE input format
298 * RLE decoding is performed all at once, rather than a one row at a time
299 */
msarette6dd0042015-10-09 11:07:34 -0700300int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes,
301 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700302 // Set RLE flags
303 static const uint8_t RLE_ESCAPE = 0;
304 static const uint8_t RLE_EOL = 0;
305 static const uint8_t RLE_EOF = 1;
306 static const uint8_t RLE_DELTA = 2;
307
msarett5406d6f2015-08-31 06:55:13 -0700308 const int width = this->getInfo().width();
msarett4946b942016-02-11 08:41:01 -0800309 int height = info.height();
scroggoe7fc14b2015-10-02 13:14:46 -0700310
311 // Account for sampling.
312 SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height);
msarett4ab9d5f2015-08-06 15:34:42 -0700313
msarett4ab9d5f2015-08-06 15:34:42 -0700314 // Set the background as transparent. Then, if the RLE code skips pixels,
315 // the skipped pixels will be transparent.
316 // Because of the need for transparent pixels, kN32 is the only color
317 // type that makes sense for the destination format.
318 SkASSERT(kN32_SkColorType == dstInfo.colorType());
msarett9b9497e2016-02-11 13:29:36 -0800319 if (dst) {
320 SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized);
321 }
msarett4ab9d5f2015-08-06 15:34:42 -0700322
msarett4946b942016-02-11 08:41:01 -0800323 // Adjust the height and the dst if the previous call to decodeRows() left us
324 // with lines that need to be skipped.
325 if (height > fLinesToSkip) {
326 height -= fLinesToSkip;
327 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
328 fLinesToSkip = 0;
329 } else {
330 fLinesToSkip -= height;
331 return height;
332 }
333
334 // Destination parameters
335 int x = 0;
336 int y = 0;
337
msarett4ab9d5f2015-08-06 15:34:42 -0700338 while (true) {
339 // If we have reached a row that is beyond the requested height, we have
340 // succeeded.
341 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700342 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700343 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700344 // would require us to stop before decoding the full height.
345 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700346 }
347
348 // Every entry takes at least two bytes
349 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700350 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
351 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700352 return y;
msarettd0375bc2015-08-12 08:08:56 -0700353 }
msarett4ab9d5f2015-08-06 15:34:42 -0700354 }
355
356 // Read the next two bytes. These bytes have different meanings
357 // depending on their values. In the first interpretation, the first
358 // byte is an escape flag and the second byte indicates what special
359 // task to perform.
360 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++];
361 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++];
362
363 // Perform decoding
364 if (RLE_ESCAPE == flag) {
365 switch (task) {
366 case RLE_EOL:
367 x = 0;
368 y++;
369 break;
370 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800371 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700372 case RLE_DELTA: {
373 // Two bytes are needed to specify delta
374 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700375 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
376 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700377 return y;
msarettd0375bc2015-08-12 08:08:56 -0700378 }
msarett4ab9d5f2015-08-06 15:34:42 -0700379 }
380 // Modify x and y
381 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++];
382 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++];
383 x += dx;
384 y += dy;
msarett4946b942016-02-11 08:41:01 -0800385 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700386 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700387 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800388 } else if (y > height) {
389 fLinesToSkip = y - height;
390 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700391 }
392 break;
393 }
394 default: {
395 // If task does not match any of the above signals, it
396 // indicates that we have a sequence of non-RLE pixels.
397 // Furthermore, the value of task is equal to the number
398 // of pixels to interpret.
399 uint8_t numPixels = task;
400 const size_t rowBytes = compute_row_bytes(numPixels,
401 this->bitsPerPixel());
402 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700403 // image.
404 if (x + numPixels > width) {
405 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700406 return y;
msarettd0375bc2015-08-12 08:08:56 -0700407 }
408 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700409 // remaining in the stream to set numPixels.
msarettd0375bc2015-08-12 08:08:56 -0700410 if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) {
411 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
412 if (this->checkForMoreData() < SkAlign2(rowBytes)) {
msarette6dd0042015-10-09 11:07:34 -0700413 return y;
msarettd0375bc2015-08-12 08:08:56 -0700414 }
msarett4ab9d5f2015-08-06 15:34:42 -0700415 }
416 // Set numPixels number of pixels
417 while (numPixels > 0) {
418 switch(this->bitsPerPixel()) {
419 case 4: {
420 SkASSERT(fCurrRLEByte < fRLEBytes);
421 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++];
422 setPixel(dst, dstRowBytes, dstInfo, x++,
423 y, val >> 4);
424 numPixels--;
425 if (numPixels != 0) {
426 setPixel(dst, dstRowBytes, dstInfo,
427 x++, y, val & 0xF);
428 numPixels--;
429 }
430 break;
431 }
432 case 8:
433 SkASSERT(fCurrRLEByte < fRLEBytes);
434 setPixel(dst, dstRowBytes, dstInfo, x++,
435 y, fStreamBuffer.get()[fCurrRLEByte++]);
436 numPixels--;
437 break;
438 case 24: {
439 SkASSERT(fCurrRLEByte + 2 < fRLEBytes);
440 uint8_t blue = fStreamBuffer.get()[fCurrRLEByte++];
441 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
442 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
443 setRGBPixel(dst, dstRowBytes, dstInfo,
444 x++, y, red, green, blue);
445 numPixels--;
446 }
447 default:
448 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700449 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700450 }
451 }
452 // Skip a byte if necessary to maintain alignment
453 if (!SkIsAlign2(rowBytes)) {
454 fCurrRLEByte++;
455 }
456 break;
457 }
458 }
459 } else {
460 // If the first byte read is not a flag, it indicates the number of
461 // pixels to set in RLE mode.
462 const uint8_t numPixels = flag;
463 const int endX = SkTMin<int>(x + numPixels, width);
464
465 if (24 == this->bitsPerPixel()) {
466 // In RLE24, the second byte read is part of the pixel color.
467 // There are two more required bytes to finish encoding the
468 // color.
469 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700470 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
471 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700472 return y;
msarettd0375bc2015-08-12 08:08:56 -0700473 }
msarett4ab9d5f2015-08-06 15:34:42 -0700474 }
475
476 // Fill the pixels up to endX with the specified color
477 uint8_t blue = task;
478 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
479 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
480 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700481 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700482 }
483 } else {
484 // In RLE8 or RLE4, the second byte read gives the index in the
485 // color table to look up the pixel color.
486 // RLE8 has one color index that gets repeated
487 // RLE4 has two color indexes in the upper and lower 4 bits of
488 // the bytes, which are alternated
489 uint8_t indices[2] = { task, task };
490 if (4 == this->bitsPerPixel()) {
491 indices[0] >>= 4;
492 indices[1] &= 0xf;
493 }
494
495 // Set the indicated number of pixels
496 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700497 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700498 which = !which;
499 }
500 }
501 }
502 }
503}
scroggoe7fc14b2015-10-02 13:14:46 -0700504
msarett9b9497e2016-02-11 13:29:36 -0800505bool SkBmpRLECodec::skipRows(int count) {
506 const SkImageInfo rowInfo = SkImageInfo::Make(this->getInfo().width(), count, kN32_SkColorType,
507 kUnpremul_SkAlphaType);
508
509 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
510}
511
msarette6dd0042015-10-09 11:07:34 -0700512// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
513// Or it should do all sampling natively.
514// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700515class SkBmpRLESampler : public SkSampler {
516public:
517 SkBmpRLESampler(SkBmpRLECodec* codec)
518 : fCodec(codec)
519 {
520 SkASSERT(fCodec);
521 }
522
523private:
msarette6dd0042015-10-09 11:07:34 -0700524 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700525 return fCodec->setSampleX(sampleX);
526 }
527
528 // Unowned pointer. fCodec will delete this class in its destructor.
529 SkBmpRLECodec* fCodec;
530};
531
msarett17315c22016-02-11 10:35:48 -0800532SkSampler* SkBmpRLECodec::getSampler(bool /*createIfNecessary*/) {
533 // We will always create an SkBmpRLESampler if one is requested.
534 // This allows clients to always use the SkBmpRLESampler's
535 // version of fill(), which does nothing since RLE decodes have
536 // already filled pixel memory. This seems fine, since creating
537 // an SkBmpRLESampler is pretty inexpensive.
538 if (!fSampler) {
scroggoe7fc14b2015-10-02 13:14:46 -0700539 fSampler.reset(new SkBmpRLESampler(this));
540 }
541
542 return fSampler;
543}
544
msarette6dd0042015-10-09 11:07:34 -0700545int SkBmpRLECodec::setSampleX(int sampleX){
scroggoe7fc14b2015-10-02 13:14:46 -0700546 fSampleX = sampleX;
547 return get_scaled_dimension(this->getInfo().width(), sampleX);
548}