blob: dc5d689235c95415546104c9374a29f4f169dd91 [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 */
msarettc30c4182016-04-20 11:53:35 -070017SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
msarett5406d6f2015-08-31 06:55:13 -070018 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)
msarettc30c4182016-04-20 11:53:35 -070022 : INHERITED(width, height, 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 }
msarett2ecc35f2016-09-08 11:55:16 -070047 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
msarett4ab9d5f2015-08-06 15:34:42 -070048 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 */
msarett34e0ec42016-04-22 16:27:24 -070073 bool SkBmpRLECodec::createColorTable(SkColorType dstColorType, int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070074 // 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
msarett34e0ec42016-04-22 16:27:24 -070099 PackColorProc packARGB = choose_pack_color_proc(false, dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -0700100 uint32_t i = 0;
benjaminwagner886e5e42015-12-04 08:48:26 -0800101 for (; i < numColorsToRead; i++) {
msarett4ab9d5f2015-08-06 15:34:42 -0700102 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
103 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
104 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
msarett34e0ec42016-04-22 16:27:24 -0700105 colorTable[i] = packARGB(0xFF, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700106 }
107
108 // To avoid segmentation faults on bad pixel data, fill the end of the
109 // color table with black. This is the same the behavior as the
110 // chromium decoder.
111 for (; i < maxColors; i++) {
112 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
113 }
114
115 // Set the color table
halcanary385fe4d2015-08-26 13:07:48 -0700116 fColorTable.reset(new SkColorTable(colorTable, maxColors));
msarett4ab9d5f2015-08-06 15:34:42 -0700117 }
118
119 // Check that we have not read past the pixel array offset
120 if(fOffset < colorBytes) {
121 // This may occur on OS 2.1 and other old versions where the color
122 // table defaults to max size, and the bmp tries to use a smaller
123 // color table. This is invalid, and our decision is to indicate
124 // an error, rather than try to guess the intended size of the
125 // color table.
126 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
127 return false;
128 }
129
130 // After reading the color table, skip to the start of the pixel array
131 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
132 SkCodecPrintf("Error: unable to skip to image data.\n");
133 return false;
134 }
135
136 // Return true on success
137 return true;
138}
139
140bool SkBmpRLECodec::initializeStreamBuffer() {
141 // Setup a buffer to contain the full input stream
msarett5406d6f2015-08-31 06:55:13 -0700142 // TODO (msarett): I'm not sure it is smart or optimal to trust fRLEBytes (read from header)
143 // as the size of our buffer. First of all, the decode fails if fRLEBytes is
144 // corrupt (negative, zero, or small) when we might be able to decode
145 // successfully with a fixed size buffer. Additionally, we would save memory
146 // using a fixed size buffer if the RLE encoding is large. On the other hand,
147 // we may also waste memory with a fixed size buffer. And determining a
148 // minimum size for our buffer would depend on the image width (so it's not
149 // really "fixed" size), and we may end up allocating a buffer that is
150 // generally larger than the average encoded size anyway.
msarett4ab9d5f2015-08-06 15:34:42 -0700151 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes);
152 if (totalBytes < fRLEBytes) {
153 fRLEBytes = totalBytes;
154 SkCodecPrintf("Warning: incomplete RLE file.\n");
155 }
156 if (fRLEBytes == 0) {
157 SkCodecPrintf("Error: could not read RLE image data.\n");
158 return false;
159 }
msarett5406d6f2015-08-31 06:55:13 -0700160 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700161 return true;
162}
163
164/*
msarettd0375bc2015-08-12 08:08:56 -0700165 * Before signalling kIncompleteInput, we should attempt to load the
166 * stream buffer with additional data.
167 *
168 * @return the number of bytes remaining in the stream buffer after
169 * attempting to read more bytes from the stream
170 */
171size_t SkBmpRLECodec::checkForMoreData() {
172 const size_t remainingBytes = fRLEBytes - fCurrRLEByte;
173 uint8_t* buffer = fStreamBuffer.get();
174
175 // We will be reusing the same buffer, starting over from the beginning.
176 // Move any remaining bytes to the start of the buffer.
177 // We use memmove() instead of memcpy() because there is risk that the dst
178 // and src memory will overlap in corrupt images.
179 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
180
181 // Adjust the buffer ptr to the start of the unfilled data.
182 buffer += remainingBytes;
183
184 // Try to read additional bytes from the stream. There are fCurrRLEByte
185 // bytes of additional space remaining in the buffer, assuming that we
186 // have already copied remainingBytes to the start of the buffer.
187 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
188
189 // Update counters and return the number of bytes we currently have
190 // available. We are at the start of the buffer again.
191 fCurrRLEByte = 0;
192 // If we were unable to fill the buffer, fRLEBytes is no longer equal to
193 // the size of the buffer. There will be unused space at the end. This
194 // should be fine, given that there are no more bytes in the stream.
195 fRLEBytes = remainingBytes + additionalBytes;
196 return fRLEBytes;
197}
198
199/*
msarett4ab9d5f2015-08-06 15:34:42 -0700200 * Set an RLE pixel using the color table
201 */
202void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
203 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
204 uint8_t index) {
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());
msarett4ab9d5f2015-08-06 15:34:42 -0700208
msarett5406d6f2015-08-31 06:55:13 -0700209 // 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:
213 case kBGRA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700214 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
215 dstRow[dstX] = fColorTable->operator[](index);
216 break;
217 }
218 case kRGB_565_SkColorType: {
219 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
220 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
221 break;
222 }
223 default:
224 // This case should not be reached. We should catch an invalid
225 // color type when we check that the conversion is possible.
226 SkASSERT(false);
227 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700228 }
msarett4ab9d5f2015-08-06 15:34:42 -0700229 }
230}
231
232/*
233 * Set an RLE pixel from R, G, B values
234 */
235void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
236 const SkImageInfo& dstInfo, uint32_t x,
237 uint32_t y, uint8_t red, uint8_t green,
238 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800239 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700240 // Set the row
241 uint32_t row = this->getDstRow(y, dstInfo.height());
242
243 // Set the pixel based on destination color type
244 const int dstX = get_dst_coord(x, fSampleX);
245 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700246 case kRGBA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700247 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
msarett34e0ec42016-04-22 16:27:24 -0700248 dstRow[dstX] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
249 break;
250 }
251 case kBGRA_8888_SkColorType: {
252 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
253 dstRow[dstX] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700254 break;
255 }
256 case kRGB_565_SkColorType: {
257 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
258 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
259 break;
260 }
261 default:
262 // This case should not be reached. We should catch an invalid
263 // color type when we check that the conversion is possible.
264 SkASSERT(false);
265 break;
266 }
267 }
268}
269
270SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo,
271 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
msarettfdb47572015-10-13 12:50:14 -0700272 // FIXME: Support subsets for scanline decodes.
273 if (options.fSubset) {
274 // Subsets are not supported.
275 return kUnimplemented;
276 }
277
scroggoe7fc14b2015-10-02 13:14:46 -0700278 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
279 // the sampler.
280 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800281 fLinesToSkip = 0;
282
msarett5406d6f2015-08-31 06:55:13 -0700283 // Create the color table if necessary and prepare the stream for decode
284 // Note that if it is non-NULL, inputColorCount will be modified
msarett34e0ec42016-04-22 16:27:24 -0700285 if (!this->createColorTable(dstInfo.colorType(), inputColorCount)) {
msarett5406d6f2015-08-31 06:55:13 -0700286 SkCodecPrintf("Error: could not create color table.\n");
287 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700288 }
289
msarett5406d6f2015-08-31 06:55:13 -0700290 // Copy the color table to the client if necessary
291 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
292
293 // Initialize a buffer for encoded RLE data
msarett4946b942016-02-11 08:41:01 -0800294 fRLEBytes = fOrigRLEBytes;
msarett5406d6f2015-08-31 06:55:13 -0700295 if (!this->initializeStreamBuffer()) {
296 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
msarett2a98bac2016-02-17 14:06:46 -0800297 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700298 }
msarett5406d6f2015-08-31 06:55:13 -0700299
msarett5406d6f2015-08-31 06:55:13 -0700300 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700301}
302
303/*
304 * Performs the bitmap decoding for RLE input format
305 * RLE decoding is performed all at once, rather than a one row at a time
306 */
msarette6dd0042015-10-09 11:07:34 -0700307int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes,
308 const Options& opts) {
msarett4ab9d5f2015-08-06 15:34:42 -0700309 // Set RLE flags
310 static const uint8_t RLE_ESCAPE = 0;
311 static const uint8_t RLE_EOL = 0;
312 static const uint8_t RLE_EOF = 1;
313 static const uint8_t RLE_DELTA = 2;
314
msarett5406d6f2015-08-31 06:55:13 -0700315 const int width = this->getInfo().width();
msarett4946b942016-02-11 08:41:01 -0800316 int height = info.height();
scroggoe7fc14b2015-10-02 13:14:46 -0700317
318 // Account for sampling.
319 SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height);
msarett4ab9d5f2015-08-06 15:34:42 -0700320
msarett4ab9d5f2015-08-06 15:34:42 -0700321 // Set the background as transparent. Then, if the RLE code skips pixels,
322 // the skipped pixels will be transparent.
323 // Because of the need for transparent pixels, kN32 is the only color
324 // type that makes sense for the destination format.
msarett34e0ec42016-04-22 16:27:24 -0700325 SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() ||
326 kBGRA_8888_SkColorType == dstInfo.colorType());
msarett9b9497e2016-02-11 13:29:36 -0800327 if (dst) {
328 SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized);
329 }
msarett4ab9d5f2015-08-06 15:34:42 -0700330
msarett4946b942016-02-11 08:41:01 -0800331 // Adjust the height and the dst if the previous call to decodeRows() left us
332 // with lines that need to be skipped.
333 if (height > fLinesToSkip) {
334 height -= fLinesToSkip;
335 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
336 fLinesToSkip = 0;
337 } else {
338 fLinesToSkip -= height;
339 return height;
340 }
341
342 // Destination parameters
343 int x = 0;
344 int y = 0;
345
msarett4ab9d5f2015-08-06 15:34:42 -0700346 while (true) {
347 // If we have reached a row that is beyond the requested height, we have
348 // succeeded.
349 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700350 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700351 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700352 // would require us to stop before decoding the full height.
353 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700354 }
355
356 // Every entry takes at least two bytes
357 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700358 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
359 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700360 return y;
msarettd0375bc2015-08-12 08:08:56 -0700361 }
msarett4ab9d5f2015-08-06 15:34:42 -0700362 }
363
364 // Read the next two bytes. These bytes have different meanings
365 // depending on their values. In the first interpretation, the first
366 // byte is an escape flag and the second byte indicates what special
367 // task to perform.
368 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++];
369 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++];
370
371 // Perform decoding
372 if (RLE_ESCAPE == flag) {
373 switch (task) {
374 case RLE_EOL:
375 x = 0;
376 y++;
377 break;
378 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800379 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700380 case RLE_DELTA: {
381 // Two bytes are needed to specify delta
382 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700383 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
384 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700385 return y;
msarettd0375bc2015-08-12 08:08:56 -0700386 }
msarett4ab9d5f2015-08-06 15:34:42 -0700387 }
388 // Modify x and y
389 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++];
390 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++];
391 x += dx;
392 y += dy;
msarett4946b942016-02-11 08:41:01 -0800393 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700394 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700395 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800396 } else if (y > height) {
397 fLinesToSkip = y - height;
398 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700399 }
400 break;
401 }
402 default: {
403 // If task does not match any of the above signals, it
404 // indicates that we have a sequence of non-RLE pixels.
405 // Furthermore, the value of task is equal to the number
406 // of pixels to interpret.
407 uint8_t numPixels = task;
408 const size_t rowBytes = compute_row_bytes(numPixels,
409 this->bitsPerPixel());
410 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700411 // image.
412 if (x + numPixels > width) {
413 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700414 return y;
msarettd0375bc2015-08-12 08:08:56 -0700415 }
416 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700417 // remaining in the stream to set numPixels.
msarettd0375bc2015-08-12 08:08:56 -0700418 if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) {
419 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
420 if (this->checkForMoreData() < SkAlign2(rowBytes)) {
msarette6dd0042015-10-09 11:07:34 -0700421 return y;
msarettd0375bc2015-08-12 08:08:56 -0700422 }
msarett4ab9d5f2015-08-06 15:34:42 -0700423 }
424 // Set numPixels number of pixels
425 while (numPixels > 0) {
426 switch(this->bitsPerPixel()) {
427 case 4: {
428 SkASSERT(fCurrRLEByte < fRLEBytes);
429 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++];
430 setPixel(dst, dstRowBytes, dstInfo, x++,
431 y, val >> 4);
432 numPixels--;
433 if (numPixels != 0) {
434 setPixel(dst, dstRowBytes, dstInfo,
435 x++, y, val & 0xF);
436 numPixels--;
437 }
438 break;
439 }
440 case 8:
441 SkASSERT(fCurrRLEByte < fRLEBytes);
442 setPixel(dst, dstRowBytes, dstInfo, x++,
443 y, fStreamBuffer.get()[fCurrRLEByte++]);
444 numPixels--;
445 break;
446 case 24: {
447 SkASSERT(fCurrRLEByte + 2 < fRLEBytes);
448 uint8_t blue = fStreamBuffer.get()[fCurrRLEByte++];
449 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
450 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
451 setRGBPixel(dst, dstRowBytes, dstInfo,
452 x++, y, red, green, blue);
453 numPixels--;
msarettd567a812016-10-05 07:19:27 -0700454 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700455 }
456 default:
457 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700458 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700459 }
460 }
461 // Skip a byte if necessary to maintain alignment
462 if (!SkIsAlign2(rowBytes)) {
463 fCurrRLEByte++;
464 }
465 break;
466 }
467 }
468 } else {
469 // If the first byte read is not a flag, it indicates the number of
470 // pixels to set in RLE mode.
471 const uint8_t numPixels = flag;
472 const int endX = SkTMin<int>(x + numPixels, width);
473
474 if (24 == this->bitsPerPixel()) {
475 // In RLE24, the second byte read is part of the pixel color.
476 // There are two more required bytes to finish encoding the
477 // color.
478 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700479 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
480 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700481 return y;
msarettd0375bc2015-08-12 08:08:56 -0700482 }
msarett4ab9d5f2015-08-06 15:34:42 -0700483 }
484
485 // Fill the pixels up to endX with the specified color
486 uint8_t blue = task;
487 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
488 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
489 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700490 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700491 }
492 } else {
493 // In RLE8 or RLE4, the second byte read gives the index in the
494 // color table to look up the pixel color.
495 // RLE8 has one color index that gets repeated
496 // RLE4 has two color indexes in the upper and lower 4 bits of
497 // the bytes, which are alternated
498 uint8_t indices[2] = { task, task };
499 if (4 == this->bitsPerPixel()) {
500 indices[0] >>= 4;
501 indices[1] &= 0xf;
502 }
503
504 // Set the indicated number of pixels
505 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700506 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700507 which = !which;
508 }
509 }
510 }
511 }
512}
scroggoe7fc14b2015-10-02 13:14:46 -0700513
msarett9b9497e2016-02-11 13:29:36 -0800514bool SkBmpRLECodec::skipRows(int count) {
515 const SkImageInfo rowInfo = SkImageInfo::Make(this->getInfo().width(), count, kN32_SkColorType,
516 kUnpremul_SkAlphaType);
517
518 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
519}
520
msarette6dd0042015-10-09 11:07:34 -0700521// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
522// Or it should do all sampling natively.
523// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700524class SkBmpRLESampler : public SkSampler {
525public:
526 SkBmpRLESampler(SkBmpRLECodec* codec)
527 : fCodec(codec)
528 {
529 SkASSERT(fCodec);
530 }
531
532private:
msarette6dd0042015-10-09 11:07:34 -0700533 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700534 return fCodec->setSampleX(sampleX);
535 }
536
537 // Unowned pointer. fCodec will delete this class in its destructor.
538 SkBmpRLECodec* fCodec;
539};
540
msarett17315c22016-02-11 10:35:48 -0800541SkSampler* SkBmpRLECodec::getSampler(bool /*createIfNecessary*/) {
542 // We will always create an SkBmpRLESampler if one is requested.
543 // This allows clients to always use the SkBmpRLESampler's
544 // version of fill(), which does nothing since RLE decodes have
545 // already filled pixel memory. This seems fine, since creating
546 // an SkBmpRLESampler is pretty inexpensive.
547 if (!fSampler) {
scroggoe7fc14b2015-10-02 13:14:46 -0700548 fSampler.reset(new SkBmpRLESampler(this));
549 }
550
551 return fSampler;
552}
553
msarette6dd0042015-10-09 11:07:34 -0700554int SkBmpRLECodec::setSampleX(int sampleX){
scroggoe7fc14b2015-10-02 13:14:46 -0700555 fSampleX = sampleX;
556 return get_scaled_dimension(this->getInfo().width(), sampleX);
557}