Home | History | Annotate | Download | only in codec
      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 "SkPngChunkReader.h"
     11 #include "SkEncodedFormat.h"
     12 #include "SkImageInfo.h"
     13 #include "SkRefCnt.h"
     14 #include "SkSwizzler.h"
     15 
     16 #include "png.h"
     17 
     18 class SkStream;
     19 
     20 class SkPngCodec : public SkCodec {
     21 public:
     22     static bool IsPng(const char*, size_t);
     23 
     24     // Assume IsPng was called and returned true.
     25     static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
     26 
     27     virtual ~SkPngCodec();
     28 
     29 protected:
     30     Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
     31             override;
     32     SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; }
     33     bool onRewind() override;
     34     uint32_t onGetFillValue(SkColorType) const override;
     35 
     36     // Helper to set up swizzler and color table. Also calls png_read_update_info.
     37     Result initializeSwizzler(const SkImageInfo& requestedInfo, const Options&,
     38                               SkPMColor*, int* ctableCount);
     39     SkSampler* getSampler(bool createIfNecessary) override {
     40         SkASSERT(fSwizzler);
     41         return fSwizzler;
     42     }
     43 
     44     SkPngCodec(const SkImageInfo&, SkStream*, SkPngChunkReader*, png_structp, png_infop, int, int);
     45 
     46     png_structp png_ptr() { return fPng_ptr; }
     47     SkSwizzler* swizzler() { return fSwizzler; }
     48     SkSwizzler::SrcConfig srcConfig() const { return fSrcConfig; }
     49     int numberPasses() const { return fNumberPasses; }
     50 
     51 private:
     52     SkAutoTUnref<SkPngChunkReader>  fPngChunkReader;
     53     png_structp                     fPng_ptr;
     54     png_infop                       fInfo_ptr;
     55 
     56     // These are stored here so they can be used both by normal decoding and scanline decoding.
     57     SkAutoTUnref<SkColorTable>      fColorTable;    // May be unpremul.
     58     SkAutoTDelete<SkSwizzler>       fSwizzler;
     59 
     60     SkSwizzler::SrcConfig           fSrcConfig;
     61     const int                       fNumberPasses;
     62     int                             fBitDepth;
     63 
     64     bool decodePalette(bool premultiply, int* ctableCount);
     65     void destroyReadStruct();
     66 
     67     typedef SkCodec INHERITED;
     68 };
     69