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 | */ |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 7 | #ifndef SkPngCodec_DEFINED |
| 8 | #define SkPngCodec_DEFINED |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 9 | |
| 10 | #include "SkCodec.h" |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 11 | #include "SkColorSpaceXform.h" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 12 | #include "SkColorTable.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 13 | #include "SkPngChunkReader.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 14 | #include "SkEncodedImageFormat.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 15 | #include "SkImageInfo.h" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 16 | #include "SkRefCnt.h" |
| 17 | #include "SkSwizzler.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 18 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | class SkStream; |
| 20 | |
| 21 | class SkPngCodec : public SkCodec { |
| 22 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 23 | static bool IsPng(const char*, size_t); |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 24 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 25 | // Assume IsPng was called and returned true. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 26 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*, |
| 27 | SkPngChunkReader* = nullptr); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 28 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 29 | // FIXME (scroggo): Temporarily needed by AutoCleanPng. |
| 30 | void setIdatLength(size_t len) { fIdatLength = len; } |
| 31 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 32 | ~SkPngCodec() override; |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 33 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 34 | protected: |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 35 | // We hold the png_ptr and info_ptr as voidp to avoid having to include png.h |
| 36 | // or forward declare their types here. voidp auto-casts to the real pointer types. |
| 37 | struct voidp { |
| 38 | voidp(void* ptr) : fPtr(ptr) {} |
| 39 | |
| 40 | template <typename T> |
| 41 | operator T*() const { return (T*)fPtr; } |
| 42 | |
| 43 | explicit operator bool() const { return fPtr != nullptr; } |
| 44 | |
| 45 | void* fPtr; |
| 46 | }; |
| 47 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 48 | SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, std::unique_ptr<SkStream>, |
| 49 | SkPngChunkReader*, void* png_ptr, void* info_ptr, int bitDepth); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 50 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 51 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | override; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 53 | SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kPNG; } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 54 | bool onRewind() override; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 55 | uint64_t onGetFillValue(const SkImageInfo&) const override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 56 | |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 57 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 58 | void applyXformRow(void* dst, const void* src); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 59 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 60 | voidp png_ptr() { return fPng_ptr; } |
| 61 | voidp info_ptr() { return fInfo_ptr; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 62 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 63 | SkSwizzler* swizzler() { return fSwizzler.get(); } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 64 | |
| 65 | // Initialize variables used by applyXformRow. |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 66 | void initializeXformParams(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Pass available input to libpng to process it. |
| 70 | * |
| 71 | * libpng will call any relevant callbacks installed. This will continue decoding |
| 72 | * until it reaches the end of the file, or until a callback tells libpng to stop. |
| 73 | */ |
| 74 | void processData(); |
| 75 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 76 | Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 77 | const SkCodec::Options&) override; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 78 | Result onIncrementalDecode(int*) override; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 79 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 80 | sk_sp<SkPngChunkReader> fPngChunkReader; |
| 81 | voidp fPng_ptr; |
| 82 | voidp fInfo_ptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 83 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 84 | // These are stored here so they can be used both by normal decoding and scanline decoding. |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 85 | sk_sp<SkColorTable> fColorTable; // May be unpremul. |
| 86 | std::unique_ptr<SkSwizzler> fSwizzler; |
| 87 | SkAutoTMalloc<uint8_t> fStorage; |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 88 | void* fColorXformSrcRow; |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 89 | const int fBitDepth; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 90 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 91 | private: |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 92 | |
| 93 | enum XformMode { |
| 94 | // Requires only a swizzle pass. |
| 95 | kSwizzleOnly_XformMode, |
| 96 | |
| 97 | // Requires only a color xform pass. |
| 98 | kColorOnly_XformMode, |
| 99 | |
| 100 | // Requires a swizzle and a color xform. |
| 101 | kSwizzleColor_XformMode, |
| 102 | }; |
| 103 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 104 | bool createColorTable(const SkImageInfo& dstInfo); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 105 | // Helper to set up swizzler, color xforms, and color table. Also calls png_read_update_info. |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 106 | SkCodec::Result initializeXforms(const SkImageInfo& dstInfo, const Options&); |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 107 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options&, bool skipFormatConversion); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 108 | void allocateStorage(const SkImageInfo& dstInfo); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 109 | void destroyReadStruct(); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 110 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 111 | virtual Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) = 0; |
| 112 | virtual void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) = 0; |
| 113 | virtual Result decode(int* rowsDecoded) = 0; |
| 114 | |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 115 | XformMode fXformMode; |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 116 | int fXformWidth; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 117 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 118 | size_t fIdatLength; |
| 119 | bool fDecodedIdat; |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 120 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 121 | typedef SkCodec INHERITED; |
| 122 | }; |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 123 | #endif // SkPngCodec_DEFINED |