Coverage Report

Created: 2026-06-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/nclx.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2020 Dirk Farin <dirk.farin@gmail.com>
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
22
#include "nclx.h"
23
#include "libheif/heif_experimental.h"
24
25
#include <cassert>
26
#include <memory>
27
#include <string>
28
#include <vector>
29
30
31
primaries::primaries(float gx, float gy, float bx, float by, float rx, float ry, float wx, float wy)
32
6
{
33
6
  defined = true;
34
6
  redX = rx;
35
6
  redY = ry;
36
6
  greenX = gx;
37
6
  greenY = gy;
38
6
  blueX = bx;
39
6
  blueY = by;
40
6
  whiteX = wx;
41
6
  whiteY = wy;
42
6
}
43
44
45
primaries get_colour_primaries(uint16_t primaries_idx)
46
7
{
47
7
  switch (primaries_idx) {
48
0
    case 1:
49
0
      return {0.300f, 0.600f, 0.150f, 0.060f, 0.640f, 0.330f, 0.3127f, 0.3290f};
50
0
    case 4:
51
0
      return {0.21f, 0.71f, 0.14f, 0.08f, 0.67f, 0.33f, 0.310f, 0.316f};
52
0
    case 5:
53
0
      return {0.29f, 0.60f, 0.15f, 0.06f, 0.64f, 0.33f, 0.3127f, 0.3290f};
54
1
    case 6:
55
2
    case 7:
56
2
      return {0.310f, 0.595f, 0.155f, 0.070f, 0.630f, 0.340f, 0.3127f, 0.3290f};
57
0
    case 8:
58
0
      return {0.243f, 0.692f, 0.145f, 0.049f, 0.681f, 0.319f, 0.310f, 0.316f};
59
0
    case 9:
60
0
      return {0.170f, 0.797f, 0.131f, 0.046f, 0.708f, 0.292f, 0.3127f, 0.3290f};
61
1
    case 10:
62
1
      return {0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.333333f, 0.33333f};
63
1
    case 11:
64
1
      return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.314f, 0.351f};
65
2
    case 12:
66
2
      return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.3127f, 0.3290f};
67
0
    case 22:
68
0
      return {0.295f, 0.605f, 0.155f, 0.077f, 0.630f, 0.340f, 0.3127f, 0.3290f};
69
1
    default:
70
1
      return {};
71
7
  }
