Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/jpeg/jpeg_data.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/jpeg/jpeg_data.h"
7
8
#include <jxl/types.h>
9
10
#include <algorithm>
11
#include <cstddef>
12
#include <cstdint>
13
#include <cstring>
14
#include <hwy/base.h>
15
#include <vector>
16
17
#include "lib/jxl/base/common.h"
18
#include "lib/jxl/base/printf_macros.h"
19
#include "lib/jxl/base/status.h"
20
#include "lib/jxl/common.h"  // kMaxNumPasses, JPEGXL_ENABLE_TRANSCODE_JPEG
21
#include "lib/jxl/field_encodings.h"
22
#include "lib/jxl/fields.h"
23
24
namespace jxl {
25
namespace jpeg {
26
27
#if JPEGXL_ENABLE_TRANSCODE_JPEG
28
29
namespace {
30
enum JPEGComponentType : uint32_t {
31
  kGray = 0,
32
  kYCbCr = 1,
33
  kRGB = 2,
34
  kCustom = 3,
35
};
36
37
struct JPEGInfo {
38
  size_t num_app_markers = 0;
39
  size_t num_com_markers = 0;
40
  size_t num_scans = 0;
41
  size_t num_intermarker = 0;
42
  bool has_dri = false;
43
};
44
45
0
Status VisitMarker(uint8_t* marker, Visitor* visitor, JPEGInfo* info) {
46
0
  uint32_t marker32 = *marker - 0xc0;
47
0
  JXL_RETURN_IF_ERROR(visitor->Bits(6, 0x00, &marker32));
48
0
  *marker = marker32 + 0xc0;
49
0
  if ((*marker & 0xf0) == 0xe0) {
50
0
    info->num_app_markers++;
51
0
  }
52
0
  if (*marker == 0xfe) {
53
0
    info->num_com_markers++;
54
0
  }
55
0
  if (*marker == 0xda) {
56
0
    info->num_scans++;
57
0
  }
58
  // We use a fake 0xff marker to signal intermarker data.
59
0
  if (*marker == 0xff) {
60
0
    info->num_intermarker++;
61
0
  }
62
0
  if (*marker == 0xdd) {
63
0
    info->has_dri = true;
64
0
  }
65
0
  return true;
66
0
}
67
68
}  // namespace
69
70
0
Status JPEGData::VisitFields(Visitor* visitor) {
71
0
  bool is_gray = components.size() == 1;
72
0
  JXL_RETURN_IF_ERROR(visitor->Bool(false, &is_gray));
73
0
  if (visitor->IsReading()) {
74
0
    components.resize(is_gray ? 1 : 3);
75
0
  }
76
0
  JPEGInfo info;
77
0
  if (visitor->IsReading()) {
78
0
    uint8_t marker = 0xc0;
79
0
    do {
80
0
      JXL_RETURN_IF_ERROR(VisitMarker(&marker, visitor, &info));
81
0
      marker_order.push_back(marker);
82
0
      if (marker_order.size() > 16384) {
83
0
        return JXL_FAILURE("Too many markers: %" PRIuS "\n",
84
0
                           marker_order.size());
85
0
      }
86
0
    } while (marker != 0xd9);
87
0
  } else {
88
0
    if (marker_order.size() > 16384) {
89
0
      return JXL_FAILURE("Too many markers: %" PRIuS "\n", marker_order.size());
90
0
    }
91
0
    for (uint8_t& marker : marker_order) {
92
0
      JXL_RETURN_IF_ERROR(VisitMarker(&marker, visitor, &info));
93
0
    }
94
0
    if (!marker_order.empty()) {
95
      // Last marker should always be EOI marker.
96
0
      JXL_ENSURE(marker_order.back() == 0xd9);
97
0
    }
98
0
  }
99
100
0
  if (info.num_scans == 0) {
101
0
    return JXL_FAILURE("JPEG: no scans\n");
102
0
  }
103
104
  // Size of the APP and COM markers.
105
0
  if (visitor->IsReading()) {
106
0
    app_data.resize(info.num_app_markers);
107
0
    app_marker_type.resize(info.num_app_markers);
108
0
    com_data.resize(info.num_com_markers);
109
0
    scan_info.resize(info.num_scans);
110
0
  }
111
0
  JXL_ENSURE(app_data.size() == info.num_app_markers);
112
0
  JXL_ENSURE(app_marker_type.size() == info.num_app_markers);
113
0
  JXL_ENSURE(com_data.size() == info.num_com_markers);
114
0
  JXL_ENSURE(scan_info.size() == info.num_scans);
115
0
  for (size_t i = 0; i < app_data.size(); i++) {
116
0
    auto& app = app_data[i];
117
    // Encodes up to 8 different values.
118
0
    JXL_RETURN_IF_ERROR(
119
0
        visitor->U32(Val(0), Val(1), BitsOffset(1, 2), BitsOffset(2, 4), 0,
120
0
                     reinterpret_cast<uint32_t*>(&app_marker_type[i])));
121
0
    if (app_marker_type[i] != AppMarkerType::kUnknown &&
122
0
        app_marker_type[i] != AppMarkerType::kICC &&
123
0
        app_marker_type[i] != AppMarkerType::kExif &&
124
0
        app_marker_type[i] != AppMarkerType::kXMP) {
125
0
      return JXL_FAILURE("Unknown app marker type %u",
126
0
                         static_cast<uint32_t>(app_marker_type[i]));
127
0
    }
128
0
    uint32_t len = app.size() - 1;
129
0
    JXL_RETURN_IF_ERROR(visitor->Bits(16, 0, &len));
130
0
    if (visitor->IsReading()) app.resize(len + 1);
131
0
    if (app.size() < 3) {
132
0
      return JXL_FAILURE("Invalid marker size: %" PRIuS "\n", app.size());
133
0
    }
134
0
  }
135
0
  for (auto& com : com_data) {
136
0
    uint32_t len = com.size() - 1;
137
0
    JXL_RETURN_IF_ERROR(visitor->Bits(16, 0, &len));
138
0
    if (visitor->IsReading()) com.resize(len + 1);
139
0
    if (com.size() < 3) {
140
0
      return JXL_FAILURE("Invalid marker size: %" PRIuS "\n", com.size());
141
0
    }
142
0
  }
143
144
0
  uint32_t num_quant_tables = quant.size();
145
0
  JXL_RETURN_IF_ERROR(
146
0
      visitor->U32(Val(1), Val(2), Val(3), Val(4), 2, &num_quant_tables));
147
0
  if (num_quant_tables == 4) {
148
0
    return JXL_FAILURE("Invalid number of quant tables");
149
0
  }
150
0
  if (visitor->IsReading()) {
151
0
    quant.resize(num_quant_tables);
152
0
  }
153
0
  for (size_t i = 0; i < num_quant_tables; i++) {
154
0
    if (quant[i].precision > 1) {
155
0
      return JXL_FAILURE(
156
0
          "Quant tables with more than 16 bits are not supported");
157
0
    }
158
0
    JXL_RETURN_IF_ERROR(visitor->Bits(1, 0, &quant[i].precision));
159
0
    JXL_RETURN_IF_ERROR(visitor->Bits(2, i, &quant[i].index));
160
0
    JXL_RETURN_IF_ERROR(visitor->Bool(true, &quant[i].is_last));
161
0
  }
162
163
0
  JPEGComponentType component_type =
164
0
      components.size() == 1 && components[0].id == 1 ? JPEGComponentType::kGray
165
0
      : components.size() == 3 && components[0].id == 1 &&
166
0
              components[1].id == 2 && components[2].id == 3
167
0
          ? JPEGComponentType::kYCbCr
168
0
      : components.size() == 3 && components[0].id == 'R' &&
169
0
              components[1].id == 'G' && components[2].id == 'B'
170
0
          ? JPEGComponentType::kRGB
171
0
          : JPEGComponentType::kCustom;
172
0
  JXL_RETURN_IF_ERROR(
173
0
      visitor->Bits(2, JPEGComponentType::kYCbCr,
174
0
                    reinterpret_cast<uint32_t*>(&component_type)));
175
0
  uint32_t num_components;
176
0
  if (component_type == JPEGComponentType::kGray) {
177
0
    num_components = 1;
178
0
  } else if (component_type != JPEGComponentType::kCustom) {
179
0
    num_components = 3;
180
0
  } else {
181
0
    num_components = components.size();
182
0
    JXL_RETURN_IF_ERROR(
183
0
        visitor->U32(Val(1), Val(2), Val(3), Val(4), 3, &num_components));
184
0
    if (num_components != 1 && num_components != 3) {
185
0
      return JXL_FAILURE("Invalid number of components: %u", num_components);
186
0
    }
187
0
  }
188
0
  if (visitor->IsReading()) {
189
0
    components.resize(num_components);
190
0
  }
191
0
  if (component_type == JPEGComponentType::kCustom) {
192
0
    for (auto& component : components) {
193
0
      JXL_RETURN_IF_ERROR(visitor->Bits(8, 0, &component.id));
194
0
    }
195
0
  } else if (component_type == JPEGComponentType::kGray) {
196
0
    components[0].id = 1;
197
0
  } else if (component_type == JPEGComponentType::kRGB) {
198
0
    components[0].id = 'R';
199
0
    components[1].id = 'G';
200
0
    components[2].id = 'B';
201
0
  } else {
202
0
    components[0].id = 1;
203
0
    components[1].id = 2;
204
0
    components[2].id = 3;
205
0
  }
206
0
  size_t used_tables = 0;
207
0
  for (size_t i = 0; i < components.size(); i++) {
208
0
    JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &components[i].quant_idx));
209
0
    if (components[i].quant_idx >= quant.size()) {
210
0
      return JXL_FAILURE("Invalid quant table for component %" PRIuS ": %u\n",
211
0
                         i, components[i].quant_idx);
212
0
    }
213
0
    used_tables |= 1U << components[i].quant_idx;
214
0
  }
215
0
  for (size_t i = 0; i < quant.size(); i++) {
216
0
    if (used_tables & (1 << i)) continue;
217
0
    if (i == 0) return JXL_FAILURE("First quant table unused.");
218
    // Unused quant table has to be set to copy of previous quant table
219
0
    for (size_t j = 0; j < 64; j++) {
220
0
      if (quant[i].values[j] != quant[i - 1].values[j]) {
221
0
        return JXL_FAILURE("Non-trivial unused quant table");
222
0
      }
223
0
    }
224
0
  }
225
226
0
  uint32_t num_huff = huffman_code.size();
227
0
  JXL_RETURN_IF_ERROR(visitor->U32(Val(4), BitsOffset(3, 2), BitsOffset(4, 10),
228
0
                                   BitsOffset(6, 26), 4, &num_huff));
229
0
  if (visitor->IsReading()) {
230
0
    huffman_code.resize(num_huff);
231
0
  }
232
0
  for (JPEGHuffmanCode& hc : huffman_code) {
233
0
    bool is_ac = ((hc.slot_id >> 4) != 0);
234
0
    uint32_t id = hc.slot_id & 0xF;
235
0
    JXL_RETURN_IF_ERROR(visitor->Bool(false, &is_ac));
236
0
    JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &id));
