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 | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 47 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 48 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 49 | if (kSuccess != result) { |
| 50 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Perform the decode |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 54 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 55 | 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; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Process the color table for the bmp input |
| 68 | */ |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 69 | bool SkBmpRLECodec::createColorTable(SkColorType dstColorType, int* numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 70 | // 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | if (nullptr != numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 77 | // 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 | } |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 82 | // Don't bother reading more than maxColors. |
| 83 | const uint32_t numColorsToRead = |
| 84 | fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 85 | |
| 86 | // Read the color table from the stream |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 87 | colorBytes = numColorsToRead * fBytesPerColor; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 88 | std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 89 | 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 |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 95 | PackColorProc packARGB = choose_pack_color_proc(false, dstColorType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 96 | uint32_t i = 0; |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 97 | for (; i < numColorsToRead; i++) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 98 | 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); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 101 | colorTable[i] = packARGB(0xFF, red, green, blue); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // To avoid segmentation faults on bad pixel data, fill the end of the |
| 105 | // color table with black. This is the same the behavior as the |
| 106 | // chromium decoder. |
| 107 | for (; i < maxColors; i++) { |
| 108 | colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 109 | } |
| 110 | |
| 111 | // Set the color table |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 112 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 113 | } |
| 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 | |
| 136 | bool SkBmpRLECodec::initializeStreamBuffer() { |
| 137 | // Setup a buffer to contain the full input stream |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 138 | // 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. |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 147 | 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 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 156 | fCurrRLEByte = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | /* |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 161 | * 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 | */ |
| 167 | size_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 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 196 | * Set an RLE pixel using the color table |
| 197 | */ |
| 198 | void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
| 199 | const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
| 200 | uint8_t index) { |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 201 | if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 202 | // Set the row |
| 203 | uint32_t row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 204 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 205 | // Set the pixel based on destination color type |
| 206 | const int dstX = get_dst_coord(x, fSampleX); |
| 207 | switch (dstInfo.colorType()) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 208 | case kRGBA_8888_SkColorType: |
| 209 | case kBGRA_8888_SkColorType: { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 210 | 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; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 224 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Set an RLE pixel from R, G, B values |
| 230 | */ |
| 231 | void 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) { |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 235 | if (dst && is_coord_necessary(x, fSampleX, dstInfo.width())) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 236 | // 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()) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 242 | case kRGBA_8888_SkColorType: { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 243 | SkPMColor* dstRow = SkTAddOffset<SkPMColor>(dst, row * (int) dstRowBytes); |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 244 | 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); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 250 | 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 Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 266 | SkCodec::Result SkBmpRLECodec::onPrepareToDecode(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 267 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 268 | // FIXME: Support subsets for scanline decodes. |
| 269 | if (options.fSubset) { |
| 270 | // Subsets are not supported. |
| 271 | return kUnimplemented; |
| 272 | } |
| 273 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 274 | // Reset fSampleX. If it needs to be a value other than 1, it will get modified by |
| 275 | // the sampler. |
| 276 | fSampleX = 1; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 277 | fLinesToSkip = 0; |
| 278 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 279 | // Create the color table if necessary and prepare the stream for decode |
| 280 | // Note that if it is non-NULL, inputColorCount will be modified |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 281 | if (!this->createColorTable(dstInfo.colorType(), inputColorCount)) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 282 | SkCodecPrintf("Error: could not create color table.\n"); |
| 283 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 284 | } |
| 285 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 286 | // 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 |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 290 | fRLEBytes = fOrigRLEBytes; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 291 | if (!this->initializeStreamBuffer()) { |
| 292 | SkCodecPrintf("Error: cannot initialize stream buffer.\n"); |
msarett | 2a98bac | 2016-02-17 14:06:46 -0800 | [diff] [blame] | 293 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 294 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 295 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 296 | return SkCodec::kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 297 | } |
| 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 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 303 | int SkBmpRLECodec::decodeRows(const SkImageInfo& info, void* dst, size_t dstRowBytes, |
| 304 | const Options& opts) { |
Matt Sarett | 6193568 | 2016-11-03 17:27:12 +0000 | [diff] [blame] | 305 | 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 Sarett | 6193568 | 2016-11-03 17:27:12 +0000 | [diff] [blame] | 313 | 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 Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 321 | if (dst) { |
| 322 | dst = SkTAddOffset<void>(dst, fLinesToSkip * dstRowBytes); |
| 323 | } |
Matt Sarett | 6193568 | 2016-11-03 17:27:12 +0000 | [diff] [blame] | 324 | fLinesToSkip = 0; |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 325 | |
| 326 | dstInfo = dstInfo.makeWH(dstInfo.width(), height); |
Matt Sarett | 6193568 | 2016-11-03 17:27:12 +0000 | [diff] [blame] | 327 | } else { |
| 328 | fLinesToSkip -= height; |
| 329 | return height; |
| 330 | } |
| 331 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 332 | 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 | |
| 360 | int 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 | |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 373 | // Destination parameters |
| 374 | int x = 0; |
| 375 | int y = 0; |
| 376 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 377 | while (true) { |
| 378 | // If we have reached a row that is beyond the requested height, we have |
| 379 | // succeeded. |
| 380 | if (y >= height) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 381 | // It would be better to check for the EOF marker before indicating |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 382 | // success, but we may be performing a scanline decode, which |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 383 | // would require us to stop before decoding the full height. |
| 384 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // Every entry takes at least two bytes |
| 388 | if ((int) fRLEBytes - fCurrRLEByte < 2) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 389 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 390 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 391 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 392 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 393 | } |
| 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: |
msarett | a376629 | 2015-11-19 08:59:12 -0800 | [diff] [blame] | 410 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 411 | case RLE_DELTA: { |
| 412 | // Two bytes are needed to specify delta |
| 413 | if ((int) fRLEBytes - fCurrRLEByte < 2) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 414 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 415 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 416 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 417 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 418 | } |
| 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; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 424 | if (x > width) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 425 | SkCodecPrintf("Warning: invalid RLE input.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 426 | return y - dy; |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 427 | } else if (y > height) { |
| 428 | fLinesToSkip = y - height; |
| 429 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 430 | } |
| 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 |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 442 | // image. |
| 443 | if (x + numPixels > width) { |
| 444 | SkCodecPrintf("Warning: invalid RLE input.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 445 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 446 | } |
| 447 | // Also abort if there are not enough bytes |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 448 | // remaining in the stream to set numPixels. |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 449 | if ((int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) { |
| 450 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 451 | if (this->checkForMoreData() < SkAlign2(rowBytes)) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 452 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 453 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 454 | } |
| 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--; |
msarett | d567a81 | 2016-10-05 07:19:27 -0700 | [diff] [blame] | 485 | break; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 486 | } |
| 487 | default: |
| 488 | SkASSERT(false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 489 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 490 | } |
| 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) { |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 510 | SkCodecPrintf("Warning: might be incomplete RLE input.\n"); |
| 511 | if (this->checkForMoreData() < 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 512 | return y; |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 513 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 514 | } |
| 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) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 521 | setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, green, blue); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 522 | } |
| 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++) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 537 | setPixel(dst, dstRowBytes, dstInfo, x, y, indices[which]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 538 | which = !which; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 544 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 545 | bool 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 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 552 | // 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. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 555 | class SkBmpRLESampler : public SkSampler { |
| 556 | public: |
| 557 | SkBmpRLESampler(SkBmpRLECodec* codec) |
| 558 | : fCodec(codec) |
| 559 | { |
| 560 | SkASSERT(fCodec); |
| 561 | } |
| 562 | |
| 563 | private: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 564 | int onSetSampleX(int sampleX) override { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 565 | return fCodec->setSampleX(sampleX); |
| 566 | } |
| 567 | |
| 568 | // Unowned pointer. fCodec will delete this class in its destructor. |
| 569 | SkBmpRLECodec* fCodec; |
| 570 | }; |
| 571 | |
msarett | 17315c2 | 2016-02-11 10:35:48 -0800 | [diff] [blame] | 572 | SkSampler* 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) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 579 | fSampler.reset(new SkBmpRLESampler(this)); |
| 580 | } |
| 581 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 582 | return fSampler.get(); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 583 | } |
| 584 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 585 | int SkBmpRLECodec::setSampleX(int sampleX){ |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 586 | fSampleX = sampleX; |
| 587 | return get_scaled_dimension(this->getInfo().width(), sampleX); |
| 588 | } |