72
7
}
73
74
Kr_Kb Kr_Kb::defaults()
75
0
{
76
0
  Kr_Kb kr_kb;
77
  // Rec 601.
78
0
  kr_kb.Kr = 0.2990f;
79
0
  kr_kb.Kb = 0.1140f;
80
0
  return kr_kb;
81
0
}
82
83
84
Kr_Kb get_Kr_Kb(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
85
1.37k
{
86
1.37k
  Kr_Kb result;
87
88
1.37k
  if (matrix_coefficients_idx == 12 ||
89
1.37k
      matrix_coefficients_idx == 13) {
90
91
0
    primaries p = get_colour_primaries(primaries_idx);
92
0
    float zr = 1 - (p.redX + p.redY);
93
0
    float zg = 1 - (p.greenX + p.greenY);
94
0
    float zb = 1 - (p.blueX + p.blueY);
95
0
    float zw = 1 - (p.whiteX + p.whiteY);
96
97
0
    float denom = p.whiteY * (p.redX * (p.greenY * zb - p.blueY * zg) + p.greenX * (p.blueY * zr - p.redY * zb) +
98
0
                              p.blueX * (p.redY * zg - p.greenY * zr));
99
100
0
    if (denom == 0.0f) {
101
0
      return result;
102
0
    }
103
104
0
    result.Kr = (p.redY * (p.whiteX * (p.greenY * zb - p.blueY * zg) + p.whiteY * (p.blueX * zg - p.greenX * zb) +
105
0
                           zw * (p.greenX * p.blueY - p.blueX * p.greenY))) / denom;
106
0
    result.Kb = (p.blueY * (p.whiteX * (p.redY * zg - p.greenY * zr) + p.whiteY * (p.greenX * zr - p.redX * zg) +
107
0
                            zw * (p.redX * p.greenY - p.greenX * p.redY))) / denom;
108
0
  }
109
1.37k
  else
110
1.37k
    switch (matrix_coefficients_idx) {
111
4
      case 1:
112
4
        result.Kr = 0.2126f;
113
4
        result.Kb = 0.0722f;
114
4
        break;
115
3
      case 4:
116
3
        result.Kr = 0.30f;
117
3
        result.Kb = 0.11f;
118
3
        break;
119
4
      case 5:
120
854
      case 6:
121
854
        result.Kr = 0.299f;
122
854
        result.Kb = 0.114f;
123
854
        break;
124
0
      case 7:
125
0
        result.Kr = 0.212f;
126
0
        result.Kb = 0.087f;
127
0
        break;
128
0
      case 9:
129
11
      case 10:
130
        // TODO: case 10 is BT.2020 constant-luminance, which is not a linear matrix conversion
131
        //   (Y' is derived from gamma-corrected linear luminance; Cb/Cr need EOTF inversion).
132
        //   We currently fall through to the NCL coefficients, which is wrong for CL content.
133
11
        result.Kr = 0.2627f;
134
11
        result.Kb = 0.0593f;
135
11
        break;
136
501
      default:;
137
1.37k
    }
138
139
1.37k
  return result;
140
1.37k
}
141
142
143
YCbCr_to_RGB_coefficients YCbCr_to_RGB_coefficients::defaults()
144
2.17k
{
145
2.17k
  YCbCr_to_RGB_coefficients coeffs;
146
2.17k
  coeffs.defined = true;
147
2.17k
  coeffs.r_cr = 1.402f;
148
2.17k
  coeffs.g_cb = -0.344136f;
149
2.17k
  coeffs.g_cr = -0.714136f;
150
2.17k
  coeffs.b_cb = 1.772f;
151
2.17k
  return coeffs;
152
2.17k
}
153
154
YCbCr_to_RGB_coefficients
155
get_YCbCr_to_RGB_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
156
1.37k
{
157
1.37k
  YCbCr_to_RGB_coefficients coeffs;
158
159
1.37k
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
160
161
1.37k
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
162
872
    coeffs.defined = true;
163
872
    coeffs.r_cr = 2 * (-k.Kr + 1);
164
872
    coeffs.g_cb = 2 * k.Kb * (-k.Kb + 1) / (k.Kb + k.Kr - 1);
165
872
    coeffs.g_cr = 2 * k.Kr * (-k.Kr + 1) / (k.Kb + k.Kr - 1);
166
872
    coeffs.b_cb = 2 * (-k.Kb + 1);
167
872
  }
168
501
  else {
169
501
    coeffs = YCbCr_to_RGB_coefficients::defaults();
170
501
  }
171
172
1.37k
  return coeffs;
173
1.37k
}
174
175
176
RGB_to_YCbCr_coefficients
177
get_RGB_to_YCbCr_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
178
0
{
179
0
  RGB_to_YCbCr_coefficients coeffs;
180
181
0
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
182
183
0
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
184
0
    coeffs.defined = true;
185
0
    coeffs.c[0][0] = k.Kr;
186
0
    coeffs.c[0][1] = 1 - k.Kr - k.Kb;
187
0
    coeffs.c[0][2] = k.Kb;
188
0
    coeffs.c[1][0] = -k.Kr / (1 - k.Kb) / 2;
189
0
    coeffs.c[1][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kb) / 2;
190
0
    coeffs.c[1][2] = 0.5f;
191
0
    coeffs.c[2][0] = 0.5f;
192
0
    coeffs.c[2][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kr) / 2;
193
0
    coeffs.c[2][2] = -k.Kb / (1 - k.Kr) / 2;
194
0
  }
195
0
  else {
196
0
    coeffs = RGB_to_YCbCr_coefficients::defaults();
197
0
  }
198
199
0
  return coeffs;
