xref: /aosp_15_r20/external/skia/src/codec/SkJpegSourceMgr.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 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 
8 #ifndef SkJpegSourceMgr_codec_DEFINED
9 #define SkJpegSourceMgr_codec_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 
13 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
14 #include "include/core/SkRefCnt.h"
15 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 
21 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
22 #include <vector>
23 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
24 
25 class SkStream;
26 
27 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
28 class SkData;
29 class SkJpegSegmentScanner;
30 struct SkJpegSegment;
31 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
32 
33 /*
34  * Interface to adapt an SkStream to the jpeg_source_mgr interface. This interface has different
35  * implementations for SkStreams with different capabilities.
36  */
37 class SkJpegSourceMgr {
38 public:
39     // Create a source manager. If the source manager will buffer data, |bufferSize| specifies
40     // the size of that buffer.
41     static std::unique_ptr<SkJpegSourceMgr> Make(SkStream* stream, size_t bufferSize = 1024);
42     virtual ~SkJpegSourceMgr();
43 
44     // Interface called by libjpeg via its jpeg_source_mgr interface.
45     virtual void initSource(const uint8_t*& nextInputByte, size_t& bytesInBuffer) = 0;
46     virtual bool fillInputBuffer(const uint8_t*& nextInputByte, size_t& bytesInBuffer) = 0;
47     virtual bool skipInputBytes(size_t bytes,
48                                 const uint8_t*& nextInputByte,
49                                 size_t& bytesInBuffer) = 0;
50 
51 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
52     // Parse this stream all the way through its EndOfImage marker and return the list of segments.
53     // Return false if there is an error or if no EndOfImage marker is found.
54     virtual const std::vector<SkJpegSegment>& getAllSegments() = 0;
55 
56     // Return an the data for a subset of this source's stream, with the specified offset and size.
57     // If the returned SkData is a copy (it does not refer directly to memory owned by |fStream|),
58     // then |wasCopied| is set to true.
59     virtual sk_sp<SkData> getSubsetData(size_t offset, size_t size, bool* wasCopied = nullptr) = 0;
60 
61     // Segments start with a 2 byte marker, followed by a 2 byte parameter length (which includes
62     // those two bytes, followed by parameters. Return the parameters portion of the specified
63     // segment. If possible, the returned SkData will refer to memory owned by |fStream|.
64     virtual sk_sp<SkData> getSegmentParameters(const SkJpegSegment& segment) = 0;
65 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
66 
67 protected:
68     SkJpegSourceMgr(SkStream* stream);
69     SkStream* const fStream;  // unowned
70 
71 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
72     // The segment scanner is lazily creatd only when needed.
73     std::unique_ptr<SkJpegSegmentScanner> fScanner;
74 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
75 };
76 
77 #endif
78