Coverage Report

Created: 2026-03-31 07:44

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