scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 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" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 9 | #include "SkColorTable.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 10 | #include "SkPngChunkReader.h" |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 11 | #include "SkEncodedFormat.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 12 | #include "SkImageInfo.h" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | #include "SkSwizzler.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 15 | |
djsollen | b2a6fe7 | 2015-04-03 12:35:27 -0700 | [diff] [blame] | 16 | #include "png.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | |
| 18 | class SkStream; |
| 19 | |
| 20 | class SkPngCodec : public SkCodec { |
| 21 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 22 | static bool IsPng(const char*, size_t); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 23 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 24 | // Assume IsPng was called and returned true. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 25 | static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 26 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 27 | virtual ~SkPngCodec(); |
| 28 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 29 | protected: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 30 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*) |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | override; |
| 32 | SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 33 | bool onRewind() override; |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame^] | 34 | uint32_t onGetFillValue(SkColorType) const override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 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); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 39 | SkSampler* getSampler(bool createIfNecessary) override { |
| 40 | SkASSERT(fSwizzler); |
| 41 | return fSwizzler; |
| 42 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 43 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 44 | SkPngCodec(const SkImageInfo&, SkStream*, SkPngChunkReader*, png_structp, png_infop, int, int); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 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 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 51 | private: |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 52 | SkAutoTUnref<SkPngChunkReader> fPngChunkReader; |
| 53 | png_structp fPng_ptr; |
| 54 | png_infop fInfo_ptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 55 | |
| 56 | // These are stored here so they can be used both by normal decoding and scanline decoding. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 57 | SkAutoTUnref<SkColorTable> fColorTable; // May be unpremul. |
| 58 | SkAutoTDelete<SkSwizzler> fSwizzler; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 59 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 60 | SkSwizzler::SrcConfig fSrcConfig; |
| 61 | const int fNumberPasses; |
| 62 | int fBitDepth; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 63 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 64 | bool decodePalette(bool premultiply, int* ctableCount); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 65 | void destroyReadStruct(); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 66 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 67 | typedef SkCodec INHERITED; |
| 68 | }; |