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