Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
434
                 size_t width) {
41
434
  size_t height = (size + width - 1) / width;  // amount of rows of input
42
434
  PaddedBytes result(memory_manager);
43
434
  JXL_ASSIGN_OR_RETURN(result,
44
434
                       PaddedBytes::WithInitialSpace(memory_manager, size));
45
46
  // i = input index, j output index
47
434
  size_t s = 0;
48
434
  size_t j = 0;
49
27.0k
  for (size_t i = 0; i < size; i++) {
50
26.5k
    result[j] = data[i];
51
26.5k
    j += height;
52
26.5k
    if (j >= size) j = ++s;
53
26.5k
  }
54
55
27.0k
  for (size_t i = 0; i < size; i++) {
56
26.5k
    data[i] = result[i];
57
26.5k
  }
58
434
  return true;
59
434
}
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
119
                         PaddedBytes* result) {
67
119
  JXL_RETURN_IF_ERROR(CheckOutOfBounds(*pos, num, size));
68
119
  JxlMemoryManager* memory_manager = result->memory_manager();
69
  // Required by the specification, see decoder. stride * 4 must be < *pos.
70
119
  if (!*pos || ((*pos - 1u) >> 2u) < stride) {
71
0
    return JXL_FAILURE("Invalid stride");
72
0
  }
73
119
  if (*pos < stride * 4) return JXL_FAILURE("Too large stride");
74
119
  size_t start = result->size();
75
6.93k
  for (size_t i = 0; i < num; i++) {
76
6.81k
    uint8_t predicted =
77
6.81k
        LinearPredictICCValue(data, *pos, i, stride, width, order);
78
6.81k
    JXL_RETURN_IF_ERROR(result->push_back(data[*pos + i] - predicted));
79
6.81k
  }
80
119
  *pos += num;
81
119
  if (width > 1) {
82
119
    JXL_RETURN_IF_ERROR(
83
119
        Unshuffle(memory_manager, result->data() + start, num, width));
84
119
  }
85
119
  return true;
86
119
}
87
88
5.29k
inline Status EncodeVarInt(uint64_t value, PaddedBytes* data) {
89
5.29k
  size_t pos = data->size();
90
5.29k
  JXL_RETURN_IF_ERROR(data->resize(data->size() + 9));
91
5.29k
  size_t output_size = data->size();
92
5.29k
  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
6.44k
  while (value > 127) {
97
    // TODO(eustas): should it be `<` ?
98
1.14k
    JXL_ENSURE(pos <= output_size);
99
    // |128: Set the next byte flag
100
1.14k
    output[pos++] = (static_cast<uint8_t>(value & 127)) | 128;
101
    // Remove the seven bits we just wrote
102
1.14k
    value >>= 7;
103
1.14k
  }
104
  // TODO(eustas): should it be `<` ?
105
5.29k
  JXL_ENSURE(pos <= output_size);
106
5.29k
  output[pos++] = static_cast<uint8_t>(value & 127);
107
108
5.29k
  return data->resize(pos);
109
5.29k
}
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
334
Status PredictICC(const uint8_t* icc, size_t size, PaddedBytes* result) {
120
334
  JxlMemoryManager* memory_manager = result->memory_manager();
121
334
  PaddedBytes commands{memory_manager};
122
334
  PaddedBytes data{memory_manager};
123
124
334
  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
334
  if (size > kSizeLimit) {
128
0
    return JXL_FAILURE("ICC profile is too large");
129
0
  }
130
131
334
  JXL_RETURN_IF_ERROR(EncodeVarInt(size, result));
132
133
  // Header
134
334
  PaddedBytes header{memory_manager};
135
334
  JXL_RETURN_IF_ERROR(header.append(ICCInitialHeaderPrediction(size)));
136
43.0k
  for (size_t i = 0; i < kICCHeaderSize && i < size; i++) {
137
42.7k
    ICCPredictHeader(icc, size, header.data(), i);
138
42.7k
    JXL_RETURN_IF_ERROR(data.push_back(icc[i] - header[i]));
139
42.7k
  }
140
334
  if (size <= kICCHeaderSize) {
141
0
    JXL_RETURN_IF_ERROR(EncodeVarInt(0, result));  // 0 commands
142
0
    for (uint8_t b : data) {
143
0
      JXL_RETURN_IF_ERROR(result->push_back(b));
144
0
    }
145
0
    return true;
146
0
  }
147
148
334
  std::vector<Tag> tags;
149
334
  std::vector<size_t> tagstarts;
150
334
  std::vector<size_t> tagsizes;
151
334
  std::map<size_t, size_t> tagmap;
152
153
  // Tag list
154
334
  size_t pos = kICCHeaderSize;
155
334
  if (pos + 4 <= size) {
156
334
    uint64_t numtags = DecodeUint32(icc, size, pos);
157
334
    pos += 4;
158
334
    JXL_RETURN_IF_ERROR(EncodeVarInt(numtags + 1, &commands));
159
334
    uint64_t prevtagstart = kICCHeaderSize + numtags * 12;
160
334
    uint32_t prevtagsize = 0;
161
2.85k
    for (size_t i = 0; i < numtags; i++) {
162
2.52k
      if (pos + 12 > size) break;
163
164
2.52k
      Tag tag = DecodeKeyword(icc, size, pos + 0);
165
2.52k
      uint32_t tagstart = DecodeUint32(icc, size, pos + 4);
166
2.52k
      uint32_t tagsize = DecodeUint32(icc, size, pos + 8);
167
2.52k
      pos += 12;
168
169
2.52k
      tags.push_back(tag);
170
2.52k
      tagstarts.push_back(tagstart);
171
2.52k
      tagsizes.push_back(tagsize);
172
2.52k
      tagmap[tagstart] = tags.size() - 1;
173
174
2.52k
      uint8_t tagcode = kCommandTagUnknown;
175
19.1k
      for (size_t j = 0; j < kNumTagStrings; j++) {
176
19.1k
        if (tag == *kTagStrings[j]) {
177
2.49k
          tagcode = j + kCommandTagStringFirst;
178
2.49k
          break;
179
2.49k
        }
180
19.1k
      }
181
182
2.52k
      if (tag == kRtrcTag && pos + 24 < size) {
183
301
        bool ok = true;
184
301
        ok &= DecodeKeyword(icc, size, pos + 0) == kGtrcTag;
185
301
        ok &= DecodeKeyword(icc, size, pos + 12) == kBtrcTag;
186
301
        if (ok) {
187
2.70k
          for (size_t kk = 0; kk < 8; kk++) {
188
2.40k
            if (icc[pos - 8 + kk] != icc[pos + 4 + kk]) ok = false;
189
2.40k
            if (icc[pos - 8 + kk] != icc[pos + 16 + kk]) ok = false;
190
2.40k
          }
191
301
        }
192
301
        if (ok) {
193
301
          tagcode = kCommandTagTRC;
194
301
          pos += 24;
195
301
          i += 2;
196
301
        }
197
301
      }
198
199
2.52k
      if (tag == kRxyzTag && pos + 24 < size) {
200
304
        bool ok = true;
201
304
        ok &= DecodeKeyword(icc, size, pos + 0) == kGxyzTag;
202
304
        ok &= DecodeKeyword(icc, size, pos + 12) == kBxyzTag;
203
304
        uint32_t offsetr = tagstart;
204
304
        uint32_t offsetg = DecodeUint32(icc, size, pos + 4);
205
304
        uint32_t offsetb = DecodeUint32(icc, size, pos + 16);
206
304
        uint32_t sizer = tagsize;
207
304
        uint32_t sizeg = DecodeUint32(icc, size, pos + 8);
208
304
        uint32_t sizeb = DecodeUint32(icc, size, pos + 20);
209
304
        ok &= sizer == 20;
210
304
        ok &= sizeg == 20;
211
304
        ok &= sizeb == 20;
212
304
        ok &= (offsetg == offsetr + 20);
213
304
        ok &= (offsetb == offsetr + 40);
214
304
        if (ok) {
215
67
          tagcode = kCommandTagXYZ;
216
67
          pos += 24;
217
67
          i += 2;
218
67
        }
219
304
      }
220
221
2.52k
      uint8_t command = tagcode;
222
2.52k
      uint64_t predicted_tagstart = prevtagstart + prevtagsize;
223
2.52k
      if (predicted_tagstart != tagstart) command |= kFlagBitOffset;
224
2.52k
      size_t predicted_tagsize = prevtagsize;
225
2.52k
      if (tag == kRxyzTag || tag == kGxyzTag || tag == kBxyzTag ||
226
1.74k
          tag == kKxyzTag || tag == kWtptTag || tag == kBkptTag ||
227
1.63k
          tag == kLumiTag) {
228
1.63k
        predicted_tagsize = 20;
229
1.63k
      }
230
2.52k
      if (predicted_tagsize != tagsize) command |= kFlagBitSize;
231
2.52k
      JXL_RETURN_IF_ERROR(commands.push_back(command));
232
2.52k
      if (tagcode == 1) {
233
28
        JXL_RETURN_IF_ERROR(AppendKeyword(tag, &data));
234
28
      }
235
2.52k
      if (command & kFlagBitOffset)
236
635
        JXL_RETURN_IF_ERROR(EncodeVarInt(tagstart, &commands));
237
2.52k
      if (command & kFlagBitSize)
238
1.40k
        JXL_RETURN_IF_ERROR(EncodeVarInt(tagsize, &commands));
239
240
2.52k
      prevtagstart = tagstart;
241
2.52k
      prevtagsize = tagsize;
242
2.52k
    }
243
334
  }
244
  // Indicate end of tag list or varint indicating there's none
245
334
  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
334
  Tag tag;
254
334
  size_t tagstart = 0;
255
334
  size_t tagsize = 0;
256
334
  size_t clutstart = 0;
257
258
  // Should always check tag_sane before doing math with tagsize.
259
440
  const auto tag_sane = [&tagsize]() {
260
440
    return (tagsize > 8) && (tagsize < kSizeLimit);
261
440
  };
262
263
334
  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
71.0k
  while (pos <= size) {
272
70.7k
    size_t last1 = pos;
273
70.7k
    PaddedBytes commands_add{memory_manager};
274
70.7k
    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
70.7k
    if ((pos > tagstart + tagsize) && (tagsize < kSizeLimit)) {
279
29.5k
      tag = {{0, 0, 0, 0}};  // nonsensical value
280
29.5k
    }
281
282
70.7k
    if (commands_add.empty() && data_add.empty() && tagmap.count(pos) &&
283
2.48k
        pos + 4 <= size) {
284
2.48k
      size_t index = tagmap[pos];
285
2.48k
      tag = DecodeKeyword(icc, size, pos);
286
2.48k
      tagstart = tagstarts[index];
287
2.48k
      tagsize = tagsizes[index];
288
289
2.48k
      if (tag == kMlucTag && tag_sane() && pos + tagsize <= size &&
290
315
          icc[pos + 4] == 0 && icc[pos + 5] == 0 && icc[pos + 6] == 0 &&
291
315
          icc[pos + 7] == 0) {
292
315
        size_t num = tagsize - 8;
293
315
        JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandTypeStartFirst + 3));
294
315
        pos += 8;
295
315
        JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandShuffle2));
