Chromium Code Reviews| Index: src/codec/SkCodec_libpng.cpp |
| diff --git a/src/codec/SkCodec_libpng.cpp b/src/codec/SkCodec_libpng.cpp |
| index c2a032e84ea8d98017998bdf5e6f9e12bf059685..657e397647f1c88bd701dd5199d89ea8276fb44a 100644 |
| --- a/src/codec/SkCodec_libpng.cpp |
| +++ b/src/codec/SkCodec_libpng.cpp |
| @@ -648,6 +648,91 @@ private: |
| typedef SkScanlineDecoder INHERITED; |
| }; |
| + |
| +class SkPngInterlacedScanlineDecoder : public SkScanlineDecoder { |
| +public: |
| + SkPngInterlacedScanlineDecoder(const SkImageInfo& dstInfo, SkPngCodec* codec) |
| + : INHERITED(dstInfo) |
| + , fCodec(codec) |
| + , fHasAlpha(false) |
| + , fCurrentRow(0) |
| + , fHeight(dstInfo.height()) |
| + , fSrcRowBytes(dstInfo.minRowBytes()) |
| + { |
| + fGarbageRow.reset(fSrcRowBytes); |
| + fGarbageRowp = static_cast<uint8_t*>(fGarbageRow.get()); |
| + } |
| + |
| + SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override { |
| + //rewind stream since we need entire progressive image to get scanlines |
| + fCodec->handleRewind(); |
| + if (setjmp(png_jmpbuf(fCodec->fPng_ptr))) { |
| + SkCodecPrintf("setjmp long jump!\n"); |
| + return SkImageGenerator::kInvalidInput; |
| + } |
| + const int number_passes = png_set_interlace_handling(fCodec->fPng_ptr); |
| + fStorage.reset(count * fSrcRowBytes); |
| + fStoragep = static_cast<uint8_t*>(fStorage.get()); |
| + |
| + for (int i = 0; i < number_passes; i++) { |
| + //read rows we planned to skip into garbage row |
| + for (int y = 0; y < fCurrentRow; y++){ |
| + png_read_rows(fCodec->fPng_ptr, &fGarbageRowp, png_bytepp_NULL, 1); |
| + } |
| + //read rows we care about into buffer |
| + fSrcRow = fStoragep; |
| + for (int y = 0; y < count; y++) { |
| + png_read_rows(fCodec->fPng_ptr, &fSrcRow, png_bytepp_NULL, 1); |
| + fSrcRow += fSrcRowBytes; |
| + } |
| + //read rows we don't want into garbage buffer |
| + for (int y = 0; y < fHeight - fCurrentRow - count; y++) { |
| + png_read_rows(fCodec->fPng_ptr, &fGarbageRowp, png_bytepp_NULL, 1); |
| + } |
| + } |
| + //swizzle the rows we care about |
| + fSrcRow = fStoragep; |
| + for (int y = 0; y < count; y++) { |
| + fCodec->fSwizzler->setDstRow(dst); |
| + fHasAlpha |= !SkSwizzler::IsOpaque(fCodec->fSwizzler->next(fSrcRow)); |
| + dst = SkTAddOffset<void>(dst, dstRowBytes); |
| + fSrcRow += fSrcRowBytes; |
| + } |
| + fCurrentRow += count; |
| + return SkImageGenerator::kSuccess; |
| + } |
| + |
| + SkImageGenerator::Result onSkipScanlines(int count) override { |
| + //when ongetScanlines is called it will skip to fCurrentRow |
| + fCurrentRow += count; |
| + return SkImageGenerator::kSuccess; |
| + } |
| + |
| + void onFinish() override { |
| + fCodec->finish(); |
| + } |
| + |
| + bool onReallyHasAlpha() const override { return fHasAlpha; } |
| + |
| +private: |
| + SkPngCodec* fCodec; // Unowned. |
| + bool fHasAlpha; |
| + int fCurrentRow; |
| + int fHeight; |
| + int fSrcRowBytes; |
| + SkAutoMalloc fStorage; |
| + uint8_t* fStoragep; |
|
scroggo
2015/06/19 15:45:53
nit: In Skia, I think we would typically name this
emmaleer
2015/06/19 22:12:20
Acknowledged.
|
| + SkAutoMalloc fGarbageRow; |
| + uint8_t* fGarbageRowp; |
| + uint8_t* fSrcRow; |
| + |
| + |
| + |
| + |
| + typedef SkScanlineDecoder INHERITED; |
| +}; |
| + |
| + |
| SkScanlineDecoder* SkPngCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| const Options& options, SkPMColor ctable[], int* ctableCount) { |
| if (!this->handleRewind()) { |
| @@ -675,8 +760,8 @@ SkScanlineDecoder* SkPngCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| SkASSERT(fNumberPasses != INVALID_NUMBER_PASSES); |
| if (fNumberPasses > 1) { |
| - // We cannot efficiently do scanline decoding. |
| - return NULL; |
| + // interlaced image |
| + return SkNEW_ARGS(SkPngInterlacedScanlineDecoder, (dstInfo, this)); |
| } |
| return SkNEW_ARGS(SkPngScanlineDecoder, (dstInfo, this)); |