Coverage Report

Created: 2025-06-16 07:00

/src/libheif/libheif/nclx.cc
Line
Count
Source (jump to first uncovered line)
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
1.16k
{
86
1.16k
  Kr_Kb result;
87
88
1.16k
  if (matrix_coefficients_idx == 12 ||
89
1.16k
      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.16k
  else
110
1.16k
    switch (matrix_coefficients_idx) {
111
2
      case 1:
112
2
        result.Kr = 0.2126f;
113
2
        result.Kb = 0.0722f;
114
2
        break;
115
7
      case 4:
116
7
        result.Kr = 0.30f;
117
7
        result.Kb = 0.11f;
118
7
        break;
119
1
      case 5:
120
289
      case 6:
121
289
        result.Kr = 0.299f;
122
289
        result.Kb = 0.114f;
123
289
        break;
124
5
      case 7:
125
5
        result.Kr = 0.212f;
126
5
        result.Kb = 0.087f;
127
5
        break;
128
6
      case 9:
129
9
      case 10:
130
9
        result.Kr = 0.2627f;
131
9
        result.Kb = 0.0593f;
132
9
        break;
133
853
      default:;
134
1.16k
    }
135
136
1.16k
  return result;
137
1.16k
}
138
139
140
YCbCr_to_RGB_coefficients YCbCr_to_RGB_coefficients::defaults()
141
2.01k
{
142
2.01k
  YCbCr_to_RGB_coefficients coeffs;
143
2.01k
  coeffs.defined = true;
144
2.01k
  coeffs.r_cr = 1.402f;
145
2.01k
  coeffs.g_cb = -0.344136f;
146
2.01k
  coeffs.g_cr = -0.714136f;
147
2.01k
  coeffs.b_cb = 1.772f;
148
2.01k
  return coeffs;
149
2.01k
}
150
151
YCbCr_to_RGB_coefficients
152
get_YCbCr_to_RGB_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
153
1.16k
{
154
1.16k
  YCbCr_to_RGB_coefficients coeffs;
155
156
1.16k
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
157
158
1.16k
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
159
312
    coeffs.defined = true;
160
312
    coeffs.r_cr = 2 * (-k.Kr + 1);
161
312
    coeffs.g_cb = 2 * k.Kb * (-k.Kb + 1) / (k.Kb + k.Kr - 1);
162
312
    coeffs.g_cr = 2 * k.Kr * (-k.Kr + 1) / (k.Kb + k.Kr - 1);
163
312
    coeffs.b_cb = 2 * (-k.Kb + 1);
164
312
  }
165
853
  else {
166
853
    coeffs = YCbCr_to_RGB_coefficients::defaults();
167
853
  }
168
169
1.16k
  return coeffs;
170
1.16k
}
171
172
173
RGB_to_YCbCr_coefficients
174
get_RGB_to_YCbCr_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx)
175
0
{
176
0
  RGB_to_YCbCr_coefficients coeffs;
177
178
0
  Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx);
179
180
0
  if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid
181
0
    coeffs.defined = true;
182
0
    coeffs.c[0][0] = k.Kr;
183
0
    coeffs.c[0][1] = 1 - k.Kr - k.Kb;
184
0
    coeffs.c[0][2] = k.Kb;
185
0
    coeffs.c[1][0] = -k.Kr / (1 - k.Kb) / 2;
186
0
    coeffs.c[1][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kb) / 2;
187
0
    coeffs.c[1][2] = 0.5f;
188
0
    coeffs.c[2][0] = 0.5f;
189
0
    coeffs.c[2][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kr) / 2;
190
0
    coeffs.c[2][2] = -k.Kb / (1 - k.Kr) / 2;
191
0
  }
192
0
  else {
193
0
    coeffs = RGB_to_YCbCr_coefficients::defaults();
194
0
  }
195
196
0
  return coeffs;
