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

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

Issue 1820073002: Add SkEncodedInfo to report properties of encoded image data (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Order of param eval bug Created 4 years, 8 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
« no previous file with comments | « src/codec/SkBmpCodec.h ('k') | src/codec/SkBmpMaskCodec.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkBmpCodec.h" 8 #include "SkBmpCodec.h"
9 #include "SkBmpMaskCodec.h" 9 #include "SkBmpMaskCodec.h"
10 #include "SkBmpRLECodec.h" 10 #include "SkBmpRLECodec.h"
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 // Seems like we can just assume that the offset is zero and try to decode? 408 // Seems like we can just assume that the offset is zero and try to decode?
409 // Maybe we don't want to try to decode corrupt images? 409 // Maybe we don't want to try to decode corrupt images?
410 SkCodecPrintf("Error: pixel data offset less than header size.\n"); 410 SkCodecPrintf("Error: pixel data offset less than header size.\n");
411 return false; 411 return false;
412 } 412 }
413 413
414 414
415 415
416 switch (inputFormat) { 416 switch (inputFormat) {
417 case kStandard_BmpInputFormat: { 417 case kStandard_BmpInputFormat: {
418 // BMPs-in-ICOs often contain an alpha mask after the image, which 418 // BMPs are generally opaque, however BMPs-in-ICOs may contain
419 // means we cannot guarantee that an image is opaque, even if the 419 // a transparency mask after the image. Therefore, we mark the
420 // embedded bmp is opaque. 420 // alpha as kBinary if the BMP is contained in an ICO.
421 // We use |isOpaque| to indicate if the BMP itself is opaque, but 421 // We use |isOpaque| to indicate if the BMP itself is opaque.
422 // still need to recommend kUnpremul when it is contained in an ICO. 422 SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
423 SkColorType colorType = kN32_SkColorType; 423 SkEncodedInfo::kOpaque_Alpha;
424 SkAlphaType alphaType = inIco ? kUnpremul_SkAlphaType : kOpaque_SkAl phaType;
425 bool isOpaque = true; 424 bool isOpaque = true;
425
426 SkEncodedInfo::Color color;
427 uint8_t bitsPerComponent;
426 switch (bitsPerPixel) { 428 switch (bitsPerPixel) {
427 // Palette formats 429 // Palette formats
428 case 1: 430 case 1:
429 case 2: 431 case 2:
430 case 4: 432 case 4:
431 case 8: 433 case 8:
432 // We cannot recommend a palette color type for ICOs because they 434 // In the case of ICO, kBGRA is actually the closest match,
433 // may contain a transparency mask. 435 // since we will need to apply a transparency mask.
434 if (!inIco) { 436 if (inIco) {
435 colorType = kIndex_8_SkColorType; 437 color = SkEncodedInfo::kBGRA_Color;
438 bitsPerComponent = 8;
439 } else {
440 color = SkEncodedInfo::kPalette_Color;
441 bitsPerComponent = (uint8_t) bitsPerPixel;
436 } 442 }
437 break; 443 break;
438 case 24: 444 case 24:
445 color = SkEncodedInfo::kBGR_Color;
446 bitsPerComponent = 8;
447 break;
439 case 32: 448 case 32:
440 // 32-bit BMP-in-ICOs actually use the alpha channel in plac e of a 449 // 32-bit BMP-in-ICOs actually use the alpha channel in plac e of a
441 // transparency mask. 450 // transparency mask.
442 if (inIco) { 451 if (inIco) {
443 isOpaque = false; 452 isOpaque = false;
453 alpha = SkEncodedInfo::kUnpremul_Alpha;
454 color = SkEncodedInfo::kBGRA_Color;
455 } else {
456 color = SkEncodedInfo::kBGRX_Color;
444 } 457 }
458 bitsPerComponent = 8;
445 break; 459 break;
446 default: 460 default:
447 SkCodecPrintf("Error: invalid input value for bits per pixel .\n"); 461 SkCodecPrintf("Error: invalid input value for bits per pixel .\n");
448 return false; 462 return false;
449 } 463 }
450 464
451 if (codecOut) { 465 if (codecOut) {
452 // We require streams to have a memory base for Bmp-in-Ico decod es. 466 // We require streams to have a memory base for Bmp-in-Ico decod es.
453 SkASSERT(!inIco || nullptr != stream->getMemoryBase()); 467 SkASSERT(!inIco || nullptr != stream->getMemoryBase());
454 468
455 // Set the image info and create a codec. 469 // Set the image info and create a codec.
456 const SkImageInfo imageInfo = SkImageInfo::Make(width, height, c olorType, 470 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bit sPerComponent);
457 alphaType); 471 *codecOut = new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel,
458 *codecOut = new SkBmpStandardCodec(imageInfo, stream, bitsPerPix el, numColors, 472 numColors, bytesPerColor, offset - bytesRead, rowOrder, isOpaque, inIco);
459 bytesPerColor, offset - bytesRead, rowOrder, isOpaque, i nIco);
460
461 } 473 }
462 return true; 474 return true;
463 } 475 }
464 476
465 case kBitMask_BmpInputFormat: { 477 case kBitMask_BmpInputFormat: {
466 // Bmp-in-Ico must be standard mode 478 // Bmp-in-Ico must be standard mode
467 if (inIco) { 479 if (inIco) {
468 SkCodecPrintf("Error: Icos may not use bit mask format.\n"); 480 SkCodecPrintf("Error: Icos may not use bit mask format.\n");
469 return false; 481 return false;
470 } 482 }
(...skipping 17 matching lines...) Expand all
488 } 500 }
489 501
490 if (codecOut) { 502 if (codecOut) {
491 // Check that input bit masks are valid and create the masks obj ect 503 // Check that input bit masks are valid and create the masks obj ect
492 SkAutoTDelete<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bi tsPerPixel)); 504 SkAutoTDelete<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bi tsPerPixel));
493 if (nullptr == masks) { 505 if (nullptr == masks) {
494 SkCodecPrintf("Error: invalid input masks.\n"); 506 SkCodecPrintf("Error: invalid input masks.\n");
495 return false; 507 return false;
496 } 508 }
497 509
498 // Set the image info 510 // Masked bmps are not a great fit for SkEncodedInfo, since they have
499 SkAlphaType alphaType = masks->getAlphaMask() ? kUnpremul_SkAlph aType : 511 // arbitrary component orderings and bits per component. Here w e choose
500 kOpaque_SkAlphaType; 512 // somewhat reasonable values - it's ok that we don't match exac tly
501 const SkImageInfo imageInfo = SkImageInfo::Make(width, height, k N32_SkColorType, 513 // because SkBmpMaskCodec has its own mask swizzler anyway.
502 alphaType); 514 SkEncodedInfo::Color color;
503 *codecOut = new SkBmpMaskCodec(imageInfo, stream, bitsPerPixel, masks.release(), 515 SkEncodedInfo::Alpha alpha;
504 rowOrder); 516 if (masks->getAlphaMask()) {
517 color = SkEncodedInfo::kBGRA_Color;
518 alpha = SkEncodedInfo::kUnpremul_Alpha;
519 } else {
520 color = SkEncodedInfo::kBGR_Color;
521 alpha = SkEncodedInfo::kOpaque_Alpha;
522 }
523 const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
524 *codecOut = new SkBmpMaskCodec(width, height, info, stream, bits PerPixel,
525 masks.release(), rowOrder);
505 } 526 }
506 return true; 527 return true;
507 } 528 }
508 529
509 case kRLE_BmpInputFormat: { 530 case kRLE_BmpInputFormat: {
510 // We should not reach this point without a valid value of bitsPerPi xel. 531 // We should not reach this point without a valid value of bitsPerPi xel.
511 SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixe l); 532 SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixe l);
512 533
513 // Check for a valid number of total bytes when in RLE mode 534 // Check for a valid number of total bytes when in RLE mode
514 if (totalBytes <= offset) { 535 if (totalBytes <= offset) {
515 SkCodecPrintf("Error: RLE requires valid input size.\n"); 536 SkCodecPrintf("Error: RLE requires valid input size.\n");
516 return false; 537 return false;
517 } 538 }
518 const size_t RLEBytes = totalBytes - offset; 539 const size_t RLEBytes = totalBytes - offset;
519 540
520 // Bmp-in-Ico must be standard mode 541 // Bmp-in-Ico must be standard mode
521 // When inIco is true, this line cannot be reached, since we 542 // When inIco is true, this line cannot be reached, since we
522 // require that RLE Bmps have a valid number of totalBytes, and 543 // require that RLE Bmps have a valid number of totalBytes, and
523 // Icos skip the header that contains totalBytes. 544 // Icos skip the header that contains totalBytes.
524 SkASSERT(!inIco); 545 SkASSERT(!inIco);
525 546
526 if (codecOut) { 547 if (codecOut) {
527 // RLE inputs may skip pixels, leaving them as transparent. Thi s 548 // RLE inputs may skip pixels, leaving them as transparent. Thi s
528 // is uncommon, but we cannot be certain that an RLE bmp will be 549 // is uncommon, but we cannot be certain that an RLE bmp will be
529 // opaque. 550 // opaque or that we will be able to represent it with a palette .
530 const SkImageInfo imageInfo = SkImageInfo::Make(width, height, k N32_SkColorType, 551 // For that reason, we always indicate that we are kBGRA.
531 kUnpremul_SkAlphaType); 552 const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kB GRA_Color,
532 *codecOut = new SkBmpRLECodec(imageInfo, stream, bitsPerPixel, n umColors, 553 SkEncodedInfo::kBinary_Alpha, 8);
554 *codecOut = new SkBmpRLECodec(width, height, info, stream, bitsP erPixel, numColors,
533 bytesPerColor, offset - bytesRead, rowOrder, RLEBytes); 555 bytesPerColor, offset - bytesRead, rowOrder, RLEBytes);
534 } 556 }
535 return true; 557 return true;
536 } 558 }
537 default: 559 default:
538 SkASSERT(false); 560 SkASSERT(false);
539 return false; 561 return false;
540 } 562 }
541 } 563 }
542 564
543 /* 565 /*
544 * Creates a bmp decoder 566 * Creates a bmp decoder
545 * Reads enough of the stream to determine the image format 567 * Reads enough of the stream to determine the image format
546 */ 568 */
547 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool inIco) { 569 SkCodec* SkBmpCodec::NewFromStream(SkStream* stream, bool inIco) {
548 SkAutoTDelete<SkStream> streamDeleter(stream); 570 SkAutoTDelete<SkStream> streamDeleter(stream);
549 SkCodec* codec = nullptr; 571 SkCodec* codec = nullptr;
550 if (ReadHeader(stream, inIco, &codec)) { 572 if (ReadHeader(stream, inIco, &codec)) {
551 // codec has taken ownership of stream, so we do not need to 573 // codec has taken ownership of stream, so we do not need to
552 // delete it. 574 // delete it.
553 SkASSERT(codec); 575 SkASSERT(codec);
554 streamDeleter.release(); 576 streamDeleter.release();
555 return codec; 577 return codec;
556 } 578 }
557 return nullptr; 579 return nullptr;
558 } 580 }
559 581
560 SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream, 582 SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStrea m* stream,
561 uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder) 583 uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
562 : INHERITED(info, stream) 584 : INHERITED(width, height, info, stream)
563 , fBitsPerPixel(bitsPerPixel) 585 , fBitsPerPixel(bitsPerPixel)
564 , fRowOrder(rowOrder) 586 , fRowOrder(rowOrder)
565 , fSrcRowBytes(SkAlign4(compute_row_bytes(info.width(), fBitsPerPixel))) 587 , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel)))
566 {} 588 {}
567 589
568 bool SkBmpCodec::onRewind() { 590 bool SkBmpCodec::onRewind() {
569 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr); 591 return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr);
570 } 592 }
571 593
572 int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const { 594 int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const {
573 if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) { 595 if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) {
574 return y; 596 return y;
575 } 597 }
(...skipping 20 matching lines...) Expand all
596 } 618 }
597 619
598 bool SkBmpCodec::skipRows(int count) { 620 bool SkBmpCodec::skipRows(int count) {
599 const size_t bytesToSkip = count * fSrcRowBytes; 621 const size_t bytesToSkip = count * fSrcRowBytes;
600 return this->stream()->skip(bytesToSkip) == bytesToSkip; 622 return this->stream()->skip(bytesToSkip) == bytesToSkip;
601 } 623 }
602 624
603 bool SkBmpCodec::onSkipScanlines(int count) { 625 bool SkBmpCodec::onSkipScanlines(int count) {
604 return this->skipRows(count); 626 return this->skipRows(count);
605 } 627 }
OLDNEW
« no previous file with comments | « src/codec/SkBmpCodec.h ('k') | src/codec/SkBmpMaskCodec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698