296
315
        JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add));
297
315
        size_t start = data_add.size();
298
20.0k
        for (size_t i = 0; i < num; i++) {
299
19.7k
          JXL_RETURN_IF_ERROR(data_add.push_back(icc[pos]));
300
19.7k
          pos++;
301
19.7k
        }
302
315
        JXL_RETURN_IF_ERROR(
303
315
            Unshuffle(memory_manager, data_add.data() + start, num, 2));
304
315
      }
305
306
2.48k
      if (tag == kCurvTag && tag_sane() && pos + tagsize <= size &&
307
116
          icc[pos + 4] == 0 && icc[pos + 5] == 0 && icc[pos + 6] == 0 &&
308
116
          icc[pos + 7] == 0) {
309
116
        size_t num = tagsize - 8;
310
116
        if (num > 16 && num < (1 << 28) && pos + num <= size && pos > 0) {
311
116
          JXL_RETURN_IF_ERROR(
312
116
              commands_add.push_back(kCommandTypeStartFirst + 5));
313
116
          pos += 8;
314
116
          JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict));
315
116
          int order = 1;
316
116
          int width = 2;
317
116
          int stride = width;
318
116
          JXL_RETURN_IF_ERROR(
319
116
              commands_add.push_back((order << 2) | (width - 1)));
