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 | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 19 | // FIXME (scroggo): GOOGLE3 is currently using an outdated version of libpng, |
| 20 | // so we need to work around the lack of the method png_process_data_pause. |
| 21 | // This code will be unnecessary once we update GOOGLE3. It would make more |
| 22 | // sense to condition this on the version of libpng being used, but we do not |
| 23 | // know that here because png.h is only included by the cpp file. |
| 24 | #define SK_GOOGLE3_PNG_HACK |
| 25 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 26 | class SkStream; |
| 27 | |
| 28 | class SkPngCodec : public SkCodec { |
| 29 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 30 | static bool IsPng(const char*, size_t); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 31 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 32 | // Assume IsPng was called and returned true. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 33 | static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 34 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 35 | virtual ~SkPngCodec(); |
| 36 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 37 | protected: |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 38 | // We hold the png_ptr and info_ptr as voidp to avoid having to include png.h |
| 39 | // or forward declare their types here. voidp auto-casts to the real pointer types. |
| 40 | struct voidp { |
| 41 | voidp(void* ptr) : fPtr(ptr) {} |
| 42 | |
| 43 | template <typename T> |
| 44 | operator T*() const { return (T*)fPtr; } |
| 45 | |
| 46 | explicit operator bool() const { return fPtr != nullptr; } |
| 47 | |
| 48 | void* fPtr; |
| 49 | }; |
| 50 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 51 | SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, SkStream*, SkPngChunkReader*, |
| 52 | void* png_ptr, void* info_ptr, int bitDepth); |
| 53 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 54 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*) |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | override; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 56 | SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kPNG; } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 57 | bool onRewind() override; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 58 | uint64_t onGetFillValue(const SkImageInfo&) const override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 59 | |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 60 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 61 | void applyXformRow(void* dst, const void* src); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 62 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 63 | voidp png_ptr() { return fPng_ptr; } |
| 64 | voidp info_ptr() { return fInfo_ptr; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 65 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 66 | SkSwizzler* swizzler() { return fSwizzler.get(); } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 67 | |
| 68 | // Initialize variables used by applyXformRow. |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 69 | void initializeXformParams(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Pass available input to libpng to process it. |
| 73 | * |
| 74 | * libpng will call any relevant callbacks installed. This will continue decoding |
| 75 | * until it reaches the end of the file, or until a callback tells libpng to stop. |
| 76 | */ |
| 77 | void processData(); |
| 78 | |
| 79 | #ifdef SK_GOOGLE3_PNG_HACK |
| 80 | // In libpng 1.2.56, png_process_data_pause does not exist, so when we wanted to |
| 81 | // read the header, we may have read too far. In that case, we need to delete the |
| 82 | // png_ptr and info_ptr and recreate them. This method does that (and attaches the |
| 83 | // chunk reader. |
| 84 | bool rereadHeaderIfNecessary(); |
| 85 | |
| 86 | // This method sets up the new png_ptr/info_ptr (created in rereadHeaderIfNecessary) |
| 87 | // the way we set up the old one the first time in AutoCleanPng.decodeBounds's callback. |
| 88 | void rereadInfoCallback(); |
| 89 | #endif |
| 90 | |
| 91 | Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
| 92 | const SkCodec::Options&, |
| 93 | SkPMColor* ctable, int* ctableCount) override; |
| 94 | Result onIncrementalDecode(int*) override; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 95 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 96 | sk_sp<SkPngChunkReader> fPngChunkReader; |
| 97 | voidp fPng_ptr; |
| 98 | voidp fInfo_ptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 99 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 100 | // 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] | 101 | sk_sp<SkColorTable> fColorTable; // May be unpremul. |
| 102 | std::unique_ptr<SkSwizzler> fSwizzler; |
| 103 | SkAutoTMalloc<uint8_t> fStorage; |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 104 | void* fColorXformSrcRow; |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 105 | const int fBitDepth; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 106 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 107 | private: |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 108 | |
| 109 | enum XformMode { |
| 110 | // Requires only a swizzle pass. |
| 111 | kSwizzleOnly_XformMode, |
| 112 | |
| 113 | // Requires only a color xform pass. |
| 114 | kColorOnly_XformMode, |
| 115 | |
| 116 | // Requires a swizzle and a color xform. |
| 117 | kSwizzleColor_XformMode, |
| 118 | }; |
| 119 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 120 | bool createColorTable(const SkImageInfo& dstInfo, int* ctableCount); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 121 | // Helper to set up swizzler, color xforms, and color table. Also calls png_read_update_info. |
| 122 | bool initializeXforms(const SkImageInfo& dstInfo, const Options&, SkPMColor* colorPtr, |
| 123 | int* colorCount); |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 124 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options&, bool skipFormatConversion); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 125 | void allocateStorage(const SkImageInfo& dstInfo); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 126 | void destroyReadStruct(); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 127 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 128 | virtual Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) = 0; |
| 129 | virtual void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) = 0; |
| 130 | virtual Result decode(int* rowsDecoded) = 0; |
| 131 | |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 132 | XformMode fXformMode; |
| 133 | SkColorSpaceXform::ColorFormat fXformColorFormat; |
| 134 | SkAlphaType fXformAlphaType; |
| 135 | int fXformWidth; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 136 | |
| 137 | #ifdef SK_GOOGLE3_PNG_HACK |
| 138 | bool fNeedsToRereadHeader; |
| 139 | #endif |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 140 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 141 | typedef SkCodec INHERITED; |
| 142 | }; |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame^] | 143 | #endif // SkPngCodec_DEFINED |