197
0
}
198
199
200
RGB_to_YCbCr_coefficients RGB_to_YCbCr_coefficients::defaults()
201
0
{
202
0
  RGB_to_YCbCr_coefficients coeffs;
203
0
  coeffs.defined = true;
204
  // Rec 601 full. Kr=0.2990f Kb=0.1140f.
205
0
  coeffs.c[0][0] = 0.299f;
206
0
  coeffs.c[0][1] = 0.587f;
207
0
  coeffs.c[0][2] = 0.114f;
208
0
  coeffs.c[1][0] = -0.168735f;
209
0
  coeffs.c[1][1] = -0.331264f;
210
0
  coeffs.c[1][2] = 0.5f;
211
0
  coeffs.c[2][0] = 0.5f;
212
0
  coeffs.c[2][1] = -0.418688f;
213
0
  coeffs.c[2][2] = -0.081312f;
214
215
0
  return coeffs;
216
0
}
217
218
219
Error color_profile_nclx::parse(BitstreamRange& range)
220
398
{
221
398
  StreamReader::grow_status status;
222
398
  status = range.wait_for_available_bytes(7);
223
398
  if (status != StreamReader::grow_status::size_reached) {
224
    // TODO: return recoverable error at timeout
225
0
    return Error(heif_error_Invalid_input,
226
0
                 heif_suberror_End_of_data);
227
0
  }
228
229
398
  m_colour_primaries = range.read16();
230
398
  m_transfer_characteristics = range.read16();
231
398
  m_matrix_coefficients = range.read16();
232
398
  m_full_range_flag = (range.read8() & 0x80 ? true : false);
233
234
398
  return Error::Ok;
235
398
}
236
237
Error color_profile_nclx::get_nclx_color_profile(struct heif_color_profile_nclx** out_data) const
238
0
{
239
0
  *out_data = nullptr;
240
241
0
  struct heif_color_profile_nclx* nclx = alloc_nclx_color_profile();
242
243
0
  if (nclx == nullptr) {
244
0
    return Error(heif_error_Memory_allocation_error,
245
0
                 heif_suberror_Unspecified);
246
0
  }
247
248
0
  struct heif_error err;
249
250
0
  nclx->version = 1;
251
252
0
  err = heif_nclx_color_profile_set_color_primaries(nclx, get_colour_primaries());
253
0
  if (err.code) {
254
0
    free_nclx_color_profile(nclx);
255
0
    return {err.code, err.subcode};
256
0
  }
257
258
0
  err = heif_nclx_color_profile_set_transfer_characteristics(nclx, get_transfer_characteristics());
259
0
  if (err.code) {
260
0
    free_nclx_color_profile(nclx);
261
0
    return {err.code, err.subcode};
262
0
  }
263
264
0
  err = heif_nclx_color_profile_set_matrix_coefficients(nclx, get_matrix_coefficients());
265
0
  if (err.code) {
266
0
    free_nclx_color_profile(nclx);
267
0
    return {err.code, err.subcode};
268
0
  }
269
270
0
  nclx->full_range_flag = get_full_range_flag();
271
272
  // fill color primaries
273
274
0
  auto primaries = ::get_colour_primaries(nclx->color_primaries);
275
276
0
  nclx->color_primary_red_x = primaries.redX;
277
0
  nclx->color_primary_red_y = primaries.redY;
278
0
  nclx->color_primary_green_x = primaries.greenX;
279
0
  nclx->color_primary_green_y = primaries.greenY;
280
0
  nclx->color_primary_blue_x = primaries.blueX;
281
0
  nclx->color_primary_blue_y = primaries.blueY;
282
0
  nclx->color_primary_white_x = primaries.whiteX;
283
0
  nclx->color_primary_white_y = primaries.whiteY;
284
285
0
  *out_data = nclx;
286
287
0
  return Error::Ok;
288
0
}
289
290
291
struct heif_color_profile_nclx* color_profile_nclx::alloc_nclx_color_profile()
292
1.21k
{
293
1.21k
  auto profile = (heif_color_profile_nclx*) malloc(sizeof(struct heif_color_profile_nclx));
294
295
1.21k
  if (profile) {
296
1.21k
    profile->version = 1;
297
298
    // sRGB defaults
299
1.21k
    profile->color_primaries = heif_color_primaries_ITU_R_BT_709_5; // 1
300
1.21k
    profile->transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; // 13
301
1.21k
    profile->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6; // 6
302
1.21k
    profile->full_range_flag = true;
303
1.21k
  }
304
305
1.21k
  return profile;
306
1.21k
}
307
308
309
void color_profile_nclx::free_nclx_color_profile(struct heif_color_profile_nclx* profile)
310
1.21k
{
311
1.21k
  free(profile);
312
1.21k
}
313
314
315
void color_profile_nclx::set_sRGB_defaults()
316
47.6k
{
317
  // sRGB defaults
318
47.6k
  m_colour_primaries = 1;
319
47.6k
  m_transfer_characteristics = 13;
320
47.6k
  m_matrix_coefficients = 6;
321
47.6k
  m_full_range_flag = true;
322
47.6k
}
323
324
325
void color_profile_nclx::set_undefined()
326
0
{
327
0
  m_colour_primaries = 2;
328
0
  m_transfer_characteristics = 2;
329
0
  m_matrix_coefficients = 2;
330
0
  m_full_range_flag = true;
331
0
}
332
333
334
void color_profile_nclx::set_from_heif_color_profile_nclx(const struct heif_color_profile_nclx* nclx)
335
0
{
336
0
  if (nclx) {
337
0
    m_colour_primaries = nclx->color_primaries;
338
0
    m_transfer_characteristics = nclx->transfer_characteristics;
339
0
    m_matrix_coefficients = nclx->matrix_coefficients;
340
0
    m_full_range_flag = nclx->full_range_flag;
341
0
  }
342
0
}
343
344
345
void color_profile_nclx::replace_undefined_values_with_sRGB_defaults()
346
1.25k
{
347
1.25k
  if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) {
348
893
    m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6;
349
893
  }