237
0
    hc.slot_id = (static_cast<uint32_t>(is_ac) << 4) | id;
238
0
    JXL_RETURN_IF_ERROR(visitor->Bool(true, &hc.is_last));
239
0
    size_t num_symbols = 0;
240
0
    for (size_t i = 0; i <= 16; i++) {
241
0
      JXL_RETURN_IF_ERROR(visitor->U32(Val(0), Val(1), BitsOffset(3, 2),
242
0
                                       Bits(8), 0, &hc.counts[i]));
243
0
      num_symbols += hc.counts[i];
244
0
    }
245
0
    if (num_symbols == 0) {
246
      // Actually, at least 2 symbols are required, since one of them is EOI.
247
      // This case is used to represent an empty DHT marker.
248
0
      continue;
249
0
    }
250
0
    if (num_symbols > hc.values.size()) {
251
0
      return JXL_FAILURE("Huffman code too large (%" PRIuS ")", num_symbols);
252
0
    }
253
    // Presence flags for 4 * 64 + 1 values.
254
0
    uint64_t value_slots[5] = {};
255
0
    for (size_t i = 0; i < num_symbols; i++) {
256
      // Goes up to 256, included. Might have the same symbol appear twice...
257
0
      JXL_RETURN_IF_ERROR(visitor->U32(Bits(2), BitsOffset(2, 4),
258
0
                                       BitsOffset(4, 8), BitsOffset(8, 1), 0,
259
0
                                       &hc.values[i]));
260
0
      value_slots[hc.values[i] >> 6] |= static_cast<uint64_t>(1)
261
0
                                        << (hc.values[i] & 0x3F);
262
0
    }
