Coverage Report

Created: 2026-07-16 06:32

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
0
{
33
0
  defined = true;
34
0
  redX = rx;
35
0
  redY = ry;
36
0
  greenX = gx;
37
0
  greenY = gy;
38
0
  blueX = bx;
39
0
  blueY = by;
40
0
  whiteX = wx;
41
0
  whiteY = wy;
42
0
}
43
44
45
primaries get_colour_primaries(uint16_t primaries_idx)
46
0
{
47
0
  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
0
    case 6:
55
0
    case 7:
56
0
      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
0
    case 10:
62
0
      return {0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.333333f, 0.33333f};
63
0
    case 11:
64
0
      return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.314f, 0.351f};
65
0
    case 12:
66
0
      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
0
    default:
70
0
      return {};
71
0
  }
72
0
}
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
76
{
86
76
  Kr_Kb result;
87
88
76
  if (matrix_coefficients_idx == 12 ||
89
76
      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
76
  else
110
76
    switch (matrix_coefficients_idx) {
111
0
      case 1:
112
0
        result.Kr = 0.2126f;
113
0
        result.Kb = 0.0722f;
114
0
        break;
115
0
      case 4:
116
0
        result.Kr = 0.30f;
117
0
        result.Kb = 0.11f;
118
0
        break;
119
0
      case 5:
120
76
      case 6:
121
76
        result.Kr = 0.299f;
122
76
        result.Kb = 0.114f;
123
76
        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
0
      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
0
        result.Kr = 0.2627f;
134
0
        result.Kb = 0.0593f;
135
0
        break;
136
0
      default:;
137
76
    }
138
139
76
  return result;
140
76
}
141
142
143
YCbCr_to_RGB_coefficients YCbCr_to_RGB_coefficients::defaults()
144
76
{
145
76
  YCbCr_to_RGB_coefficients coeffs;
146
76
  coeffs.defined = true;
147
76
  coeffs.r_cr = 1.402f;
148
76
  coeffs.g_cb = -0.344136f;
149
76
  coeffs.g_cr = -0.714136f;
150
76
  coeffs.b_cb = 1.772f;
151
76
  return coeffs;
152
76
}
153
154
YCbCr_to_RGB_coefficients
155
get_YCbCr_to_RGB_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
156
0
{
157
0
  YCbCr_to_RGB_coefficients coeffs;
158
159
0
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
160
161
0
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
162
0
    coeffs.defined = true;
163
0
    coeffs.r_cr = 2 * (-k.Kr + 1);
164
0
    coeffs.g_cb = 2 * k.Kb * (-k.Kb + 1) / (k.Kb + k.Kr - 1);
165
0
    coeffs.g_cr = 2 * k.Kr * (-k.Kr + 1) / (k.Kb + k.Kr - 1);
166
0
    coeffs.b_cb = 2 * (-k.Kb + 1);
167
0
  }
168
0
  else {
169
0
    coeffs = YCbCr_to_RGB_coefficients::defaults();
170
0
  }
171
172
0
  return coeffs;
173
0
}
174
175
176
RGB_to_YCbCr_coefficients
177
get_RGB_to_YCbCr_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
178
76
{
179
76
  RGB_to_YCbCr_coefficients coeffs;
180
181
76
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
182
183
76
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
184
76
    coeffs.defined = true;
185
76
    coeffs.c[0][0] = k.Kr;
186
76
    coeffs.c[0][1] = 1 - k.Kr - k.Kb;
187
76
    coeffs.c[0][2] = k.Kb;
188
76
    coeffs.c[1][0] = -k.Kr / (1 - k.Kb) / 2;
189
76
    coeffs.c[1][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kb) / 2;
190
76
    coeffs.c[1][2] = 0.5f;
191
76
    coeffs.c[2][0] = 0.5f;
192
76
    coeffs.c[2][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kr) / 2;
193
76
    coeffs.c[2][2] = -k.Kb / (1 - k.Kr) / 2;
194
76
  }
195
0
  else {
196
0
    coeffs = RGB_to_YCbCr_coefficients::defaults();
197
0
  }
198
199
76
  return coeffs;
