Debian Codegraph @

src/encode/SkPngEncoderBase.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 SkPngEncoderBase_DEFINED
8 #define SkPngEncoderBase_DEFINED
9
10 #include <cstddef>
11 #include <cstdint>
12
13 #include <optional>
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkSpan.h"
16 #include "include/encode/SkEncoder.h"
17 #include "include/private/SkEncodedInfo.h"
18
19 class SkPixmap;
20
21 // This class implements functionality shared between `SkPngEncoderImpl` and
22 // `SkPngRustEncoderImpl`.
23 class SkPngEncoderBase : public SkEncoder {
24 public:
25 struct TargetInfo {
26 std::optional<SkImageInfo> fSrcRowInfo;
27 std::optional<SkImageInfo> fDstRowInfo;
28 SkEncodedInfo fDstInfo;
29 size_t fDstRowSize;
30 };
31
32 // Gets the `fDstRowInfo` that `fSrcRowInfo` should be converted into before
33 // encoding and uses SkConvertPixels to transform source rows into
34 // ready-to-encode rows (and the `fDstRowSize` of such rows).
35 //
36 // For example, `kRGBA_F32_SkColorType` source will be encoded as
37 // `SkEncodedInfo::kRGBA_Color` with 16 `bitsPerComponent`.
38 //
39 // 'fDstInfo' stores extra information for libpng.
40 //
41 // Returns `std::nullopt` if `srcInfo` is not supported by the PNG encoder.
42 static std::optional<TargetInfo> getTargetInfo(const SkImageInfo& srcInfo);
43
44 protected:
45 SkPngEncoderBase(TargetInfo targetInfo, const SkPixmap& src);
46
47 // SkEncoder override:
48 bool onEncodeRows(int numRows) final;
49
50 // Called from `onEncodeRows` to encode the given `row` (in `dstInfo` format
51 // that was passed to the `SkPngEncoderBase`'s constructor).
52 virtual bool onEncodeRow(SkSpan<const uint8_t> row) = 0;
53
54 // Called from `onEncodeRows` to finalize the encoded PNG (e.g. write the
55 // `IEND` chunk).
56 virtual bool onFinishEncoding() = 0;
57
58 const TargetInfo& targetInfo() const { return fTargetInfo; }
59
60 private:
61 TargetInfo fTargetInfo;
62 bool fFinishedEncoding = false;
63 };
64
65 #endif // SkPngEncoderBase_DEFINED