263
0
    if (hc.values[num_symbols - 1] != kJpegHuffmanAlphabetSize) {
264
0
      return JXL_FAILURE("Missing EOI symbol");
265
0
    }
266
    // Last element, denoting EOI, have to be 1 after the loop.
267
0
    JXL_ENSURE(value_slots[4] == 1);
268
0
    size_t num_values = 1;
269
0
    for (size_t i = 0; i < 4; ++i) num_values += hwy::PopCount(value_slots[i]);
270
0
    if (num_values != num_symbols) {
271
0
      return JXL_FAILURE("Duplicate Huffman symbols");
272
0
    }
273
0
    if (!is_ac) {
274
0
      bool only_dc = ((value_slots[0] >> kJpegDCAlphabetSize) | value_slots[1] |
275
0
                      value_slots[2] | value_slots[3]) == 0;
276
0
      if (!only_dc) return JXL_FAILURE("Huffman symbols out of DC range");
277
0
    }
278
0
  }
279
280
0
  for (auto& scan : scan_info) {
281
0
    JXL_RETURN_IF_ERROR(
282
0
        visitor->U32(Val(1), Val(2), Val(3), Val(4), 1, &scan.num_components));
283
0
    if (scan.num_components >= 4) {
284
0
      return JXL_FAILURE("Invalid number of components in SOS marker");
285
0
    }
286
0
    JXL_RETURN_IF_ERROR(visitor->Bits(6, 0, &scan.Ss));
287
0
    JXL_RETURN_IF_ERROR(visitor->Bits(6, 63, &scan.Se));
288
0
    JXL_RETURN_IF_ERROR(visitor->Bits(4, 0, &scan.Al));
289
0
    JXL_RETURN_IF_ERROR(visitor->Bits(4, 0, &scan.Ah));
290
0
    for (size_t i = 0; i < scan.num_components; i++) {
291
0
      JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &scan.components[i].comp_idx));