320
116
          JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add));
321
116
          JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc,
322
116
                                                size, &pos, &data_add));
323
116
        }
324
116
      }
325
2.48k
    }
326
327
70.7k
    if (tag == kMab_Tag || tag == kMba_Tag) {
328
4.03k
      Tag subTag = DecodeKeyword(icc, size, pos);
329
4.03k
      if (pos + 12 < size && (subTag == kCurvTag || subTag == kVcgtTag) &&
330
0
          DecodeUint32(icc, size, pos + 4) == 0) {
331
0
        uint32_t num = DecodeUint32(icc, size, pos + 8) * 2;
332
0
        if (num > 16 && num < (1 << 28) && pos + 12 + num <= size) {
333
0
          pos += 12;
334
0
          last1 = pos;
335
0
          JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict));
336
0
          int order = 1;
337
0
          int width = 2;
338
0
          int stride = width;
339
0
          JXL_RETURN_IF_ERROR(
340
0
              commands_add.push_back((order << 2) | (width - 1)));
341
0
          JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add));
342
0
          JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc,
343
0
                                                size, &pos, &data_add));
344
0
        }
345
0
      }
346
347
4.03k
      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
26
        clutstart = tagstart + DecodeUint32(icc, size, pos);
352
26
      }
353
354
4.03k
      if (pos == clutstart && clutstart + 16 < size) {
355
13
        size_t numi = icc[tagstart + 8];
356
13
        size_t numo = icc[tagstart + 9];
357
13
        size_t width = icc[clutstart + 16];
358
13
        size_t stride = width * numo;
359
13
        size_t num = width * numo;
360
52
        for (size_t i = 0; i < numi && clutstart + i < size; i++) {
361
39
          num *= icc[clutstart + i];
362
39
        }
363
13
        if ((width == 1 || width == 2) && num > 64 && num < (1 << 28) &&
364
0
            pos + num <= size && pos > stride * 4) {
365
0
          JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict));
