blob: adcc0611139042a8125a2b28b11f1a26f612e15a [file] [log] [blame]
Matt Sarettc367d032017-05-05 11:13:26 -04001/*
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
13class SkPngEncoderMgr;
14class SkWStream;
15
Matt Sarett94fd06f2017-05-08 17:31:00 -040016class SK_API SkPngEncoder : public SkEncoder {
Matt Sarettc367d032017-05-05 11:13:26 -040017public:
18
Matt Sarettbe4c9b02017-05-08 12:11:44 -040019 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 Sarettc367d032017-05-05 11:13:26 -040028
29 struct Options {
Matt Sarett04c37312017-05-05 14:02:13 -040030 /**
Matt Sarettbe4c9b02017-05-08 12:11:44 -040031 * 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 Sarett04c37312017-05-05 14:02:13 -040056 * 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 Sarettc367d032017-05-05 11:13:26 -040060 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
84protected:
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 Sarettbe4c9b02017-05-08 12:11:44 -040093static inline SkPngEncoder::FilterFlag operator|(SkPngEncoder::FilterFlag x,
94 SkPngEncoder::FilterFlag y) {
95 return (SkPngEncoder::FilterFlag)((int)x | (int)y);
96}
97
Matt Sarettc367d032017-05-05 11:13:26 -040098#endif