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 SkEncoder; 18 class SkPixmap; 19 class SkWStream; 20 21 namespace SkPngRustEncoder { 22 23 /* 24 * Compression level. 25 */ 26 enum class CompressionLevel : uint8_t { 27 // Low compression level - fast, but may result in bigger PNG files. 28 kLow, 29 30 // Medium compression level - somewhere in-between `kLow` and `kHigh`. 31 kMedium, 32 33 // High compression level - slow, but should results in smaller PNG files. 34 kHigh, 35 }; 36 37 /* 38 * PNG encoding options. 39 * 40 * TODO(https://crbug.com/379312510): Add support for `SkPngEncoder::Options` 41 * like: 42 * - Comments - `tEXt` chunks. 43 * - Color profile - `iCCP` chunk. 44 */ 45 struct Options { 46 CompressionLevel fCompressionLevel = CompressionLevel::kMedium; 47 48 /** 49 * Represents comments to be written into tEXt chunks of the png. 50 * 51 * The 2i-th entry is the keyword for the i-th comment, 52 * and the (2i + 1)-th entry is the text for the i-th comment. 53 * 54 * All entries are treated as strings encoded as Latin-1 (i.e. 55 * ISO-8859-1). The strings may, but don't have to be NUL-terminated 56 * (trailing NUL characters will be stripped). Encoding will fail if 57 * keyword or text don't meet the requirements of the PNG spec - text may 58 * have any length and contain any of the 191 Latin-1 characters (and/or 59 * the linefeed character), but keyword's length is restricted to at most 60 * 79 characters and it can't contain a non-breaking space character. 61 */ 62 sk_sp<SkDataTable> fComments; 63 }; 64 65 /** 66 * Encode the |src| pixels to the |dst| stream. 67 * |options| may be used to control the encoding behavior. 68 * 69 * Returns true on success. Returns false on an invalid or unsupported |src|. 70 * 71 */ 72 SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); 73 74 /** 75 * Create a png encoder that will encode the |src| pixels to the |dst| stream. 76 * |options| may be used to control the encoding behavior. 77 * 78 * The primary use of this is incremental encoding of the pixels. 79 * 80 * |dst| is unowned but must remain valid for the lifetime of the object. 81 * 82 * This returns nullptr on an invalid or unsupported |src|. 83 */ 84 SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, const Options& options); 85 86 } // namespace SkPngRustEncoder 87 88 #endif // SkPngRustEncoder_DEFINED 89