msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 1 | /* |
| 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" |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | |
| 13 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 14 | * Creates an instance of the decoder |
| 15 | * Called only by NewFromStream |
| 16 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 17 | SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 18 | uint16_t bitsPerPixel, uint32_t numColors, |
| 19 | uint32_t bytesPerColor, uint32_t offset, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 20 | SkCodec::SkScanlineOrder rowOrder, |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 21 | size_t RLEBytes) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 22 | : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | , fColorTable(nullptr) |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 24 | , fNumColors(numColors) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 25 | , fBytesPerColor(bytesPerColor) |
| 26 | , fOffset(offset) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 27 | , fStreamBuffer(new uint8_t[RLEBytes]) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 28 | , fRLEBytes(RLEBytes) |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 29 | , fOrigRLEBytes(RLEBytes) |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 30 | , fCurrRLEByte(0) |
| 31 | , fSampleX(1) |
| 32 | {} |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Initiates the bitmap decode |
| 36 | */ |
| 37 | SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 38 | void* dst, size_t dstRowBytes, |
| 39 | const Options& opts, |
| 40 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 41 | int* inputColorCount, |
| 42 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 43 | if (opts.fSubset) { |
| 44 | // Subsets are not supported. |
| 45 | return kUnimplemented; |
| 46 | } |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 47 | if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 48 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 49 | return kInvalidConversion; |
| 50 | } |
| 51 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 52 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 53 | if (kSuccess != result) { |
| 54 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Perform the decode |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 58 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 59 | 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; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Process the color table for the bmp input |
| 72 | */ |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 73 | bool SkBmpRLECodec::createColorTable(SkColorType dstColorType, int* numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | if (nullptr != numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 81 | // 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 | } |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 86 | // Don't bother reading more than maxColors. |
| 87 | const uint32_t numColorsToRead = |
| 88 | fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 89 | |
| 90 | // Read the color table from the stream |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 91 | colorBytes = numColorsToRead * fBytesPerColor; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 92 | SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 93 | 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 |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 99 | PackColorProc packARGB = choose_pack_color_proc(false, dstColorType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 100 | uint32_t i = 0; |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 101 | for (; i < numColorsToRead; i++) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 102 | 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); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 105 | colorTable[i] = packARGB(0xFF, red, green, blue); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 106 | } |
| 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 |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 116 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 117 | } |
| 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 | |
| 140 | bool SkBmpRLECodec::initializeStreamBuffer() { |
| 141 | // Setup a buffer to contain the full input stream |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 142 | // 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. |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 151 | 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 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 160 | fCurrRLEByte = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | /* |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 165 | * 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 | */ |
| 171 | size_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 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 200 | * Set an RLE pixel using the color table |
| 201 | */ |
| 202 | void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
| 203 | const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
| 204 | uint8_t index) { |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 205 | if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 206 | // Set the row |
| 207 | uint32_t row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 208 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 209 | // Set the pixel based on destination color type |
| 210 | const int dstX = get_dst_coord(x, fSampleX); |
| 211 | switch (dstInfo.colorType()) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 212 | case kRGBA_8888_SkColorType: |
| 213 | case kBGRA_8888_SkColorType: { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 214 | 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; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 228 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Set an RLE pixel from R, G, B values |
| 234 | */ |
| 235 | void 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) { |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 239 | if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 240 | // 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()) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 246 | case kRGBA_8888_SkColorType: { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 247 | SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 248 | 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); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 254 | 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 | |
| 270 | SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 271 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 272 | // FIXME: Support subsets for scanline decodes. |
| 273 | if (options.fSubset) { |
| 274 | // Subsets are not supported. |
| 275 | return kUnimplemented; |
| 276 | } |
| 277 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 278 | // Reset fSampleX. If it needs to be a value other than 1, it will get modified by |
| 279 | // the sampler. |
| 280 | fSampleX = 1; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 281 | fLinesToSkip = 0; |
| 282 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 283 | // Create the color table if necessary and prepare the stream for decode |
| 284 | // Note that if it is non-NULL, inputColorCount will be modified |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 285 | if (!this->createColorTable(dstInfo.colorType(), inputColorCount)) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 286 | SkCodecPrintf("Error: could not create color table.\n"); |
| 287 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 288 | } |
| 289 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 290 | // 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 |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 294 | fRLEBytes = fOrigRLEBytes; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 295 | if (!this->initializeStreamBuffer()) { |
| 296 | SkCodecPrintf("Error: cannot initialize stream buffer.\n"); |
msarett | 2a98bac | 2016-02-17 14:06:46 -0800 | [diff] [blame] | 297 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 298 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 299 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 300 | return SkCodec::kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 301 | } |
| 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 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 307 | int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes, |
| 308 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 309 | // 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 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 315 | const int width = this->getInfo().width(); |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 316 | int height = info.height(); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 317 | |
| 318 | // Account for sampling. |
| 319 | SkImageInfo dstInfo = info.makeWH(get_scaled_dimension(width, fSampleX), height); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 320 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 321 | // 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. |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 325 | SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() || |
| 326 | kBGRA_8888_SkColorType == dstInfo.colorType()); |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 327 | if (dst) { |
| 328 | SkSampler::Fill(dstInfo, dst, dstRowBytes, SK_ColorTRANSPARENT, opts.fZeroInitialized); |
| 329 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 330 | |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 331 | // 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 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 346 | while (true) { |
| 347 | // If we have reached a row that is beyond the requested height, we have |
| 348 | // succeeded. |
| 349 | if (y >= height) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 350 | // It would be better to check for the EOF marker before indicating |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 351 | // success, but we may be performing a scanline decode, which |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 352 | // would require us to stop before decoding the full height. |
| 353 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Every entry takes at least two bytes |
| 357 | if ((int) fRLEBytes - fCurrRLEByte < 2) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 358 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 359 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 360 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 361 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 362 | } |
| 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: |
msarett | a376629 | 2015-11-19 08:59:12 -0800 | [diff] [blame] | 379 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 380 | case RLE_DELTA: { |
| 381 | // Two bytes are needed to specify delta |
| 382 | if ((int) fRLEBytes - fCurrRLEByte < 2) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 383 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 384 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 385 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 386 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 387 | } |
| 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; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 393 | if (x > width) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 394 | SkCodecPrintf("Warning: invalid RLE input.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 395 | return y - dy; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 396 | } else if (y > height) { |
| 397 | fLinesToSkip = y - height; |
| 398 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 399 | } |
| 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 |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 411 | // image. |
| 412 | if (x + numPixels > width) { |
| 413 | SkCodecPrintf("Warning: invalid RLE input.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 414 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 415 | } |
| 416 | // Also abort if there are not enough bytes |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 417 | // remaining in the stream to set numPixels. |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 418 | if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) { |
| 419 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 420 | if (this->checkForMoreData() < SkAlign2(rowBytes)) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 421 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 422 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 423 | } |
| 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--; |
msarett | d567a81 | 2016-10-05 07:19:27 -0700 | [diff] [blame] | 454 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 455 | } |
| 456 | default: |
| 457 | SkASSERT(false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 458 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 459 | } |
| 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) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 479 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 480 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 481 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 482 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 483 | } |
| 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) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 490 | setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 491 | } |
| 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++) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 506 | setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 507 | which = !which; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 513 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 514 | bool 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 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 521 | // 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. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 524 | class SkBmpRLESampler : public SkSampler { |
| 525 | public: |
| 526 | SkBmpRLESampler(SkBmpRLECodec* codec) |
| 527 | : fCodec(codec) |
| 528 | { |
| 529 | SkASSERT(fCodec); |
| 530 | } |
| 531 | |
| 532 | private: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 533 | int onSetSampleX(int sampleX) override { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 534 | return fCodec->setSampleX(sampleX); |
| 535 | } |
| 536 | |
| 537 | // Unowned pointer. fCodec will delete this class in its destructor. |
| 538 | SkBmpRLECodec* fCodec; |
| 539 | }; |
| 540 | |
msarett | 17315c2 | 2016-02-11 10:35:48 -0800 | [diff] [blame] | 541 | SkSampler* 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) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 548 | fSampler.reset(new SkBmpRLESampler(this)); |
| 549 | } |
| 550 | |
| 551 | return fSampler; |
| 552 | } |
| 553 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 554 | int SkBmpRLECodec::setSampleX(int sampleX){ |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 555 | fSampleX = sampleX; |
| 556 | return get_scaled_dimension(this->getInfo().width(), sampleX); |
| 557 | } |