blob: 95fd61316342f5f46dd875f724e3df755b34b6ca [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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"
scroggo05245902015-03-25 11:11:52 -07009#include "SkColorTable.h"
scroggocf98fa92015-11-23 08:14:40 -080010#include "SkPngChunkReader.h"
scroggo1dd3ea92015-03-20 11:55:55 -070011#include "SkEncodedFormat.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkImageInfo.h"
scroggo05245902015-03-25 11:11:52 -070013#include "SkRefCnt.h"
14#include "SkSwizzler.h"
scroggof24f2242015-03-03 08:59:20 -080015
djsollenb2a6fe72015-04-03 12:35:27 -070016#include "png.h"
scroggof24f2242015-03-03 08:59:20 -080017
18class SkStream;
19
20class SkPngCodec : public SkCodec {
21public:
scroggodb30be22015-12-08 18:54:13 -080022 static bool IsPng(const char*, size_t);
scroggo9b2cdbf42015-07-10 12:07:02 -070023
scroggo1c005e42015-08-04 09:24:45 -070024 // Assume IsPng was called and returned true.
scroggocf98fa92015-11-23 08:14:40 -080025 static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
scroggo1c005e42015-08-04 09:24:45 -070026
scroggo9b2cdbf42015-07-10 12:07:02 -070027 virtual ~SkPngCodec();
28
scroggof24f2242015-03-03 08:59:20 -080029protected:
msarette6dd0042015-10-09 11:07:34 -070030 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
mtklein36352bf2015-03-25 18:17:31 -070031 override;
32 SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; }
scroggob427db12015-08-12 07:24:13 -070033 bool onRewind() override;
scroggoc5560be2016-02-03 09:42:42 -080034 uint32_t onGetFillValue(SkColorType) const override;
scroggo46c57472015-09-30 08:57:13 -070035
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);
msarette6dd0042015-10-09 11:07:34 -070039 SkSampler* getSampler(bool createIfNecessary) override {
40 SkASSERT(fSwizzler);
41 return fSwizzler;
42 }
scroggo46c57472015-09-30 08:57:13 -070043
scroggocf98fa92015-11-23 08:14:40 -080044 SkPngCodec(const SkImageInfo&, SkStream*, SkPngChunkReader*, png_structp, png_infop, int, int);
scroggo46c57472015-09-30 08:57:13 -070045
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
scroggof24f2242015-03-03 08:59:20 -080051private:
scroggocf98fa92015-11-23 08:14:40 -080052 SkAutoTUnref<SkPngChunkReader> fPngChunkReader;
53 png_structp fPng_ptr;
54 png_infop fInfo_ptr;
scroggo05245902015-03-25 11:11:52 -070055
56 // These are stored here so they can be used both by normal decoding and scanline decoding.
scroggocf98fa92015-11-23 08:14:40 -080057 SkAutoTUnref<SkColorTable> fColorTable; // May be unpremul.
58 SkAutoTDelete<SkSwizzler> fSwizzler;
scroggo05245902015-03-25 11:11:52 -070059
scroggocf98fa92015-11-23 08:14:40 -080060 SkSwizzler::SrcConfig fSrcConfig;
61 const int fNumberPasses;
62 int fBitDepth;
scroggo6f29a3c2015-07-07 06:09:08 -070063
scroggo9b2cdbf42015-07-10 12:07:02 -070064 bool decodePalette(bool premultiply, int* ctableCount);
scroggo3eada2a2015-04-01 09:33:23 -070065 void destroyReadStruct();
scroggo05245902015-03-25 11:11:52 -070066
scroggof24f2242015-03-03 08:59:20 -080067 typedef SkCodec INHERITED;
68};