1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkCodec.h" 9 #include "SkColorTable.h" 10 #include "SkEncodedFormat.h" 11 #include "SkImageInfo.h" 12 #include "SkRefCnt.h" 13 #include "SkSwizzler.h" 14 15 #ifdef SKIA_PNG_PREFIXED 16 // this must proceed png.h 17 #include "pngprefix.h" 18 #endif 19 #include "png.h" 20 21 class SkScanlineDecoder; 22 class SkStream; 23 24 class SkPngCodec : public SkCodec { 25 public: 26 // Assumes IsPng was called and returned true. 27 static SkCodec* NewFromStream(SkStream*); 28 static bool IsPng(SkStream*); 29 protected: 30 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*) 31 override; 32 SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; } 33 SkScanlineDecoder* onGetScanlineDecoder(const SkImageInfo& dstInfo, const Options& options, 34 SkPMColor ctable[], int* ctableCount) override; 35 bool onReallyHasAlpha() const override { return fReallyHasAlpha; } 36 private: 37 png_structp fPng_ptr; 38 png_infop fInfo_ptr; 39 40 // These are stored here so they can be used both by normal decoding and scanline decoding. 41 SkAutoTUnref<SkColorTable> fColorTable; // May be unpremul. 42 SkAutoTDelete<SkSwizzler> fSwizzler; 43 44 SkSwizzler::SrcConfig fSrcConfig; 45 int fNumberPasses; 46 bool fReallyHasAlpha; 47 48 SkPngCodec(const SkImageInfo&, SkStream*, png_structp, png_infop); 49 ~SkPngCodec(); 50 51 // Helper to set up swizzler and color table. Also calls png_read_update_info. 52 Result initializeSwizzler(const SkImageInfo& requestedInfo, void* dst, 53 size_t rowBytes, const Options&, SkPMColor*, int* ctableCount); 54 // Calls rewindIfNeeded, and returns true if the decoder can continue. 55 bool handleRewind(); 56 bool decodePalette(bool premultiply, int bitDepth, int* ctableCount); 57 void finish(); 58 void destroyReadStruct(); 59 60 friend class SkPngScanlineDecoder; 61 62 typedef SkCodec INHERITED; 63 }; 64