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_libpng.h" | 8 #include "SkCodec_libpng.h" |
9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 public: | 603 public: |
604 SkPngScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec) | 604 SkPngScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec) |
605 : INHERITED(dstInfo) | 605 : INHERITED(dstInfo) |
606 , fCodec(codec) | 606 , fCodec(codec) |
607 , fHasAlpha(false) | 607 , fHasAlpha(false) |
608 { | 608 { |
609 fStorage.reset(dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC
onfig)); | 609 fStorage.reset(dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC
onfig)); |
610 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | 610 fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
611 } | 611 } |
612 | 612 |
613 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte
s) override { | 613 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri
de { |
614 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { | 614 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { |
615 SkCodecPrintf("setjmp long jump!\n"); | 615 SkCodecPrintf("setjmp long jump!\n"); |
616 return SkImageGenerator::kInvalidInput; | 616 return SkCodec::kInvalidInput; |
617 } | 617 } |
618 | 618 |
619 for (int i = 0; i < count; i++) { | 619 for (int i = 0; i < count; i++) { |
620 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); | 620 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); |
621 fCodec->fSwizzler->setDstRow(dst); | 621 fCodec->fSwizzler->setDstRow(dst); |
622 fHasAlpha |= !SkSwizzler::IsOpaque(fCodec->fSwizzler->next(fSrcRow))
; | 622 fHasAlpha |= !SkSwizzler::IsOpaque(fCodec->fSwizzler->next(fSrcRow))
; |
623 dst = SkTAddOffset<void>(dst, rowBytes); | 623 dst = SkTAddOffset<void>(dst, rowBytes); |
624 } | 624 } |
625 return SkImageGenerator::kSuccess; | 625 return SkCodec::kSuccess; |
626 } | 626 } |
627 | 627 |
628 SkImageGenerator::Result onSkipScanlines(int count) override { | 628 SkCodec::Result onSkipScanlines(int count) override { |
629 // FIXME: Could we use the return value of setjmp to specify the type of | 629 // FIXME: Could we use the return value of setjmp to specify the type of |
630 // error? | 630 // error? |
631 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { | 631 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { |
632 SkCodecPrintf("setjmp long jump!\n"); | 632 SkCodecPrintf("setjmp long jump!\n"); |
633 return SkImageGenerator::kInvalidInput; | 633 return SkCodec::kInvalidInput; |
634 } | 634 } |
635 //there is a potential tradeoff of memory vs speed created by putting th
is in a loop. | 635 //there is a potential tradeoff of memory vs speed created by putting th
is in a loop. |
636 //calling png_read_rows in a loop is insignificantly slower than calling
it once with count | 636 //calling png_read_rows in a loop is insignificantly slower than calling
it once with count |
637 //as png_read_rows has it's own loop which calls png_read_row count time
s. | 637 //as png_read_rows has it's own loop which calls png_read_row count time
s. |
638 for (int i = 0; i < count; i++) { | 638 for (int i = 0; i < count; i++) { |
639 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); | 639 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); |
640 } | 640 } |
641 return SkImageGenerator::kSuccess; | 641 return SkCodec::kSuccess; |
642 } | 642 } |
643 | 643 |
644 bool onReallyHasAlpha() const override { return fHasAlpha; } | 644 bool onReallyHasAlpha() const override { return fHasAlpha; } |
645 | 645 |
646 private: | 646 private: |
647 SkPngCodec* fCodec; // Unowned. | 647 SkPngCodec* fCodec; // Unowned. |
648 bool fHasAlpha; | 648 bool fHasAlpha; |
649 SkAutoMalloc fStorage; | 649 SkAutoMalloc fStorage; |
650 uint8_t* fSrcRow; | 650 uint8_t* fSrcRow; |
651 | 651 |
652 typedef SkScanlineDecoder INHERITED; | 652 typedef SkScanlineDecoder INHERITED; |
653 }; | 653 }; |
654 | 654 |
655 | 655 |
656 class SkPngInterlacedScanlineDecoder : public SkScanlineDecoder { | 656 class SkPngInterlacedScanlineDecoder : public SkScanlineDecoder { |
657 public: | 657 public: |
658 SkPngInterlacedScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec
) | 658 SkPngInterlacedScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec
) |
659 : INHERITED(dstInfo) | 659 : INHERITED(dstInfo) |
660 , fCodec(codec) | 660 , fCodec(codec) |
661 , fHasAlpha(false) | 661 , fHasAlpha(false) |
662 , fCurrentRow(0) | 662 , fCurrentRow(0) |
663 , fHeight(dstInfo.height()) | 663 , fHeight(dstInfo.height()) |
664 , fRewindNeeded(false) | 664 , fRewindNeeded(false) |
665 { | 665 { |
666 fSrcRowBytes = dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC
onfig); | 666 fSrcRowBytes = dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC
onfig); |
667 fGarbageRow.reset(fSrcRowBytes); | 667 fGarbageRow.reset(fSrcRowBytes); |
668 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get()); | 668 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get()); |
669 } | 669 } |
670 | 670 |
671 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t dstRowB
ytes) override { | 671 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove
rride { |
672 //rewind stream if have previously called onGetScanlines, | 672 //rewind stream if have previously called onGetScanlines, |
673 //since we need entire progressive image to get scanlines | 673 //since we need entire progressive image to get scanlines |
674 if (fRewindNeeded) { | 674 if (fRewindNeeded) { |
675 if(false == fCodec->handleRewind()) { | 675 if(false == fCodec->handleRewind()) { |
676 return SkImageGenerator::kCouldNotRewind; | 676 return SkCodec::kCouldNotRewind; |
677 } | 677 } |
678 } else { | 678 } else { |
679 fRewindNeeded = true; | 679 fRewindNeeded = true; |
680 } | 680 } |
681 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { | 681 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { |
682 SkCodecPrintf("setjmp long jump!\n"); | 682 SkCodecPrintf("setjmp long jump!\n"); |
683 return SkImageGenerator::kInvalidInput; | 683 return SkCodec::kInvalidInput; |
684 } | 684 } |
685 const int number_passes = png_set_interlace_handling(fCodec->fPng_ptr); | 685 const int number_passes = png_set_interlace_handling(fCodec->fPng_ptr); |
686 SkAutoMalloc storage(count * fSrcRowBytes); | 686 SkAutoMalloc storage(count * fSrcRowBytes); |
687 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); | 687 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); |
688 uint8_t* srcRow; | 688 uint8_t* srcRow; |
689 for (int i = 0; i < number_passes; i++) { | 689 for (int i = 0; i < number_passes; i++) { |
690 //read rows we planned to skip into garbage row | 690 //read rows we planned to skip into garbage row |
691 for (int y = 0; y < fCurrentRow; y++){ | 691 for (int y = 0; y < fCurrentRow; y++){ |
692 png_read_rows(fCodec->fPng_ptr, &fGarbageRowPtr, png_bytepp_NULL
, 1); | 692 png_read_rows(fCodec->fPng_ptr, &fGarbageRowPtr, png_bytepp_NULL
, 1); |
693 } | 693 } |
(...skipping 10 matching lines...) Expand all Loading... |
704 } | 704 } |
705 //swizzle the rows we care about | 705 //swizzle the rows we care about |
706 srcRow = storagePtr; | 706 srcRow = storagePtr; |
707 for (int y = 0; y < count; y++) { | 707 for (int y = 0; y < count; y++) { |
708 fCodec->fSwizzler->setDstRow(dst); | 708 fCodec->fSwizzler->setDstRow(dst); |
709 fHasAlpha |= !SkSwizzler::IsOpaque(fCodec->fSwizzler->next(srcRow)); | 709 fHasAlpha |= !SkSwizzler::IsOpaque(fCodec->fSwizzler->next(srcRow)); |
710 dst = SkTAddOffset<void>(dst, dstRowBytes); | 710 dst = SkTAddOffset<void>(dst, dstRowBytes); |
711 srcRow += fSrcRowBytes; | 711 srcRow += fSrcRowBytes; |
712 } | 712 } |
713 fCurrentRow += count; | 713 fCurrentRow += count; |
714 return SkImageGenerator::kSuccess; | 714 return SkCodec::kSuccess; |
715 } | 715 } |
716 | 716 |
717 SkImageGenerator::Result onSkipScanlines(int count) override { | 717 SkCodec::Result onSkipScanlines(int count) override { |
718 //when ongetScanlines is called it will skip to fCurrentRow | 718 //when ongetScanlines is called it will skip to fCurrentRow |
719 fCurrentRow += count; | 719 fCurrentRow += count; |
720 return SkImageGenerator::kSuccess; | 720 return SkCodec::kSuccess; |
721 } | 721 } |
722 | 722 |
723 bool onReallyHasAlpha() const override { return fHasAlpha; } | 723 bool onReallyHasAlpha() const override { return fHasAlpha; } |
724 | 724 |
725 private: | 725 private: |
726 SkPngCodec* fCodec; // Unowned. | 726 SkPngCodec* fCodec; // Unowned. |
727 bool fHasAlpha; | 727 bool fHasAlpha; |
728 int fCurrentRow; | 728 int fCurrentRow; |
729 int fHeight; | 729 int fHeight; |
730 size_t fSrcRowBytes; | 730 size_t fSrcRowBytes; |
(...skipping 30 matching lines...) Expand all Loading... |
761 | 761 |
762 SkASSERT(fNumberPasses != INVALID_NUMBER_PASSES); | 762 SkASSERT(fNumberPasses != INVALID_NUMBER_PASSES); |
763 if (fNumberPasses > 1) { | 763 if (fNumberPasses > 1) { |
764 // interlaced image | 764 // interlaced image |
765 return SkNEW_ARGS(SkPngInterlacedScanlineDecoder, (dstInfo, this)); | 765 return SkNEW_ARGS(SkPngInterlacedScanlineDecoder, (dstInfo, this)); |
766 } | 766 } |
767 | 767 |
768 return SkNEW_ARGS(SkPngScanlineDecoder, (dstInfo, this)); | 768 return SkNEW_ARGS(SkPngScanlineDecoder, (dstInfo, this)); |
769 } | 769 } |
770 | 770 |
OLD | NEW |