Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #ifndef SkPngEncoder_DEFINED |
| 9 | #define SkPngEncoder_DEFINED |
| 10 | |
| 11 | #include "SkEncoder.h" |
Yuqian Li | d0dbee6 | 2017-06-09 11:35:58 -0400 | [diff] [blame] | 12 | #include "SkDataTable.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 13 | |
| 14 | class SkPngEncoderMgr; |
| 15 | class SkWStream; |
| 16 | |
Matt Sarett | 94fd06f | 2017-05-08 17:31:00 -0400 | [diff] [blame] | 17 | class SK_API SkPngEncoder : public SkEncoder { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 18 | public: |
| 19 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 20 | enum class FilterFlag : int { |
| 21 | kZero = 0x00, |
| 22 | kNone = 0x08, |
| 23 | kSub = 0x10, |
| 24 | kUp = 0x20, |
| 25 | kAvg = 0x40, |
| 26 | kPaeth = 0x80, |
| 27 | kAll = kNone | kSub | kUp | kAvg | kPaeth, |
| 28 | }; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 29 | |
| 30 | struct Options { |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 31 | /** |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 32 | * Selects which filtering strategies to use. |
| 33 | * |
| 34 | * If a single filter is chosen, libpng will use that filter for every row. |
| 35 | * |
| 36 | * If multiple filters are chosen, libpng will use a heuristic to guess which filter |
| 37 | * will encode smallest, then apply that filter. This happens on a per row basis, |
| 38 | * different rows can use different filters. |
| 39 | * |
| 40 | * Using a single filter (or less filters) is typically faster. Trying all of the |
| 41 | * filters may help minimize the output file size. |
| 42 | * |
| 43 | * Our default value matches libpng's default. |
| 44 | */ |
| 45 | FilterFlag fFilterFlags = FilterFlag::kAll; |
| 46 | |
| 47 | /** |
| 48 | * Must be in [0, 9] where 9 corresponds to maximal compression. This value is passed |
| 49 | * directly to zlib. 0 is a special case to skip zlib entirely, creating dramatically |
| 50 | * larger pngs. |
| 51 | * |
| 52 | * Our default value matches libpng's default. |
| 53 | */ |
| 54 | int fZLibLevel = 6; |
| 55 | |
| 56 | /** |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 57 | * If the input is premultiplied, this controls the unpremultiplication behavior. |
| 58 | * The encoder can convert to linear before unpremultiplying or ignore the transfer |
| 59 | * function and unpremultiply the input as is. |
| 60 | */ |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 61 | SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kRespect; |
Yuqian Li | d0dbee6 | 2017-06-09 11:35:58 -0400 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Represents comments in the tEXt ancillary chunk of the png. |
| 65 | * The 2i-th entry is the keyword for the i-th comment, |
| 66 | * and the (2i + 1)-th entry is the text for the i-th comment. |
| 67 | */ |
| 68 | sk_sp<SkDataTable> fComments; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * Encode the |src| pixels to the |dst| stream. |
| 73 | * |options| may be used to control the encoding behavior. |
| 74 | * |
| 75 | * Returns true on success. Returns false on an invalid or unsupported |src|. |
| 76 | */ |
| 77 | static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 78 | |
| 79 | /** |
| 80 | * Create a png encoder that will encode the |src| pixels to the |dst| stream. |
| 81 | * |options| may be used to control the encoding behavior. |
| 82 | * |
| 83 | * |dst| is unowned but must remain valid for the lifetime of the object. |
| 84 | * |
| 85 | * This returns nullptr on an invalid or unsupported |src|. |
| 86 | */ |
Matt Sarett | 6a4dc66 | 2017-05-11 09:32:59 -0400 | [diff] [blame] | 87 | static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, |
| 88 | const Options& options); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 89 | |
| 90 | ~SkPngEncoder() override; |
| 91 | |
| 92 | protected: |
| 93 | bool onEncodeRows(int numRows) override; |
| 94 | |
| 95 | SkPngEncoder(std::unique_ptr<SkPngEncoderMgr>, const SkPixmap& src); |
| 96 | |
| 97 | std::unique_ptr<SkPngEncoderMgr> fEncoderMgr; |
| 98 | typedef SkEncoder INHERITED; |
| 99 | }; |
| 100 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 101 | static inline SkPngEncoder::FilterFlag operator|(SkPngEncoder::FilterFlag x, |
| 102 | SkPngEncoder::FilterFlag y) { |
| 103 | return (SkPngEncoder::FilterFlag)((int)x | (int)y); |
| 104 | } |
| 105 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 106 | #endif |