200
76
}
201
202
203
RGB_to_YCbCr_coefficients RGB_to_YCbCr_coefficients::defaults()
204
76
{
205
76
  RGB_to_YCbCr_coefficients coeffs;
206
76
  coeffs.defined = true;
207
  // Rec 601 full. Kr=0.2990f Kb=0.1140f.
208
76
  coeffs.c[0][0] = 0.299f;
209
76
  coeffs.c[0][1] = 0.587f;
210
76
  coeffs.c[0][2] = 0.114f;
211
76
  coeffs.c[1][0] = -0.168735f;
212
76
  coeffs.c[1][1] = -0.331264f;
213
76
  coeffs.c[1][2] = 0.5f;
214
76
  coeffs.c[2][0] = 0.5f;
215
76
  coeffs.c[2][1] = -0.418688f;
216
76
  coeffs.c[2][2] = -0.081312f;
217
218
76
  return coeffs;
219
76
}
220
221
222
Error color_profile_nclx::parse(BitstreamRange& range)
223
0
{
224
0
  StreamReader::grow_status status;
225
0
  status = range.wait_for_available_bytes(7);
226
0
  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
0
  m_profile.m_colour_primaries = range.read16();
233
0
  m_profile.m_transfer_characteristics = range.read16();
234
0
  m_profile.m_matrix_coefficients = range.read16();
235
0
  m_profile.m_full_range_flag = (range.read8() & 0x80 ? true : false);
236
237
0
  return Error::Ok;
238
0
}
239
240
Error color_profile_nclx::parse_nclc(BitstreamRange& range)
241
0
{
242
0
  StreamReader::grow_status status;
243
0
  status = range.wait_for_available_bytes(6);
244
0
  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
0
  m_profile.m_colour_primaries = range.read16();
251
0
  m_profile.m_transfer_characteristics = range.read16();
252
0
  m_profile.m_matrix_coefficients = range.read16();
253
254
  // use full range for RGB, limited range otherwise
255
0
  m_profile.m_full_range_flag = (m_profile.m_matrix_coefficients == 0);
256
257
0
  return Error::Ok;
258
0
}
259
260
Error nclx_profile::get_nclx_color_profile(heif_color_profile_nclx** out_data) const
261
0
{
262
0
  *out_data = nullptr;
263
264
0
  heif_color_profile_nclx* nclx = heif_nclx_color_profile_alloc();
265
266
0
  if (nclx == nullptr) {
267
0
    return Error(heif_error_Memory_allocation_error,
268
0
                 heif_suberror_Unspecified);
269
0
  }
270
271
0
  heif_error err;
272
273
0
  nclx->version = 1;
274
275
0
  err = heif_nclx_color_profile_set_color_primaries(nclx, get_colour_primaries());
276
0
  if (err.code) {
277
0
    heif_nclx_color_profile_free(nclx);
278
0
    return {err.code, err.subcode};
279
0
  }
280
281
0
  err = heif_nclx_color_profile_set_transfer_characteristics(nclx, get_transfer_characteristics());
282
0
  if (err.code) {
283
0
    heif_nclx_color_profile_free(nclx);
284
0
    return {err.code, err.subcode};
285
0
  }
286
287
0
  err = heif_nclx_color_profile_set_matrix_coefficients(nclx, get_matrix_coefficients());
288
0
  if (err.code) {
289
0
    heif_nclx_color_profile_free(nclx);
290
0
    return {err.code, err.subcode};
291
0
  }
292
293
0
  nclx->full_range_flag = get_full_range_flag();
294
295
  // fill color primaries
296
297
0
  auto primaries = ::get_colour_primaries(nclx->color_primaries);
298
299
0
  nclx->color_primary_red_x = primaries.redX;
300
0
  nclx->color_primary_red_y = primaries.redY;
301
0
  nclx->color_primary_green_x = primaries.greenX;
302
0
  nclx->color_primary_green_y = primaries.greenY;
303
0
  nclx->color_primary_blue_x = primaries.blueX;
304
0
  nclx->color_primary_blue_y = primaries.blueY;
305
0
  nclx->color_primary_white_x = primaries.whiteX;
306
0
  nclx->color_primary_white_y = primaries.whiteY;
307
308
0
  *out_data = nclx;
309
310
0
  return Error::Ok;
311
0
}
312
313
314
void nclx_profile::set_sRGB_defaults()
315
76
{
316
  // sRGB defaults
317
76
  m_colour_primaries = 1;
318
76
  m_transfer_characteristics = 13;
319
76
  m_matrix_coefficients = 6;
320
76
  m_full_range_flag = true;
321
76
}
322
323
324
void nclx_profile::set_undefined()
325
3.77k
{
326
3.77k
  m_colour_primaries = 2;
327
3.77k
  m_transfer_characteristics = 2;
328
3.77k
  m_matrix_coefficients = 2;
329
3.77k
  m_full_range_flag = true;
330
3.77k
}
331
332
333
bool nclx_profile::is_undefined() const
334
0
{
335
0
  return *this == nclx_profile::undefined();
336
0
}
337
338
339
void nclx_profile::set_from_heif_color_profile_nclx(const heif_color_profile_nclx* nclx)
340
152
{
341
152
  if (nclx) {
342
152
    m_colour_primaries = nclx->color_primaries;
343
152
    m_transfer_characteristics = nclx->transfer_characteristics;
344
152
    m_matrix_coefficients = nclx->matrix_coefficients;
345
152
    m_full_range_flag = nclx->full_range_flag;
346
152
  }
347
152
}
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
4.37k
{
362
4.37k
  if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) {
363
4.30k
    m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6;
364
4.30k
  }