292
0
      if (scan.components[i].comp_idx >= components.size()) {
293
0
        return JXL_FAILURE("Invalid component idx in SOS marker");
294
0
      }
295
0
      JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &scan.components[i].ac_tbl_idx));
296
0
      JXL_RETURN_IF_ERROR(visitor->Bits(2, 0, &scan.components[i].dc_tbl_idx));
297
0
    }
298
    // TODO(veluca): actually set and use this value.
299
0
    JXL_RETURN_IF_ERROR(visitor->U32(Val(0), Val(1), Val(2), BitsOffset(3, 3),
300
0
                                     kMaxNumPasses - 1,
301
0
                                     &scan.last_needed_pass));
302
0
  }
303
304
  // From here on, this is data that is not strictly necessary to get a valid
305
  // JPEG, but necessary for bit-exact JPEG reconstruction.
306
0
  if (info.has_dri) {
307
0
    JXL_RETURN_IF_ERROR(visitor->Bits(16, 0, &restart_interval));
308
0
  }
309
310
0
  for (auto& scan : scan_info) {
311
0
    uint32_t num_reset_points = scan.reset_points.size();
312
0
    JXL_RETURN_IF_ERROR(visitor->U32(Val(0), BitsOffset(2, 1), BitsOffset(4, 4),
313
0
                                     BitsOffset(16, 20), 0, &num_reset_points));
314
0
    if (visitor->IsReading()) {
315
0
      scan.reset_points.resize(num_reset_points);
316
0
    }
317
0
    int last_block_idx = -1;
318
0
    for (auto& block_idx : scan.reset_points) {
319
0
      block_idx -= last_block_idx + 1;
320
0
      JXL_RETURN_IF_ERROR(visitor->U32(Val(0), BitsOffset(3, 1),
321
0
                                       BitsOffset(5, 9), BitsOffset(28, 41), 0,
322
0
                                       &block_idx));
323
0
      block_idx += last_block_idx + 1;
324
0
      if (block_idx >= (3u << 26)) {
325
        // At most 8K x 8K x num_channels blocks are possible in a JPEG.
326
        // So valid block indices are below 3 * 2^26.
327
0
        return JXL_FAILURE("Invalid block ID: %u", block_idx);
328
0
      }
329
0
      last_block_idx = block_idx;
330
0
    }
331
332
0
    uint32_t num_extra_zero_runs = scan.extra_zero_runs.size();
333
0
    JXL_RETURN_IF_ERROR(visitor->U32(Val(0), BitsOffset(2, 1), BitsOffset(4, 4),
334
0
                                     BitsOffset(16, 20), 0,
335
0
                                     &num_extra_zero_runs));
336
0
    if (visitor->IsReading()) {
337
0
      scan.extra_zero_runs.resize(num_extra_zero_runs);
338
0
    }
339
0
    last_block_idx = -1;
340
0
    for (auto& extra_zero_run : scan.extra_zero_runs) {
341
0
      uint32_t& block_idx = extra_zero_run.block_idx;
342
0
      JXL_RETURN_IF_ERROR(visitor->U32(Val(1), BitsOffset(2, 2),
343
0
                                       BitsOffset(4, 5), BitsOffset(8, 20), 1,
344
0
                                       &extra_zero_run.num_extra_zero_runs));
345
0
      block_idx -= last_block_idx + 1;
346
0
      JXL_RETURN_IF_ERROR(visitor->U32(Val(0), BitsOffset(3, 1),
347
0
                                       BitsOffset(5, 9), BitsOffset(28, 41), 0,
348
0
                                       &block_idx));
349
0
      block_idx += last_block_idx + 1;
350
0
      if (block_idx > (3u << 26)) {
351
0
        return JXL_FAILURE("Invalid block ID: %u", block_idx);
352
0
      }
353
0
      last_block_idx = block_idx;
354
0
    }
355
0
  }
