Coverage Report

Created: 2026-06-15 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/codecs/jpeg2000_boxes.cc
Line
Count
Source
1
/*
2
 * HEIF JPEG 2000 codec.
3
 * Copyright (c) 2023 Brad Hards <bradh@frogmouth.net>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "jpeg2000_boxes.h"
22
#include "api_structs.h"
23
#include <cstdint>
24
#include <iostream>
25
#include <cstdio>
26
27
static const uint16_t JPEG2000_CAP_MARKER = 0xFF50;
28
static const uint16_t JPEG2000_SIZ_MARKER = 0xFF51;
29
static const uint16_t JPEG2000_SOC_MARKER = 0xFF4F;
30
31
32
Error Box_cdef::parse(BitstreamRange& range, const heif_security_limits* limits)
33
0
{
34
0
  uint16_t channel_count = range.read16();
35
36
0
  if (limits->max_components && channel_count > limits->max_components) {
37
0
    std::stringstream sstr;
38
0
    sstr << "cdef box wants to define " << channel_count << " JPEG-2000 channels, but the security limit is set to "
39
0
         << limits->max_components << " components";
40
0
    return {heif_error_Invalid_input,
41
0
            heif_suberror_Security_limit_exceeded,
42
0
            sstr.str()};
43
0
  }
44
45
0
  if (channel_count > range.get_remaining_bytes() / 6) {
46
0
    std::stringstream sstr;
47
0
    sstr << "cdef box wants to define " << channel_count << " JPEG-2000 channels, but file only contains "
48
0
         << range.get_remaining_bytes() / 6 << " components";
49
0
    return {heif_error_Invalid_input,
50
0
            heif_suberror_End_of_data,
51
0
            sstr.str()};
52
0
  }
53
54
0
  m_channels.resize(channel_count);
55
56
0
  for (uint16_t i = 0; i < channel_count && !range.error() && !range.eof(); i++) {
57
0
    Channel channel;
58
0
    channel.channel_index = range.read16();
59
0
    channel.channel_type = range.read16();
60
0
    channel.channel_association = range.read16();
61
0
    m_channels[i] = channel;
62
0
  }
63
64
0
  return range.get_error();
65
0
}
66
67
std::string Box_cdef::dump(Indent& indent) const
68
0
{
69
0
  std::ostringstream sstr;
70
0
  sstr << Box::dump(indent);
71
72
0
  for (const auto& channel : m_channels) {
73
0
    sstr << indent << "channel_index: " << channel.channel_index
74
0
         << ", channel_type: " << channel.channel_type
75
0
         << ", channel_association: " << channel.channel_association << "\n";
76
0
  }
77
78
0
  return sstr.str();
79
0
}
80
81
82
Error Box_cdef::write(StreamWriter& writer) const
83
0
{
84
0
  size_t box_start = reserve_box_header_space(writer);
85
86
0
  writer.write16((uint16_t) m_channels.size());
87
0
  for (const auto& channel : m_channels) {
88
0
    writer.write16(channel.channel_index);
89
0
    writer.write16(channel.channel_type);
90
0
    writer.write16(channel.channel_association);
91
0
  }
92
93
0
  prepend_header(writer, box_start);
94
95
0
  return Error::Ok;
96
0
}
97
98
99
void Box_cdef::set_channels(heif_colorspace colorspace)
100
0
{
101
  // TODO - Check for the presence of a cmap box which specifies channel indices.
102
103
0
  const uint16_t TYPE_COLOR = 0;
104
0
  const uint16_t ASOC_GREY = 1;
105
0
  const uint16_t ASOC_RED = 1;
106
0
  const uint16_t ASOC_GREEN = 2;
107
0
  const uint16_t ASOC_BLUE = 3;
108
0
  const uint16_t ASOC_Y = 1;
109
0
  const uint16_t ASOC_Cb = 2;
110
0
  const uint16_t ASOC_Cr = 3;
111
112
0
  switch (colorspace) {
113
0
    case heif_colorspace_RGB:
114
0
      m_channels.push_back({0, TYPE_COLOR, ASOC_RED});
115
0
      m_channels.push_back({1, TYPE_COLOR, ASOC_GREEN});
116
0
      m_channels.push_back({2, TYPE_COLOR, ASOC_BLUE});
117
0
      break;
118
119
0
    case heif_colorspace_YCbCr:
120
0
      m_channels.push_back({0, TYPE_COLOR, ASOC_Y});
121
0
      m_channels.push_back({1, TYPE_COLOR, ASOC_Cb});
122
0
      m_channels.push_back({2, TYPE_COLOR, ASOC_Cr});
123
0
      break;
124
125
0
    case heif_colorspace_monochrome:
126
0
      m_channels.push_back({0, TYPE_COLOR, ASOC_GREY});
127
0
      break;
128
129
0
    default:
130
      //TODO - Handle remaining cases.
131
0
      break;
132
0
  }
133
0
}
134
135
Error Box_cmap::parse(BitstreamRange& range, const heif_security_limits* limits)
136
0
{
137
0
  while (!range.eof() && !range.error()) {
138
0
    Component component;
139
0
    component.component_index = range.read16();
140
0
    component.mapping_type = range.read8();
141
0
    component.palette_colour = range.read8();
142
0
    m_components.push_back(component);
143
0
  }
144
145
0
  return range.get_error();
146
0
}
147
148
149
std::string Box_cmap::dump(Indent& indent) const
150
0
{
151
0
  std::ostringstream sstr;
152
0
  sstr << Box::dump(indent);
153
154
0
  for (const auto& component : m_components) {
155
0
    sstr << indent << "component_index: " << component.component_index
156
0
         << ", mapping_type: " << (int) (component.mapping_type)
157
0
         << ", palette_colour: " << (int) (component.palette_colour) << "\n";
158
0
  }
159
160
0
  return sstr.str();
161
0
}
162
163
164
Error Box_cmap::write(StreamWriter& writer) const
165
0
{
166
0
  size_t box_start = reserve_box_header_space(writer);
167
168
0
  for (const auto& component : m_components) {
169
0
    writer.write16(component.component_index);
170
0
    writer.write8(component.mapping_type);
171
0
    writer.write8(component.palette_colour);
172
0
  }
173
174
0
  prepend_header(writer, box_start);
175
176
0
  return Error::Ok;
177
0
}
178
179
180
Error Box_pclr::parse(BitstreamRange& range, const heif_security_limits* limits)
181
0
{
182
0
  uint16_t num_entries = range.read16();
183
0
  uint8_t num_palette_columns = range.read8();
184
0
  for (uint8_t i = 0; i < num_palette_columns; i++) {
185
0
    uint8_t bit_depth = range.read8();
186
0
    if (bit_depth & 0x80) {
187
0
      return Error(heif_error_Unsupported_feature,
188
0
                   heif_suberror_Unsupported_data_version,
189
0
                   "pclr with signed data is not supported");
190
0
    }
191
0
    if (bit_depth > 16) {
192
0
      return Error(heif_error_Unsupported_feature,
193
0
                   heif_suberror_Unsupported_data_version,
194
0
                   "pclr more than 16 bits per channel is not supported");
195
0
    }
196
0
    m_bitDepths.push_back(bit_depth);
197
0
  }
198
0
  for (uint16_t j = 0; j < num_entries; j++) {
199
0
    PaletteEntry entry;
200
0
    for (unsigned long int i = 0; i < entry.columns.size(); i++) {
201
0
      if (m_bitDepths[i] <= 8) {
202
0
        entry.columns.push_back(range.read8());
203
0
      }
204
0
      else {
205
0
        entry.columns.push_back(range.read16());
206
0
      }
207
0
    }
208
0
    m_entries.push_back(entry);
209
0
  }
210
211
0
  return range.get_error();
212
0
}
213
214
215
std::string Box_pclr::dump(Indent& indent) const
216
0
{
217
0
  std::ostringstream sstr;
218
0
  sstr << Box::dump(indent);
219
220
0
  sstr << indent << "NE: " << m_entries.size();
221
0
  sstr << ", NPC: " << (int) get_num_columns();
222
0
  sstr << ", B: ";
223
0
  for (uint8_t b : m_bitDepths) {
224
0
    sstr << (int) b << ", ";
225
0
  }
226
  // TODO: maybe dump entries too?
227
0
  sstr << "\n";
228
229
0
  return sstr.str();
230
0
}
231
232
233
Error Box_pclr::write(StreamWriter& writer) const
234
0
{
235
0
  if (get_num_columns() == 0) {
236
    // skip
237
0
    return Error::Ok;
238
0
  }
239
240
0
  size_t box_start = reserve_box_header_space(writer);
241
242
0
  writer.write16(get_num_entries());
243
0
  writer.write8(get_num_columns());
244
0
  for (uint8_t b : m_bitDepths) {
245
0
    writer.write8(b);
246
0
  }
247
0
  for (PaletteEntry entry : m_entries) {
248
0
    for (unsigned long int i = 0; i < entry.columns.size(); i++) {
249
0
      if (m_bitDepths[i] <= 8) {
250
0
        writer.write8((uint8_t) (entry.columns[i]));
251
0
      }
252
0
      else {
253
0
        writer.write16(entry.columns[i]);
254
0
      }
255
0
    }
256
0
  }
257
258
0
  prepend_header(writer, box_start);
259
260
0
  return Error::Ok;
261
0
}
262
263
void Box_pclr::set_columns(uint8_t num_columns, uint8_t bit_depth)
264
0
{
265
0
  m_bitDepths.clear();
266
0
  m_entries.clear();
267
0
  for (int i = 0; i < num_columns; i++) {
268
0
    m_bitDepths.push_back(bit_depth);
269
0
  }
270
0
}
271
272
Error Box_j2kL::parse(BitstreamRange& range, const heif_security_limits* limits)
273
0
{
274
0
  uint16_t layer_count = range.read16();
275
276
0
  if (layer_count > range.get_remaining_bytes() / (2+1+2)) {
277
0
    std::stringstream sstr;
278
0
    sstr << "j2kL box wants to define " << layer_count << "JPEG-2000 layers, but the box only contains "
279
0
         << range.get_remaining_bytes() / (2 + 1 + 2) << " layers entries";
280
0
    return {heif_error_Invalid_input,
281
0
            heif_suberror_End_of_data,
282
0
            sstr.str()};
283
0
  }
284
285
0
  m_layers.resize(layer_count);
286
287
0
  for (int i = 0; i < layer_count && !range.error() && !range.eof(); i++) {
288
0
    Layer layer;
289
0
    layer.layer_id = range.read16();
290
0
    layer.discard_levels = range.read8();
291
0
    layer.decode_layers = range.read16();
292
0
    m_layers[i] = layer;
293
0
  }
294
295
0
  if (range.get_error()) {
296
0
    m_layers.clear();
297
0
  }
298
299
0
  return range.get_error();
300
0
}
301
302
std::string Box_j2kL::dump(Indent& indent) const
303
0
{
304
0
  std::ostringstream sstr;
305
0
  sstr << Box::dump(indent);
306
307
0
  for (const auto& layer : m_layers) {
308
0
    sstr << indent << "layer_id: " << layer.layer_id
309
0
         << ", discard_levels: " << (int) (layer.discard_levels)
310
0
         << ", decode_layers: " << layer.decode_layers << "\n";
311
0
  }
312
313
0
  return sstr.str();
314
0
}
315
316
317
Error Box_j2kL::write(StreamWriter& writer) const
318
0
{
319
0
  size_t box_start = reserve_box_header_space(writer);
320
321
0
  writer.write16((uint16_t) m_layers.size());
322
0
  for (const auto& layer : m_layers) {
323
0
    writer.write16(layer.layer_id);
324
0
    writer.write8(layer.discard_levels);
325
0
    writer.write16(layer.decode_layers);
326
0
  }
327
328
0
  prepend_header(writer, box_start);
329
330
0
  return Error::Ok;
331
0
}
332
333
334
Error Box_j2kH::parse(BitstreamRange& range, const heif_security_limits* limits)
335
0
{
336
0
  return read_children(range, READ_CHILDREN_ALL, limits);
337
0
}
338
339
std::string Box_j2kH::dump(Indent& indent) const
340
0
{
341
0
  std::ostringstream sstr;
342
0
  sstr << Box::dump(indent);
343
344
0
  sstr << dump_children(indent);
345
346
0
  return sstr.str();
347
0
}
348
349
350
Error JPEG2000MainHeader::parseHeader(const std::vector<uint8_t>& compressedImageData)
351
0
{
352
  // TODO: it is very inefficient to store the whole image data when we only need the header
353
354
0
  headerData = compressedImageData;
355
0
  return doParse();
356
0
}
357
358
Error JPEG2000MainHeader::doParse()
359
0
{
360
0
  cursor = 0;
361
0
  Error err = parse_SOC_segment();
362
0
  if (err) {
363
0
    return err;
364
0
  }
365
0
  err = parse_SIZ_segment();
366
0
  if (err) {
367
0
    return err;
368
0
  }
369
0
  if (cursor < headerData.size() - MARKER_LEN) {
370
0
    uint16_t marker = read16();
371
0
    if (marker == JPEG2000_CAP_MARKER) {
372
0
      return parse_CAP_segment_body();
373
0
    }
374
0
    return Error::Ok;
375
0
  }
376
  // we should have at least COD and QCD, so this is probably broken.
377
0
  return Error(heif_error_Invalid_input,
378
0
               heif_suberror_Invalid_J2K_codestream,
379
0
               std::string("Missing required header marker(s)"));
380
0
}
381
382
Error JPEG2000MainHeader::parse_SOC_segment()
383
0
{
384
0
  const size_t REQUIRED_BYTES = MARKER_LEN;
385
0
  if ((headerData.size() < REQUIRED_BYTES) || (cursor > (headerData.size() - REQUIRED_BYTES))) {
386
0
    return Error(heif_error_Invalid_input,
387
0
                 heif_suberror_Invalid_J2K_codestream);
388
0
  }
389
0
  uint16_t marker = read16();
390
0
  if (marker == JPEG2000_SOC_MARKER) {
391
0
    return Error::Ok;
392
0
  }
393
0
  return Error(heif_error_Invalid_input,
394
0
               heif_suberror_Invalid_J2K_codestream,
395
0
               std::string("Missing required SOC Marker"));
396
0
}
397
398
Error JPEG2000MainHeader::parse_SIZ_segment()
399
0
{
400
0
  size_t REQUIRED_BYTES = MARKER_LEN + 38 + 3 * 1;
401
0
  if ((headerData.size() < REQUIRED_BYTES) || (cursor > (headerData.size() - REQUIRED_BYTES))) {
402
0
    return Error(heif_error_Invalid_input,
403
0
                 heif_suberror_Invalid_J2K_codestream);
404
0
  }
405
406
0
  uint16_t marker = read16();
407
0
  if (marker != JPEG2000_SIZ_MARKER) {
408
0
    return Error(heif_error_Invalid_input,
409
0
                 heif_suberror_Invalid_J2K_codestream,
410
0
                 std::string("Missing required SIZ Marker"));
411
0
  }
412
0
  uint16_t lsiz = read16();
413
0
  if ((lsiz < 41) || (lsiz > 49190)) {
414
0
    return Error(heif_error_Invalid_input,
415
0
                 heif_suberror_Invalid_J2K_codestream,
416
0
                 std::string("Out of range Lsiz value"));
417
0
  }
418
0
  siz.decoder_capabilities = read16();
419
0
  siz.reference_grid_width = read32();
420
0
  siz.reference_grid_height = read32();
421
0
  siz.image_horizontal_offset = read32();
422
0
  siz.image_vertical_offset = read32();
423
0
  siz.tile_width = read32();
424
0
  siz.tile_height = read32();
425
0
  siz.tile_offset_x = read32();
426
0
  siz.tile_offset_y = read32();
427
0
  uint16_t csiz = read16();
428
0
  if ((csiz < 1) || (csiz > 16384)) {
429
0
    return Error(heif_error_Invalid_input,
430
0
                 heif_suberror_Invalid_J2K_codestream,
431
0
                 std::string("Out of range Csiz value"));
432
0
  }
433
0
  if (3 * static_cast<size_t>(csiz) > headerData.size() - cursor) {
434
0
    return Error(heif_error_Invalid_input,
435
0
                 heif_suberror_Invalid_J2K_codestream);
436
0
  }
437
  // TODO: consider checking for Lsiz consistent with Csiz
438
0
  for (uint16_t c = 0; c < csiz; c++) {
439
0
    JPEG2000_SIZ_segment::component comp;
440
0
    uint8_t ssiz = read8();
441
0
    comp.is_signed = (ssiz & 0x80);
442
0
    comp.precision = uint8_t((ssiz & 0x7F) + 1);
443
0
    comp.h_separation = read8();
444
0
    comp.v_separation = read8();
445
0
    siz.components.push_back(comp);
446
0
  }
447
0
  return Error::Ok;
448
0
}
449
450
Error JPEG2000MainHeader::parse_CAP_segment_body()
451
0
{
452
  // Need at least Lcap (2) + Pcap (4) = 6 bytes.
453
0
  if (headerData.size() - cursor < 6) {
454
0
    return Error(heif_error_Invalid_input,
455
0
                 heif_suberror_Invalid_J2K_codestream);
456
0
  }
457
0
  uint16_t lcap = read16();
458
0
  if ((lcap < 8) || (lcap > 70)) {
459
0
    return Error(heif_error_Invalid_input,
460
0
                 heif_suberror_Invalid_J2K_codestream,
461
0
                 std::string("Out of range Lcap value"));
462
0
  }
463
  // Lcap counts the Lcap field itself, so Lcap-2 bytes must follow it.
464
0
  if (headerData.size() - cursor < static_cast<size_t>(lcap) - 2) {
465
0
    return Error(heif_error_Invalid_input,
466
0
                 heif_suberror_Invalid_J2K_codestream);
467
0
  }
468
0
  uint32_t pcap = read32();
469
0
  size_t segment_end = cursor + (static_cast<size_t>(lcap) - 6);
470
0
  for (uint8_t i = 2; i <= 32; i++) {
471
0
    if (pcap & (1u << (32 - i))) {
472
0
      if (segment_end - cursor < 2) {
473
0
        return Error(heif_error_Invalid_input,
474
0
                     heif_suberror_Invalid_J2K_codestream,
475
0
                     std::string("CAP segment Pcap inconsistent with Lcap"));
476
0
      }
477
0
      switch (i) {
478
0
        case JPEG2000_Extension_Capability_HT::IDENT:
479
0
          parse_Ccap15();
480
0
          break;
481
0
        default:
482
0
          read16();
483
0
      }
484
0
    }
485
0
  }
486
0
  return Error::Ok;
487
0
}
488
489
void JPEG2000MainHeader::parse_Ccap15()
490
0
{
491
0
  uint16_t val = read16();
492
0
  JPEG2000_Extension_Capability_HT ccap;
493
  // We could parse more here, but we don't need that yet.
494
0
  ccap.setValue(val);
495
0
  cap.push_back(ccap);
496
0
}
497
498
heif_chroma JPEG2000MainHeader::get_chroma_format() const
499
0
{
500
  // Y-plane must be full resolution
501
0
  if (siz.components[0].h_separation != 1 || siz.components[0].v_separation != 1) {
502
0
    return heif_chroma_undefined;
503
0
  }
504
505
0
  if (siz.components.size() == 1) {
506
0
    return heif_chroma_monochrome;
507
0
  }
508
0
  else if (siz.components.size() == 3) {
509
    // TODO: we should map channels through `cdef` ?
510
511
    // both chroma components must have the same sampling
512
0
    if (siz.components[1].h_separation != siz.components[2].h_separation ||
513
0
        siz.components[1].v_separation != siz.components[2].v_separation) {
514
0
      return heif_chroma_undefined;
515
0
    }
516
517
0
    if (siz.components[1].h_separation == 2 && siz.components[1].v_separation==2) {
518
0
      return heif_chroma_420;
519
0
    }
520
0
    if (siz.components[1].h_separation == 2 && siz.components[1].v_separation==1) {
521
0
      return heif_chroma_422;
522
0
    }
523
0
    if (siz.components[1].h_separation == 1 && siz.components[1].v_separation==1) {
524
0
      return heif_chroma_444;
525
0
    }
526
0
  }
527
528
0
  return heif_chroma_undefined;
529
0
}