xref: /aosp_15_r20/external/skia/src/codec/SkPngCompositeChunkReader.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google Inc.
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 SkPngCompositeChunkReader_DEFINED
8 #define SkPngCompositeChunkReader_DEFINED
9 
10 #include "include/codec/SkPngChunkReader.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkStream.h"
13 #include "include/private/SkGainmapInfo.h"
14 
15 #include <cstddef>
16 #include <memory>
17 #include <optional>
18 #include <utility>
19 
20 /**
21  * Reader for PNG chunks that handles the composite of:
22  * 1. Chunks currently unknown to the PNG spec, but are necessary to correctly
23  *    decode certain images. For example, images that contain gainmap chunks.
24  * 2. Client-provided callbacks that may listen to other unknown chunks.
25  *
26  * The PNG api can only register one callback for unknown chunks at a time,
27  * hence this wrapper class.
28  */
29 class SkPngCompositeChunkReader : public SkPngChunkReader {
30 public:
SkPngCompositeChunkReader(SkPngChunkReader * chunkReader)31     explicit SkPngCompositeChunkReader(SkPngChunkReader* chunkReader)
32             : fChunkReader(SkSafeRef(chunkReader)) {}
33 
34     bool readChunk(const char tag[], const void* data, size_t length) override;
35 
takeGaimapStream()36     std::unique_ptr<SkStream> takeGaimapStream() { return std::move(fGainmapStream); }
37 
getGainmapInfo()38     std::optional<SkGainmapInfo> getGainmapInfo() const { return fGainmapInfo; }
39 
40 private:
41     sk_sp<SkPngChunkReader> fChunkReader = nullptr;
42     std::optional<SkGainmapInfo> fGainmapInfo;
43     std::unique_ptr<SkStream> fGainmapStream = nullptr;
44 };
45 
46 #endif  // SkPngCompositeChunkReader_DEFINED
47