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