Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec_libbmp.h" | 8 #include "SkCodec_libbmp.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 // TODO: ICO files may contain a BMP and need to use this decoder | 81 // TODO: ICO files may contain a BMP and need to use this decoder |
| 82 const char bmpSig[] = { 'B', 'M' }; | 82 const char bmpSig[] = { 'B', 'M' }; |
| 83 char buffer[sizeof(bmpSig)]; | 83 char buffer[sizeof(bmpSig)]; |
| 84 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && | 84 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && |
| 85 !memcmp(buffer, bmpSig, sizeof(bmpSig)); | 85 !memcmp(buffer, bmpSig, sizeof(bmpSig)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 /* | 88 /* |
| 89 * | 89 * |
| 90 * Assumes IsBmp was called and returned true | 90 * Assumes IsBmp was called and returned true |
| 91 * Creates a bitmap decoder | 91 * Creates a bmp decoder |
| 92 * Reads enough of the stream to determine the image format | 92 * Reads enough of the stream to determine the image format |
| 93 * | 93 * |
| 94 */ | 94 */ |
| 95 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { | 95 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { |
| 96 return SkBmpCodec::NewFromStream(stream, false); | |
| 97 } | |
| 98 | |
| 99 /* | |
| 100 * | |
| 101 * Creates a bmp decoder for a bmp embedded in ico | |
| 102 * Reads enough of the stream to determine the image format | |
| 103 * | |
| 104 */ | |
| 105 SkCodec* SkBmpCodec::NewFromIco(SkStream* stream) { | |
| 106 return SkBmpCodec::NewFromStream(stream, true); | |
| 107 } | |
| 108 | |
| 109 /* | |
| 110 * | |
| 111 * Creates a bmp decoder | |
| 112 * Reads enough of the stream to determine the image format | |
| 113 * | |
| 114 */ | |
| 115 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, const bool isIco) { | |
| 96 // Header size constants | 116 // Header size constants |
| 97 static const uint32_t kBmpHeaderBytes = 14; | 117 static const uint32_t kBmpHeaderBytes = 14; |
| 98 static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; | 118 static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
| 99 static const uint32_t kBmpOS2V1Bytes = 12; | 119 static const uint32_t kBmpOS2V1Bytes = 12; |
| 100 static const uint32_t kBmpOS2V2Bytes = 64; | 120 static const uint32_t kBmpOS2V2Bytes = 64; |
| 101 static const uint32_t kBmpInfoBaseBytes = 16; | 121 static const uint32_t kBmpInfoBaseBytes = 16; |
| 102 static const uint32_t kBmpInfoV1Bytes = 40; | 122 static const uint32_t kBmpInfoV1Bytes = 40; |
| 103 static const uint32_t kBmpInfoV2Bytes = 52; | 123 static const uint32_t kBmpInfoV2Bytes = 52; |
| 104 static const uint32_t kBmpInfoV3Bytes = 56; | 124 static const uint32_t kBmpInfoV3Bytes = 56; |
| 105 static const uint32_t kBmpInfoV4Bytes = 108; | 125 static const uint32_t kBmpInfoV4Bytes = 108; |
| 106 static const uint32_t kBmpInfoV5Bytes = 124; | 126 static const uint32_t kBmpInfoV5Bytes = 124; |
| 107 static const uint32_t kBmpMaskBytes = 12; | 127 static const uint32_t kBmpMaskBytes = 12; |
| 108 | 128 |
| 109 // Read the first header and the size of the second header | |
| 110 SkAutoTDeleteArray<uint8_t> hBuffer( | |
| 111 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); | |
| 112 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != | |
| 113 kBmpHeaderBytesPlusFour) { | |
| 114 SkDebugf("Error: unable to read first bitmap header.\n"); | |
| 115 return NULL; | |
| 116 } | |
| 117 | |
| 118 // The total bytes in the bmp file | 129 // The total bytes in the bmp file |
| 119 // We only need to use this value for RLE decoding, so we will only check | 130 // We only need to use this value for RLE decoding, so we will only |
| 120 // that it is valid in the RLE case. | 131 // check that it is valid in the RLE case. |
| 121 const uint32_t totalBytes = get_int(hBuffer.get(), 2); | 132 uint32_t totalBytes; |
| 122 | |
| 123 // The offset from the start of the file where the pixel data begins | 133 // The offset from the start of the file where the pixel data begins |
| 124 const uint32_t offset = get_int(hBuffer.get(), 10); | 134 uint32_t offset; |
| 125 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { | |
| 126 SkDebugf("Error: invalid starting location for pixel data\n"); | |
| 127 return NULL; | |
| 128 } | |
| 129 | |
| 130 // The size of the second (info) header in bytes | 135 // The size of the second (info) header in bytes |
| 131 // The size is the first field of the second header, so we have already | 136 // The size is the first field of the second header, so we have already |
| 132 // read the first four infoBytes. | 137 // read the first four infoBytes. |
|
scroggo
2015/03/18 21:39:18
No longer true.
msarett
2015/03/20 18:35:56
Done.
| |
| 133 const uint32_t infoBytes = get_int(hBuffer.get(), 14); | 138 uint32_t infoBytes; |
| 134 if (infoBytes < kBmpOS2V1Bytes) { | 139 // Bmps embedded in Icos skip the first Bmp header |
| 135 SkDebugf("Error: invalid second header size.\n"); | 140 if (!isIco) { |
| 136 return NULL; | 141 // Read the first header and the size of the second header |
| 142 SkAutoTDeleteArray<uint8_t> hBuffer( | |
| 143 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); | |
| 144 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != | |
| 145 kBmpHeaderBytesPlusFour) { | |
| 146 SkDebugf("Error: unable to read first bitmap header.\n"); | |
| 147 return NULL; | |
| 148 } | |
| 149 | |
| 150 totalBytes = get_int(hBuffer.get(), 2); | |
| 151 offset = get_int(hBuffer.get(), 10); | |
| 152 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { | |
| 153 SkDebugf("Error: invalid starting location for pixel data\n"); | |
| 154 return NULL; | |
| 155 } | |
| 156 | |
| 157 // The size of the second (info) header in bytes | |
| 158 // The size is the first field of the second header, so we have already | |
| 159 // read the first four infoBytes. | |
| 160 infoBytes = get_int(hBuffer.get(), 14); | |
| 161 if (infoBytes < kBmpOS2V1Bytes) { | |
| 162 SkDebugf("Error: invalid second header size.\n"); | |
| 163 return NULL; | |
| 164 } | |
| 165 } else { | |
| 166 // This value is only used by RLE compression. Bmp in Ico files do not | |
| 167 // use RLE. If the compression field is incorrectly signaled as RLE, | |
| 168 // we will catch this and signal an error below. | |
| 169 totalBytes = 0; | |
| 170 | |
| 171 // Bmps in Ico cannot specify an offset. We will always assume that | |
|
scroggo
2015/03/18 21:39:18
Really? SkImageDecoder_libico reads an offset fiel
msarett
2015/03/20 18:35:56
Ico files have multiple directory entries. Each s
| |
| 172 // pixel data begins immediately after the color table. This value | |
| 173 // will be corrected below. | |
| 174 offset = 0; | |
| 175 | |
| 176 // Read the size of the second header | |
| 177 SkAutoTDeleteArray<uint8_t> hBuffer( | |
| 178 SkNEW_ARRAY(uint8_t, 4)); | |
| 179 if (stream->read(hBuffer.get(), 4) != 4) { | |
| 180 SkDebugf("Error: unable to read size of second bitmap header.\n"); | |
| 181 return NULL; | |
| 182 } | |
| 183 infoBytes = get_int(hBuffer.get(), 0); | |
| 184 if (infoBytes < kBmpOS2V1Bytes) { | |
| 185 SkDebugf("Error: invalid second header size.\n"); | |
| 186 return NULL; | |
| 187 } | |
| 137 } | 188 } |
| 138 const uint32_t infoBytesRemaining = infoBytes - 4; | |
| 139 hBuffer.free(); | |
| 140 | 189 |
| 141 // Read the second header | 190 // Read the second header |
| 191 const uint32_t infoBytesRemaining = infoBytes - 4; | |
| 142 SkAutoTDeleteArray<uint8_t> iBuffer( | 192 SkAutoTDeleteArray<uint8_t> iBuffer( |
| 143 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); | 193 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); |
| 144 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { | 194 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
| 145 SkDebugf("Error: unable to read second bitmap header.\n"); | 195 SkDebugf("Error: unable to read second bitmap header.\n"); |
| 146 return NULL; | 196 return NULL; |
| 147 } | 197 } |
| 148 | 198 |
| 149 // The number of bits used per pixel in the pixel data | 199 // The number of bits used per pixel in the pixel data |
| 150 uint16_t bitsPerPixel; | 200 uint16_t bitsPerPixel; |
| 151 | 201 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 SkDebugf("Error: second bitmap header size is invalid.\n"); | 286 SkDebugf("Error: second bitmap header size is invalid.\n"); |
| 237 return NULL; | 287 return NULL; |
| 238 } | 288 } |
| 239 | 289 |
| 240 // Check for valid dimensions from header | 290 // Check for valid dimensions from header |
| 241 RowOrder rowOrder = kBottomUp_RowOrder; | 291 RowOrder rowOrder = kBottomUp_RowOrder; |
| 242 if (height < 0) { | 292 if (height < 0) { |
| 243 height = -height; | 293 height = -height; |
| 244 rowOrder = kTopDown_RowOrder; | 294 rowOrder = kTopDown_RowOrder; |
| 245 } | 295 } |
| 296 // The height field for bmp in ico is double the actual height because they | |
| 297 // contain an XOR mask followed by an AND mask | |
| 298 if (isIco) { | |
| 299 height /= 2; | |
| 300 } | |
| 246 static const int kBmpMaxDim = 1 << 16; | 301 static const int kBmpMaxDim = 1 << 16; |
| 247 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { | 302 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { |
| 248 // TODO: Decide if we want to support really large bmps. | 303 // TODO: Decide if we want to support really large bmps. |
| 249 SkDebugf("Error: invalid bitmap dimensions.\n"); | 304 SkDebugf("Error: invalid bitmap dimensions.\n"); |
| 250 return NULL; | 305 return NULL; |
| 251 } | 306 } |
| 252 | 307 |
| 253 // Create mask struct | 308 // Create mask struct |
| 254 SkMasks::InputMasks inputMasks; | 309 SkMasks::InputMasks inputMasks; |
| 255 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); | 310 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 // TODO: Same as above. | 392 // TODO: Same as above. |
| 338 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); | 393 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); |
| 339 return NULL; | 394 return NULL; |
| 340 default: | 395 default: |
| 341 SkDebugf("Error: invalid format for bitmap decoding.\n"); | 396 SkDebugf("Error: invalid format for bitmap decoding.\n"); |
| 342 return NULL; | 397 return NULL; |
| 343 } | 398 } |
| 344 | 399 |
| 345 // Most versions of bmps should be rendered as opaque. Either they do | 400 // Most versions of bmps should be rendered as opaque. Either they do |
| 346 // not have an alpha channel, or they expect the alpha channel to be | 401 // not have an alpha channel, or they expect the alpha channel to be |
| 347 // ignored. V4+ bmp files introduce an alpha mask and allow the creator | 402 // ignored. V3+ bmp files introduce an alpha mask and allow the creator |
| 348 // of the image to use the alpha channels. However, many of these images | 403 // of the image to use the alpha channels. However, many of these images |
| 349 // leave the alpha channel blank and expect to be rendered as opaque. For | 404 // leave the alpha channel blank and expect to be rendered as opaque. This |
| 350 // this reason, we set the alpha type to kUnknown for V4+ bmps and figure | 405 // is the case for almost all V3 images, so we render these as opaque. For |
| 351 // out the alpha type during the decode. | 406 // V4+, we will use the alpha channel, and fix the image later if it turns |
| 407 // out to be fully transparent. | |
| 408 // As an exception, V3 bmp-in-ico may use an alpha mask. | |
| 352 SkAlphaType alphaType = kOpaque_SkAlphaType; | 409 SkAlphaType alphaType = kOpaque_SkAlphaType; |
| 353 if (kInfoV4_BitmapHeaderType == headerType || | 410 if ((kInfoV3_BitmapHeaderType == headerType && isIco) || |
| 411 kInfoV4_BitmapHeaderType == headerType || | |
| 354 kInfoV5_BitmapHeaderType == headerType) { | 412 kInfoV5_BitmapHeaderType == headerType) { |
| 355 // Header types are matched based on size. If the header is | 413 // Header types are matched based on size. If the header is |
| 356 // V4+, we are guaranteed to be able to read at least this size. | 414 // V3+, we are guaranteed to be able to read at least this size. |
| 357 SkASSERT(infoBytesRemaining > 52); | 415 SkASSERT(infoBytesRemaining > 52); |
| 358 inputMasks.alpha = get_int(iBuffer.get(), 48); | 416 inputMasks.alpha = get_int(iBuffer.get(), 48); |
| 359 if (inputMasks.alpha != 0) { | 417 if (inputMasks.alpha != 0) { |
| 360 alphaType = kUnpremul_SkAlphaType; | 418 alphaType = kUnpremul_SkAlphaType; |
| 361 } | 419 } |
| 362 } | 420 } |
| 363 iBuffer.free(); | 421 iBuffer.free(); |
| 364 | 422 |
| 423 // Additionally, 32 bit bmp-in-icos use the alpha channel | |
| 424 if (isIco && 32 == bitsPerPixel) { | |
| 425 alphaType = kUnpremul_SkAlphaType; | |
| 426 } | |
| 427 | |
| 365 // Check for valid bits per pixel input | 428 // Check for valid bits per pixel input |
| 366 switch (bitsPerPixel) { | 429 switch (bitsPerPixel) { |
| 367 // In addition to more standard pixel compression formats, bmp supports | 430 // In addition to more standard pixel compression formats, bmp supports |
| 368 // the use of bit masks to determine pixel components. The standard | 431 // the use of bit masks to determine pixel components. The standard |
| 369 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), | 432 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
| 370 // which does not map well to any Skia color formats. For this reason, | 433 // which does not map well to any Skia color formats. For this reason, |
| 371 // we will always enable mask mode with 16 bits per pixel. | 434 // we will always enable mask mode with 16 bits per pixel. |
| 372 case 16: | 435 case 16: |
| 373 if (kBitMask_BitmapInputFormat != inputFormat) { | 436 if (kBitMask_BitmapInputFormat != inputFormat) { |
| 374 inputMasks.red = 0x7C00; | 437 inputMasks.red = 0x7C00; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 399 | 462 |
| 400 // Check for a valid number of total bytes when in RLE mode | 463 // Check for a valid number of total bytes when in RLE mode |
| 401 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { | 464 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { |
| 402 SkDebugf("Error: RLE requires valid input size.\n"); | 465 SkDebugf("Error: RLE requires valid input size.\n"); |
| 403 return NULL; | 466 return NULL; |
| 404 } | 467 } |
| 405 const size_t RLEBytes = totalBytes - offset; | 468 const size_t RLEBytes = totalBytes - offset; |
| 406 | 469 |
| 407 // Calculate the number of bytes read so far | 470 // Calculate the number of bytes read so far |
| 408 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; | 471 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
| 409 if (offset < bytesRead) { | 472 if (!isIco && offset < bytesRead) { |
| 410 SkDebugf("Error: pixel data offset less than header size.\n"); | 473 SkDebugf("Error: pixel data offset less than header size.\n"); |
| 411 return NULL; | 474 return NULL; |
| 412 } | 475 } |
| 413 | 476 |
| 414 // Return the codec | 477 // Return the codec |
| 415 // We will use ImageInfo to store width, height, and alpha type. We will | 478 // We will use ImageInfo to store width, height, and alpha type. We will |
| 416 // set color type to kN32_SkColorType because that should be the default | 479 // set color type to kN32_SkColorType because that should be the default |
| 417 // output. | 480 // output. |
| 418 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, | 481 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, |
| 419 kN32_SkColorType, alphaType); | 482 kN32_SkColorType, alphaType); |
| 420 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, | 483 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, |
| 421 inputFormat, masks.detach(), numColors, | 484 inputFormat, masks.detach(), numColors, |
| 422 bytesPerColor, offset - bytesRead, | 485 bytesPerColor, offset - bytesRead, |
| 423 rowOrder, RLEBytes)); | 486 rowOrder, RLEBytes, isIco)); |
| 424 } | 487 } |
| 425 | 488 |
| 426 /* | 489 /* |
| 427 * | 490 * |
| 428 * Creates an instance of the decoder | 491 * Creates an instance of the decoder |
| 429 * Called only by NewFromStream | 492 * Called only by NewFromStream |
| 430 * | 493 * |
| 431 */ | 494 */ |
| 432 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, | 495 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, |
| 433 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, | 496 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, |
| 434 SkMasks* masks, uint32_t numColors, | 497 SkMasks* masks, uint32_t numColors, |
| 435 uint32_t bytesPerColor, uint32_t offset, | 498 uint32_t bytesPerColor, uint32_t offset, |
| 436 RowOrder rowOrder, size_t RLEBytes) | 499 RowOrder rowOrder, size_t RLEBytes, bool isIco) |
| 437 : INHERITED(info, stream) | 500 : INHERITED(info, stream) |
| 438 , fBitsPerPixel(bitsPerPixel) | 501 , fBitsPerPixel(bitsPerPixel) |
| 439 , fInputFormat(inputFormat) | 502 , fInputFormat(inputFormat) |
| 440 , fMasks(masks) | 503 , fMasks(masks) |
| 441 , fColorTable(NULL) | 504 , fColorTable(NULL) |
| 442 , fNumColors(numColors) | 505 , fNumColors(numColors) |
| 443 , fBytesPerColor(bytesPerColor) | 506 , fBytesPerColor(bytesPerColor) |
| 444 , fOffset(offset) | 507 , fOffset(offset) |
| 445 , fRowOrder(rowOrder) | 508 , fRowOrder(rowOrder) |
| 446 , fRLEBytes(RLEBytes) | 509 , fRLEBytes(RLEBytes) |
| 510 , fIsIco(isIco) | |
| 447 {} | 511 {} |
| 448 | 512 |
| 449 /* | 513 /* |
| 450 * | 514 * |
| 451 * Initiates the bitmap decode | 515 * Initiates the bitmap decode |
| 452 * | 516 * |
| 453 */ | 517 */ |
| 454 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, | 518 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 455 void* dst, size_t dstRowBytes, | 519 void* dst, size_t dstRowBytes, |
| 456 const Options&, | 520 const Options&, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 } | 610 } |
| 547 | 611 |
| 548 // To avoid segmentation faults on bad pixel data, fill the end of the | 612 // To avoid segmentation faults on bad pixel data, fill the end of the |
| 549 // color table with black. This is the same the behavior as the | 613 // color table with black. This is the same the behavior as the |
| 550 // chromium decoder. | 614 // chromium decoder. |
| 551 for (; i < maxColors; i++) { | 615 for (; i < maxColors; i++) { |
| 552 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); | 616 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 553 } | 617 } |
| 554 } | 618 } |
| 555 | 619 |
| 556 // Check that we have not read past the pixel array offset | 620 // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
| 557 if(fOffset < colorBytes) { | 621 // begins. Pixel data always begins immediately after the color table. |
| 558 // This may occur on OS 2.1 and other old versions where the color | 622 if (!fIsIco) { |
|
scroggo
2015/03/18 21:39:18
I wonder if it would be possible/better to split i
msarett
2015/03/20 18:35:56
It wouldn't be a clean split, but it's possible th
| |
| 559 // table defaults to max size, and the bmp tries to use a smaller color | 623 // Check that we have not read past the pixel array offset |
| 560 // table. This is invalid, and our decision is to indicate an error, | 624 if(fOffset < colorBytes) { |
| 561 // rather than try to guess the intended size of the color table. | 625 // This may occur on OS 2.1 and other old versions where the color |
| 562 SkDebugf("Error: pixel data offset less than color table size.\n"); | 626 // table defaults to max size, and the bmp tries to use a smaller |
| 563 return false; | 627 // color table. This is invalid, and our decision is to indicate |
| 564 } | 628 // an error, rather than try to guess the intended size of the |
| 629 // color table. | |
| 630 SkDebugf("Error: pixel data offset less than color table size.\n"); | |
| 631 return false; | |
| 632 } | |
| 565 | 633 |
| 566 // After reading the color table, skip to the start of the pixel array | 634 // After reading the color table, skip to the start of the pixel array |
| 567 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { | 635 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 568 SkDebugf("Error: unable to skip to image data.\n"); | 636 SkDebugf("Error: unable to skip to image data.\n"); |
| 569 return false; | 637 return false; |
| 638 } | |
| 570 } | 639 } |
| 571 | 640 |
| 572 // Set the color table and return true on success | 641 // Set the color table and return true on success |
| 573 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); | 642 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); |
| 574 return true; | 643 return true; |
| 575 } | 644 } |
| 576 | 645 |
| 577 /* | 646 /* |
| 578 * | 647 * |
| 579 * Performs the bitmap decoding for bit masks input format | 648 * Performs the bitmap decoding for bit masks input format |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 929 case 4: | 998 case 4: |
| 930 config = SkSwizzler::kIndex4; | 999 config = SkSwizzler::kIndex4; |
| 931 break; | 1000 break; |
| 932 case 8: | 1001 case 8: |
| 933 config = SkSwizzler::kIndex; | 1002 config = SkSwizzler::kIndex; |
| 934 break; | 1003 break; |
| 935 case 24: | 1004 case 24: |
| 936 config = SkSwizzler::kBGR; | 1005 config = SkSwizzler::kBGR; |
| 937 break; | 1006 break; |
| 938 case 32: | 1007 case 32: |
| 939 if (kOpaque_SkAlphaType == dstInfo.alphaType()) { | 1008 if (kOpaque_SkAlphaType == dstInfo.alphaType() && !fIsIco) { |
| 940 config = SkSwizzler::kBGRX; | 1009 config = SkSwizzler::kBGRX; |
| 941 } else { | 1010 } else { |
| 942 config = SkSwizzler::kBGRA; | 1011 config = SkSwizzler::kBGRA; |
| 943 } | 1012 } |
| 944 break; | 1013 break; |
| 945 default: | 1014 default: |
| 946 SkASSERT(false); | 1015 SkASSERT(false); |
| 947 return kInvalidInput; | 1016 return kInvalidInput; |
| 948 } | 1017 } |
| 949 | 1018 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 980 | 1049 |
| 981 // FIXME: This code exists to match the behavior in the chromium decoder | 1050 // FIXME: This code exists to match the behavior in the chromium decoder |
| 982 // and to follow the bmp specification as it relates to alpha masks. It is | 1051 // and to follow the bmp specification as it relates to alpha masks. It is |
| 983 // commented out because we have yet to discover a test image that provides | 1052 // commented out because we have yet to discover a test image that provides |
| 984 // an alpha mask and uses this decode mode. | 1053 // an alpha mask and uses this decode mode. |
| 985 | 1054 |
| 986 // Now we adjust the output image with some additional behavior that | 1055 // Now we adjust the output image with some additional behavior that |
| 987 // SkSwizzler does not support. Firstly, all bmp images that contain | 1056 // SkSwizzler does not support. Firstly, all bmp images that contain |
| 988 // alpha are masked by the alpha mask. Secondly, many fully transparent | 1057 // alpha are masked by the alpha mask. Secondly, many fully transparent |
| 989 // bmp images are intended to be opaque. Here, we make those corrections. | 1058 // bmp images are intended to be opaque. Here, we make those corrections. |
| 990 // Modifying alpha is safe because colors are stored unpremultiplied. | |
| 991 /* | 1059 /* |
| 992 SkPMColor* dstRow = (SkPMColor*) dst; | 1060 SkPMColor* dstRow = (SkPMColor*) dst; |
| 993 if (SkSwizzler::kBGRA == config) { | 1061 if (SkSwizzler::kBGRA == config) { |
| 994 for (int y = 0; y < height; y++) { | 1062 for (int y = 0; y < height; y++) { |
| 995 for (int x = 0; x < width; x++) { | 1063 for (int x = 0; x < width; x++) { |
| 996 if (transparent) { | 1064 if (transparent) { |
| 997 dstRow[x] |= 0xFF000000; | 1065 dstRow[x] |= 0xFF000000; |
| 998 } else { | 1066 } else { |
| 999 dstRow[x] &= alphaMask; | 1067 dstRow[x] &= alphaMask; |
| 1000 } | 1068 } |
| 1001 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); | 1069 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); |
| 1002 } | 1070 } |
| 1003 } | 1071 } |
| 1004 } | 1072 } |
| 1005 */ | 1073 */ |
| 1006 | 1074 |
| 1075 // Finally, apply the AND mask for bmp-in-ico images | |
| 1076 if (fIsIco) { | |
| 1077 // The AND mask is always 1 bit per pixel | |
| 1078 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1)); | |
| 1079 | |
| 1080 // Find the proper start row and delta | |
| 1081 int delta; | |
| 1082 if (kTopDown_RowOrder == fRowOrder) { | |
| 1083 dst = (SkPMColor*) dst; | |
| 1084 delta = dstRowBytes; | |
| 1085 } else { | |
| 1086 dst = (SkPMColor*) SkTAddOffset<void>(dst, | |
| 1087 (height-1) * dstRowBytes); | |
| 1088 delta = -dstRowBytes; | |
| 1089 } | |
| 1090 | |
| 1091 SkPMColor* dstRow32 = (SkPMColor*) dst; | |
| 1092 uint16_t* dstRow16 = (uint16_t*) dst; | |
| 1093 for (int y = 0; y < height; y++) { | |
| 1094 // The srcBuffer will at least be large enough | |
| 1095 if (stream()->read(srcBuffer.get(), rowBytes) != rowBytes) { | |
| 1096 SkDebugf("Warning: incomplete AND mask for bmp-in-ico.\n"); | |
| 1097 return kIncompleteInput; | |
| 1098 } | |
| 1099 | |
| 1100 // If the AND bit is set, the pixel is transparent, and if the bit | |
| 1101 // is zero, the pixel is unchanged. | |
| 1102 switch (dstInfo.colorType()) { | |
| 1103 case kN32_SkColorType: { | |
| 1104 for (int x = 0; x < width; x++) { | |
| 1105 uint32_t shift = 7 - (x % 8); | |
| 1106 uint32_t alphaBit = | |
| 1107 (srcBuffer.get()[x / 8] >> shift) & 0x1; | |
|
scroggo
2015/03/18 21:39:18
nit: Avoiding divides in a loop is generally a goo
msarett
2015/03/20 18:35:56
That's clever! Done.
| |
| 1108 dstRow32[x] &= alphaBit - 1; | |
| 1109 } | |
| 1110 dstRow32 = SkTAddOffset<SkPMColor>(dstRow32, delta); | |
| 1111 break; | |
| 1112 } | |
| 1113 case kRGB_565_SkColorType: { | |
|
msarett
2015/03/18 20:04:23
Forgot to mention part 2:
This case can be removed
| |
| 1114 for (int x = 0; x < width; x++) { | |
| 1115 uint16_t shift = 7 - (x % 8); | |
| 1116 uint16_t alphaBit = | |
| 1117 (srcBuffer.get()[x / 8] >> shift) & 0x1; | |
| 1118 // Since there is no transparency in 565, we set the | |
| 1119 // pixel to white when the AND bit is set. | |
| 1120 dstRow16[x] |= ~(alphaBit - 1); | |
| 1121 } | |
| 1122 dstRow16 = SkTAddOffset<uint16_t>(dstRow16, delta); | |
| 1123 break; | |
| 1124 } | |
| 1125 default: | |
| 1126 // This case should not be reached. We should catch an | |
| 1127 // invalid color type when we check that the conversion is | |
| 1128 // possible. | |
| 1129 SkASSERT(false); | |
| 1130 break; | |
| 1131 } | |
| 1132 } | |
| 1133 } | |
| 1134 | |
| 1007 // Finished decoding the entire image | 1135 // Finished decoding the entire image |
| 1008 return kSuccess; | 1136 return kSuccess; |
| 1009 } | 1137 } |
| OLD | NEW |