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 11 matching lines...) Expand all Loading... |
22 if (dst.profileType() != src.profileType()) { | 22 if (dst.profileType() != src.profileType()) { |
23 return false; | 23 return false; |
24 } | 24 } |
25 | 25 |
26 // Check for supported color and alpha types | 26 // Check for supported color and alpha types |
27 switch (dst.colorType()) { | 27 switch (dst.colorType()) { |
28 case kN32_SkColorType: | 28 case kN32_SkColorType: |
29 return src.alphaType() == dst.alphaType() || | 29 return src.alphaType() == dst.alphaType() || |
30 (kPremul_SkAlphaType == dst.alphaType() && | 30 (kPremul_SkAlphaType == dst.alphaType() && |
31 kUnpremul_SkAlphaType == src.alphaType()); | 31 kUnpremul_SkAlphaType == src.alphaType()); |
32 case kRGB_565_SkColorType: | |
33 return src.alphaType() == dst.alphaType() && | |
34 kOpaque_SkAlphaType == dst.alphaType(); | |
35 default: | 32 default: |
36 return false; | 33 return false; |
37 } | 34 } |
38 } | 35 } |
39 | 36 |
40 /* | 37 /* |
41 * | 38 * |
42 * Defines the version and type of the second bitmap header | 39 * Defines the version and type of the second bitmap header |
43 * | 40 * |
44 */ | 41 */ |
(...skipping 36 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 | 78 // TODO: ICO files may contain a BMP and need to use this decoder |
82 const char bmpSig[] = { 'B', 'M' }; | 79 const char bmpSig[] = { 'B', 'M' }; |
83 char buffer[sizeof(bmpSig)]; | 80 char buffer[sizeof(bmpSig)]; |
84 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && | 81 return stream->read(buffer, sizeof(bmpSig)) == sizeof(bmpSig) && |
85 !memcmp(buffer, bmpSig, sizeof(bmpSig)); | 82 !memcmp(buffer, bmpSig, sizeof(bmpSig)); |
86 } | 83 } |
87 | 84 |
88 /* | 85 /* |
89 * | 86 * |
90 * Assumes IsBmp was called and returned true | 87 * Assumes IsBmp was called and returned true |
91 * Creates a bitmap decoder | 88 * Creates a bmp decoder |
92 * Reads enough of the stream to determine the image format | 89 * Reads enough of the stream to determine the image format |
93 * | 90 * |
94 */ | 91 */ |
95 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { | 92 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream) { |
| 93 return SkBmpCodec::NewFromStream(stream, false); |
| 94 } |
| 95 |
| 96 /* |
| 97 * |
| 98 * Creates a bmp decoder for a bmp embedded in ico |
| 99 * Reads enough of the stream to determine the image format |
| 100 * |
| 101 */ |
| 102 SkCodec* SkBmpCodec::NewFromIco(SkStream* stream) { |
| 103 return SkBmpCodec::NewFromStream(stream, true); |
| 104 } |
| 105 |
| 106 /* |
| 107 * |
| 108 * Creates a bmp decoder |
| 109 * Reads enough of the stream to determine the image format |
| 110 * |
| 111 */ |
| 112 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool isIco) { |
96 // Header size constants | 113 // Header size constants |
97 static const uint32_t kBmpHeaderBytes = 14; | 114 static const uint32_t kBmpHeaderBytes = 14; |
98 static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; | 115 static const uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4; |
99 static const uint32_t kBmpOS2V1Bytes = 12; | 116 static const uint32_t kBmpOS2V1Bytes = 12; |
100 static const uint32_t kBmpOS2V2Bytes = 64; | 117 static const uint32_t kBmpOS2V2Bytes = 64; |
101 static const uint32_t kBmpInfoBaseBytes = 16; | 118 static const uint32_t kBmpInfoBaseBytes = 16; |
102 static const uint32_t kBmpInfoV1Bytes = 40; | 119 static const uint32_t kBmpInfoV1Bytes = 40; |
103 static const uint32_t kBmpInfoV2Bytes = 52; | 120 static const uint32_t kBmpInfoV2Bytes = 52; |
104 static const uint32_t kBmpInfoV3Bytes = 56; | 121 static const uint32_t kBmpInfoV3Bytes = 56; |
105 static const uint32_t kBmpInfoV4Bytes = 108; | 122 static const uint32_t kBmpInfoV4Bytes = 108; |
106 static const uint32_t kBmpInfoV5Bytes = 124; | 123 static const uint32_t kBmpInfoV5Bytes = 124; |
107 static const uint32_t kBmpMaskBytes = 12; | 124 static const uint32_t kBmpMaskBytes = 12; |
108 | 125 |
109 // Read the first header and the size of the second header | 126 // The total bytes in the bmp file |
110 SkAutoTDeleteArray<uint8_t> hBuffer( | 127 // We only need to use this value for RLE decoding, so we will only |
111 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); | 128 // check that it is valid in the RLE case. |
112 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != | 129 uint32_t totalBytes; |
113 kBmpHeaderBytesPlusFour) { | 130 // The offset from the start of the file where the pixel data begins |
114 SkDebugf("Error: unable to read first bitmap header.\n"); | 131 uint32_t offset; |
115 return NULL; | 132 // The size of the second (info) header in bytes |
| 133 uint32_t infoBytes; |
| 134 |
| 135 // Bmps embedded in Icos skip the first Bmp header |
| 136 if (!isIco) { |
| 137 // Read the first header and the size of the second header |
| 138 SkAutoTDeleteArray<uint8_t> hBuffer( |
| 139 SkNEW_ARRAY(uint8_t, kBmpHeaderBytesPlusFour)); |
| 140 if (stream->read(hBuffer.get(), kBmpHeaderBytesPlusFour) != |
| 141 kBmpHeaderBytesPlusFour) { |
| 142 SkDebugf("Error: unable to read first bitmap header.\n"); |
| 143 return NULL; |
| 144 } |
| 145 |
| 146 totalBytes = get_int(hBuffer.get(), 2); |
| 147 offset = get_int(hBuffer.get(), 10); |
| 148 if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) { |
| 149 SkDebugf("Error: invalid starting location for pixel data\n"); |
| 150 return NULL; |
| 151 } |
| 152 |
| 153 // The size of the second (info) header in bytes |
| 154 // The size is the first field of the second header, so we have already |
| 155 // read the first four infoBytes. |
| 156 infoBytes = get_int(hBuffer.get(), 14); |
| 157 if (infoBytes < kBmpOS2V1Bytes) { |
| 158 SkDebugf("Error: invalid second header size.\n"); |
| 159 return NULL; |
| 160 } |
| 161 } else { |
| 162 // This value is only used by RLE compression. Bmp in Ico files do not |
| 163 // use RLE. If the compression field is incorrectly signaled as RLE, |
| 164 // we will catch this and signal an error below. |
| 165 totalBytes = 0; |
| 166 |
| 167 // Bmps in Ico cannot specify an offset. We will always assume that |
| 168 // pixel data begins immediately after the color table. This value |
| 169 // will be corrected below. |
| 170 offset = 0; |
| 171 |
| 172 // Read the size of the second header |
| 173 SkAutoTDeleteArray<uint8_t> hBuffer( |
| 174 SkNEW_ARRAY(uint8_t, 4)); |
| 175 if (stream->read(hBuffer.get(), 4) != 4) { |
| 176 SkDebugf("Error: unable to read size of second bitmap header.\n"); |
| 177 return NULL; |
| 178 } |
| 179 infoBytes = get_int(hBuffer.get(), 0); |
| 180 if (infoBytes < kBmpOS2V1Bytes) { |
| 181 SkDebugf("Error: invalid second header size.\n"); |
| 182 return NULL; |
| 183 } |
116 } | 184 } |
117 | 185 |
118 // The total bytes in the bmp file | 186 // 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; | 187 const uint32_t infoBytesRemaining = infoBytes - 4; |
139 hBuffer.free(); | |
140 | 188 |
141 // Read the second header | 189 // Read the second header |
142 SkAutoTDeleteArray<uint8_t> iBuffer( | 190 SkAutoTDeleteArray<uint8_t> iBuffer( |
143 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); | 191 SkNEW_ARRAY(uint8_t, infoBytesRemaining)); |
144 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { | 192 if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) { |
145 SkDebugf("Error: unable to read second bitmap header.\n"); | 193 SkDebugf("Error: unable to read second bitmap header.\n"); |
146 return NULL; | 194 return NULL; |
147 } | 195 } |
148 | 196 |
149 // The number of bits used per pixel in the pixel data | 197 // The number of bits used per pixel in the pixel data |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 SkDebugf("Error: second bitmap header size is invalid.\n"); | 284 SkDebugf("Error: second bitmap header size is invalid.\n"); |
237 return NULL; | 285 return NULL; |
238 } | 286 } |
239 | 287 |
240 // Check for valid dimensions from header | 288 // Check for valid dimensions from header |
241 RowOrder rowOrder = kBottomUp_RowOrder; | 289 RowOrder rowOrder = kBottomUp_RowOrder; |
242 if (height < 0) { | 290 if (height < 0) { |
243 height = -height; | 291 height = -height; |
244 rowOrder = kTopDown_RowOrder; | 292 rowOrder = kTopDown_RowOrder; |
245 } | 293 } |
| 294 // The height field for bmp in ico is double the actual height because they |
| 295 // contain an XOR mask followed by an AND mask |
| 296 if (isIco) { |
| 297 height /= 2; |
| 298 } |
246 static const int kBmpMaxDim = 1 << 16; | 299 static const int kBmpMaxDim = 1 << 16; |
247 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { | 300 if (width < 0 || width >= kBmpMaxDim || height >= kBmpMaxDim) { |
248 // TODO: Decide if we want to support really large bmps. | 301 // TODO: Decide if we want to support really large bmps. |
249 SkDebugf("Error: invalid bitmap dimensions.\n"); | 302 SkDebugf("Error: invalid bitmap dimensions.\n"); |
250 return NULL; | 303 return NULL; |
251 } | 304 } |
252 | 305 |
253 // Create mask struct | 306 // Create mask struct |
254 SkMasks::InputMasks inputMasks; | 307 SkMasks::InputMasks inputMasks; |
255 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); | 308 memset(&inputMasks, 0, sizeof(SkMasks::InputMasks)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 // TODO: Same as above. | 390 // TODO: Same as above. |
338 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); | 391 SkDebugf("Error: CMYK not supported for bitmap decoding.\n"); |
339 return NULL; | 392 return NULL; |
340 default: | 393 default: |
341 SkDebugf("Error: invalid format for bitmap decoding.\n"); | 394 SkDebugf("Error: invalid format for bitmap decoding.\n"); |
342 return NULL; | 395 return NULL; |
343 } | 396 } |
344 | 397 |
345 // Most versions of bmps should be rendered as opaque. Either they do | 398 // 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 | 399 // 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 | 400 // 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 | 401 // 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 | 402 // 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 | 403 // is the case for almost all V3 images, so we render these as opaque. For |
351 // out the alpha type during the decode. | 404 // V4+, we will use the alpha channel, and fix the image later if it turns |
| 405 // out to be fully transparent. |
| 406 // As an exception, V3 bmp-in-ico may use an alpha mask. |
352 SkAlphaType alphaType = kOpaque_SkAlphaType; | 407 SkAlphaType alphaType = kOpaque_SkAlphaType; |
353 if (kInfoV4_BitmapHeaderType == headerType || | 408 if ((kInfoV3_BitmapHeaderType == headerType && isIco) || |
| 409 kInfoV4_BitmapHeaderType == headerType || |
354 kInfoV5_BitmapHeaderType == headerType) { | 410 kInfoV5_BitmapHeaderType == headerType) { |
355 // Header types are matched based on size. If the header is | 411 // 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. | 412 // V3+, we are guaranteed to be able to read at least this size. |
357 SkASSERT(infoBytesRemaining > 52); | 413 SkASSERT(infoBytesRemaining > 52); |
358 inputMasks.alpha = get_int(iBuffer.get(), 48); | 414 inputMasks.alpha = get_int(iBuffer.get(), 48); |
359 if (inputMasks.alpha != 0) { | 415 if (inputMasks.alpha != 0) { |
360 alphaType = kUnpremul_SkAlphaType; | 416 alphaType = kUnpremul_SkAlphaType; |
361 } | 417 } |
362 } | 418 } |
363 iBuffer.free(); | 419 iBuffer.free(); |
364 | 420 |
| 421 // Additionally, 32 bit bmp-in-icos use the alpha channel |
| 422 if (isIco && 32 == bitsPerPixel) { |
| 423 alphaType = kUnpremul_SkAlphaType; |
| 424 } |
| 425 |
365 // Check for valid bits per pixel input | 426 // Check for valid bits per pixel input |
366 switch (bitsPerPixel) { | 427 switch (bitsPerPixel) { |
367 // In addition to more standard pixel compression formats, bmp supports | 428 // In addition to more standard pixel compression formats, bmp supports |
368 // the use of bit masks to determine pixel components. The standard | 429 // the use of bit masks to determine pixel components. The standard |
369 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), | 430 // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB), |
370 // which does not map well to any Skia color formats. For this reason, | 431 // 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. | 432 // we will always enable mask mode with 16 bits per pixel. |
372 case 16: | 433 case 16: |
373 if (kBitMask_BitmapInputFormat != inputFormat) { | 434 if (kBitMask_BitmapInputFormat != inputFormat) { |
374 inputMasks.red = 0x7C00; | 435 inputMasks.red = 0x7C00; |
(...skipping 24 matching lines...) Expand all Loading... |
399 | 460 |
400 // Check for a valid number of total bytes when in RLE mode | 461 // Check for a valid number of total bytes when in RLE mode |
401 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { | 462 if (totalBytes <= offset && kRLE_BitmapInputFormat == inputFormat) { |
402 SkDebugf("Error: RLE requires valid input size.\n"); | 463 SkDebugf("Error: RLE requires valid input size.\n"); |
403 return NULL; | 464 return NULL; |
404 } | 465 } |
405 const size_t RLEBytes = totalBytes - offset; | 466 const size_t RLEBytes = totalBytes - offset; |
406 | 467 |
407 // Calculate the number of bytes read so far | 468 // Calculate the number of bytes read so far |
408 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; | 469 const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes; |
409 if (offset < bytesRead) { | 470 if (!isIco && offset < bytesRead) { |
410 SkDebugf("Error: pixel data offset less than header size.\n"); | 471 SkDebugf("Error: pixel data offset less than header size.\n"); |
411 return NULL; | 472 return NULL; |
412 } | 473 } |
413 | 474 |
414 // Return the codec | 475 // Return the codec |
415 // We will use ImageInfo to store width, height, and alpha type. We will | 476 // 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 | 477 // set color type to kN32_SkColorType because that should be the default |
417 // output. | 478 // output. |
418 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, | 479 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, |
419 kN32_SkColorType, alphaType); | 480 kN32_SkColorType, alphaType); |
420 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, | 481 return SkNEW_ARGS(SkBmpCodec, (imageInfo, stream, bitsPerPixel, |
421 inputFormat, masks.detach(), numColors, | 482 inputFormat, masks.detach(), numColors, |
422 bytesPerColor, offset - bytesRead, | 483 bytesPerColor, offset - bytesRead, |
423 rowOrder, RLEBytes)); | 484 rowOrder, RLEBytes, isIco)); |
424 } | 485 } |
425 | 486 |
426 /* | 487 /* |
427 * | 488 * |
428 * Creates an instance of the decoder | 489 * Creates an instance of the decoder |
429 * Called only by NewFromStream | 490 * Called only by NewFromStream |
430 * | 491 * |
431 */ | 492 */ |
432 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, | 493 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, |
433 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, | 494 uint16_t bitsPerPixel, BitmapInputFormat inputFormat, |
434 SkMasks* masks, uint32_t numColors, | 495 SkMasks* masks, uint32_t numColors, |
435 uint32_t bytesPerColor, uint32_t offset, | 496 uint32_t bytesPerColor, uint32_t offset, |
436 RowOrder rowOrder, size_t RLEBytes) | 497 RowOrder rowOrder, size_t RLEBytes, bool isIco) |
437 : INHERITED(info, stream) | 498 : INHERITED(info, stream) |
438 , fBitsPerPixel(bitsPerPixel) | 499 , fBitsPerPixel(bitsPerPixel) |
439 , fInputFormat(inputFormat) | 500 , fInputFormat(inputFormat) |
440 , fMasks(masks) | 501 , fMasks(masks) |
441 , fColorTable(NULL) | 502 , fColorTable(NULL) |
442 , fNumColors(numColors) | 503 , fNumColors(numColors) |
443 , fBytesPerColor(bytesPerColor) | 504 , fBytesPerColor(bytesPerColor) |
444 , fOffset(offset) | 505 , fOffset(offset) |
445 , fRowOrder(rowOrder) | 506 , fRowOrder(rowOrder) |
446 , fRLEBytes(RLEBytes) | 507 , fRLEBytes(RLEBytes) |
| 508 , fIsIco(isIco) |
| 509 |
447 {} | 510 {} |
448 | 511 |
449 /* | 512 /* |
450 * | 513 * |
451 * Initiates the bitmap decode | 514 * Initiates the bitmap decode |
452 * | 515 * |
453 */ | 516 */ |
454 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, | 517 SkCodec::Result SkBmpCodec::onGetPixels(const SkImageInfo& dstInfo, |
455 void* dst, size_t dstRowBytes, | 518 void* dst, size_t dstRowBytes, |
456 const Options&, | 519 const Options&, |
457 SkPMColor*, int*) { | 520 SkPMColor*, int*) { |
458 // Check for proper input and output formats | 521 // Check for proper input and output formats |
459 if (!this->rewindIfNeeded()) { | 522 if (!this->rewindIfNeeded()) { |
460 return kCouldNotRewind; | 523 return kCouldNotRewind; |
461 } | 524 } |
462 if (dstInfo.dimensions() != this->getOriginalInfo().dimensions()) { | 525 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
463 SkDebugf("Error: scaling not supported.\n"); | 526 SkDebugf("Error: scaling not supported.\n"); |
464 return kInvalidScale; | 527 return kInvalidScale; |
465 } | 528 } |
466 if (!conversion_possible(dstInfo, this->getOriginalInfo())) { | 529 if (!conversion_possible(dstInfo, this->getInfo())) { |
467 SkDebugf("Error: cannot convert input type to output type.\n"); | 530 SkDebugf("Error: cannot convert input type to output type.\n"); |
468 return kInvalidConversion; | 531 return kInvalidConversion; |
469 } | 532 } |
470 | 533 |
471 // Create the color table if necessary and prepare the stream for decode | 534 // Create the color table if necessary and prepare the stream for decode |
472 if (!createColorTable(dstInfo.alphaType())) { | 535 if (!createColorTable(dstInfo.alphaType())) { |
473 SkDebugf("Error: could not create color table.\n"); | 536 SkDebugf("Error: could not create color table.\n"); |
474 return kInvalidInput; | 537 return kInvalidInput; |
475 } | 538 } |
476 | 539 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 } | 609 } |
547 | 610 |
548 // To avoid segmentation faults on bad pixel data, fill the end of the | 611 // 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 | 612 // color table with black. This is the same the behavior as the |
550 // chromium decoder. | 613 // chromium decoder. |
551 for (; i < maxColors; i++) { | 614 for (; i < maxColors; i++) { |
552 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); | 615 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
553 } | 616 } |
554 } | 617 } |
555 | 618 |
556 // Check that we have not read past the pixel array offset | 619 // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
557 if(fOffset < colorBytes) { | 620 // 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 | 621 if (!fIsIco) { |
559 // table defaults to max size, and the bmp tries to use a smaller color | 622 // Check that we have not read past the pixel array offset |
560 // table. This is invalid, and our decision is to indicate an error, | 623 if(fOffset < colorBytes) { |
561 // rather than try to guess the intended size of the color table. | 624 // 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"); | 625 // table defaults to max size, and the bmp tries to use a smaller |
563 return false; | 626 // color table. This is invalid, and our decision is to indicate |
564 } | 627 // an error, rather than try to guess the intended size of the |
| 628 // color table. |
| 629 SkDebugf("Error: pixel data offset less than color table size.\n"); |
| 630 return false; |
| 631 } |
565 | 632 |
566 // After reading the color table, skip to the start of the pixel array | 633 // After reading the color table, skip to the start of the pixel array |
567 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { | 634 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
568 SkDebugf("Error: unable to skip to image data.\n"); | 635 SkDebugf("Error: unable to skip to image data.\n"); |
569 return false; | 636 return false; |
| 637 } |
570 } | 638 } |
571 | 639 |
572 // Set the color table and return true on success | 640 // Set the color table and return true on success |
573 if (maxColors > 0) { | 641 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); |
574 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors))); | |
575 } | |
576 return true; | 642 return true; |
577 } | 643 } |
578 | 644 |
579 /* | 645 /* |
580 * | 646 * |
581 * Performs the bitmap decoding for bit masks input format | 647 * Performs the bitmap decoding for bit masks input format |
582 * | 648 * |
583 */ | 649 */ |
584 SkCodec::Result SkBmpCodec::decodeMask(const SkImageInfo& dstInfo, | 650 SkCodec::Result SkBmpCodec::decodeMask(const SkImageInfo& dstInfo, |
585 void* dst, size_t dstRowBytes) { | 651 void* dst, size_t dstRowBytes) { |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 | 1048 |
983 // FIXME: This code exists to match the behavior in the chromium decoder | 1049 // 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 | 1050 // 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 | 1051 // commented out because we have yet to discover a test image that provides |
986 // an alpha mask and uses this decode mode. | 1052 // an alpha mask and uses this decode mode. |
987 | 1053 |
988 // Now we adjust the output image with some additional behavior that | 1054 // Now we adjust the output image with some additional behavior that |
989 // SkSwizzler does not support. Firstly, all bmp images that contain | 1055 // SkSwizzler does not support. Firstly, all bmp images that contain |
990 // alpha are masked by the alpha mask. Secondly, many fully transparent | 1056 // alpha are masked by the alpha mask. Secondly, many fully transparent |
991 // bmp images are intended to be opaque. Here, we make those corrections. | 1057 // bmp images are intended to be opaque. Here, we make those corrections. |
992 // Modifying alpha is safe because colors are stored unpremultiplied. | |
993 /* | 1058 /* |
994 SkPMColor* dstRow = (SkPMColor*) dst; | 1059 SkPMColor* dstRow = (SkPMColor*) dst; |
995 if (SkSwizzler::kBGRA == config) { | 1060 if (SkSwizzler::kBGRA == config) { |
996 for (int y = 0; y < height; y++) { | 1061 for (int y = 0; y < height; y++) { |
997 for (int x = 0; x < width; x++) { | 1062 for (int x = 0; x < width; x++) { |
998 if (transparent) { | 1063 if (transparent) { |
999 dstRow[x] |= 0xFF000000; | 1064 dstRow[x] |= 0xFF000000; |
1000 } else { | 1065 } else { |
1001 dstRow[x] &= alphaMask; | 1066 dstRow[x] &= alphaMask; |
1002 } | 1067 } |
1003 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); | 1068 dstRow = SkTAddOffset<SkPMColor>(dstRow, dstRowBytes); |
1004 } | 1069 } |
1005 } | 1070 } |
1006 } | 1071 } |
1007 */ | 1072 */ |
1008 | 1073 |
| 1074 // Finally, apply the AND mask for bmp-in-ico images |
| 1075 if (fIsIco) { |
| 1076 // The AND mask is always 1 bit per pixel |
| 1077 const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1)); |
| 1078 |
| 1079 SkPMColor* dstPtr = (SkPMColor*) dst; |
| 1080 for (int y = 0; y < height; y++) { |
| 1081 // The srcBuffer will at least be large enough |
| 1082 if (stream()->read(srcBuffer.get(), rowBytes) != rowBytes) { |
| 1083 SkDebugf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
| 1084 return kIncompleteInput; |
| 1085 } |
| 1086 |
| 1087 int row; |
| 1088 if (kBottomUp_RowOrder == fRowOrder) { |
| 1089 row = height - y - 1; |
| 1090 } else { |
| 1091 row = y; |
| 1092 } |
| 1093 |
| 1094 SkPMColor* dstRow = |
| 1095 SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
| 1096 |
| 1097 for (int x = 0; x < width; x++) { |
| 1098 int quotient; |
| 1099 int modulus; |
| 1100 SkTDivMod(x, 8, "ient, &modulus); |
| 1101 uint32_t shift = 7 - modulus; |
| 1102 uint32_t alphaBit = |
| 1103 (srcBuffer.get()[quotient] >> shift) & 0x1; |
| 1104 dstRow[x] &= alphaBit - 1; |
| 1105 } |
| 1106 } |
| 1107 } |
| 1108 |
1009 // Finished decoding the entire image | 1109 // Finished decoding the entire image |
1010 return kSuccess; | 1110 return kSuccess; |
1011 } | 1111 } |
OLD | NEW |