366
0
          int order = 1;
367
0
          uint8_t flags =
368
0
              (order << 2) | (width - 1) | (stride == width ? 0 : 16);
369
0
          JXL_RETURN_IF_ERROR(commands_add.push_back(flags));
370
0
          if (flags & 16) {
371
0
            JXL_RETURN_IF_ERROR(EncodeVarInt(stride, &commands_add));
372
0
          }
373
0
          JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add));
374
0
          JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc,
375
0
                                                size, &pos, &data_add));
376
0
        }
377
13
      }
378
4.03k
    }
379
380
70.7k
    if (commands_add.empty() && data_add.empty() && tag == kGbd_Tag &&
381
8
        tag_sane() && pos == tagstart + 8 && pos + tagsize - 8 <= size &&
382
3
        pos > 16) {
383
3
      size_t width = 4;
384
3
      size_t order = 0;
385
3
      size_t stride = width;
386
3
      size_t num = tagsize - 8;
387
3
      uint8_t flags = (order << 2) | (width - 1) | (stride == width ? 0 : 16);
388
3
      JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandPredict));
389
3
      JXL_RETURN_IF_ERROR(commands_add.push_back(flags));
390
3
      if (flags & 16) {
391
0
        JXL_RETURN_IF_ERROR(EncodeVarInt(stride, &commands_add));
392
0
      }
393
3
      JXL_RETURN_IF_ERROR(EncodeVarInt(num, &commands_add));
394
3
      JXL_RETURN_IF_ERROR(PredictAndShuffle(stride, width, order, num, icc,
395
3
                                            size, &pos, &data_add));
396
3
    }
397
398
70.7k
    if (commands_add.empty() && data_add.empty() && pos + 20 <= size) {
399
64.7k
      Tag subTag = DecodeKeyword(icc, size, pos);
400
64.7k
      if (subTag == kXyz_Tag && DecodeUint32(icc, size, pos + 4) == 0) {
401
1.24k
        JXL_RETURN_IF_ERROR(commands_add.push_back(kCommandXYZ));
402
1.24k
        pos += 8;
403
16.1k
        for (size_t j = 0; j < 12; j++) {
404
14.9k
          JXL_RETURN_IF_ERROR(data_add.push_back(icc[pos++]));
405
14.9k
        }
406
1.24k
      }
407
64.7k
    }