365
366
4.37k
  if (m_colour_primaries == heif_color_primaries_unspecified) {
367
4.30k
    m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5;
368
4.30k
  }
369
370
4.37k
  if (m_transfer_characteristics == heif_transfer_characteristic_unspecified) {
371
4.30k
    m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1;
372
4.30k
  }
373
4.37k
}
374
375
376
bool nclx_profile::equal_except_transfer_curve(const nclx_profile& b) const
377
3.15k
{
378
3.15k
  return (m_matrix_coefficients == b.m_matrix_coefficients &&
379
3.15k
          m_colour_primaries == b.m_colour_primaries &&
380
2.54k
          m_full_range_flag == b.m_full_range_flag);
381
3.15k
}
382
383
384
Error Box_colr::parse(BitstreamRange& range, const heif_security_limits* limits)
385
0
{
386
0
  StreamReader::grow_status status;
387
0
  uint32_t colour_type = range.read32();
388
389
0
  if (colour_type == fourcc("nclx")) {
390
0
    auto color_profile = std::make_shared<color_profile_nclx>();
391
0
    m_color_profile = color_profile;
392
0
    Error err = color_profile->parse(range);
393
0
    if (err) {
394
0
      return err;
395
0
    }
396
0
  }
397
0
  else if (colour_type == fourcc("nclc")) {
398
0
    auto color_profile = std::make_shared<color_profile_nclx>();
399
0
    m_color_profile = color_profile;
400
0
    Error err = color_profile->parse_nclc(range);
401
0
    if (err) {
402
0
      return err;
403
0
    }
404
0
  }
405
0
  else if (colour_type == fourcc("prof") ||
406
0
           colour_type == fourcc("rICC")) {
407
0
    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
0
    uint64_t profile_size_64 = get_box_size() - get_header_size() - 4;
412
0
    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
0
    size_t profile_size = static_cast<size_t>(profile_size_64);
417
418
0
    status = range.wait_for_available_bytes(profile_size);
419
0
    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
0
    std::vector<uint8_t> rawData(profile_size);
426
0
    for (size_t i = 0; i < profile_size; i++) {
427
0
      rawData[i] = range.read8();
428
0
    }
429
430
0
    m_color_profile = std::make_shared<color_profile_raw>(colour_type, rawData);
431
0
  }
432
0
  else {
433
0
    return Error(heif_error_Invalid_input,
434
0
                 heif_suberror_Unknown_color_profile_type);
435
0
  }
436
437
0
  return range.get_error();
438
0
}
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
76
{
480
76
  writer.write16(m_profile.m_colour_primaries);
481
76
  writer.write16(m_profile.m_transfer_characteristics);
482
76
  writer.write16(m_profile.m_matrix_coefficients);
483
76
  writer.write8(m_profile.m_full_range_flag ? 0x80 : 0x00);
484
485
76
  return Error::Ok;
486
76
}
487
488
Error color_profile_raw::write(StreamWriter& writer) const
489
0
{
490
0
  writer.write(m_data);
491
492
0
  return Error::Ok;
493
0
}
494
495
Error Box_colr::write(StreamWriter& writer) const
496
76
{
497
76
  size_t box_start = reserve_box_header_space(writer);
498
499
76
  assert(m_color_profile);
500
501
76
  writer.write32(m_color_profile->get_type());
502
503
76
  Error err = m_color_profile->write(writer);
504
76
  if (err) {
505
0
    return err;
506
0
  }
507
508
76
  prepend_header(writer, box_start);
509
510
76
  return Error::Ok;
511
76
}
512
513