200
0
}
201
202
203
RGB_to_YCbCr_coefficients RGB_to_YCbCr_coefficients::defaults()
204
0
{
205
0
  RGB_to_YCbCr_coefficients coeffs;
206
0
  coeffs.defined = true;
207
  // Rec 601 full. Kr=0.2990f Kb=0.1140f.
208
0
  coeffs.c[0][0] = 0.299f;
209
0
  coeffs.c[0][1] = 0.587f;
210
0
  coeffs.c[0][2] = 0.114f;
211
0
  coeffs.c[1][0] = -0.168735f;
212
0
  coeffs.c[1][1] = -0.331264f;
213
0
  coeffs.c[1][2] = 0.5f;
214
0
  coeffs.c[2][0] = 0.5f;
215
0
  coeffs.c[2][1] = -0.418688f;
216
0
  coeffs.c[2][2] = -0.081312f;
217
218
0
  return coeffs;
219
0
}
220
221
222
Error color_profile_nclx::parse(BitstreamRange& range)
223
192
{
224
192
  StreamReader::grow_status status;
225
192
  status = range.wait_for_available_bytes(7);
226
192
  if (status != StreamReader::grow_status::size_reached) {
227
    // TODO: return recoverable error at timeout
228
0
    return Error(heif_error_Invalid_input,
229
0
                 heif_suberror_End_of_data);
230
0
  }
231
232
192
  m_profile.m_colour_primaries = range.read16();
233
192
  m_profile.m_transfer_characteristics = range.read16();
234
192
  m_profile.m_matrix_coefficients = range.read16();
235
192
  m_profile.m_full_range_flag = (range.read8() & 0x80 ? true : false);
236
237
192
  return Error::Ok;
238
192
}
239
240
Error color_profile_nclx::parse_nclc(BitstreamRange& range)
241
4
{
242
4
  StreamReader::grow_status status;
243
4
  status = range.wait_for_available_bytes(6);
244
4
  if (status != StreamReader::grow_status::size_reached) {
245
    // TODO: return recoverable error at timeout
246
0
    return Error(heif_error_Invalid_input,
247
0
                 heif_suberror_End_of_data);
248
0
  }
249
250
4
  m_profile.m_colour_primaries = range.read16();
251
4
  m_profile.m_transfer_characteristics = range.read16();
252
4
  m_profile.m_matrix_coefficients = range.read16();
253
254
  // use full range for RGB, limited range otherwise
255
4
  m_profile.m_full_range_flag = (m_profile.m_matrix_coefficients == 0);
256
257
4
  return Error::Ok;
258
4
}
259
260
Error nclx_profile::get_nclx_color_profile(heif_color_profile_nclx** out_data) const
261
11
{
262
11
  *out_data = nullptr;
263
264
11
  heif_color_profile_nclx* nclx = heif_nclx_color_profile_alloc();
265
266
11
  if (nclx == nullptr) {
267
0
    return Error(heif_error_Memory_allocation_error,
268
0
                 heif_suberror_Unspecified);
269
0
  }
270
271
11
  heif_error err;
272
273
11
  nclx->version = 1;
274
275
11
  err = heif_nclx_color_profile_set_color_primaries(nclx, get_colour_primaries());
276
11
  if (err.code) {
277
3
    heif_nclx_color_profile_free(nclx);
278
3
    return {err.code, err.subcode};
279
3
  }
280
281
8
  err = heif_nclx_color_profile_set_transfer_characteristics(nclx, get_transfer_characteristics());
282
8
  if (err.code) {
283
1
    heif_nclx_color_profile_free(nclx);
284
1
    return {err.code, err.subcode};
285
1
  }
286
287
7
  err = heif_nclx_color_profile_set_matrix_coefficients(nclx, get_matrix_coefficients());
288
7
  if (err.code) {
289
0
    heif_nclx_color_profile_free(nclx);
290
0
    return {err.code, err.subcode};
291
0
  }
292
293
7
  nclx->full_range_flag = get_full_range_flag();
294
295
  // fill color primaries
296
297
7
  auto primaries = ::get_colour_primaries(nclx->color_primaries);
298
299
7
  nclx->color_primary_red_x = primaries.redX;
300
7
  nclx->color_primary_red_y = primaries.redY;
301
7
  nclx->color_primary_green_x = primaries.greenX;
302
7
  nclx->color_primary_green_y = primaries.greenY;
303
7
  nclx->color_primary_blue_x = primaries.blueX;
304
7
  nclx->color_primary_blue_y = primaries.blueY;
305
7
  nclx->color_primary_white_x = primaries.whiteX;
306
7
  nclx->color_primary_white_y = primaries.whiteY;
307
308
7
  *out_data = nclx;
309
310
7
  return Error::Ok;
311
7
}
312
313
314
void nclx_profile::set_sRGB_defaults()
315
2.15k
{
316
  // sRGB defaults
317
2.15k
  m_colour_primaries = 1;
318
2.15k
  m_transfer_characteristics = 13;
319
2.15k
  m_matrix_coefficients = 6;
320
2.15k
  m_full_range_flag = true;
321
2.15k
}
322
323
324
void nclx_profile::set_undefined()
325
0
{
326
0
  m_colour_primaries = 2;
327
0
  m_transfer_characteristics = 2;
328
0
  m_matrix_coefficients = 2;
329
0
  m_full_range_flag = true;
330
0
}
331
332
333
bool nclx_profile::is_undefined() const
334
1.84k
{
335
1.84k
  return *this == nclx_profile::undefined();
336
1.84k
}
337
338
339
void nclx_profile::set_from_heif_color_profile_nclx(const heif_color_profile_nclx* nclx)
340
0
{
341
0
  if (nclx) {
342
0
    m_colour_primaries = nclx->color_primaries;
343
0
    m_transfer_characteristics = nclx->transfer_characteristics;
344
0
    m_matrix_coefficients = nclx->matrix_coefficients;
345
0
    m_full_range_flag = nclx->full_range_flag;
346
0
  }
347
0
}
348
349
350
void nclx_profile::copy_to_heif_color_profile_nclx(heif_color_profile_nclx* nclx)
351
0
{
352
0
  assert(nclx);
353
0
  nclx->color_primaries = (enum heif_color_primaries)m_colour_primaries;
354
0
  nclx->transfer_characteristics = (enum heif_transfer_characteristics)m_transfer_characteristics;
355
0
  nclx->matrix_coefficients = (enum heif_matrix_coefficients)m_matrix_coefficients;
356
0
  nclx->full_range_flag = m_full_range_flag;
357
0
}
358
359
360
void nclx_profile::replace_undefined_values_with_sRGB_defaults()
361
1.73k
{
362
1.73k
  if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) {
363
671
    m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6;
364
671
  }