356
0
  std::vector<uint32_t> inter_marker_data_sizes;
357
0
  inter_marker_data_sizes.reserve(info.num_intermarker);
358
0
  for (size_t i = 0; i < info.num_intermarker; ++i) {
359
0
    uint32_t len = visitor->IsReading() ? 0 : inter_marker_data[i].size();
360
0
    JXL_RETURN_IF_ERROR(visitor->Bits(16, 0, &len));
361
0
    if (visitor->IsReading()) inter_marker_data_sizes.emplace_back(len);
362
0
  }
363
0
  uint32_t tail_data_len = tail_data.size();
364
0
  if (!visitor->IsReading() && tail_data_len > 4260096) {
365
0
    return JXL_FAILURE("Tail data too large (max size = 4260096, size = %u)",
366
0
                       tail_data_len);
367
0
  }
368
0
  JXL_RETURN_IF_ERROR(visitor->U32(Val(0), BitsOffset(8, 1),
369
0
                                   BitsOffset(16, 257), BitsOffset(22, 65793),
370
0
                                   0, &tail_data_len));
371
372
0
  JXL_RETURN_IF_ERROR(visitor->Bool(false, &has_zero_padding_bit));
373
0
  if (has_zero_padding_bit) {
374
0
    uint32_t nbit = padding_bits.size();
375
0
    JXL_RETURN_IF_ERROR(visitor->Bits(24, 0, &nbit));
376
0
    if (visitor->IsReading()) {
377
0
      JXL_RETURN_IF_ERROR(CheckHasEnoughBits(visitor, nbit));
378
0
      padding_bits.reserve(std::min<uint32_t>(1024u, nbit));
379
0
      for (uint32_t i = 0; i < nbit; i++) {
380
0
        bool bbit = false;
381
0
        JXL_RETURN_IF_ERROR(visitor->Bool(false, &bbit));
382
0
        padding_bits.push_back(TO_JXL_BOOL(bbit));
383
0
      }
384
0
    } else {
385
0
      for (uint8_t& bit : padding_bits) {
386
0
        bool bbit = FROM_JXL_BOOL(bit);
387
0
        JXL_RETURN_IF_ERROR(visitor->Bool(false, &bbit));
388
0
        bit = TO_JXL_BOOL(bbit);
389
0
      }
390
0
    }
391
0
  }
392
393
0
  {
394
0
    size_t dht_index = 0;
395
0
    size_t scan_index = 0;
396
0
    bool is_progressive = false;
397
0
    bool ac_ok[kMaxHuffmanTables] = {false};
398
0
    bool dc_ok[kMaxHuffmanTables] = {false};
399
0
    for (uint8_t marker : marker_order) {
400
0
      if (marker == 0xC2) {
401
0
        is_progressive = true;
402
0
      } else if (marker == 0xC4) {
403
0
        for (; dht_index < huffman_code.size();) {
404
0
          const JPEGHuffmanCode& huff = huffman_code[dht_index++];
405
0
          size_t index = huff.slot_id;
406
0
          if (index & 0x10) {
407
0
            index -= 0x10;
408
0
            ac_ok[index] = true;
409
0
          } else {
410
0
            dc_ok[index] = true;
411
0
          }
412
0
          if (huff.is_last) break;
413
0
        }
414
0
      } else if (marker == 0xDA) {
415
0
        const JPEGScanInfo& si = scan_info[scan_index++];
416
0
        for (size_t i = 0; i < si.num_components; ++i) {
417
0
          const JPEGComponentScanInfo& csi = si.components[i];
418
0
          size_t dc_tbl_idx = csi.dc_tbl_idx;
419
0
          size_t ac_tbl_idx = csi.ac_tbl_idx;
420
0
          bool want_dc = !is_progressive || (si.Ss == 0);
421
0
          if (want_dc && !dc_ok[dc_tbl_idx]) {
422
0
            return JXL_FAILURE("DC Huffman table used before defined");
423
0
          }
424
0
          bool want_ac = !is_progressive || (si.Ss != 0) || (si.Se != 0);
425
0
          if (want_ac && !ac_ok[ac_tbl_idx]) {
426
0
            return JXL_FAILURE("AC Huffman table used before defined");
427
0
          }
428
0
        }
429
0
      }
430
0
    }
431
0
  }
