/src/libjxl/lib/jxl/enc_icc_codec.cc
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 | | #include "lib/jxl/enc_icc_codec.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstdint> |
12 | | #include <limits> |
13 | | #include <map> |
14 | | #include <vector> |
15 | | |
16 | | #include "lib/jxl/base/compiler_specific.h" |
17 | | #include "lib/jxl/base/span.h" |
18 | | #include "lib/jxl/base/status.h" |
19 | | #include "lib/jxl/enc_ans.h" |
20 | | #include "lib/jxl/enc_ans_params.h" |
21 | | #include "lib/jxl/enc_aux_out.h" |
22 | | #include "lib/jxl/fields.h" |
23 | | #include "lib/jxl/icc_codec_common.h" |
24 | | #include "lib/jxl/padded_bytes.h" |
25 | | |
26 | | namespace jxl { |
27 | | namespace { |
28 | | |
29 | | // Unshuffles or de-interleaves bytes, for example with width 2, turns |
30 | | // "AaBbCcDc" into "ABCDabcd", this for example de-interleaves UTF-16 bytes into |
31 | | // first all the high order bytes, then all the low order bytes. |
32 | | // Transposes a matrix of width columns and ceil(size / width) rows. There are |
33 | | // size elements, size may be < width * height, if so the |
34 | | // last elements of the bottom row are missing, the missing spots are |
35 | | // transposed along with the filled spots, and the result has the missing |
36 | | // elements at the bottom of the rightmost column. The input is the input matrix |
37 | | // in scanline order, the output is the result matrix in scanline order, with |
38 | | // missing elements skipped over (this may occur at multiple positions). |
39 | | Status Unshuffle(JxlMemoryManager* memory_manager, uint8_t* data, size_t size, |
40 | 488k | size_t width) { |
41 | 488k | size_t height = (size + width - 1) / width; // amount of rows of input |
42 | 488k | PaddedBytes result(memory_manager); |
43 | 488k | JXL_ASSIGN_OR_RETURN(result, |
44 | 488k | PaddedBytes::WithInitialSpace(memory_manager, size)); |
45 | | |
46 | | // i = input index, j output index |
47 | 488k | size_t s = 0; |
48 | 488k | size_t j = 0; |
49 | 29.1M | for (size_t i = 0; i < size; i++) { |
50 | 28.6M | result[j] = data[i]; |
51 | 28.6M | j += height; |
52 | 28.6M | if (j >= size) j = ++s; |
53 | 28.6M | } |
54 | | |
55 | 29.1M | for (size_t i = 0; i < size; i++) { |
56 | 28.6M | data[i] = result[i]; |
57 | 28.6M | } |
58 | 488k | return true; |
59 | 488k | } |
60 | | |
61 | | // This is performed by the encoder, the encoder must be able to encode any |
62 | | // random byte stream (not just byte streams that are a valid ICC profile), so |
63 | | // an error returned by this function is an implementation error. |
64 | | Status PredictAndShuffle(size_t stride, size_t width, int order, size_t num, |
65 | | const uint8_t* data, size_t size, size_t* pos, |
66 | 488k | PaddedBytes* result) { |
67 | 488k | JXL_RETURN_IF_ERROR(CheckOutOfBounds(*pos, num, size)); |
68 | 488k | JxlMemoryManager* memory_manager = result->memory_manager(); |
69 | | // Required by the specification, see decoder. stride * 4 must be < *pos. |
70 | 488k | if (!*pos || ((*pos - 1u) >> 2u) < stride) { |
71 | 0 | return JXL_FAILURE("Invalid stride"); |
72 | 0 | } |
73 | 488k | if (*pos < stride * 4) return JXL_FAILURE("Too large stride"); |
74 | 488k | size_t start = result->size(); |
75 | 28.0M | for (size_t i = 0; i < num; i++) { |
76 | 27.6M | uint8_t predicted = |
77 | 27.6M | LinearPredictICCValue(data, *pos, i, stride, width, order); |
78 | 27.6M | JXL_RETURN_IF_ERROR(result->push_back(data[*pos + i] - predicted)); |
79 | 27.6M | } |
80 | 488k | *pos += num; |
81 | 488k | if (width > 1) { |
82 | 488k | JXL_RETURN_IF_ERROR( |
83 | 488k | Unshuffle(memory_manager, result->data() + start, num, width)); |
84 | 488k | } |
85 | 488k | return true; |
86 | 488k | } |
87 | | |
88 | 18.4M | inline Status EncodeVarInt(uint64_t value, PaddedBytes* data) { |
89 | 18.4M | size_t pos = data->size(); |
90 | 18.4M | JXL_RETURN_IF_ERROR(data->resize(data->size() + 9)); |
91 | 18.4M | size_t output_size = data->size(); |
92 | 18.4M | uint8_t* output = data->data(); |
93 | | |
94 | | // While more than 7 bits of data are left, |
95 | | // store 7 bits and set the next byte flag |
96 | 79.5M | while (value > 127) { |
97 | | // TODO(eustas): should it be `<` ? |
98 | 61.0M | JXL_ENSURE(pos <= output_size); |
99 | | // |128: Set the next byte flag |
100 | 61.0M | output[pos++] = (static_cast<uint8_t>(value & 127)) | 128; |
101 | | // Remove the seven bits we just wrote |
102 | 61.0M | value >>= 7; |
103 | 61.0M | } |
104 | | // TODO(eustas): should it be `<` ? |
105 | 18.4M | JXL_ENSURE(pos <= output_size); |
106 | 18.4M | output[pos++] = static_cast<uint8_t>(value & 127); |
107 | | |
108 | 18.4M | return data->resize(pos); |
109 | 18.4M | } |
110 | | |
111 | | constexpr size_t kSizeLimit = std::numeric_limits<uint32_t>::max() >> 2; |
112 | | |
113 | | } // namespace |
114 | | |
115 | | // Outputs a transformed form of the given icc profile. The result itself is |
116 | | // not particularly smaller than the input data in bytes, but it will be in a |
117 | | // form that is easier to compress (more zeroes, ...) and will compress better |
118 | | // with brotli. |
119 | 3.07k | Status PredictICC(const uint8_t* icc, size_t size, PaddedBytes* result) { |
120 | 3.07k | JxlMemoryManager* memory_manager = result->memory_manager(); |
121 | 3.07k | PaddedBytes commands{memory_manager}; |
122 | 3.07k | PaddedBytes data{memory_manager}; |
123 | | |
124 | 3.07k | static_assert(sizeof(size_t) >= 4, "size_t is too short"); |
125 | | // Fuzzer expects that PredictICC can accept any input, |
126 | | // but 1GB should be enough for any purpose. |
127 | 3.07k | if (size > kSizeLimit) { |
128 | 0 | return JXL_FAILURE("ICC profile is too large"); |
129 | 0 | } |
130 | | |
131 | 3.07k | JXL_RETURN_IF_ERROR(EncodeVarInt(size, result)); |
132 | | |
133 | | // Header |
134 | 3.07k | PaddedBytes header{memory_manager}; |
135 | 3.07k | JXL_RETURN_IF_ERROR(header.append(ICCInitialHeaderPrediction(size))); |
136 | 376k | for (size_t i = 0; i < kICCHeaderSize && i < size; i++) { |
137 | 373k | ICCPredictHeader(icc, size, header.data(), i); |
138 | 373k | JXL_RETURN_IF_ERROR(data.push_back(icc[i] - header[i])); |
139 | 373k | } |
140 | 3.07k | if (size <= kICCHeaderSize) { |
141 | 210 | JXL_RETURN_IF_ERROR(EncodeVarInt(0, result)); // 0 commands |
142 | 7.56k | for (uint8_t b : data) { |
143 | 7.56k | JXL_RETURN_IF_ERROR(result->push_back(b)); |
144 | 7.56k | } |
145 | 210 | return true; |
146 | 210 | } |
147 | | |
148 | 2.86k | std::vector<Tag> tags; |
149 | 2.86k | std::vector<size_t> tagstarts; |
150 | 2.86k | std::vector<size_t> tagsizes; |
151 | 2.86k | std::map<size_t, size_t> tagmap; |
152 | | |
153 | | // Tag list |
154 | 2.86k | size_t pos = kICCHeaderSize; |
155 | 2.86k | if (pos + 4 <= size) { |
156 | 2.85k | uint64_t numtags = DecodeUint32(icc, size, pos); |
157 | 2.85k | pos += 4; |
158 | 2.85k | JXL_RETURN_IF_ERROR(EncodeVarInt(numtags + 1, &commands)); |
159 | 2.85k | uint64_t prevtagstart = kICCHeaderSize + numtags * 12; |
160 | 2.85k | uint32_t prevtagsize = 0; |
161 | 9.35M | for (size_t i = 0; i < numtags; i++) { |
162 | 9.35M | if (pos + 12 > size) break; |
163 | | |
164 | 9.35M | Tag tag = DecodeKeyword(icc, size, pos + 0); |
165 | 9.35M | uint32_t tagstart = DecodeUint32(icc, size, pos + 4); |
166 | 9.35M | uint32_t tagsize = DecodeUint32(icc, size, pos + 8); |
167 | 9.35M | pos += 12; |
168 | | |
169 | 9.35M | tags.push_back(tag); |
170 | 9.35M | tagstarts.push_back(tagstart); |
171 | 9.35M | tagsizes.push_back(tagsize); |
172 | 9.35M | tagmap[tagstart] = tags.size() - 1; |
173 | | |
174 | 9.35M | uint8_t tagcode = kCommandTagUnknown; |
175 | 168M | for (size_t j = 0; j < kNumTagStrings; j++) { |
176 | 158M | if (tag == *kTagStrings[j]) { |
177 | 27.3k | tagcode = j + kCommandTagStringFirst; |
178 | 27.3k | break; |
179 | 27.3k | } |
180 | 158M | } |
181 | | |
182 | 9.35M | if (tag == kRtrcTag && pos + 24 < size) { |
183 | 6.70k | bool ok = true; |
184 | 6.70k | ok &= DecodeKeyword(icc, size, pos + 0) == kGtrcTag; |
185 | 6.70k | ok &= DecodeKeyword(icc, size, pos + 12) == kBtrcTag; |
186 | 6.70k | if (ok) { |
187 | 24.2k | for (size_t kk = 0; kk < 8; kk++) { |
188 | 21.5k | if (icc[pos - 8 + kk] != icc[pos + 4 + kk]) ok = false; |
189 | 21.5k | if (icc[pos - 8 + kk] != icc[pos + 16 + kk]) ok = false; |
190 | 21.5k | } |
191 | 2.68k | } |
192 | 6.70k | if (ok) { |
193 | 350 | tagcode = kCommandTagTRC; |
194 | 350 | pos += 24; |
195 | 350 | i += 2; |
196 | 350 | } |
197 | 6.70k | } |
198 | | |
199 | 9.35M | if (tag == kRxyzTag && pos + 24 < size) { |
200 | 3.70k | bool ok = true; |
201 | 3.70k | ok &= DecodeKeyword(icc, size, pos + 0) == kGxyzTag; |
202 | 3.70k | ok &= DecodeKeyword(icc, size, pos + 12) == kBxyzTag; |
203 | 3.70k | uint32_t offsetr = tagstart; |
204 | 3.70k | uint32_t offsetg = DecodeUint32(icc, size, pos + 4); |
205 | 3.70k | uint32_t offsetb = DecodeUint32(icc, size, pos + 16); |
206 | 3.70k | uint32_t sizer = tagsize; |
207 | 3.70k | uint32_t sizeg = DecodeUint32(icc, size, pos + 8); |
208 | 3.70k | uint32_t sizeb = DecodeUint32(icc, size, pos + 20); |
209 | 3.70k | ok &= sizer == 20; |
210 | 3.70k | ok &= sizeg == 20; |
211 | 3.70k | ok &= sizeb == 20; |
212 | 3.70k | ok &= (offsetg == offsetr + 20); |
213 | 3.70k | ok &= (offsetb == offsetr + 40); |
214 | 3.70k | if (ok) { |
215 | 539 | tagcode = kCommandTagXYZ; |
216 | 539 | pos += 24; |
217 | 539 | i += 2; |
218 | 539 | } |
219 | 3.70k | } |
220 | | |
221 | 9.35M | uint8_t command = tagcode; |
222 | 9.35M | uint64_t predicted_tagstart = prevtagstart + prevtagsize; |
223 | 9.35M | if (predicted_tagstart != tagstart) command |= kFlagBitOffset; |
224 | 9.35M | size_t predicted_tagsize = prevtagsize; |
225 | 9.35M | if (tag == kRxyzTag || tag == kGxyzTag || tag == kBxyzTag || |
226 | 9.34M | tag == kKxyzTag || tag == kWtptTag || tag == kBkptTag || |
227 | 9.34M | tag == kLumiTag) { |
228 | 10.5k | predicted_tagsize = 20; |
229 | 10.5k | } |
230 | 9.35M | if (predicted_tagsize != tagsize) command |= kFlagBitSize; |
231 | 9.35M | JXL_RETURN_IF_ERROR(commands.push_back(command)); |
232 | 9.35M | if (tagcode == 1) { |
233 | 9.32M | JXL_RETURN_IF_ERROR(AppendKeyword(tag, &data)); |
234 | 9.32M | } |
235 | 9.35M | if (command & kFlagBitOffset) |
236 | 8.88M | JXL_RETURN_IF_ERROR(EncodeVarInt(tagstart, &commands)); |
237 | 9.35M | if (command & kFlagBitSize) |
238 | 8.16M | JXL_RETURN_IF_ERROR(EncodeVarInt(tagsize, &commands)); |
239 | | |
240 | 9.35M | prevtagstart = tagstart; |
241 | 9.35M | prevtagsize = tagsize; |
242 | 9.35M | } |
243 | 2.85k | } |
244 | | // Indicate end of tag list or varint indicating there's none |
245 | 2.86k | JXL_RETURN_IF_ERROR(commands.push_back(0)); |
246 | | |
247 | | // Main content |
248 | | // The main content in a valid ICC profile contains tagged elements, with the |
249 | | // tag types (4 letter names) given by the tag list above, and the tag list |
250 | | // pointing to the start and indicating the size of each tagged element. It is |
251 | | // allowed for tagged elements to overlap, e.g. the curve for R, G and B could |
252 | | // all point to the same one. |
253 | 2.86k | Tag tag; |
254 | 2.86k | size_t tagstart = 0; |
255 | 2.86k | size_t tagsize = 0; |
256 | 2.86k | size_t clutstart = 0; |
257 | | |
258 | | // Should always check tag_sane before doing math with tagsize. |
259 | 940k | const auto tag_sane = [&tagsize]() { |
260 | 940k | return (tagsize > 8) && (tagsize < kSizeLimit); |
261 | 940k | }; |
262 | | |
263 | 2.86k | size_t last0 = pos; |
264 | | // This loop appends commands to the output, processing some sub-section of a |
265 | | // current tagged element each time. We need to keep track of the tagtype of |
266 | | // the current element, and update it when we encounter the boundary of a |
267 | | // next one. |
268 | | // It is not required that the input data is a valid ICC profile, if the |
269 | | // encoder does not recognize the data it will still be able to output bytes |
270 | | // but will not predict as well. |
271 | 44.7M | while (pos <= size) { |
272 | 44.7M | size_t last1 = pos; |
273 | 44.7M | PaddedBytes commands_add{memory_manager}; |
274 | 44.7M | PaddedBytes data_add{memory_manager}; |
275 | | |
276 | | // This means the loop brought the position beyond the tag end. |
277 | | // If tagsize is nonsensical, any pos looks "ok-ish". |
278 | 44.7M | if ((pos > tagstart + tagsize) && (tagsize < kSizeLimit)) { |
279 | 21.2M | tag = {{0, 0, 0, 0}}; // nonsensical value |
280 | 21.2M | } |
281 | | |
282 | 44.7M | if (commands_add.empty() && data_add.empty() && tagmap.count(pos) && |
283 | 13.0k | pos + 4 <= size) { |
284 | 12.9k | size_t index = tagmap[pos]; |
285 | 12.9k | tag = DecodeKeyword(icc, size, pos); |
286 | 12.9k | tagstart = tagstarts[index]; |
287 | 12.9k | tagsize = tagsizes[index]; |
288 | | |
289 | 12.9k | if (tag == kMlucTag && tag_sane() && pos + tagsize <= size && |
290 | 302 | icc[pos + 4] == 0 && icc[pos + 5] == 0 && icc[pos + 6] == 0 && |
291 | 171 | icc[pos + 7] == 0) { |
292 | 89 | size_t num = tagsize - 8; |
293 | 89 | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandTypeStartFirst + 3)); |
294 | 89 | pos += 8; |
295 | 89 | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandShuffle2)); |
296 | 89 | JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add)); |
297 | 89 | size_t start = data_add.size(); |
298 | 1.08M | for (size_t i = 0; i < num; i++) { |
299 | 1.08M | JXL_RETURN_IF_ERROR(data_add.push_back(icc[pos])); |
300 | 1.08M | pos++; |
301 | 1.08M | } |
302 | 89 | JXL_RETURN_IF_ERROR( |
303 | 89 | Unshuffle(memory_manager, data_add.data() + start, num, 2)); |
304 | 89 | } |
305 | | |
306 | 12.9k | if (tag == kCurvTag && tag_sane() && pos + tagsize <= size && |
307 | 558 | icc[pos + 4] == 0 && icc[pos + 5] == 0 && icc[pos + 6] == 0 && |
308 | 151 | icc[pos + 7] == 0) { |
309 | 137 | size_t num = tagsize - 8; |
310 | 137 | if (num > 16 && num < (1 << 28) && pos + num <= size && pos > 0) { |
311 | 123 | JXL_RETURN_IF_ERROR( |
312 | 123 | commands_add.push_back(kCommandTypeStartFirst + 5)); |
313 | 123 | pos += 8; |
314 | 123 | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict)); |
315 | 123 | int order = 1; |
316 | 123 | int width = 2; |
317 | 123 | int stride = width; |
318 | 123 | JXL_RETURN_IF_ERROR( |
319 | 123 | commands_add.push_back((order << 2) | (width - 1))); |
320 | 123 | JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add)); |
321 | 123 | JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc, |
322 | 123 | size, &pos, &data_add)); |
323 | 123 | } |
324 | 137 | } |
325 | 12.9k | } |
326 | | |
327 | 44.7M | if (tag == kMab_Tag || tag == kMba_Tag) { |
328 | 14.0M | Tag subTag = DecodeKeyword(icc, size, pos); |
329 | 14.0M | if (pos + 12 < size && (subTag == kCurvTag || subTag == kVcgtTag) && |
330 | 576k | DecodeUint32(icc, size, pos + 4) == 0) { |
331 | 510k | uint32_t num = DecodeUint32(icc, size, pos + 8) * 2; |
332 | 510k | if (num > 16 && num < (1 << 28) && pos + 12 + num <= size) { |
333 | 488k | pos += 12; |
334 | 488k | last1 = pos; |
335 | 488k | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict)); |
336 | 488k | int order = 1; |
337 | 488k | int width = 2; |
338 | 488k | int stride = width; |
339 | 488k | JXL_RETURN_IF_ERROR( |
340 | 488k | commands_add.push_back((order << 2) | (width - 1))); |
341 | 488k | JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add)); |
342 | 488k | JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc, |
343 | 488k | size, &pos, &data_add)); |
344 | 488k | } |
345 | 510k | } |
346 | | |
347 | 14.0M | if (pos == tagstart + 24 && pos + 4 < size) { |
348 | | // Note that this value can be remembered for next iterations of the |
349 | | // loop, so the "pos == clutstart" if below can trigger during a later |
350 | | // iteration. |
351 | 677 | clutstart = tagstart + DecodeUint32(icc, size, pos); |
352 | 677 | } |
353 | | |
354 | 14.0M | if (pos == clutstart && clutstart + 16 < size) { |
355 | 320 | size_t numi = icc[tagstart + 8]; |
356 | 320 | size_t numo = icc[tagstart + 9]; |
357 | 320 | size_t width = icc[clutstart + 16]; |
358 | 320 | size_t stride = width * numo; |
359 | 320 | size_t num = width * numo; |
360 | 6.77k | for (size_t i = 0; i < numi && clutstart + i < size; i++) { |
361 | 6.45k | num *= icc[clutstart + i]; |
362 | 6.45k | } |
363 | 320 | if ((width == 1 || width == 2) && num > 64 && num < (1 << 28) && |
364 | 165 | pos + num <= size && pos > stride * 4) { |
365 | 75 | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict)); |
366 | 75 | int order = 1; |
367 | 75 | uint8_t flags = |
368 | 75 | (order << 2) | (width - 1) | (stride == width ? 0 : 16); |
369 | 75 | JXL_RETURN_IF_ERROR(commands_add.push_back(flags)); |
370 | 75 | if (flags & 16) { |
371 | 59 | JXL_RETURN_IF_ERROR(EncodeVarInt(stride, &commands_add)); |
372 | 59 | } |
373 | 75 | JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add)); |
374 | 75 | JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc, |
375 | 75 | size, &pos, &data_add)); |
376 | 75 | } |
377 | 320 | } |
378 | 14.0M | } |
379 | | |
380 | 44.7M | if (commands_add.empty() && data_add.empty() && tag == kGbd_Tag && |
381 | 938k | tag_sane() && pos == tagstart + 8 && pos + tagsize - 8 <= size && |
382 | 98 | pos > 16) { |
383 | 98 | size_t width = 4; |
384 | 98 | size_t order = 0; |
385 | 98 | size_t stride = width; |
386 | 98 | size_t num = tagsize - 8; |
387 | 98 | uint8_t flags = (order << 2) | (width - 1) | (stride == width ? 0 : 16); |
388 | 98 | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict)); |
389 | 98 | JXL_RETURN_IF_ERROR(commands_add.push_back(flags)); |
390 | 98 | if (flags & 16) { |
391 | 0 | JXL_RETURN_IF_ERROR(EncodeVarInt(stride, &commands_add)); |
392 | 0 | } |
393 | 98 | JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add)); |
394 | 98 | JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc, |
395 | 98 | size, &pos, &data_add)); |
396 | 98 | } |
397 | | |
398 | 44.7M | if (commands_add.empty() && data_add.empty() && pos + 20 <= size) { |
399 | 44.2M | Tag subTag = DecodeKeyword(icc, size, pos); |
400 | 44.2M | if (subTag == kXyz_Tag && DecodeUint32(icc, size, pos + 4) == 0) { |
401 | 13.4k | JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandXYZ)); |
402 | 13.4k | pos += 8; |
403 | 174k | for (size_t j = 0; j < 12; j++) { |
404 | 161k | JXL_RETURN_IF_ERROR(data_add.push_back(icc[pos++])); |
405 | 161k | } |
406 | 13.4k | } |
407 | 44.2M | } |
408 | | |
409 | 44.7M | if (commands_add.empty() && data_add.empty() && pos + 8 <= size) { |
410 | 44.2M | if (DecodeUint32(icc, size, pos + 4) == 0) { |
411 | 19.4M | Tag subTag = DecodeKeyword(icc, size, pos); |
412 | 173M | for (size_t i = 0; i < kNumTypeStrings; i++) { |
413 | 154M | if (subTag == *kTypeStrings[i]) { |
414 | 777k | JXL_RETURN_IF_ERROR( |
415 | 777k | commands_add.push_back(kCommandTypeStartFirst + i)); |
416 | 777k | pos += 8; |
417 | 777k | break; |
418 | 777k | } |
419 | 154M | } |
420 | 19.4M | } |
421 | 44.2M | } |
422 | | |
423 | 44.7M | if (!(commands_add.empty() && data_add.empty()) || pos == size) { |
424 | 1.28M | if (last0 < last1) { |
425 | 901k | JXL_RETURN_IF_ERROR(commands.push_back(kCommandInsert)); |
426 | 901k | JXL_RETURN_IF_ERROR(EncodeVarInt(last1 - last0, &commands)); |
427 | 50.2M | while (last0 < last1) { |
428 | 49.3M | JXL_RETURN_IF_ERROR(data.push_back(icc[last0++])); |
429 | 49.3M | } |
430 | 901k | } |
431 | 2.25M | for (uint8_t b : commands_add) { |
432 | 2.25M | JXL_RETURN_IF_ERROR(commands.push_back(b)); |
433 | 2.25M | } |
434 | 28.8M | for (uint8_t b : data_add) { |
435 | 28.8M | JXL_RETURN_IF_ERROR(data.push_back(b)); |
436 | 28.8M | } |
437 | 1.28M | last0 = pos; |
438 | 1.28M | } |
439 | 44.7M | if (commands_add.empty() && data_add.empty()) { |
440 | 43.4M | pos++; |
441 | 43.4M | } |
442 | 44.7M | } |
443 | | |
444 | 2.86k | JXL_RETURN_IF_ERROR(EncodeVarInt(commands.size(), result)); |
445 | 91.5M | for (uint8_t b : commands) { |
446 | 91.5M | JXL_RETURN_IF_ERROR(result->push_back(b)); |
447 | 91.5M | } |
448 | 115M | for (uint8_t b : data) { |
449 | 115M | JXL_RETURN_IF_ERROR(result->push_back(b)); |
450 | 115M | } |
451 | | |
452 | 2.86k | return true; |
453 | 2.86k | } |
454 | | |
455 | | Status WriteICC(const Span<const uint8_t> icc, BitWriter* JXL_RESTRICT writer, |
456 | 0 | LayerType layer, AuxOut* JXL_RESTRICT aux_out) { |
457 | 0 | if (icc.empty()) return JXL_FAILURE("ICC must be non-empty"); |
458 | 0 | JxlMemoryManager* memory_manager = writer->memory_manager(); |
459 | 0 | PaddedBytes enc{memory_manager}; |
460 | 0 | JXL_RETURN_IF_ERROR(PredictICC(icc.data(), icc.size(), &enc)); |
461 | 0 | std::vector<std::vector<Token>> tokens(1); |
462 | 0 | JXL_RETURN_IF_ERROR(writer->WithMaxBits(128, layer, aux_out, [&] { |
463 | 0 | return U64Coder::Write(enc.size(), writer); |
464 | 0 | })); |
465 | | |
466 | 0 | for (size_t i = 0; i < enc.size(); i++) { |
467 | 0 | uint32_t ctx = static_cast<uint32_t>( |
468 | 0 | ICCANSContext(i, i > 0 ? enc[i - 1] : 0, i > 1 ? enc[i - 2] : 0)); |
469 | 0 | tokens[0].emplace_back(ctx, enc[i]); |
470 | 0 | } |
471 | 0 | HistogramParams params; |
472 | 0 | params.lz77_method = enc.size() < 16384 ? HistogramParams::LZ77Method::kOptimal |
473 | 0 | : HistogramParams::LZ77Method::kLZ77; |
474 | 0 | EntropyEncodingData code; |
475 | 0 | params.force_huffman = true; |
476 | 0 | JXL_ASSIGN_OR_RETURN(size_t cost, BuildAndEncodeHistograms( |
477 | 0 | memory_manager, params, kNumICCContexts, |
478 | 0 | tokens, &code, writer, layer, aux_out)); |
479 | 0 | (void)cost; |
480 | 0 | JXL_RETURN_IF_ERROR(WriteTokens(tokens[0], code, 0, writer, layer, aux_out)); |
481 | 0 | return true; |
482 | 0 | } |
483 | | |
484 | | } // namespace jxl |