Debian Codegraph @

include/encode/SkPngRustEncoder.h

raw
1 /*
2 * Copyright 2024 Google LLC.
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 #ifndef SkPngRustEncoder_DEFINED
8 #define SkPngRustEncoder_DEFINED
9
10 #include <cstdint>
11 #include <memory>
12
13 #include "include/core/SkDataTable.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/private/base/SkAPI.h"
16
17 class GrDirectContext;
18 class SkData;
19 class SkEncoder;
20 class SkImage;
21 class SkPixmap;
22 class SkWStream;
23
24 namespace SkPngRustEncoder {
25
26 /*
27 * Compression level.
28 */
29 enum class CompressionLevel : uint8_t {
30 // Low compression level - fast, but may result in bigger PNG files.
31 kLow,
32
33 // Medium compression level - somewhere in-between `kLow` and `kHigh`.
34 kMedium,
35
36 // High compression level - slow, but should results in smaller PNG files.
37 kHigh,
38 };
39
40 /*
41 * PNG encoding options.
42 *
43 * TODO(https://crbug.com/379312510): Add support for `SkPngEncoder::Options`
44 * like:
45 * - Comments - `tEXt` chunks.
46 * - Color profile - `iCCP` chunk.
47 */
48 struct Options {
49 CompressionLevel fCompressionLevel = CompressionLevel::kMedium;
50
51 /**
52 * Represents comments to be written into tEXt chunks of the png.
53 *
54 * The 2i-th entry is the keyword for the i-th comment,
55 * and the (2i + 1)-th entry is the text for the i-th comment.
56 *
57 * All entries are treated as strings encoded as Latin-1 (i.e.
58 * ISO-8859-1). The strings may, but don't have to be NUL-terminated
59 * (trailing NUL characters will be stripped). Encoding will fail if
60 * keyword or text don't meet the requirements of the PNG spec - text may
61 * have any length and contain any of the 191 Latin-1 characters (and/or
62 * the linefeed character), but keyword's length is restricted to at most
63 * 79 characters and it can't contain a non-breaking space character.
64 */
65 sk_sp<SkDataTable> fComments;
66 };
67
68 /**
69 * Encode the |src| pixels to the |dst| stream.
70 * |options| may be used to control the encoding behavior.
71 *
72 * Returns true on success. Returns false on an invalid or unsupported |src|.
73 *
74 */
75 SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
76
77 /**
78 * Returns the encoded data for the pixmap, or nullptr on failure.
79 */
80 SK_API sk_sp<SkData> Encode(const SkPixmap& src, const Options& options);
81
82 /**
83 * Encode the provided image and return the resulting bytes. If the image was created as
84 * a texture-backed image on a GPU context, that |ctx| must be provided so the pixels
85 * can be read before being encoded. For raster-backed images, |ctx| can be nullptr.
86 * |options| may be used to control the encoding behavior.
87 *
88 * Returns nullptr if the pixels could not be read or encoding otherwise fails.
89 */
90 SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options);
91
92 /**
93 * Create a png encoder that will encode the |src| pixels to the |dst| stream.
94 * |options| may be used to control the encoding behavior.
95 *
96 * The primary use of this is incremental encoding of the pixels.
97 *
98 * |dst| is unowned but must remain valid for the lifetime of the object.
99 *
100 * This returns nullptr on an invalid or unsupported |src|.
101 */
102 SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, const Options& options);
103
104 } // namespace SkPngRustEncoder
105
106 #endif // SkPngRustEncoder_DEFINED