408
409
70.7k
    if (commands_add.empty() && data_add.empty() && pos + 8 <= size) {
410
66.6k
      if (DecodeUint32(icc, size, pos + 4) == 0) {
411
14.8k
        Tag subTag = DecodeKeyword(icc, size, pos);
412
126k
        for (size_t i = 0; i < kNumTypeStrings; i++) {
413
113k
          if (subTag == *kTypeStrings[i]) {
414
1.45k
            JXL_RETURN_IF_ERROR(
415
1.45k
                commands_add.push_back(kCommandTypeStartFirst + i));
416
1.45k
            pos += 8;
417
1.45k
            break;
418
1.45k
          }
419
113k
        }
420
14.8k
      }
421
66.6k
    }
422
423
70.7k
    if (!(commands_add.empty() && data_add.empty()) || pos == size) {
424
3.46k
      if (last0 < last1) {
425
1.81k
        JXL_RETURN_IF_ERROR(commands.push_back(kCommandInsert));
426
1.81k
        JXL_RETURN_IF_ERROR(EncodeVarInt(last1 - last0, &commands));
427
69.0k
        while (last0 < last1) {
428
67.2k
          JXL_RETURN_IF_ERROR(data.push_back(icc[last0++]));
429
67.2k
        }
430
1.81k
      }
431
4.20k
      for (uint8_t b : commands_add) {
432
4.20k
        JXL_RETURN_IF_ERROR(commands.push_back(b));
433
4.20k
      }
434
41.5k
      for (uint8_t b : data_add) {
435
41.5k
        JXL_RETURN_IF_ERROR(data.push_back(b));
436
41.5k
      }
437
3.46k
      last0 = pos;
438
3.46k
    }
439
70.7k
    if (commands_add.empty() && data_add.empty()) {
440
67.5k
      pos++;
441
67.5k
    }
442
70.7k
  }
443
444
334
  JXL_RETURN_IF_ERROR(EncodeVarInt(commands.size(), result));
445
13.7k
  for (uint8_t b : commands) {
446
13.7k
    JXL_RETURN_IF_ERROR(result->push_back(b));
447
13.7k
  }
448
151k
  for (uint8_t b : data) {
449
151k
    JXL_RETURN_IF_ERROR(result->push_back(b));
450
151k
  }
451
452
334
  return true;
453
334
}
454
455
Status WriteICC(const Span<const uint8_t> icc, BitWriter* JXL_RESTRICT writer,
456
334
                LayerType layer, AuxOut* JXL_RESTRICT aux_out) {
457
334
  if (icc.empty()) return JXL_FAILURE("ICC must be non-empty");
458
334
  JxlMemoryManager* memory_manager = writer->memory_manager();
459
334
  PaddedBytes enc{memory_manager};
460
334
  JXL_RETURN_IF_ERROR(PredictICC(icc.data(), icc.size(), &enc));
461
334
  std::vector<std::vector<Token>> tokens(1);
462
334
  JXL_RETURN_IF_ERROR(writer->WithMaxBits(128, layer, aux_out, [&] {
463
334
    return U64Coder::Write(enc.size(), writer);
464
334
  }));
465
466
166k
  for (size_t i = 0; i < enc.size(); i++) {
467
166k
    uint32_t ctx = static_cast<uint32_t>(
468
166k
        ICCANSContext(i, i > 0 ? enc[i - 1] : 0, i > 1 ? enc[i - 2] : 0));
469
166k
    tokens[0].emplace_back(ctx, enc[i]);
470
166k
  }
471
334
  HistogramParams params;
472
334
  params.lz77_method = enc.size() < 16384 ? HistogramParams::LZ77Method::kOptc256
473
334
                                         : HistogramParams::LZ77Method::kLZ77b3w3f;
474
334
  EntropyEncodingData code;
475
334
  params.force_huffman = true;
476
334
  JXL_ASSIGN_OR_RETURN(size_t cost, BuildAndEncodeHistograms(
477
334
                                        memory_manager, params, kNumICCContexts,
478
334
                                        tokens, &code, writer, layer, aux_out));
479
334
  (void)cost;
480
334
  JXL_RETURN_IF_ERROR(WriteTokens(tokens[0], code, 0, writer, layer, aux_out));
481
334
  return true;
482
334
}
483
484
}  // namespace jxl