/src/libjxl/lib/jxl/decode_to_jpeg.h
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef LIB_JXL_DECODE_TO_JPEG_H_ |
7 | | #define LIB_JXL_DECODE_TO_JPEG_H_ |
8 | | |
9 | | // JPEG XL to JPEG bytes decoder logic. The JxlToJpegDecoder class keeps track |
10 | | // of the decoder state needed to parse the JPEG reconstruction box and provide |
11 | | // the reconstructed JPEG to the output buffer. |
12 | | |
13 | | #include <jxl/decode.h> |
14 | | #include <stdint.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstring> |
19 | | #include <memory> |
20 | | #include <utility> |
21 | | #include <vector> |
22 | | |
23 | | #include "lib/jxl/base/status.h" |
24 | | #include "lib/jxl/common.h" // JPEGXL_ENABLE_TRANSCODE_JPEG |
25 | | #include "lib/jxl/image_bundle.h" |
26 | | #include "lib/jxl/jpeg/jpeg_data.h" |
27 | | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
28 | | #include "lib/jxl/jpeg/dec_jpeg_data_writer.h" |
29 | | #endif // JPEGXL_ENABLE_TRANSCODE_JPEG |
30 | | |
31 | | namespace jxl { |
32 | | |
33 | | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
34 | | |
35 | | class JxlToJpegDecoder { |
36 | | public: |
37 | 2.15k | JxlToJpegDecoder() { Reset(); } |
38 | | |
39 | | // Returns whether an output buffer is set. |
40 | 114k | bool IsOutputSet() const { return next_out_ != nullptr; } |
41 | | |
42 | | // Returns whether the decoder is parsing a boxa JPEG box was parsed. |
43 | 0 | bool IsParsingBox() const { return inside_box_; } |
44 | | |
45 | | // Sets the output buffer used when producing JPEG output. |
46 | 0 | JxlDecoderStatus SetOutputBuffer(uint8_t* data, size_t size) { |
47 | 0 | if (next_out_) return JXL_DEC_ERROR; |
48 | 0 | next_out_ = data; |
49 | 0 | avail_size_ = size; |
50 | 0 | return JXL_DEC_SUCCESS; |
51 | 0 | } |
52 | | |
53 | | // Releases the buffer set with SetOutputBuffer(). |
54 | 0 | size_t ReleaseOutputBuffer() { |
55 | 0 | size_t result = avail_size_; |
56 | 0 | next_out_ = nullptr; |
57 | 0 | avail_size_ = 0; |
58 | 0 | return result; |
59 | 0 | } |
60 | | |
61 | | // Sets all per-decode state to its initial value, forgetting the |
62 | | // caller-owned output buffer. Used by the constructor and when the decoder is |
63 | | // rewound or reset, after which the buffer the caller passed to |
64 | | // SetOutputBuffer() may no longer be valid. |
65 | 7.92k | void Reset() { |
66 | 7.92k | buffer_.clear(); |
67 | 7.92k | jpeg_data_.reset(); |
68 | 7.92k | inside_box_ = false; |
69 | 7.92k | box_until_eof_ = false; |
70 | 7.92k | box_size_ = 0; |
71 | 7.92k | next_out_ = nullptr; |
72 | 7.92k | avail_size_ = 0; |
73 | 7.92k | } |
74 | | |
75 | 0 | void StartBox(bool box_until_eof, size_t contents_size) { |
76 | | // A new box implies that we clear the buffer. |
77 | 0 | buffer_.clear(); |
78 | 0 | inside_box_ = true; |
79 | 0 | if (box_until_eof) { |
80 | 0 | box_until_eof_ = true; |
81 | 0 | } else { |
82 | 0 | box_size_ = contents_size; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | // Consumes data from next_in/avail_in to reconstruct JPEG data. |
87 | | // Uses box_size_, inside_box_ and box_until_eof_ to calculate how much to |
88 | | // consume. Potentially stores unparsed data in buffer_. |
89 | | // Potentially populates jpeg_data_. Potentially updates inside_box_. |
90 | | // Returns JXL_DEC_JPEG_RECONSTRUCTION when finished, JXL_DEC_NEED_MORE_INPUT |
91 | | // if more input is needed, JXL_DEC_ERROR on parsing error. |
92 | | JxlDecoderStatus Process(const uint8_t** next_in, size_t* avail_in); |
93 | | |
94 | | // Returns non-owned copy of the JPEGData, only after Process finished and |
95 | | // the JPEGData was not yet moved to an image bundle with |
96 | | // SetImageBundleJpegData. |
97 | 0 | jpeg::JPEGData* GetJpegData() { return jpeg_data_.get(); } |
98 | | |
99 | | // Returns how many exif or xmp app markers are present in the JPEG data. A |
100 | | // return value higher than 1 would require multiple exif boxes or multiple |
101 | | // xmp boxes in the container format, and this is not supported by the API and |
102 | | // considered an error. May only be called after Process returned success. |
103 | | static size_t NumExifMarkers(const jpeg::JPEGData& jpeg_data); |
104 | | static size_t NumXmpMarkers(const jpeg::JPEGData& jpeg_data); |
105 | | |
106 | | // Returns box content size for metadata, using the known data from the app |
107 | | // markers. |
108 | | static JxlDecoderStatus ExifBoxContentSize(const jpeg::JPEGData& jpeg_data, |
109 | | size_t* size); |
110 | | static JxlDecoderStatus XmlBoxContentSize(const jpeg::JPEGData& jpeg_data, |
111 | | size_t* size); |
112 | | |
113 | | // Returns JXL_DEC_ERROR if there is no exif/XMP marker or the data size |
114 | | // does not match, or this function is called before Process returned |
115 | | // success, JXL_DEC_SUCCESS otherwise. As input, provide the full box contents |
116 | | // but not the box header. In case of exif, this includes the 4-byte TIFF |
117 | | // header, even though it won't be copied into the JPEG. |
118 | | static JxlDecoderStatus SetExif(const uint8_t* data, size_t size, |
119 | | jpeg::JPEGData* jpeg_data); |
120 | | static JxlDecoderStatus SetXmp(const uint8_t* data, size_t size, |
121 | | jpeg::JPEGData* jpeg_data); |
122 | | |
123 | | // Sets the JpegData of the ImageBundle passed if there is anything to set. |
124 | | // Releases the JpegData from this decoder if set. |
125 | 81.9k | Status SetImageBundleJpegData(ImageBundle* ib) { |
126 | 81.9k | if (IsOutputSet() && jpeg_data_ != nullptr) { |
127 | 0 | if (!jpeg::SetJPEGDataFromICC(ib->metadata()->color_encoding.ICC(), |
128 | 0 | jpeg_data_.get())) { |
129 | 0 | return false; |
130 | 0 | } |
131 | 0 | ib->jpeg_data = std::move(jpeg_data_); |
132 | 0 | } |
133 | 81.9k | return true; |
134 | 81.9k | } |
135 | | |
136 | 0 | JxlDecoderStatus WriteOutput(const jpeg::JPEGData& jpeg_data) { |
137 | | // Copy JPEG bytestream if desired. |
138 | 0 | uint8_t* tmp_next_out = next_out_; |
139 | 0 | size_t tmp_avail_size = avail_size_; |
140 | 0 | auto write = [&tmp_next_out, &tmp_avail_size](const uint8_t* buf, |
141 | 0 | size_t len) { |
142 | 0 | size_t to_write = std::min<size_t>(tmp_avail_size, len); |
143 | 0 | if (to_write != 0) memcpy(tmp_next_out, buf, to_write); |
144 | 0 | tmp_next_out += to_write; |
145 | 0 | tmp_avail_size -= to_write; |
146 | 0 | return to_write; |
147 | 0 | }; |
148 | 0 | Status write_result = jpeg::WriteJpeg(jpeg_data, write); |
149 | 0 | if (!write_result) { |
150 | 0 | if (tmp_avail_size == 0) { |
151 | 0 | return JXL_DEC_JPEG_NEED_MORE_OUTPUT; |
152 | 0 | } |
153 | 0 | return JXL_DEC_ERROR; |
154 | 0 | } |
155 | 0 | next_out_ = tmp_next_out; |
156 | 0 | avail_size_ = tmp_avail_size; |
157 | 0 | return JXL_DEC_SUCCESS; |
158 | 0 | } |
159 | | |
160 | | private: |
161 | | // Content of the most recently parsed JPEG reconstruction box if any. |
162 | | std::vector<uint8_t> buffer_; |
163 | | |
164 | | // Decoded content of the most recently parsed JPEG reconstruction box is |
165 | | // stored here. |
166 | | std::unique_ptr<jpeg::JPEGData> jpeg_data_; |
167 | | |
168 | | // True if the decoder is currently reading bytes inside a JPEG reconstruction |
169 | | // box. |
170 | | bool inside_box_; |
171 | | |
172 | | // True if the JPEG reconstruction box had undefined size (all remaining |
173 | | // bytes). |
174 | | bool box_until_eof_; |
175 | | // Size of most recently parsed JPEG reconstruction box contents. |
176 | | size_t box_size_; |
177 | | |
178 | | // Next bytes to write JPEG reconstruction to. |
179 | | uint8_t* next_out_; |
180 | | // Available bytes to write JPEG reconstruction to. |
181 | | size_t avail_size_; |
182 | | }; |
183 | | |
184 | | #else |
185 | | |
186 | | // Fake class that disables support for decoding JPEG XL to JPEG. |
187 | | class JxlToJpegDecoder { |
188 | | public: |
189 | | bool IsOutputSet() const { return false; } |
190 | | bool IsParsingBox() const { return false; } |
191 | | |
192 | | JxlDecoderStatus SetOutputBuffer(uint8_t* /* data */, size_t /* size */) { |
193 | | return JXL_DEC_ERROR; |
194 | | } |
195 | | size_t ReleaseOutputBuffer() { return 0; } |
196 | | |
197 | | void Reset() {} |
198 | | |
199 | | void StartBox(bool /* box_until_eof */, size_t /* contents_size */) {} |
200 | | |
201 | | JxlDecoderStatus Process(const uint8_t** next_in, size_t* avail_in) { |
202 | | return JXL_DEC_ERROR; |
203 | | } |
204 | | jpeg::JPEGData* GetJpegData() { return nullptr; } |
205 | | |
206 | | Status SetImageBundleJpegData(ImageBundle* /* ib */) { return true; } |
207 | | |
208 | | static size_t NumExifMarkers(const jpeg::JPEGData& /*jpeg_data*/) { |
209 | | return 0; |
210 | | } |
211 | | static size_t NumXmpMarkers(const jpeg::JPEGData& /*jpeg_data*/) { return 0; } |
212 | | static size_t ExifBoxContentSize(const jpeg::JPEGData& /*jpeg_data*/, |
213 | | size_t* /*size*/) { |
214 | | return JXL_DEC_ERROR; |
215 | | } |
216 | | static size_t XmlBoxContentSize(const jpeg::JPEGData& /*jpeg_data*/, |
217 | | size_t* /*size*/) { |
218 | | return JXL_DEC_ERROR; |
219 | | } |
220 | | static JxlDecoderStatus SetExif(const uint8_t* /*data*/, size_t /*size*/, |
221 | | jpeg::JPEGData* /*jpeg_data*/) { |
222 | | return JXL_DEC_ERROR; |
223 | | } |
224 | | static JxlDecoderStatus SetXmp(const uint8_t* /*data*/, size_t /*size*/, |
225 | | jpeg::JPEGData* /*jpeg_data*/) { |
226 | | return JXL_DEC_ERROR; |
227 | | } |
228 | | |
229 | | JxlDecoderStatus WriteOutput(const jpeg::JPEGData& /* jpeg_data */) { |
230 | | return JXL_DEC_SUCCESS; |
231 | | } |
232 | | }; |
233 | | |
234 | | #endif // JPEGXL_ENABLE_TRANSCODE_JPEG |
235 | | |
236 | | } // namespace jxl |
237 | | |
238 | | #endif // LIB_JXL_DECODE_TO_JPEG_H_ |