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

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: Provide the user with an option of which ico to decode 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, 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 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)
447 {} 512 {}
448 513
449 /* 514 /*
450 * 515 *
451 * Initiates the bitmap decode 516 * Initiates the bitmap decode
452 * 517 *
453 */ 518 */
454 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, 519 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo,
455 void* dst, size_t dstRowBytes, 520 void* dst, size_t dstRowBytes,
456 const Options&, 521 const Options&,
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 } 611 }
547 612
548 // To avoid segmentation faults on bad pixel data, fill the end of the 613 // 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 614 // color table with black. This is the same the behavior as the
550 // chromium decoder. 615 // chromium decoder.
551 for (; i < maxColors; i++) { 616 for (; i < maxColors; i++) {
552 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); 617 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
553 } 618 }
554 } 619 }
555 620
556 // Check that we have not read past the pixel array offset 621 // Bmp-in-Ico files do not use an offset to indicate where the pixel data
557 if(fOffset < colorBytes) { 622 // 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 623 if (!fIsIco) {
559 // table defaults to max size, and the bmp tries to use a smaller color 624 // Check that we have not read past the pixel array offset
560 // table. This is invalid, and our decision is to indicate an error, 625 if(fOffset < colorBytes) {
561 // rather than try to guess the intended size of the color table. 626 // 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"); 627 // table defaults to max size, and the bmp tries to use a smaller
563 return false; 628 // color table. This is invalid, and our decision is to indicate
564 } 629 // an error, rather than try to guess the intended size of the
630 // color table.
631 SkDebugf("Error: pixel data offset less than color table size.\n");
632 return false;
633 }
565 634
566 // After reading the color table, skip to the start of the pixel array 635 // After reading the color table, skip to the start of the pixel array
567 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { 636 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
568 SkDebugf("Error: unable to skip to image data.\n"); 637 SkDebugf("Error: unable to skip to image data.\n");
569 return false; 638 return false;
639 }
570 } 640 }
571 641
572 // Set the color table and return true on success 642 // Set the color table and return true on success
573 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); 643 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors)));
574 return true; 644 return true;
575 } 645 }
576 646
577 /* 647 /*
578 * 648 *
579 * Performs the bitmap decoding for bit masks input format 649 * Performs the bitmap decoding for bit masks input format
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 case 4: 999 case 4:
930 config = SkSwizzler::kIndex4; 1000 config = SkSwizzler::kIndex4;
931 break; 1001 break;
932 case 8: 1002 case 8:
933 config = SkSwizzler::kIndex; 1003 config = SkSwizzler::kIndex;
934 break; 1004 break;
935 case 24: 1005 case 24:
936 config = SkSwizzler::kBGR; 1006 config = SkSwizzler::kBGR;
937 break; 1007 break;
938 case 32: 1008 case 32:
939 if (kOpaque_SkAlphaType == dstInfo.alphaType()) { 1009 if (kOpaque_SkAlphaType == dstInfo.alphaType() && !fIsIco) {
940 config = SkSwizzler::kBGRX; 1010 config = SkSwizzler::kBGRX;
941 } else { 1011 } else {
942 config = SkSwizzler::kBGRA; 1012 config = SkSwizzler::kBGRA;
943 } 1013 }
944 break; 1014 break;
945 default: 1015 default:
946 SkASSERT(false); 1016 SkASSERT(false);
947 return kInvalidInput; 1017 return kInvalidInput;
948 } 1018 }
949 1019
(...skipping 30 matching lines...) Expand all
980 1050
981 // FIXME: This code exists to match the behavior in the chromium decoder 1051 // 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 1052 // 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 1053 // commented out because we have yet to discover a test image that provides
984 // an alpha mask and uses this decode mode. 1054 // an alpha mask and uses this decode mode.
985 1055
986 // Now we adjust the output image with some additional behavior that 1056 // Now we adjust the output image with some additional behavior that
987 // SkSwizzler does not support. Firstly, all bmp images that contain 1057 // SkSwizzler does not support. Firstly, all bmp images that contain
988 // alpha are masked by the alpha mask. Secondly, many fully transparent 1058 // alpha are masked by the alpha mask. Secondly, many fully transparent
989 // bmp images are intended to be opaque. Here, we make those corrections. 1059 // bmp images are intended to be opaque. Here, we make those corrections.
990 // Modifying alpha is safe because colors are stored unpremultiplied.
991 /* 1060 /*
992 SkPMColor* dstRow = (SkPMColor*) dst; 1061 SkPMColor* dstRow = (SkPMColor*) dst;
993 if (SkSwizzler::kBGRA == config) { 1062 if (SkSwizzler::kBGRA == config) {
994 for (int y = 0; y < height; y++) { 1063 for (int y = 0; y < height; y++) {
995 for (int x = 0; x < width; x++) { 1064 for (int x = 0; x < width; x++) {
996 if (transparent) { 1065 if (transparent) {
997 dstRow[x] |= 0xFF000000; 1066 dstRow[x] |= 0xFF000000;
998 } else { 1067 } else {
999 dstRow[x] &= alphaMask; 1068 dstRow[x] &= alphaMask;
1000 } 1069 }
1001 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); 1070 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes);
1002 } 1071 }
1003 } 1072 }
1004 } 1073 }
1005 */ 1074 */
1006 1075
1076 // Finally, apply the AND mask for bmp-in-ico images
1077 if (fIsIco) {
1078 // The AND mask is always 1 bit per pixel
1079 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1));
1080
1081 // Find the proper start row and delta
1082 int delta;
1083 if (kTopDown_RowOrder == fRowOrder) {
1084 dst = (SkPMColor*) dst;
1085 delta = dstRowBytes;
1086 } else {
1087 dst = (SkPMColor*) SkTAddOffset<void>(dst,
1088 (height-1) * dstRowBytes);
1089 delta = -dstRowBytes;
1090 }
1091
1092 SkPMColor* dstRow = (SkPMColor*) 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 for (int x = 0; x < width; x++) {
1101 int quotient;
1102 int modulus;
1103 SkTDivMod(x, 8, &quotient, &modulus);
1104 uint32_t shift = 7 - modulus;
1105 uint32_t alphaBit =
1106 (srcBuffer.get()[quotient] >> shift) & 0x1;
1107 dstRow[x] &= alphaBit - 1;
1108 }
1109
1110 dstRow = SkTAddOffset<SkPMColor>(dstRow, delta);
1111
1112 }
1113 }
1114
1007 // Finished decoding the entire image 1115 // Finished decoding the entire image
1008 return kSuccess; 1116 return kSuccess;
1009 } 1117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698