432
433
  // Apply postponed actions.
434
0
  if (visitor->IsReading()) {
435
0
    tail_data.resize(tail_data_len);
436
0
    JXL_ENSURE(inter_marker_data_sizes.size() == info.num_intermarker);
437
0
    inter_marker_data.reserve(info.num_intermarker);
438
0
    for (size_t i = 0; i < info.num_intermarker; ++i) {
439
0
      inter_marker_data.emplace_back(inter_marker_data_sizes[i]);
440
0
    }
441
0
  }
442
443
0
  return true;
444
0
}
445
446
#endif  // JPEGXL_ENABLE_TRANSCODE_JPEG
447
448
void JPEGData::CalculateMcuSize(const JPEGScanInfo& scan, int* MCUs_per_row,
449
0
                                int* MCU_rows) const {
450
0
  const bool is_interleaved = (scan.num_components > 1);
451
0
  const JPEGComponent& base_component = components[scan.components[0].comp_idx];
452
  // h_group / v_group act as numerators for converting number of blocks to
453
  // number of MCU. In interleaved mode it is 1, so MCU is represented with
454
  // max_*_samp_factor blocks. In non-interleaved mode we choose numerator to
455
  // be the sampling factor, consequently MCU is always represented with single
456
  // block.
457
0
  const int h_group = is_interleaved ? 1 : base_component.h_samp_factor;
458
0
  const int v_group = is_interleaved ? 1 : base_component.v_samp_factor;
459
0
  int max_h_samp_factor = 1;
460
0
  int max_v_samp_factor = 1;
461
0
  for (const auto& c : components) {
462
0
    max_h_samp_factor = std::max(c.h_samp_factor, max_h_samp_factor);
463
0
    max_v_samp_factor = std::max(c.v_samp_factor, max_v_samp_factor);
464
0
  }
465
0
  *MCUs_per_row = DivCeil(width * h_group, 8 * max_h_samp_factor);
466
0
  *MCU_rows = DivCeil(height * v_group, 8 * max_v_samp_factor);
467
0
}
468
469
#if JPEGXL_ENABLE_TRANSCODE_JPEG
470
471
Status SetJPEGDataFromICC(const std::vector<uint8_t>& icc,
472
0
                          jpeg::JPEGData* jpeg_data) {
473
0
  size_t icc_pos = 0;
474
0
  for (size_t i = 0; i < jpeg_data->app_data.size(); i++) {
475
0
    if (jpeg_data->app_marker_type[i] != jpeg::AppMarkerType::kICC) {
476
0
      continue;
477
0
    }
478
0
    if (jpeg_data->app_data[i].size() < 17) {
479
0
      return JXL_FAILURE("ICC APP marker too small: %" PRIuS,
480
0
                         jpeg_data->app_data[i].size());
481
0
    }
482
0
    size_t len = jpeg_data->app_data[i].size() - 17;
483
0
    if (icc_pos + len > icc.size()) {
484
0
      return JXL_FAILURE(
485
0
          "ICC length is less than APP markers: requested %" PRIuS
486
0
          " more bytes, "
487
0
          "%" PRIuS " available",
488
0
          len, icc.size() - icc_pos);
489
0
    }
490
0
    memcpy(&jpeg_data->app_data[i][17], icc.data() + icc_pos, len);
491
0
    icc_pos += len;
492
0
  }
493
0
  if (icc_pos != icc.size() && icc_pos != 0) {
494
0
    return JXL_FAILURE("ICC length is more than APP markers");
495
0
  }
496
0
  return true;
497
0
}
498
499
#endif  // JPEGXL_ENABLE_TRANSCODE_JPEG
500
501
}  // namespace jpeg
502
}  // namespace jxl