File: SkPngRustEncoder.h

package info (click to toggle)
webkit2gtk 2.51.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 455,340 kB
  • sloc: cpp: 3,865,253; javascript: 197,710; ansic: 165,177; python: 49,241; asm: 21,868; ruby: 18,095; perl: 16,926; xml: 4,623; sh: 2,409; yacc: 2,356; java: 2,019; lex: 1,330; pascal: 372; makefile: 210
file content (106 lines) | stat: -rw-r--r-- 3,384 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright 2024 Google LLC.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkPngRustEncoder_DEFINED
#define SkPngRustEncoder_DEFINED

#include <cstdint>
#include <memory>

#include "include/core/SkDataTable.h"
#include "include/core/SkRefCnt.h"
#include "include/private/base/SkAPI.h"

class GrDirectContext;
class SkData;
class SkEncoder;
class SkImage;
class SkPixmap;
class SkWStream;

namespace SkPngRustEncoder {

/*
 * Compression level.
 */
enum class CompressionLevel : uint8_t {
    // Low compression level - fast, but may result in bigger PNG files.
    kLow,

    // Medium compression level - somewhere in-between `kLow` and `kHigh`.
    kMedium,

    // High compression level - slow, but should results in smaller PNG files.
    kHigh,
};

/*
 * PNG encoding options.
 *
 * TODO(https://crbug.com/379312510): Add support for `SkPngEncoder::Options`
 * like:
 *  - Comments - `tEXt` chunks.
 *  - Color profile - `iCCP` chunk.
 */
struct Options {
    CompressionLevel fCompressionLevel = CompressionLevel::kMedium;

    /**
     *  Represents comments to be written into tEXt chunks of the png.
     *
     *  The 2i-th entry is the keyword for the i-th comment,
     *  and the (2i + 1)-th entry is the text for the i-th comment.
     *
     *  All entries are treated as strings encoded as Latin-1 (i.e.
     *  ISO-8859-1).  The strings may, but don't have to be NUL-terminated
     *  (trailing NUL characters will be stripped).  Encoding will fail if
     *  keyword or text don't meet the requirements of the PNG spec - text may
     *  have any length and contain any of the 191 Latin-1 characters (and/or
     *  the linefeed character), but keyword's length is restricted to at most
     *  79 characters and it can't contain a non-breaking space character.
     */
    sk_sp<SkDataTable> fComments;
};

/**
 *  Encode the |src| pixels to the |dst| stream.
 *  |options| may be used to control the encoding behavior.
 *
 *  Returns true on success.  Returns false on an invalid or unsupported |src|.
 *
 */
SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);

/**
 *  Returns the encoded data for the pixmap, or nullptr on failure.
 */
SK_API sk_sp<SkData> Encode(const SkPixmap& src, const Options& options);

/**
 *  Encode the provided image and return the resulting bytes. If the image was created as
 *  a texture-backed image on a GPU context, that |ctx| must be provided so the pixels
 *  can be read before being encoded. For raster-backed images, |ctx| can be nullptr.
 *  |options| may be used to control the encoding behavior.
 *
 *  Returns nullptr if the pixels could not be read or encoding otherwise fails.
 */
SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options);

/**
 *  Create a png encoder that will encode the |src| pixels to the |dst| stream.
 *  |options| may be used to control the encoding behavior.
 *
 *  The primary use of this is incremental encoding of the pixels.
 *
 *  |dst| is unowned but must remain valid for the lifetime of the object.
 *
 *  This returns nullptr on an invalid or unsupported |src|.
 */
SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, const Options& options);

}  // namespace SkPngRustEncoder

#endif  // SkPngRustEncoder_DEFINED