365
366
1.73k
  if (m_colour_primaries == heif_color_primaries_unspecified) {
367
749
    m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5;
368
749
  }
369
370
1.73k
  if (m_transfer_characteristics == heif_transfer_characteristic_unspecified) {
371
859
    m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1;
372
859
  }
373
1.73k
}
374
375
376
bool nclx_profile::equal_except_transfer_curve(const nclx_profile& b) const
377
1.90k
{
378
1.90k
  return (m_matrix_coefficients == b.m_matrix_coefficients &&
379
1.90k
          m_colour_primaries == b.m_colour_primaries &&
380
1.90k
          m_full_range_flag == b.m_full_range_flag);
381
1.90k
}
382
383
384
Error Box_colr::parse(BitstreamRange& range, const heif_security_limits* limits)
385
1.41k
{
386
1.41k
  StreamReader::grow_status status;
387
1.41k
  uint32_t colour_type = range.read32();
388
389
1.41k
  if (colour_type == fourcc("nclx")) {
390
192
    auto color_profile = std::make_shared<color_profile_nclx>();
391
192
    m_color_profile = color_profile;
392
192
    Error err = color_profile->parse(range);
393
192
    if (err) {
394
0
      return err;
395
0
    }
396
192
  }
397
1.22k
  else if (colour_type == fourcc("nclc")) {
398
4
    auto color_profile = std::make_shared<color_profile_nclx>();
399
4
    m_color_profile = color_profile;
400
4
    Error err = color_profile->parse_nclc(range);
401
4
    if (err) {
402
0
      return err;
403
0
    }
404
4
  }
405
1.22k
  else if (colour_type == fourcc("prof") ||
406
1.21k
           colour_type == fourcc("rICC")) {
407
1.21k
    if (!has_fixed_box_size()) {
408
0
      return Error(heif_error_Unsupported_feature, heif_suberror_Unspecified, "colr boxes with undefined box size are not supported");
409
0
    }
410
411
1.21k
    uint64_t profile_size_64 = get_box_size() - get_header_size() - 4;
412
1.21k
    if (limits->max_color_profile_size && profile_size_64 > limits->max_color_profile_size) {
413
0
      return Error(heif_error_Invalid_input, heif_suberror_Security_limit_exceeded, "Color profile exceeds maximum supported size");
414
0
    }
415
416
1.21k
    size_t profile_size = static_cast<size_t>(profile_size_64);
417
418
1.21k
    status = range.wait_for_available_bytes(profile_size);
419
1.21k
    if (status != StreamReader::grow_status::size_reached) {
420
      // TODO: return recoverable error at timeout
421
0
      return Error(heif_error_Invalid_input,
422
0
                   heif_suberror_End_of_data);
423
0
    }
424
425
1.21k
    std::vector<uint8_t> rawData(profile_size);
426
788k
    for (size_t i = 0; i < profile_size; i++) {
427
786k
      rawData[i] = range.read8();
428
786k
    }
429
430
1.21k
    m_color_profile = std::make_shared<color_profile_raw>(colour_type, rawData);
431
1.21k
  }
432
5
  else {
433
5
    return Error(heif_error_Invalid_input,
434
5
                 heif_suberror_Unknown_color_profile_type);
435
5
  }
436
437
1.41k
  return range.get_error();
438
1.41k
}
439
440
441
std::string Box_colr::dump(Indent& indent) const
442
0
{
443
0
  std::ostringstream sstr;
444
0
  sstr << Box::dump(indent);
445
446
0
  if (m_color_profile) {
447
0
    sstr << indent << "colour_type: " << fourcc_to_string(get_color_profile_type()) << "\n";
448
0
    sstr << m_color_profile->dump(indent);
449
0
  }
450
0
  else {
451
0
    sstr << indent << "colour_type: ---\n";
452
0
    sstr << "no color profile\n";
453
0
  }
454
455
0
  return sstr.str();
456
0
}
457
458
459
std::string color_profile_raw::dump(Indent& indent) const
460
0
{
461
0
  std::ostringstream sstr;
462
0
  sstr << indent << "profile size: " << m_data.size() << "\n";
463
0
  return sstr.str();
464
0
}
465
466
467
std::string color_profile_nclx::dump(Indent& indent) const
468
0
{
469
0
  std::ostringstream sstr;
470
0
  sstr << indent << "colour_primaries: " << m_profile.m_colour_primaries << "\n"
471
0
       << indent << "transfer_characteristics: " << m_profile.m_transfer_characteristics << "\n"
472
0
       << indent << "matrix_coefficients: " << m_profile.m_matrix_coefficients << "\n"
473
0
       << indent << "full_range_flag: " << m_profile.m_full_range_flag << "\n";
474
0
  return sstr.str();
475
0
}
476
477
478
Error color_profile_nclx::write(StreamWriter& writer) const
479
261
{
480
261
  writer.write16(m_profile.m_colour_primaries);
481
261
  writer.write16(m_profile.m_transfer_characteristics);
482
261
  writer.write16(m_profile.m_matrix_coefficients);
483
261
  writer.write8(m_profile.m_full_range_flag ? 0x80 : 0x00);
484
485
261
  return Error::Ok;
486
261
}
487
488
Error color_profile_raw::write(StreamWriter& writer) const
489
6.49k
{
490
6.49k
  writer.write(m_data);
491
492
6.49k
  return Error::Ok;
493
6.49k
}
494
495
Error Box_colr::write(StreamWriter& writer) const
496
6.75k
{
497
6.75k
  size_t box_start = reserve_box_header_space(writer);
498
499
6.75k
  assert(m_color_profile);
500
501
6.75k
  writer.write32(m_color_profile->get_type());
502
503
6.75k
  Error err = m_color_profile->write(writer);
504
6.75k
  if (err) {
505
0
    return err;
506
0
  }
507
508
6.75k
  prepend_header(writer, box_start);
509
510
6.75k
  return Error::Ok;
511
6.75k
}
512
513