OLD | NEW |
(Empty) | |
| 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" |
| 11 #include "SkScanlineDecoder.h" |
| 12 #include "SkStream.h" |
| 13 |
| 14 /* |
| 15 * Checks if the conversion between the input image and the requested output |
| 16 * image has been implemented |
| 17 */ |
| 18 static bool conversion_possible(const SkImageInfo& dst, |
| 19 const SkImageInfo& src) { |
| 20 // Ensure that the profile type is unchanged |
| 21 if (dst.profileType() != src.profileType()) { |
| 22 return false; |
| 23 } |
| 24 |
| 25 // Ensure the alpha type is valid |
| 26 if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
| 27 return false; |
| 28 } |
| 29 |
| 30 // Check for supported color types |
| 31 switch (dst.colorType()) { |
| 32 // Allow output to kN32 from any type of input |
| 33 case kN32_SkColorType: |
| 34 return true; |
| 35 // Allow output to kIndex_8 from compatible inputs |
| 36 case kIndex_8_SkColorType: |
| 37 return kIndex_8_SkColorType == src.colorType(); |
| 38 default: |
| 39 return false; |
| 40 } |
| 41 } |
| 42 |
| 43 /* |
| 44 * Creates an instance of the decoder |
| 45 * Called only by NewFromStream |
| 46 */ |
| 47 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream, |
| 48 uint16_t bitsPerPixel, uint32_t numColors, |
| 49 uint32_t bytesPerColor, uint32_t offset, |
| 50 SkBmpCodec::RowOrder rowOrder, size_t RLEBytes) |
| 51 : INHERITED(info, stream, bitsPerPixel, rowOrder) |
| 52 , fColorTable(NULL) |
| 53 , fNumColors(numColors) |
| 54 , fBytesPerColor(bytesPerColor) |
| 55 , fOffset(offset) |
| 56 , fStreamBuffer(SkNEW_ARRAY(uint8_t, RLEBytes)) |
| 57 , fRLEBytes(RLEBytes) |
| 58 , fCurrRLEByte(0) |
| 59 {} |
| 60 |
| 61 /* |
| 62 * Initiates the bitmap decode |
| 63 */ |
| 64 SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo, |
| 65 void* dst, size_t dstRowBytes, |
| 66 const Options& opts, |
| 67 SkPMColor* inputColorPtr, |
| 68 int* inputColorCount) { |
| 69 if (!this->handleRewind(false)) { |
| 70 return kCouldNotRewind; |
| 71 } |
| 72 if (opts.fSubset) { |
| 73 // Subsets are not supported. |
| 74 return kUnimplemented; |
| 75 } |
| 76 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 77 SkCodecPrintf("Error: scaling not supported.\n"); |
| 78 return kInvalidScale; |
| 79 } |
| 80 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 81 SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 82 return kInvalidConversion; |
| 83 } |
| 84 |
| 85 // Create the color table if necessary and prepare the stream for decode |
| 86 // Note that if it is non-NULL, inputColorCount will be modified |
| 87 if (!this->createColorTable(inputColorCount)) { |
| 88 SkCodecPrintf("Error: could not create color table.\n"); |
| 89 return kInvalidInput; |
| 90 } |
| 91 |
| 92 // Copy the color table to the client if necessary |
| 93 copy_color_table(dstInfo, fColorTable, inputColorPtr, inputColorCount); |
| 94 |
| 95 // Initialize a swizzler if necessary |
| 96 if (!this->initializeStreamBuffer()) { |
| 97 SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
| 98 return kInvalidConversion; |
| 99 } |
| 100 |
| 101 // Perform the decode |
| 102 return decode(dstInfo, dst, dstRowBytes, opts); |
| 103 } |
| 104 |
| 105 /* |
| 106 * Process the color table for the bmp input |
| 107 */ |
| 108 bool SkBmpRLECodec::createColorTable(int* numColors) { |
| 109 // Allocate memory for color table |
| 110 uint32_t colorBytes = 0; |
| 111 uint32_t maxColors = 0; |
| 112 SkPMColor colorTable[256]; |
| 113 if (this->bitsPerPixel() <= 8) { |
| 114 // Zero is a default for maxColors |
| 115 // Also set fNumColors to maxColors when it is too large |
| 116 maxColors = 1 << this->bitsPerPixel(); |
| 117 if (fNumColors == 0 || fNumColors >= maxColors) { |
| 118 fNumColors = maxColors; |
| 119 } |
| 120 |
| 121 // Inform the caller of the number of colors |
| 122 if (NULL != numColors) { |
| 123 // We set the number of colors to maxColors in order to ensure |
| 124 // safe memory accesses. Otherwise, an invalid pixel could |
| 125 // access memory outside of our color table array. |
| 126 *numColors = maxColors; |
| 127 } |
| 128 |
| 129 // Read the color table from the stream |
| 130 colorBytes = fNumColors * fBytesPerColor; |
| 131 SkAutoTDeleteArray<uint8_t> cBuffer(SkNEW_ARRAY(uint8_t, colorBytes)); |
| 132 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
| 133 SkCodecPrintf("Error: unable to read color table.\n"); |
| 134 return false; |
| 135 } |
| 136 |
| 137 // Fill in the color table |
| 138 uint32_t i = 0; |
| 139 for (; i < fNumColors; i++) { |
| 140 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 141 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 142 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 143 colorTable[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 144 } |
| 145 |
| 146 // To avoid segmentation faults on bad pixel data, fill the end of the |
| 147 // color table with black. This is the same the behavior as the |
| 148 // chromium decoder. |
| 149 for (; i < maxColors; i++) { |
| 150 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 151 } |
| 152 |
| 153 // Set the color table |
| 154 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); |
| 155 } |
| 156 |
| 157 // Check that we have not read past the pixel array offset |
| 158 if(fOffset < colorBytes) { |
| 159 // This may occur on OS 2.1 and other old versions where the color |
| 160 // table defaults to max size, and the bmp tries to use a smaller |
| 161 // color table. This is invalid, and our decision is to indicate |
| 162 // an error, rather than try to guess the intended size of the |
| 163 // color table. |
| 164 SkCodecPrintf("Error: pixel data offset less than color table size.\n"); |
| 165 return false; |
| 166 } |
| 167 |
| 168 // After reading the color table, skip to the start of the pixel array |
| 169 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 170 SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 171 return false; |
| 172 } |
| 173 |
| 174 // Return true on success |
| 175 return true; |
| 176 } |
| 177 |
| 178 bool SkBmpRLECodec::initializeStreamBuffer() { |
| 179 // Setup a buffer to contain the full input stream |
| 180 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes); |
| 181 if (totalBytes < fRLEBytes) { |
| 182 fRLEBytes = totalBytes; |
| 183 SkCodecPrintf("Warning: incomplete RLE file.\n"); |
| 184 } |
| 185 if (fRLEBytes == 0) { |
| 186 SkCodecPrintf("Error: could not read RLE image data.\n"); |
| 187 return false; |
| 188 } |
| 189 return true; |
| 190 } |
| 191 |
| 192 /* |
| 193 * Set an RLE pixel using the color table |
| 194 */ |
| 195 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, |
| 196 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, |
| 197 uint8_t index) { |
| 198 // Set the row |
| 199 int height = dstInfo.height(); |
| 200 int row; |
| 201 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { |
| 202 row = height - y - 1; |
| 203 } else { |
| 204 row = y; |
| 205 } |
| 206 |
| 207 // Set the pixel based on destination color type |
| 208 switch (dstInfo.colorType()) { |
| 209 case kN32_SkColorType: { |
| 210 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
| 211 row * (int) dstRowBytes); |
| 212 dstRow[x] = fColorTable->operator[](index); |
| 213 break; |
| 214 } |
| 215 default: |
| 216 // This case should not be reached. We should catch an invalid |
| 217 // color type when we check that the conversion is possible. |
| 218 SkASSERT(false); |
| 219 break; |
| 220 } |
| 221 } |
| 222 |
| 223 /* |
| 224 * Set an RLE pixel from R, G, B values |
| 225 */ |
| 226 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, |
| 227 const SkImageInfo& dstInfo, uint32_t x, |
| 228 uint32_t y, uint8_t red, uint8_t green, |
| 229 uint8_t blue) { |
| 230 // Set the row |
| 231 int height = dstInfo.height(); |
| 232 int row; |
| 233 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { |
| 234 row = height - y - 1; |
| 235 } else { |
| 236 row = y; |
| 237 } |
| 238 |
| 239 // Set the pixel based on destination color type |
| 240 switch (dstInfo.colorType()) { |
| 241 case kN32_SkColorType: { |
| 242 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, |
| 243 row * (int) dstRowBytes); |
| 244 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 245 break; |
| 246 } |
| 247 default: |
| 248 // This case should not be reached. We should catch an invalid |
| 249 // color type when we check that the conversion is possible. |
| 250 SkASSERT(false); |
| 251 break; |
| 252 } |
| 253 } |
| 254 |
| 255 /* |
| 256 * Performs the bitmap decoding for RLE input format |
| 257 * RLE decoding is performed all at once, rather than a one row at a time |
| 258 */ |
| 259 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, |
| 260 void* dst, size_t dstRowBytes, |
| 261 const Options& opts) { |
| 262 // Set RLE flags |
| 263 static const uint8_t RLE_ESCAPE = 0; |
| 264 static const uint8_t RLE_EOL = 0; |
| 265 static const uint8_t RLE_EOF = 1; |
| 266 static const uint8_t RLE_DELTA = 2; |
| 267 |
| 268 // Set constant values |
| 269 const int width = dstInfo.width(); |
| 270 const int height = dstInfo.height(); |
| 271 |
| 272 // Destination parameters |
| 273 int x = 0; |
| 274 int y = 0; |
| 275 |
| 276 // Set the background as transparent. Then, if the RLE code skips pixels, |
| 277 // the skipped pixels will be transparent. |
| 278 // Because of the need for transparent pixels, kN32 is the only color |
| 279 // type that makes sense for the destination format. |
| 280 SkASSERT(kN32_SkColorType == dstInfo.colorType()); |
| 281 if (kNo_ZeroInitialized == opts.fZeroInitialized) { |
| 282 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT,
NULL); |
| 283 } |
| 284 |
| 285 while (true) { |
| 286 // If we have reached a row that is beyond the requested height, we have |
| 287 // succeeded. |
| 288 if (y >= height) { |
| 289 // It would be better to check for the EOF marker before returning |
| 290 // success, but we may be performing a scanline decode, which |
| 291 // may require us to stop before decoding the full height. |
| 292 return kSuccess; |
| 293 } |
| 294 |
| 295 // Every entry takes at least two bytes |
| 296 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
| 297 SkCodecPrintf("Warning: incomplete RLE input.\n"); |
| 298 return kIncompleteInput; |
| 299 } |
| 300 |
| 301 // Read the next two bytes. These bytes have different meanings |
| 302 // depending on their values. In the first interpretation, the first |
| 303 // byte is an escape flag and the second byte indicates what special |
| 304 // task to perform. |
| 305 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++]; |
| 306 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++]; |
| 307 |
| 308 // Perform decoding |
| 309 if (RLE_ESCAPE == flag) { |
| 310 switch (task) { |
| 311 case RLE_EOL: |
| 312 x = 0; |
| 313 y++; |
| 314 break; |
| 315 case RLE_EOF: |
| 316 return kSuccess; |
| 317 case RLE_DELTA: { |
| 318 // Two bytes are needed to specify delta |
| 319 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
| 320 SkCodecPrintf("Warning: incomplete RLE input\n"); |
| 321 return kIncompleteInput; |
| 322 } |
| 323 // Modify x and y |
| 324 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++]; |
| 325 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++]; |
| 326 x += dx; |
| 327 y += dy; |
| 328 if (x > width || y > height) { |
| 329 SkCodecPrintf("Warning: invalid RLE input 1.\n"); |
| 330 return kIncompleteInput; |
| 331 } |
| 332 break; |
| 333 } |
| 334 default: { |
| 335 // If task does not match any of the above signals, it |
| 336 // indicates that we have a sequence of non-RLE pixels. |
| 337 // Furthermore, the value of task is equal to the number |
| 338 // of pixels to interpret. |
| 339 uint8_t numPixels = task; |
| 340 const size_t rowBytes = compute_row_bytes(numPixels, |
| 341 this->bitsPerPixel()); |
| 342 // Abort if setting numPixels moves us off the edge of the |
| 343 // image. Also abort if there are not enough bytes |
| 344 // remaining in the stream to set numPixels. |
| 345 if (x + numPixels > width || |
| 346 (int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes))
{ |
| 347 SkCodecPrintf("Warning: invalid RLE input 2.\n"); |
| 348 return kIncompleteInput; |
| 349 } |
| 350 // Set numPixels number of pixels |
| 351 while (numPixels > 0) { |
| 352 switch(this->bitsPerPixel()) { |
| 353 case 4: { |
| 354 SkASSERT(fCurrRLEByte < fRLEBytes); |
| 355 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++
]; |
| 356 setPixel(dst, dstRowBytes, dstInfo, x++, |
| 357 y, val >> 4); |
| 358 numPixels--; |
| 359 if (numPixels != 0) { |
| 360 setPixel(dst, dstRowBytes, dstInfo, |
| 361 x++, y, val & 0xF); |
| 362 numPixels--; |
| 363 } |
| 364 break; |
| 365 } |
| 366 case 8: |
| 367 SkASSERT(fCurrRLEByte < fRLEBytes); |
| 368 setPixel(dst, dstRowBytes, dstInfo, x++, |
| 369 y, fStreamBuffer.get()[fCurrRLEByte++]); |
| 370 numPixels--; |
| 371 break; |
| 372 case 24: { |
| 373 SkASSERT(fCurrRLEByte + 2 < fRLEBytes); |
| 374 uint8_t blue = fStreamBuffer.get()[fCurrRLEByte+
+]; |
| 375 uint8_t green = fStreamBuffer.get()[fCurrRLEByte
++]; |
| 376 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++
]; |
| 377 setRGBPixel(dst, dstRowBytes, dstInfo, |
| 378 x++, y, red, green, blue); |
| 379 numPixels--; |
| 380 } |
| 381 default: |
| 382 SkASSERT(false); |
| 383 return kInvalidInput; |
| 384 } |
| 385 } |
| 386 // Skip a byte if necessary to maintain alignment |
| 387 if (!SkIsAlign2(rowBytes)) { |
| 388 fCurrRLEByte++; |
| 389 } |
| 390 break; |
| 391 } |
| 392 } |
| 393 } else { |
| 394 // If the first byte read is not a flag, it indicates the number of |
| 395 // pixels to set in RLE mode. |
| 396 const uint8_t numPixels = flag; |
| 397 const int endX = SkTMin<int>(x + numPixels, width); |
| 398 |
| 399 if (24 == this->bitsPerPixel()) { |
| 400 // In RLE24, the second byte read is part of the pixel color. |
| 401 // There are two more required bytes to finish encoding the |
| 402 // color. |
| 403 if ((int) fRLEBytes - fCurrRLEByte < 2) { |
| 404 SkCodecPrintf("Warning: incomplete RLE input\n"); |
| 405 return kIncompleteInput; |
| 406 } |
| 407 |
| 408 // Fill the pixels up to endX with the specified color |
| 409 uint8_t blue = task; |
| 410 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++]; |
| 411 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++]; |
| 412 while (x < endX) { |
| 413 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red, |
| 414 green, blue); |
| 415 } |
| 416 } else { |
| 417 // In RLE8 or RLE4, the second byte read gives the index in the |
| 418 // color table to look up the pixel color. |
| 419 // RLE8 has one color index that gets repeated |
| 420 // RLE4 has two color indexes in the upper and lower 4 bits of |
| 421 // the bytes, which are alternated |
| 422 uint8_t indices[2] = { task, task }; |
| 423 if (4 == this->bitsPerPixel()) { |
| 424 indices[0] >>= 4; |
| 425 indices[1] &= 0xf; |
| 426 } |
| 427 |
| 428 // Set the indicated number of pixels |
| 429 for (int which = 0; x < endX; x++) { |
| 430 setPixel(dst, dstRowBytes, dstInfo, x, y, |
| 431 indices[which]); |
| 432 which = !which; |
| 433 } |
| 434 } |
| 435 } |
| 436 } |
| 437 } |
OLD | NEW |