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

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

Issue 1260673002: SkScaledCodec class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Swizzler and SkScaledCodec use same get_sample_size() function Created 5 years, 4 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_libpng.h" 8 #include "SkCodec_libpng.h"
9 #include "SkCodecPriv.h" 9 #include "SkCodecPriv.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 //would have exited before now if the colorType was supported by png 467 //would have exited before now if the colorType was supported by png
468 SkASSERT(false); 468 SkASSERT(false);
469 } 469 }
470 470
471 // Copy the color table to the client if they request kIndex8 mode 471 // Copy the color table to the client if they request kIndex8 mode
472 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount); 472 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount);
473 473
474 // Create the swizzler. SkPngCodec retains ownership of the color table. 474 // Create the swizzler. SkPngCodec retains ownership of the color table.
475 const SkPMColor* colors = fColorTable ? fColorTable->readColors() : NULL; 475 const SkPMColor* colors = fColorTable ? fColorTable->readColors() : NULL;
476 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo , 476 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo ,
477 dst, rowBytes, options.fZeroInitialized)); 477 dst, rowBytes, options.fZeroInitialized, this->getInfo().width()));
478 if (!fSwizzler) { 478 if (!fSwizzler) {
479 // FIXME: CreateSwizzler could fail for another reason. 479 // FIXME: CreateSwizzler could fail for another reason.
480 return kUnimplemented; 480 return kUnimplemented;
481 } 481 }
482 return kSuccess; 482 return kSuccess;
483 } 483 }
484 484
485 485
486 bool SkPngCodec::handleRewind() { 486 bool SkPngCodec::handleRewind() {
487 switch (this->rewindIfNeeded()) { 487 switch (this->rewindIfNeeded()) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 return kSuccess; 590 return kSuccess;
591 } 591 }
592 592
593 class SkPngScanlineDecoder : public SkScanlineDecoder { 593 class SkPngScanlineDecoder : public SkScanlineDecoder {
594 public: 594 public:
595 SkPngScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec) 595 SkPngScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec)
596 : INHERITED(dstInfo) 596 : INHERITED(dstInfo)
597 , fCodec(codec) 597 , fCodec(codec)
598 , fHasAlpha(false) 598 , fHasAlpha(false)
599 { 599 {
600 fStorage.reset(dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC onfig)); 600 fStorage.reset(fCodec->getInfo().width() * SkSwizzler::BytesPerPixel(fCo dec->fSrcConfig));
601 fSrcRow = static_cast<uint8_t*>(fStorage.get()); 601 fSrcRow = static_cast<uint8_t*>(fStorage.get());
602 } 602 }
603 603
604 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { 604 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de {
605 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { 605 if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) {
606 SkCodecPrintf("setjmp long jump!\n"); 606 SkCodecPrintf("setjmp long jump!\n");
607 return SkCodec::kInvalidInput; 607 return SkCodec::kInvalidInput;
608 } 608 }
609 609
610 for (int i = 0; i < count; i++) { 610 for (int i = 0; i < count; i++) {
(...skipping 16 matching lines...) Expand all
627 //calling png_read_rows in a loop is insignificantly slower than calling it once with count 627 //calling png_read_rows in a loop is insignificantly slower than calling it once with count
628 //as png_read_rows has it's own loop which calls png_read_row count time s. 628 //as png_read_rows has it's own loop which calls png_read_row count time s.
629 for (int i = 0; i < count; i++) { 629 for (int i = 0; i < count; i++) {
630 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); 630 png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1);
631 } 631 }
632 return SkCodec::kSuccess; 632 return SkCodec::kSuccess;
633 } 633 }
634 634
635 bool onReallyHasAlpha() const override { return fHasAlpha; } 635 bool onReallyHasAlpha() const override { return fHasAlpha; }
636 636
637 bool onIsHardToSample() override {
638 return false;
639 }
640
637 private: 641 private:
638 SkAutoTDelete<SkPngCodec> fCodec; 642 SkAutoTDelete<SkPngCodec> fCodec;
639 bool fHasAlpha; 643 bool fHasAlpha;
640 SkAutoMalloc fStorage; 644 SkAutoMalloc fStorage;
641 uint8_t* fSrcRow; 645 uint8_t* fSrcRow;
642 646
643 typedef SkScanlineDecoder INHERITED; 647 typedef SkScanlineDecoder INHERITED;
644 }; 648 };
645 649
646 650
647 class SkPngInterlacedScanlineDecoder : public SkScanlineDecoder { 651 class SkPngInterlacedScanlineDecoder : public SkScanlineDecoder {
648 public: 652 public:
649 SkPngInterlacedScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec ) 653 SkPngInterlacedScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec )
650 : INHERITED(dstInfo) 654 : INHERITED(dstInfo)
651 , fCodec(codec) 655 , fCodec(codec)
652 , fHasAlpha(false) 656 , fHasAlpha(false)
653 , fCurrentRow(0) 657 , fCurrentRow(0)
654 , fHeight(dstInfo.height()) 658 , fHeight(dstInfo.height())
655 { 659 {
656 fSrcRowBytes = dstInfo.width() * SkSwizzler::BytesPerPixel(fCodec->fSrcC onfig); 660 fSrcRowBytes = codec->getInfo().width() * SkSwizzler::BytesPerPixel(fCod ec->fSrcConfig);
657 fGarbageRow.reset(fSrcRowBytes); 661 fGarbageRow.reset(fSrcRowBytes);
658 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get()); 662 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get());
659 } 663 }
660 664
661 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride { 665 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
662 //rewind stream if have previously called onGetScanlines, 666 //rewind stream if have previously called onGetScanlines,
663 //since we need entire progressive image to get scanlines 667 //since we need entire progressive image to get scanlines
664 if (!fCodec->handleRewind()) { 668 if (!fCodec->handleRewind()) {
665 return SkCodec::kCouldNotRewind; 669 return SkCodec::kCouldNotRewind;
666 } 670 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 } 705 }
702 706
703 SkCodec::Result onSkipScanlines(int count) override { 707 SkCodec::Result onSkipScanlines(int count) override {
704 //when ongetScanlines is called it will skip to fCurrentRow 708 //when ongetScanlines is called it will skip to fCurrentRow
705 fCurrentRow += count; 709 fCurrentRow += count;
706 return SkCodec::kSuccess; 710 return SkCodec::kSuccess;
707 } 711 }
708 712
709 bool onReallyHasAlpha() const override { return fHasAlpha; } 713 bool onReallyHasAlpha() const override { return fHasAlpha; }
710 714
715 bool onIsHardToSample() override {
716 return true;
717 }
718
711 private: 719 private:
712 SkAutoTDelete<SkPngCodec> fCodec; 720 SkAutoTDelete<SkPngCodec> fCodec;
713 bool fHasAlpha; 721 bool fHasAlpha;
714 int fCurrentRow; 722 int fCurrentRow;
715 int fHeight; 723 int fHeight;
716 size_t fSrcRowBytes; 724 size_t fSrcRowBytes;
717 SkAutoMalloc fGarbageRow; 725 SkAutoMalloc fGarbageRow;
718 uint8_t* fGarbageRowPtr; 726 uint8_t* fGarbageRowPtr;
719 727
720 typedef SkScanlineDecoder INHERITED; 728 typedef SkScanlineDecoder INHERITED;
721 }; 729 };
722 730
723 731
724 SkScanlineDecoder* SkPngCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 732 SkScanlineDecoder* SkPngCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
725 const Options& options, SkPMColor ctable[], int* ctableCount) { 733 const Options& options, SkPMColor ctable[], int* ctableCount) {
726 if (!conversion_possible(dstInfo, this->getInfo())) { 734 if (!conversion_possible(dstInfo, this->getInfo())) {
727 SkCodecPrintf("no conversion possible\n"); 735 SkCodecPrintf("no conversion possible\n");
728 return NULL; 736 return NULL;
729 } 737 }
730 // Check to see if scaling was requested. 738 // Check to see if scaling was requested.
731 if (dstInfo.dimensions() != this->getInfo().dimensions()) { 739 if(dstInfo.dimensions() != this->getInfo().dimensions()) {
732 return NULL; 740 // the caller is sampling
741 // heights must be equal as SkCodec_libpng has no native y sampling
742 if (dstInfo.height() != this->getInfo().height()) {
scroggo 2015/07/31 13:35:33 Maybe this should be another static method on SkSc
emmaleer 2015/07/31 18:41:56 Acknowledged.
743 return NULL;
744 }
745 // only support down sampling, dstWidth cannot be larger that srcWidth
746 if(dstInfo.width() > this->getInfo().width()) {
747 return NULL;
748 }
733 } 749 }
734 // Create a new SkPngCodec, to be owned by the scanline decoder. 750 // Create a new SkPngCodec, to be owned by the scanline decoder.
735 SkStream* stream = this->stream()->duplicate(); 751 SkStream* stream = this->stream()->duplicate();
736 if (!stream) { 752 if (!stream) {
737 return NULL; 753 return NULL;
738 } 754 }
739 SkAutoTDelete<SkPngCodec> codec (static_cast<SkPngCodec*>(SkPngCodec::NewFro mStream(stream))); 755 SkAutoTDelete<SkPngCodec> codec (static_cast<SkPngCodec*>(SkPngCodec::NewFro mStream(stream)));
740 if (!codec) { 756 if (!codec) {
741 return NULL; 757 return NULL;
742 } 758 }
743 759
744 // Note: We set dst to NULL since we do not know it yet. rowBytes is not nee ded, 760 // Note: We set dst to NULL since we do not know it yet. rowBytes is not nee ded,
745 // since we'll be manually updating the dstRow, but the SkSwizzler requires it to 761 // since we'll be manually updating the dstRow, but the SkSwizzler requires it to
746 // be at least dstInfo.minRowBytes. 762 // be at least dstInfo.minRowBytes.
747 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options, ctable, 763 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options, ctable,
748 ctableCount) != kSuccess) { 764 ctableCount) != kSuccess) {
749 SkCodecPrintf("failed to initialize the swizzler.\n"); 765 SkCodecPrintf("failed to initialize the swizzler.\n");
750 return NULL; 766 return NULL;
751 } 767 }
752 768
753 SkASSERT(codec->fNumberPasses != INVALID_NUMBER_PASSES); 769 SkASSERT(codec->fNumberPasses != INVALID_NUMBER_PASSES);
754 if (codec->fNumberPasses > 1) { 770 if (codec->fNumberPasses > 1) {
755 // interlaced image 771 // interlaced image
756 return SkNEW_ARGS(SkPngInterlacedScanlineDecoder, (dstInfo, codec.detach ())); 772 return SkNEW_ARGS(SkPngInterlacedScanlineDecoder, (dstInfo, codec.detach ()));
757 } 773 }
758 774
759 return SkNEW_ARGS(SkPngScanlineDecoder, (dstInfo, codec.detach())); 775 return SkNEW_ARGS(SkPngScanlineDecoder, (dstInfo, codec.detach()));
760 } 776 }
761 777
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698