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