350
351
1.25k
  if (m_colour_primaries == heif_color_primaries_unspecified) {
352
1.17k
    m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5;
353
1.17k
  }
354
355
1.25k
  if (m_transfer_characteristics == heif_transfer_characteristic_unspecified) {
356
1.16k
    m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1;
357
1.16k
  }
358
1.25k
}
359
360
361
Error Box_colr::parse(BitstreamRange& range, const heif_security_limits* limits)
362
465
{
363
465
  StreamReader::grow_status status;
364
465
  uint32_t colour_type = range.read32();
365
366
465
  if (colour_type == fourcc("nclx")) {
367
398
    auto color_profile = std::make_shared<color_profile_nclx>();
368
398
    m_color_profile = color_profile;
369
398
    Error err = color_profile->parse(range);
370
398
    if (err) {
371
0
      return err;
372
0
    }
373
398
  }
374
67
  else if (colour_type == fourcc("prof") ||
375
67
           colour_type == fourcc("rICC")) {
376
45
    if (!has_fixed_box_size()) {
377
1
      return Error(heif_error_Unsupported_feature, heif_suberror_Unspecified, "colr boxes with undefined box size are not supported");
378
1
    }
379
380
44
    uint64_t profile_size_64 = get_box_size() - get_header_size() - 4;
381
44
    if (limits->max_color_profile_size && profile_size_64 > limits->max_color_profile_size) {
382
0
      return Error(heif_error_Invalid_input, heif_suberror_Security_limit_exceeded, "Color profile exceeds maximum supported size");
383
0
    }
384
385
44
    size_t profile_size = static_cast<size_t>(profile_size_64);
386
387
44
    status = range.wait_for_available_bytes(profile_size);
388
44
    if (status != StreamReader::grow_status::size_reached) {
389
      // TODO: return recoverable error at timeout
390
0
      return Error(heif_error_Invalid_input,
391
0
                   heif_suberror_End_of_data);
392
0
    }
393
394
44
    std::vector<uint8_t> rawData(profile_size);
395
9.73k
    for (size_t i = 0; i < profile_size; i++) {
396
9.69k
      rawData[i] = range.read8();
397
9.69k
    }
398
399
44
    m_color_profile = std::make_shared<color_profile_raw>(colour_type, rawData);
400
44
  }
401
22
  else {
402
22
    return Error(heif_error_Invalid_input,
403
22
                 heif_suberror_Unknown_color_profile_type);
404
22
  }
405
406
442
  return range.get_error();
407
465
}
408
409
410
std::string Box_colr::dump(Indent& indent) const
411
0
{
412
0
  std::ostringstream sstr;
413
0
  sstr << Box::dump(indent);
414
415
0
  if (m_color_profile) {
416
0
    sstr << indent << "colour_type: " << fourcc_to_string(get_color_profile_type()) << "\n";
417
0
    sstr << m_color_profile->dump(indent);
418
0
  }
419
0
  else {
420
0
    sstr << indent << "colour_type: ---\n";
421
0
    sstr << "no color profile\n";
422
0
  }
423
424
0
  return sstr.str();
425
0
}
426
427
428
std::string color_profile_raw::dump(Indent& indent) const
429
0
{
430
0
  std::ostringstream sstr;
431
0
  sstr << indent << "profile size: " << m_data.size() << "\n";
432
0
  return sstr.str();
433
0
}
434
435
436
std::string color_profile_nclx::dump(Indent& indent) const
437
0
{
438
0
  std::ostringstream sstr;
439
0
  sstr << indent << "colour_primaries: " << m_colour_primaries << "\n"
440
0
       << indent << "transfer_characteristics: " << m_transfer_characteristics << "\n"
441
0
       << indent << "matrix_coefficients: " << m_matrix_coefficients << "\n"
442
0
       << indent << "full_range_flag: " << m_full_range_flag << "\n";
443
0
  return sstr.str();
444
0
}
445
446
447
Error color_profile_nclx::write(StreamWriter& writer) const
448
0
{
449
0
  writer.write16(m_colour_primaries);
450
0
  writer.write16(m_transfer_characteristics);
451
0
  writer.write16(m_matrix_coefficients);
452
0
  writer.write8(m_full_range_flag ? 0x80 : 0x00);
453
454
0
  return Error::Ok;
455
0
}
456
457
Error color_profile_raw::write(StreamWriter& writer) const
458
0
{
459
0
  writer.write(m_data);
460
461
0
  return Error::Ok;
462
0
}
463
464
Error Box_colr::write(StreamWriter& writer) const
465
0
{
466
0
  size_t box_start = reserve_box_header_space(writer);
467
468
0
  assert(m_color_profile);
469
470
0
  writer.write32(m_color_profile->get_type());
471
472
0
  Error err = m_color_profile->write(writer);
473
0
  if (err) {
474
0
    return err;
475
0
  }
476
477
0
  prepend_header(writer, box_start);
478
479
0
  return Error::Ok;
480
0
}
481
482