Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: src/codec/SkCodec_libbmp.cpp

Issue 1011343003: Enabling ico decoding with use of png and bmp decoders (Closed) Base URL: https://skia.googlesource.com/skia.git@swizzle
Patch Set: Using appropriate Sk data types for array of codecs, removing getOriginalInfo Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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, 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 129 // The total bytes in the bmp file
110 SkAutoTDeleteArray<uint8_t> hBuffer( 130 // We only need to use this value for RLE decoding, so we will only
111 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); 131 // check that it is valid in the RLE case.
112 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != 132 uint32_t totalBytes;
113 kBmpHeaderBytesPlusFour) { 133 // The offset from the start of the file where the pixel data begins
114 SkDebugf("Error: unable to read first bitmap header.\n"); 134 uint32_t offset;
115 return NULL; 135 // The size of the second (info) header in bytes
136 uint32_t infoBytes;
137
138 // Bmps embedded in Icos skip the first Bmp header
139 if (!isIco) {
140 // Read the first header and the size of the second header
141 SkAutoTDeleteArray<uint8_t> hBuffer(
142 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour));
143 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) !=
144 kBmpHeaderBytesPlusFour) {
145 SkDebugf("Error: unable to read first bitmap header.\n");
146 return NULL;
147 }
148
149 totalBytes = get_int(hBuffer.get(), 2);
150 offset = get_int(hBuffer.get(), 10);
151 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) {
152 SkDebugf("Error: invalid starting location for pixel data\n");
153 return NULL;
154 }
155
156 // The size of the second (info) header in bytes
157 // The size is the first field of the second header, so we have already
158 // read the first four infoBytes.
159 infoBytes = get_int(hBuffer.get(), 14);
160 if (infoBytes < kBmpOS2V1Bytes) {
161 SkDebugf("Error: invalid second header size.\n");
162 return NULL;
163 }
164 } else {
165 // This value is only used by RLE compression. Bmp in Ico files do not
166 // use RLE. If the compression field is incorrectly signaled as RLE,
167 // we will catch this and signal an error below.
168 totalBytes = 0;
169
170 // Bmps in Ico cannot specify an offset. We will always assume that
171 // pixel data begins immediately after the color table. This value
172 // will be corrected below.
173 offset = 0;
174
175 // Read the size of the second header
176 SkAutoTDeleteArray<uint8_t> hBuffer(
177 SkNEW_ARRAY(uint8_t, 4));
178 if (stream->read(hBuffer.get(), 4) != 4) {
179 SkDebugf("Error: unable to read size of second bitmap header.\n");
180 return NULL;
181 }
182 infoBytes = get_int(hBuffer.get(), 0);
183 if (infoBytes < kBmpOS2V1Bytes) {
184 SkDebugf("Error: invalid second header size.\n");
185 return NULL;
186 }
116 } 187 }
117 188
118 // The total bytes in the bmp file 189 // We already read the first four bytes of the info header to get the size
119 // We only need to use this value for RLE decoding, so we will only check
120 // that it is valid in the RLE case.
121 const uint32_t totalBytes = get_int(hBuffer.get(), 2);
122
123 // The offset from the start of the file where the pixel data begins
124 const uint32_t offset = get_int(hBuffer.get(), 10);
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
131 // The size is the first field of the second header, so we have already
132 // read the first four infoBytes.
133 const uint32_t infoBytes = get_int(hBuffer.get(), 14);
134 if (infoBytes < kBmpOS2V1Bytes) {
135 SkDebugf("Error: invalid second header size.\n");
136 return NULL;
137 }
138 const uint32_t infoBytesRemaining = infoBytes - 4; 190 const uint32_t infoBytesRemaining = infoBytes - 4;
139 hBuffer.free();
140 191
141 // Read the second header 192 // Read the second header
142 SkAutoTDeleteArray<uint8_t> iBuffer( 193 SkAutoTDeleteArray<uint8_t> iBuffer(
143 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); 194 SkNEW_ARRAY(uint8_t, infoBytesRemaining));
144 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { 195 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) {
145 SkDebugf("Error: unable to read second bitmap header.\n"); 196 SkDebugf("Error: unable to read second bitmap header.\n");
146 return NULL; 197 return NULL;
147 } 198 }
148 199
149 // The number of bits used per pixel in the pixel data 200 // The number of bits used per pixel in the pixel data
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 SkDebugf("Error: second bitmap header size is invalid.\n"); 287 SkDebugf("Error: second bitmap header size is invalid.\n");
237 return NULL; 288 return NULL;
238 } 289 }
239 290
240 // Check for valid dimensions from header 291 // Check for valid dimensions from header
241 RowOrder rowOrder = kBottomUp_RowOrder; 292 RowOrder rowOrder = kBottomUp_RowOrder;
242 if (height < 0) { 293 if (height < 0) {
243 height = -height; 294 height = -height;
244 rowOrder = kTopDown_RowOrder; 295 rowOrder = kTopDown_RowOrder;
245 } 296 }
297 // The height field for bmp in ico is double the actual height because they
298 // contain an XOR mask followed by an AND mask
299 if (isIco) {
300 height /= 2;
301 }
246 static const int kBmpMaxDim = 1 << 16; 302 static const int kBmpMaxDim = 1 << 16;
247 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { 303 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) {
248 // TODO: Decide if we want to support really large bmps. 304 // TODO: Decide if we want to support really large bmps.
249 SkDebugf("Error: invalid bitmap dimensions.\n"); 305 SkDebugf("Error: invalid bitmap dimensions.\n");
250 return NULL; 306 return NULL;
251 } 307 }
252 308
253 // Create mask struct 309 // Create mask struct
254 SkMasks::InputMasks inputMasks; 310 SkMasks::InputMasks inputMasks;
255 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); 311 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // TODO: Same as above. 393 // TODO: Same as above.
338 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); 394 SkDebugf("Error: CMYK not supported for bitmap decoding.\n");
339 return NULL; 395 return NULL;
340 default: 396 default:
341 SkDebugf("Error: invalid format for bitmap decoding.\n"); 397 SkDebugf("Error: invalid format for bitmap decoding.\n");
342 return NULL; 398 return NULL;
343 } 399 }
344 400
345 // Most versions of bmps should be rendered as opaque. Either they do 401 // 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 402 // 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 403 // 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 404 // 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 405 // 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 406 // is the case for almost all V3 images, so we render these as opaque. For
351 // out the alpha type during the decode. 407 // V4+, we will use the alpha channel, and fix the image later if it turns
408 // out to be fully transparent.
409 // As an exception, V3 bmp-in-ico may use an alpha mask.
352 SkAlphaType alphaType = kOpaque_SkAlphaType; 410 SkAlphaType alphaType = kOpaque_SkAlphaType;
353 if (kInfoV4_BitmapHeaderType == headerType || 411 if ((kInfoV3_BitmapHeaderType == headerType && isIco) ||
412 kInfoV4_BitmapHeaderType == headerType ||
354 kInfoV5_BitmapHeaderType == headerType) { 413 kInfoV5_BitmapHeaderType == headerType) {
355 // Header types are matched based on size. If the header is 414 // 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. 415 // V3+, we are guaranteed to be able to read at least this size.
357 SkASSERT(infoBytesRemaining > 52); 416 SkASSERT(infoBytesRemaining > 52);
358 inputMasks.alpha = get_int(iBuffer.get(), 48); 417 inputMasks.alpha = get_int(iBuffer.get(), 48);
359 if (inputMasks.alpha != 0) { 418 if (inputMasks.alpha != 0) {
360 alphaType = kUnpremul_SkAlphaType; 419 alphaType = kUnpremul_SkAlphaType;
361 } 420 }
362 } 421 }
363 iBuffer.free(); 422 iBuffer.free();
364 423
424 // Additionally, 32 bit bmp-in-icos use the alpha channel
425 if (isIco && 32 == bitsPerPixel) {
426 alphaType = kUnpremul_SkAlphaType;
427 }
428
365 // Check for valid bits per pixel input 429 // Check for valid bits per pixel input
366 switch (bitsPerPixel) { 430 switch (bitsPerPixel) {
367 // In addition to more standard pixel compression formats, bmp supports 431 // In addition to more standard pixel compression formats, bmp supports
368 // the use of bit masks to determine pixel components. The standard 432 // the use of bit masks to determine pixel components. The standard
369 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), 433 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB),
370 // which does not map well to any Skia color formats. For this reason, 434 // 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. 435 // we will always enable mask mode with 16 bits per pixel.
372 case 16: 436 case 16:
373 if (kBitMask_BitmapInputFormat != inputFormat) { 437 if (kBitMask_BitmapInputFormat != inputFormat) {
374 inputMasks.red = 0x7C00; 438 inputMasks.red = 0x7C00;
(...skipping 24 matching lines...) Expand all
399 463
400 // Check for a valid number of total bytes when in RLE mode 464 // Check for a valid number of total bytes when in RLE mode
401 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { 465 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) {
402 SkDebugf("Error: RLE requires valid input size.\n"); 466 SkDebugf("Error: RLE requires valid input size.\n");
403 return NULL; 467 return NULL;
404 } 468 }
405 const size_t RLEBytes = totalBytes - offset; 469 const size_t RLEBytes = totalBytes - offset;
406 470
407 // Calculate the number of bytes read so far 471 // Calculate the number of bytes read so far
408 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; 472 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes;
409 if (offset < bytesRead) { 473 if (!isIco && offset < bytesRead) {
410 SkDebugf("Error: pixel data offset less than header size.\n"); 474 SkDebugf("Error: pixel data offset less than header size.\n");
411 return NULL; 475 return NULL;
412 } 476 }
413 477
414 // Return the codec 478 // Return the codec
415 // We will use ImageInfo to store width, height, and alpha type. We will 479 // 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 480 // set color type to kN32_SkColorType because that should be the default
417 // output. 481 // output.
418 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, 482 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height,
419 kN32_SkColorType, alphaType); 483 kN32_SkColorType, alphaType);
420 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, 484 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel,
421 inputFormat, masks.detach(), numColors, 485 inputFormat, masks.detach(), numColors,
422 bytesPerColor, offset - bytesRead, 486 bytesPerColor, offset - bytesRead,
423 rowOrder, RLEBytes)); 487 rowOrder, RLEBytes, isIco));
424 } 488 }
425 489
426 /* 490 /*
427 * 491 *
428 * Creates an instance of the decoder 492 * Creates an instance of the decoder
429 * Called only by NewFromStream 493 * Called only by NewFromStream
430 * 494 *
431 */ 495 */
432 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, 496 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream,
433 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, 497 uint16_t bitsPerPixel, BitmapInputFormat inputFormat,
434 SkMasks* masks, uint32_t numColors, 498 SkMasks* masks, uint32_t numColors,
435 uint32_t bytesPerColor, uint32_t offset, 499 uint32_t bytesPerColor, uint32_t offset,
436 RowOrder rowOrder, size_t RLEBytes) 500 RowOrder rowOrder, size_t RLEBytes, bool isIco)
437 : INHERITED(info, stream) 501 : INHERITED(info, stream)
438 , fBitsPerPixel(bitsPerPixel) 502 , fBitsPerPixel(bitsPerPixel)
439 , fInputFormat(inputFormat) 503 , fInputFormat(inputFormat)
440 , fMasks(masks) 504 , fMasks(masks)
441 , fColorTable(NULL) 505 , fColorTable(NULL)
442 , fNumColors(numColors) 506 , fNumColors(numColors)
443 , fBytesPerColor(bytesPerColor) 507 , fBytesPerColor(bytesPerColor)
444 , fOffset(offset) 508 , fOffset(offset)
445 , fRowOrder(rowOrder) 509 , fRowOrder(rowOrder)
446 , fRLEBytes(RLEBytes) 510 , fRLEBytes(RLEBytes)
511 , fIsIco(isIco)
512
447 {} 513 {}
448 514
449 /* 515 /*
450 * 516 *
451 * Initiates the bitmap decode 517 * Initiates the bitmap decode
452 * 518 *
453 */ 519 */
454 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, 520 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo,
455 void* dst, size_t dstRowBytes, 521 void* dst, size_t dstRowBytes,
456 const Options&, 522 const Options&,
457 SkPMColor*, int*) { 523 SkPMColor*, int*) {
458 // Check for proper input and output formats 524 // Check for proper input and output formats
459 if (!this->rewindIfNeeded()) { 525 if (!this->rewindIfNeeded()) {
460 return kCouldNotRewind; 526 return kCouldNotRewind;
461 } 527 }
462 if (dstInfo.dimensions() != this->getOriginalInfo().dimensions()) { 528 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
463 SkDebugf("Error: scaling not supported.\n"); 529 SkDebugf("Error: scaling not supported.\n");
464 return kInvalidScale; 530 return kInvalidScale;
465 } 531 }
466 if (!conversion_possible(dstInfo, this->getOriginalInfo())) { 532 if (!conversion_possible(dstInfo, this->getInfo())) {
467 SkDebugf("Error: cannot convert input type to output type.\n"); 533 SkDebugf("Error: cannot convert input type to output type.\n");
468 return kInvalidConversion; 534 return kInvalidConversion;
469 } 535 }
470 536
471 // Create the color table if necessary and prepare the stream for decode 537 // Create the color table if necessary and prepare the stream for decode
472 if (!createColorTable(dstInfo.alphaType())) { 538 if (!createColorTable(dstInfo.alphaType())) {
473 SkDebugf("Error: could not create color table.\n"); 539 SkDebugf("Error: could not create color table.\n");
474 return kInvalidInput; 540 return kInvalidInput;
475 } 541 }
476 542
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 } 612 }
547 613
548 // To avoid segmentation faults on bad pixel data, fill the end of the 614 // 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 615 // color table with black. This is the same the behavior as the
550 // chromium decoder. 616 // chromium decoder.
551 for (; i < maxColors; i++) { 617 for (; i < maxColors; i++) {
552 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); 618 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
553 } 619 }
554 } 620 }
555 621
556 // Check that we have not read past the pixel array offset 622 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
557 if(fOffset < colorBytes) { 623 // 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 624 if (!fIsIco) {
559 // table defaults to max size, and the bmp tries to use a smaller color 625 // Check that we have not read past the pixel array offset
560 // table. This is invalid, and our decision is to indicate an error, 626 if(fOffset < colorBytes) {
561 // rather than try to guess the intended size of the color table. 627 // 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"); 628 // table defaults to max size, and the bmp tries to use a smaller
563 return false; 629 // color table. This is invalid, and our decision is to indicate
564 } 630 // an error, rather than try to guess the intended size of the
631 // color table.
632 SkDebugf("Error: pixel data offset less than color table size.\n");
633 return false;
634 }
565 635
566 // After reading the color table, skip to the start of the pixel array 636 // After reading the color table, skip to the start of the pixel array
567 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { 637 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
568 SkDebugf("Error: unable to skip to image data.\n"); 638 SkDebugf("Error: unable to skip to image data.\n");
569 return false; 639 return false;
640 }
570 } 641 }
571 642
572 // Set the color table and return true on success 643 // Set the color table and return true on success
573 if (maxColors > 0) { 644 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors)));
574 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors)));
575 }
576 return true; 645 return true;
577 } 646 }
578 647
579 /* 648 /*
580 * 649 *
581 * Performs the bitmap decoding for bit masks input format 650 * Performs the bitmap decoding for bit masks input format
582 * 651 *
583 */ 652 */
584 SkCodec::Result SkBmpCodec::decodeMask(const SkImageInfo& dstInfo, 653 SkCodec::Result SkBmpCodec::decodeMask(const SkImageInfo& dstInfo,
585 void* dst, size_t dstRowBytes) { 654 void* dst, size_t dstRowBytes) {
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 case 4: 1000 case 4:
932 config = SkSwizzler::kIndex4; 1001 config = SkSwizzler::kIndex4;
933 break; 1002 break;
934 case 8: 1003 case 8:
935 config = SkSwizzler::kIndex; 1004 config = SkSwizzler::kIndex;
936 break; 1005 break;
937 case 24: 1006 case 24:
938 config = SkSwizzler::kBGR; 1007 config = SkSwizzler::kBGR;
939 break; 1008 break;
940 case 32: 1009 case 32:
941 if (kOpaque_SkAlphaType == dstInfo.alphaType()) { 1010 if (kOpaque_SkAlphaType == dstInfo.alphaType() && !fIsIco) {
scroggo 2015/03/23 20:34:50 If the caller requested opaque, but the image actu
msarett 2015/03/24 13:08:37 Thanks, I made a mistake when I merged.
942 config = SkSwizzler::kBGRX; 1011 config = SkSwizzler::kBGRX;
943 } else { 1012 } else {
944 config = SkSwizzler::kBGRA; 1013 config = SkSwizzler::kBGRA;
945 } 1014 }
946 break; 1015 break;
947 default: 1016 default:
948 SkASSERT(false); 1017 SkASSERT(false);
949 return kInvalidInput; 1018 return kInvalidInput;
950 } 1019 }
951 1020
(...skipping 30 matching lines...) Expand all
982 1051
983 // FIXME: This code exists to match the behavior in the chromium decoder 1052 // FIXME: This code exists to match the behavior in the chromium decoder
984 // and to follow the bmp specification as it relates to alpha masks. It is 1053 // and to follow the bmp specification as it relates to alpha masks. It is
985 // commented out because we have yet to discover a test image that provides 1054 // commented out because we have yet to discover a test image that provides
986 // an alpha mask and uses this decode mode. 1055 // an alpha mask and uses this decode mode.
987 1056
988 // Now we adjust the output image with some additional behavior that 1057 // Now we adjust the output image with some additional behavior that
989 // SkSwizzler does not support. Firstly, all bmp images that contain 1058 // SkSwizzler does not support. Firstly, all bmp images that contain
990 // alpha are masked by the alpha mask. Secondly, many fully transparent 1059 // alpha are masked by the alpha mask. Secondly, many fully transparent
991 // bmp images are intended to be opaque. Here, we make those corrections. 1060 // bmp images are intended to be opaque. Here, we make those corrections.
992 // Modifying alpha is safe because colors are stored unpremultiplied.
993 /* 1061 /*
994 SkPMColor* dstRow = (SkPMColor*) dst; 1062 SkPMColor* dstRow = (SkPMColor*) dst;
995 if (SkSwizzler::kBGRA == config) { 1063 if (SkSwizzler::kBGRA == config) {
996 for (int y = 0; y < height; y++) { 1064 for (int y = 0; y < height; y++) {
997 for (int x = 0; x < width; x++) { 1065 for (int x = 0; x < width; x++) {
998 if (transparent) { 1066 if (transparent) {
999 dstRow[x] |= 0xFF000000; 1067 dstRow[x] |= 0xFF000000;
1000 } else { 1068 } else {
1001 dstRow[x] &= alphaMask; 1069 dstRow[x] &= alphaMask;
1002 } 1070 }
1003 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); 1071 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes);
1004 } 1072 }
1005 } 1073 }
1006 } 1074 }
1007 */ 1075 */
1008 1076
1077 // Finally, apply the AND mask for bmp-in-ico images
1078 if (fIsIco) {
1079 // The AND mask is always 1 bit per pixel
1080 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1));
1081
1082 // Find the proper start row and delta
1083 int delta;
1084 if (kTopDown_RowOrder == fRowOrder) {
1085 dst = (SkPMColor*) dst;
1086 delta = dstRowBytes;
1087 } else {
1088 dst = (SkPMColor*) SkTAddOffset<void>(dst,
1089 (height-1) * dstRowBytes);
1090 delta = -dstRowBytes;
1091 }
1092
1093 SkPMColor* dstRow = (SkPMColor*) dst;
1094 for (int y = 0; y < height; y++) {
1095 // The srcBuffer will at least be large enough
1096 if (stream()->read(srcBuffer.get(), rowBytes) != rowBytes) {
1097 SkDebugf("Warning: incomplete AND mask for bmp-in-ico.\n");
1098 return kIncompleteInput;
1099 }
1100
1101 for (int x = 0; x < width; x++) {
1102 int quotient;
1103 int modulus;
1104 SkTDivMod(x, 8, &quotient, &modulus);
1105 uint32_t shift = 7 - modulus;
1106 uint32_t alphaBit =
1107 (srcBuffer.get()[quotient] >> shift) & 0x1;
1108 dstRow[x] &= alphaBit - 1;
1109 }
1110
1111 dstRow = SkTAddOffset<SkPMColor>(dstRow, delta);
scroggo 2015/03/23 20:34:51 I commented on this before, and I don't remember i
msarett 2015/03/24 13:08:37 You're right my fault. I swear I fixed this, but
1112
1113 }
1114 }
1115
1009 // Finished decoding the entire image 1116 // Finished decoding the entire image
1010 return kSuccess; 1117 return kSuccess;
1011 } 1118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698