blob: 85b965e6112572ed76bd83c666e6b3b9804ff5eb [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 }
msarett4ab9d5f2015-08-06 15:34:42 -070047
msarett5406d6f2015-08-31 06:55:13 -070048 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount);
49 if (kSuccess != result) {
50 return result;
msarett4ab9d5f2015-08-06 15:34:42 -070051 }
52
53 // Perform the decode
msarettf724b992015-10-15 06:41:06 -070054 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
msarette6dd0042015-10-09 11:07:34 -070055 if (rows != dstInfo.height()) {
56 // We set rowsDecoded equal to the height because the background has already
57 // been filled. RLE encodings sometimes skip pixels, so we always start by
58 // filling the background.
59 *rowsDecoded = dstInfo.height();
60 return kIncompleteInput;
61 }
62
63 return kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -070064}
65
66/*
67 * Process the color table for the bmp input
68 */
msarett34e0ec42016-04-22 16:27:24 -070069 bool SkBmpRLECodec::createColorTable(SkColorType dstColorType, int* numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070070 // Allocate memory for color table
71 uint32_t colorBytes = 0;
72 SkPMColor colorTable[256];
73 if (this->bitsPerPixel() <= 8) {
74 // Inform the caller of the number of colors
75 uint32_t maxColors = 1 << this->bitsPerPixel();
halcanary96fcdcc2015-08-27 07:41:13 -070076 if (nullptr != numColors) {
msarett4ab9d5f2015-08-06 15:34:42 -070077 // We set the number of colors to maxColors in order to ensure
78 // safe memory accesses. Otherwise, an invalid pixel could
79 // access memory outside of our color table array.
80 *numColors = maxColors;
81 }
benjaminwagner886e5e42015-12-04 08:48:26 -080082 // Don't bother reading more than maxColors.
83 const uint32_t numColorsToRead =
84 fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors);
msarett4ab9d5f2015-08-06 15:34:42 -070085
86 // Read the color table from the stream
benjaminwagner886e5e42015-12-04 08:48:26 -080087 colorBytes = numColorsToRead * fBytesPerColor;
Ben Wagner7ecc5962016-11-02 17:07:33 -040088 std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]);
msarett4ab9d5f2015-08-06 15:34:42 -070089 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
90 SkCodecPrintf("Error: unable to read color table.\n");
91 return false;
92 }
93
94 // Fill in the color table
msarett34e0ec42016-04-22 16:27:24 -070095 PackColorProc packARGB = choose_pack_color_proc(false, dstColorType);
msarett4ab9d5f2015-08-06 15:34:42 -070096 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);
msarett34e0ec42016-04-22 16:27:24 -0700101 colorTable[i] = packARGB(0xFF, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700102 }
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() {
137 // Setup a buffer to contain the full input stream
msarett5406d6f2015-08-31 06:55:13 -0700138 // TODO (msarett): I'm not sure it is smart or optimal to trust fRLEBytes (read from header)
139 // as the size of our buffer. First of all, the decode fails if fRLEBytes is
140 // corrupt (negative, zero, or small) when we might be able to decode
141 // successfully with a fixed size buffer. Additionally, we would save memory
142 // using a fixed size buffer if the RLE encoding is large. On the other hand,
143 // we may also waste memory with a fixed size buffer. And determining a
144 // minimum size for our buffer would depend on the image width (so it's not
145 // really "fixed" size), and we may end up allocating a buffer that is
146 // generally larger than the average encoded size anyway.
msarett4ab9d5f2015-08-06 15:34:42 -0700147 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes);
148 if (totalBytes < fRLEBytes) {
149 fRLEBytes = totalBytes;
150 SkCodecPrintf("Warning: incomplete RLE file.\n");
151 }
152 if (fRLEBytes == 0) {
153 SkCodecPrintf("Error: could not read RLE image data.\n");
154 return false;
155 }
msarett5406d6f2015-08-31 06:55:13 -0700156 fCurrRLEByte = 0;
msarett4ab9d5f2015-08-06 15:34:42 -0700157 return true;
158}
159
160/*
msarettd0375bc2015-08-12 08:08:56 -0700161 * Before signalling kIncompleteInput, we should attempt to load the
162 * stream buffer with additional data.
163 *
164 * @return the number of bytes remaining in the stream buffer after
165 * attempting to read more bytes from the stream
166 */
167size_t SkBmpRLECodec::checkForMoreData() {
168 const size_t remainingBytes = fRLEBytes - fCurrRLEByte;
169 uint8_t* buffer = fStreamBuffer.get();
170
171 // We will be reusing the same buffer, starting over from the beginning.
172 // Move any remaining bytes to the start of the buffer.
173 // We use memmove() instead of memcpy() because there is risk that the dst
174 // and src memory will overlap in corrupt images.
175 memmove(buffer, SkTAddOffset<uint8_t>(buffer, fCurrRLEByte), remainingBytes);
176
177 // Adjust the buffer ptr to the start of the unfilled data.
178 buffer += remainingBytes;
179
180 // Try to read additional bytes from the stream. There are fCurrRLEByte
181 // bytes of additional space remaining in the buffer, assuming that we
182 // have already copied remainingBytes to the start of the buffer.
183 size_t additionalBytes = this->stream()->read(buffer, fCurrRLEByte);
184
185 // Update counters and return the number of bytes we currently have
186 // available. We are at the start of the buffer again.
187 fCurrRLEByte = 0;
188 // If we were unable to fill the buffer, fRLEBytes is no longer equal to
189 // the size of the buffer. There will be unused space at the end. This
190 // should be fine, given that there are no more bytes in the stream.
191 fRLEBytes = remainingBytes + additionalBytes;
192 return fRLEBytes;
193}
194
195/*
msarett4ab9d5f2015-08-06 15:34:42 -0700196 * Set an RLE pixel using the color table
197 */
198void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
199 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
200 uint8_t index) {
msarett9b9497e2016-02-11 13:29:36 -0800201 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700202 // Set the row
203 uint32_t row = this->getDstRow(y, dstInfo.height());
msarett4ab9d5f2015-08-06 15:34:42 -0700204
msarett5406d6f2015-08-31 06:55:13 -0700205 // Set the pixel based on destination color type
206 const int dstX = get_dst_coord(x, fSampleX);
207 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700208 case kRGBA_8888_SkColorType:
209 case kBGRA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700210 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
211 dstRow[dstX] = fColorTable->operator[](index);
212 break;
213 }
214 case kRGB_565_SkColorType: {
215 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
216 dstRow[dstX] = SkPixel32ToPixel16(fColorTable->operator[](index));
217 break;
218 }
219 default:
220 // This case should not be reached. We should catch an invalid
221 // color type when we check that the conversion is possible.
222 SkASSERT(false);
223 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700224 }
msarett4ab9d5f2015-08-06 15:34:42 -0700225 }
226}
227
228/*
229 * Set an RLE pixel from R, G, B values
230 */
231void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
232 const SkImageInfo& dstInfo, uint32_t x,
233 uint32_t y, uint8_t red, uint8_t green,
234 uint8_t blue) {
msarett9b9497e2016-02-11 13:29:36 -0800235 if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) {
msarett5406d6f2015-08-31 06:55:13 -0700236 // Set the row
237 uint32_t row = this->getDstRow(y, dstInfo.height());
238
239 // Set the pixel based on destination color type
240 const int dstX = get_dst_coord(x, fSampleX);
241 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700242 case kRGBA_8888_SkColorType: {
msarett5406d6f2015-08-31 06:55:13 -0700243 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
msarett34e0ec42016-04-22 16:27:24 -0700244 dstRow[dstX] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
245 break;
246 }
247 case kBGRA_8888_SkColorType: {
248 SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes);
249 dstRow[dstX] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700250 break;
251 }
252 case kRGB_565_SkColorType: {
253 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowBytes);
254 dstRow[dstX] = SkPack888ToRGB16(red, green, blue);
255 break;
256 }
257 default:
258 // This case should not be reached. We should catch an invalid
259 // color type when we check that the conversion is possible.
260 SkASSERT(false);
261 break;
262 }
263 }
264}
265
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400266SkCodec::Result SkBmpRLECodec::onPrepareToDecode(const SkImageInfo& dstInfo,
msarett5406d6f2015-08-31 06:55:13 -0700267 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
msarettfdb47572015-10-13 12:50:14 -0700268 // FIXME: Support subsets for scanline decodes.
269 if (options.fSubset) {
270 // Subsets are not supported.
271 return kUnimplemented;
272 }
273
scroggoe7fc14b2015-10-02 13:14:46 -0700274 // Reset fSampleX. If it needs to be a value other than 1, it will get modified by
275 // the sampler.
276 fSampleX = 1;
msarett4946b942016-02-11 08:41:01 -0800277 fLinesToSkip = 0;
278
msarett5406d6f2015-08-31 06:55:13 -0700279 // Create the color table if necessary and prepare the stream for decode
280 // Note that if it is non-NULL, inputColorCount will be modified
msarett34e0ec42016-04-22 16:27:24 -0700281 if (!this->createColorTable(dstInfo.colorType(), inputColorCount)) {
msarett5406d6f2015-08-31 06:55:13 -0700282 SkCodecPrintf("Error: could not create color table.\n");
283 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700284 }
285
msarett5406d6f2015-08-31 06:55:13 -0700286 // Copy the color table to the client if necessary
287 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
288
289 // Initialize a buffer for encoded RLE data
msarett4946b942016-02-11 08:41:01 -0800290 fRLEBytes = fOrigRLEBytes;
msarett5406d6f2015-08-31 06:55:13 -0700291 if (!this->initializeStreamBuffer()) {
292 SkCodecPrintf("Error: cannot initialize stream buffer.\n");
msarett2a98bac2016-02-17 14:06:46 -0800293 return SkCodec::kInvalidInput;
msarett4ab9d5f2015-08-06 15:34:42 -0700294 }
msarett5406d6f2015-08-31 06:55:13 -0700295
msarett5406d6f2015-08-31 06:55:13 -0700296 return SkCodec::kSuccess;
msarett4ab9d5f2015-08-06 15:34:42 -0700297}
298
299/*
300 * Performs the bitmap decoding for RLE input format
301 * RLE decoding is performed all at once, rather than a one row at a time
302 */
msarette6dd0042015-10-09 11:07:34 -0700303int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes,
304 const Options& opts) {
Matt Sarett61935682016-11-03 17:27:12 +0000305 const int width = this->getInfo().width();
306 int height = info.height();
307
308 // Account for sampling.
309 SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height);
310
311 // Set the background as transparent. Then, if the RLE code skips pixels,
312 // the skipped pixels will be transparent.
Matt Sarett61935682016-11-03 17:27:12 +0000313 if (dst) {
314 SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized);
315 }
316
317 // Adjust the height and the dst if the previous call to decodeRows() left us
318 // with lines that need to be skipped.
319 if (height > fLinesToSkip) {
320 height -= fLinesToSkip;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400321 if (dst) {
322 dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes);
323 }
Matt Sarett61935682016-11-03 17:27:12 +0000324 fLinesToSkip = 0;
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400325
326 dstInfo = dstInfo.makeWH(dstInfo.width(), height);
Matt Sarett61935682016-11-03 17:27:12 +0000327 } else {
328 fLinesToSkip -= height;
329 return height;
330 }
331
Matt Sarett1b96c6f2016-11-03 16:15:20 -0400332 void* decodeDst = dst;
333 size_t decodeRowBytes = dstRowBytes;
334 SkImageInfo decodeInfo = dstInfo;
335 if (decodeDst) {
336 if (this->colorXform()) {
337 decodeInfo = decodeInfo.makeColorType(kBGRA_8888_SkColorType);
338 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
339 int count = height * dstInfo.width();
340 this->resetXformBuffer(count);
341 sk_bzero(this->xformBuffer(), count * sizeof(uint32_t));
342 decodeDst = this->xformBuffer();
343 decodeRowBytes = dstInfo.width() * sizeof(uint32_t);
344 }
345 }
346 }
347
348 int decodedHeight = this->decodeRLE(decodeInfo, decodeDst, decodeRowBytes);
349 if (this->colorXform() && decodeDst) {
350 for (int y = 0; y < decodedHeight; y++) {
351 this->applyColorXform(dstInfo, dst, decodeDst);
352 decodeDst = SkTAddOffset<void>(decodeDst, decodeRowBytes);
353 dst = SkTAddOffset<void>(dst, dstRowBytes);
354 }
355 }
356
357 return decodedHeight;
358}
359
360int SkBmpRLECodec::decodeRLE(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes) {
361 // Use the original width to count the number of pixels in each row.
362 const int width = this->getInfo().width();
363
364 // This tells us the number of rows that we are meant to decode.
365 const int height = dstInfo.height();
366
367 // Set RLE flags
368 static const uint8_t RLE_ESCAPE = 0;
369 static const uint8_t RLE_EOL = 0;
370 static const uint8_t RLE_EOF = 1;
371 static const uint8_t RLE_DELTA = 2;
372
msarett4946b942016-02-11 08:41:01 -0800373 // Destination parameters
374 int x = 0;
375 int y = 0;
376
msarett4ab9d5f2015-08-06 15:34:42 -0700377 while (true) {
378 // If we have reached a row that is beyond the requested height, we have
379 // succeeded.
380 if (y >= height) {
msarette6dd0042015-10-09 11:07:34 -0700381 // It would be better to check for the EOF marker before indicating
msarett4ab9d5f2015-08-06 15:34:42 -0700382 // success, but we may be performing a scanline decode, which
msarette6dd0042015-10-09 11:07:34 -0700383 // would require us to stop before decoding the full height.
384 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700385 }
386
387 // Every entry takes at least two bytes
388 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700389 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
390 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700391 return y;
msarettd0375bc2015-08-12 08:08:56 -0700392 }
msarett4ab9d5f2015-08-06 15:34:42 -0700393 }
394
395 // Read the next two bytes. These bytes have different meanings
396 // depending on their values. In the first interpretation, the first
397 // byte is an escape flag and the second byte indicates what special
398 // task to perform.
399 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++];
400 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++];
401
402 // Perform decoding
403 if (RLE_ESCAPE == flag) {
404 switch (task) {
405 case RLE_EOL:
406 x = 0;
407 y++;
408 break;
409 case RLE_EOF:
msaretta3766292015-11-19 08:59:12 -0800410 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700411 case RLE_DELTA: {
412 // Two bytes are needed to specify delta
413 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700414 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
415 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700416 return y;
msarettd0375bc2015-08-12 08:08:56 -0700417 }
msarett4ab9d5f2015-08-06 15:34:42 -0700418 }
419 // Modify x and y
420 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++];
421 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++];
422 x += dx;
423 y += dy;
msarett4946b942016-02-11 08:41:01 -0800424 if (x > width) {
msarettd0375bc2015-08-12 08:08:56 -0700425 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700426 return y - dy;
msarett4946b942016-02-11 08:41:01 -0800427 } else if (y > height) {
428 fLinesToSkip = y - height;
429 return height;
msarett4ab9d5f2015-08-06 15:34:42 -0700430 }
431 break;
432 }
433 default: {
434 // If task does not match any of the above signals, it
435 // indicates that we have a sequence of non-RLE pixels.
436 // Furthermore, the value of task is equal to the number
437 // of pixels to interpret.
438 uint8_t numPixels = task;
439 const size_t rowBytes = compute_row_bytes(numPixels,
440 this->bitsPerPixel());
441 // Abort if setting numPixels moves us off the edge of the
msarettd0375bc2015-08-12 08:08:56 -0700442 // image.
443 if (x + numPixels > width) {
444 SkCodecPrintf("Warning: invalid RLE input.\n");
msarette6dd0042015-10-09 11:07:34 -0700445 return y;
msarettd0375bc2015-08-12 08:08:56 -0700446 }
447 // Also abort if there are not enough bytes
msarett4ab9d5f2015-08-06 15:34:42 -0700448 // remaining in the stream to set numPixels.
msarettd0375bc2015-08-12 08:08:56 -0700449 if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) {
450 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
451 if (this->checkForMoreData() < SkAlign2(rowBytes)) {
msarette6dd0042015-10-09 11:07:34 -0700452 return y;
msarettd0375bc2015-08-12 08:08:56 -0700453 }
msarett4ab9d5f2015-08-06 15:34:42 -0700454 }
455 // Set numPixels number of pixels
456 while (numPixels > 0) {
457 switch(this->bitsPerPixel()) {
458 case 4: {
459 SkASSERT(fCurrRLEByte < fRLEBytes);
460 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++];
461 setPixel(dst, dstRowBytes, dstInfo, x++,
462 y, val >> 4);
463 numPixels--;
464 if (numPixels != 0) {
465 setPixel(dst, dstRowBytes, dstInfo,
466 x++, y, val & 0xF);
467 numPixels--;
468 }
469 break;
470 }
471 case 8:
472 SkASSERT(fCurrRLEByte < fRLEBytes);
473 setPixel(dst, dstRowBytes, dstInfo, x++,
474 y, fStreamBuffer.get()[fCurrRLEByte++]);
475 numPixels--;
476 break;
477 case 24: {
478 SkASSERT(fCurrRLEByte + 2 < fRLEBytes);
479 uint8_t blue = fStreamBuffer.get()[fCurrRLEByte++];
480 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
481 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
482 setRGBPixel(dst, dstRowBytes, dstInfo,
483 x++, y, red, green, blue);
484 numPixels--;
msarettd567a812016-10-05 07:19:27 -0700485 break;
msarett4ab9d5f2015-08-06 15:34:42 -0700486 }
487 default:
488 SkASSERT(false);
msarette6dd0042015-10-09 11:07:34 -0700489 return y;
msarett4ab9d5f2015-08-06 15:34:42 -0700490 }
491 }
492 // Skip a byte if necessary to maintain alignment
493 if (!SkIsAlign2(rowBytes)) {
494 fCurrRLEByte++;
495 }
496 break;
497 }
498 }
499 } else {
500 // If the first byte read is not a flag, it indicates the number of
501 // pixels to set in RLE mode.
502 const uint8_t numPixels = flag;
503 const int endX = SkTMin<int>(x + numPixels, width);
504
505 if (24 == this->bitsPerPixel()) {
506 // In RLE24, the second byte read is part of the pixel color.
507 // There are two more required bytes to finish encoding the
508 // color.
509 if ((int) fRLEBytes - fCurrRLEByte < 2) {
msarettd0375bc2015-08-12 08:08:56 -0700510 SkCodecPrintf("Warning: might be incomplete RLE input.\n");
511 if (this->checkForMoreData() < 2) {
msarette6dd0042015-10-09 11:07:34 -0700512 return y;
msarettd0375bc2015-08-12 08:08:56 -0700513 }
msarett4ab9d5f2015-08-06 15:34:42 -0700514 }
515
516 // Fill the pixels up to endX with the specified color
517 uint8_t blue = task;
518 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
519 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
520 while (x < endX) {
bungeman0153dea2015-08-27 16:43:42 -0700521 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue);
msarett4ab9d5f2015-08-06 15:34:42 -0700522 }
523 } else {
524 // In RLE8 or RLE4, the second byte read gives the index in the
525 // color table to look up the pixel color.
526 // RLE8 has one color index that gets repeated
527 // RLE4 has two color indexes in the upper and lower 4 bits of
528 // the bytes, which are alternated
529 uint8_t indices[2] = { task, task };
530 if (4 == this->bitsPerPixel()) {
531 indices[0] >>= 4;
532 indices[1] &= 0xf;
533 }
534
535 // Set the indicated number of pixels
536 for (int which = 0; x < endX; x++) {
bungeman0153dea2015-08-27 16:43:42 -0700537 setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]);
msarett4ab9d5f2015-08-06 15:34:42 -0700538 which = !which;
539 }
540 }
541 }
542 }
543}
scroggoe7fc14b2015-10-02 13:14:46 -0700544
msarett9b9497e2016-02-11 13:29:36 -0800545bool SkBmpRLECodec::skipRows(int count) {
546 const SkImageInfo rowInfo = SkImageInfo::Make(this->getInfo().width(), count, kN32_SkColorType,
547 kUnpremul_SkAlphaType);
548
549 return count == this->decodeRows(rowInfo, nullptr, 0, this->options());
550}
551
msarette6dd0042015-10-09 11:07:34 -0700552// FIXME: Make SkBmpRLECodec have no knowledge of sampling.
553// Or it should do all sampling natively.
554// It currently is a hybrid that needs to know what SkScaledCodec is doing.
scroggoe7fc14b2015-10-02 13:14:46 -0700555class SkBmpRLESampler : public SkSampler {
556public:
557 SkBmpRLESampler(SkBmpRLECodec* codec)
558 : fCodec(codec)
559 {
560 SkASSERT(fCodec);
561 }
562
563private:
msarette6dd0042015-10-09 11:07:34 -0700564 int onSetSampleX(int sampleX) override {
scroggoe7fc14b2015-10-02 13:14:46 -0700565 return fCodec->setSampleX(sampleX);
566 }
567
568 // Unowned pointer. fCodec will delete this class in its destructor.
569 SkBmpRLECodec* fCodec;
570};
571
msarett17315c22016-02-11 10:35:48 -0800572SkSampler* SkBmpRLECodec::getSampler(bool /*createIfNecessary*/) {
573 // We will always create an SkBmpRLESampler if one is requested.
574 // This allows clients to always use the SkBmpRLESampler's
575 // version of fill(), which does nothing since RLE decodes have
576 // already filled pixel memory. This seems fine, since creating
577 // an SkBmpRLESampler is pretty inexpensive.
578 if (!fSampler) {
scroggoe7fc14b2015-10-02 13:14:46 -0700579 fSampler.reset(new SkBmpRLESampler(this));
580 }
581
Ben Wagner145dbcd2016-11-03 14:40:50 -0400582 return fSampler.get();
scroggoe7fc14b2015-10-02 13:14:46 -0700583}
584
msarette6dd0042015-10-09 11:07:34 -0700585int SkBmpRLECodec::setSampleX(int sampleX){
scroggoe7fc14b2015-10-02 13:14:46 -0700586 fSampleX = sampleX;
587 return get_scaled_dimension(this->getInfo().width(), sampleX);
588}