Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/src/colr.c
Line
Count
Source
1
// Copyright 2019 Joe Drago. All rights reserved.
2
// SPDX-License-Identifier: BSD-2-Clause
3
4
#include "avif/internal.h"
5
6
#include <float.h>
7
#include <math.h>
8
#include <string.h>
9
10
struct avifColorPrimariesTable
11
{
12
    avifColorPrimaries colorPrimariesEnum;
13
    const char * name;
14
    float primaries[8]; // rX, rY, gX, gY, bX, bY, wX, wY
15
};
16
static const struct avifColorPrimariesTable avifColorPrimariesTables[] = {
17
    { AVIF_COLOR_PRIMARIES_BT709, "BT.709", { 0.64f, 0.33f, 0.3f, 0.6f, 0.15f, 0.06f, 0.3127f, 0.329f } },
18
    { AVIF_COLOR_PRIMARIES_BT470M, "BT.470-6 System M", { 0.67f, 0.33f, 0.21f, 0.71f, 0.14f, 0.08f, 0.310f, 0.316f } },
19
    { AVIF_COLOR_PRIMARIES_BT470BG, "BT.470-6 System BG", { 0.64f, 0.33f, 0.29f, 0.60f, 0.15f, 0.06f, 0.3127f, 0.3290f } },
20
    { AVIF_COLOR_PRIMARIES_BT601, "BT.601", { 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f } },
21
    { AVIF_COLOR_PRIMARIES_SMPTE240, "SMPTE 240M", { 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f } },
22
    { AVIF_COLOR_PRIMARIES_GENERIC_FILM, "Generic film", { 0.681f, 0.319f, 0.243f, 0.692f, 0.145f, 0.049f, 0.310f, 0.316f } },
23
    { AVIF_COLOR_PRIMARIES_BT2020, "BT.2020", { 0.708f, 0.292f, 0.170f, 0.797f, 0.131f, 0.046f, 0.3127f, 0.3290f } },
24
    { AVIF_COLOR_PRIMARIES_XYZ, "XYZ", { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.3333f, 0.3333f } },
25
    { AVIF_COLOR_PRIMARIES_SMPTE431, "SMPTE RP 431-2", { 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.314f, 0.351f } },
26
    { AVIF_COLOR_PRIMARIES_SMPTE432, "SMPTE EG 432-1 (DCI P3)", { 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.3127f, 0.3290f } },
27
    { AVIF_COLOR_PRIMARIES_EBU3213, "EBU Tech. 3213-E", { 0.630f, 0.340f, 0.295f, 0.605f, 0.155f, 0.077f, 0.3127f, 0.3290f } }
28
};
29
static const int avifColorPrimariesTableSize = sizeof(avifColorPrimariesTables) / sizeof(avifColorPrimariesTables[0]);
30
31
void avifColorPrimariesGetValues(avifColorPrimaries acp, float outPrimaries[8])
32
1.03k
{
33
11.3k
    for (int i = 0; i < avifColorPrimariesTableSize; ++i) {
34
10.4k
        if (avifColorPrimariesTables[i].colorPrimariesEnum == acp) {
35
115
            memcpy(outPrimaries, avifColorPrimariesTables[i].primaries, sizeof(avifColorPrimariesTables[i].primaries));
36
115
            return;
37
115
        }
38
10.4k
    }
39
40
    // if we get here, the color primaries are unknown. Just return a reasonable default.
41
915
    memcpy(outPrimaries, avifColorPrimariesTables[0].primaries, sizeof(avifColorPrimariesTables[0].primaries));
42
915
}
43
44
static avifBool matchesTo3RoundedPlaces(float a, float b)
45
0
{
46
0
    return (fabsf(a - b) < 0.001f);
47
0
}
48
49
static avifBool primariesMatch(const float p1[8], const float p2[8])
50
0
{
51
0
    return matchesTo3RoundedPlaces(p1[0], p2[0]) && matchesTo3RoundedPlaces(p1[1], p2[1]) &&
52
0
           matchesTo3RoundedPlaces(p1[2], p2[2]) && matchesTo3RoundedPlaces(p1[3], p2[3]) && matchesTo3RoundedPlaces(p1[4], p2[4]) &&
53
0
           matchesTo3RoundedPlaces(p1[5], p2[5]) && matchesTo3RoundedPlaces(p1[6], p2[6]) && matchesTo3RoundedPlaces(p1[7], p2[7]);
54
0
}
55
56
avifColorPrimaries avifColorPrimariesFind(const float inPrimaries[8], const char ** outName)
57
0
{
58
0
    if (outName) {
59
0
        *outName = NULL;
60
0
    }
61
62
0
    for (int i = 0; i < avifColorPrimariesTableSize; ++i) {
63
0
        if (primariesMatch(inPrimaries, avifColorPrimariesTables[i].primaries)) {
64
0
            if (outName) {
65
0
                *outName = avifColorPrimariesTables[i].name;
66
0
            }
67
0
            return avifColorPrimariesTables[i].colorPrimariesEnum;
68
0
        }
69
0
    }
70
0
    return AVIF_COLOR_PRIMARIES_UNKNOWN;
71
0
}
72
73
avifResult avifTransferCharacteristicsGetGamma(avifTransferCharacteristics atc, float * gamma)
74
0
{
75
0
    switch (atc) {
76
0
        case AVIF_TRANSFER_CHARACTERISTICS_BT470M:
77
0
            *gamma = 2.2f;
78
0
            return AVIF_RESULT_OK;
79
0
        case AVIF_TRANSFER_CHARACTERISTICS_BT470BG:
80
0
            *gamma = 2.8f;
81
0
            return AVIF_RESULT_OK;
82
0
        case AVIF_TRANSFER_CHARACTERISTICS_LINEAR:
83
0
            *gamma = 1.0f;
84
0
            return AVIF_RESULT_OK;
85
0
        default:
86
0
            return AVIF_RESULT_INVALID_ARGUMENT;
87
0
    }
88
0
}
89
90
avifTransferCharacteristics avifTransferCharacteristicsFindByGamma(float gamma)
91
0
{
92
0
    if (matchesTo3RoundedPlaces(gamma, 2.2f)) {
93
0
        return AVIF_TRANSFER_CHARACTERISTICS_BT470M;
94
0
    } else if (matchesTo3RoundedPlaces(gamma, 1.0f)) {
95
0
        return AVIF_TRANSFER_CHARACTERISTICS_LINEAR;
96
0
    } else if (matchesTo3RoundedPlaces(gamma, 2.8f)) {
97
0
        return AVIF_TRANSFER_CHARACTERISTICS_BT470BG;
98
0
    }
99
100
0
    return AVIF_TRANSFER_CHARACTERISTICS_UNKNOWN;
101
0
}
102
103
struct avifMatrixCoefficientsTable
104
{
105
    avifMatrixCoefficients matrixCoefficientsEnum;
106
    const char * name;
107
    const float kr;
108
    const float kb;
109
};
110
111
// https://www.itu.int/rec/T-REC-H.273-201612-S
112
static const struct avifMatrixCoefficientsTable matrixCoefficientsTables[] = {
113
    //{ AVIF_MATRIX_COEFFICIENTS_IDENTITY, "Identity", 0.0f, 0.0f, }, // Handled elsewhere
114
    { AVIF_MATRIX_COEFFICIENTS_BT709, "BT.709", 0.2126f, 0.0722f },
115
    { AVIF_MATRIX_COEFFICIENTS_FCC, "FCC USFC 73.682", 0.30f, 0.11f },
116
    { AVIF_MATRIX_COEFFICIENTS_BT470BG, "BT.470-6 System BG", 0.299f, 0.114f },
117
    { AVIF_MATRIX_COEFFICIENTS_BT601, "BT.601", 0.299f, 0.114f },
118
    { AVIF_MATRIX_COEFFICIENTS_SMPTE240, "SMPTE ST 240", 0.212f, 0.087f },
119
    //{ AVIF_MATRIX_COEFFICIENTS_YCGCO, "YCgCo", 0.0f, 0.0f, }, // Handled elsewhere
120
    { AVIF_MATRIX_COEFFICIENTS_BT2020_NCL, "BT.2020 (non-constant luminance)", 0.2627f, 0.0593f },
121
    //{ AVIF_MATRIX_COEFFICIENTS_BT2020_CL, "BT.2020 (constant luminance)", 0.2627f, 0.0593f }, // FIXME: It is not an linear transformation.
122
    //{ AVIF_MATRIX_COEFFICIENTS_SMPTE2085, "ST 2085", 0.0f, 0.0f }, // FIXME: ST2085 can't represent using Kr and Kb.
123
    //{ AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL, "Chromaticity-derived constant luminance system", 0.0f, 0.0f } // FIXME: It is not an linear transformation.
124
    //{ AVIF_MATRIX_COEFFICIENTS_ICTCP, "BT.2100-0 ICtCp", 0.0f, 0.0f }, // FIXME: This can't represent using Kr and Kb.
125
};
126
127
static const int avifMatrixCoefficientsTableSize = sizeof(matrixCoefficientsTables) / sizeof(matrixCoefficientsTables[0]);
128
129
static avifBool calcYUVInfoFromCICP(const avifImage * image, float coeffs[3])
130
1.61k
{
131
1.61k
    if (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL) {
132
36
        avifColorPrimariesComputeYCoeffs(image->colorPrimaries, coeffs);
133
36
        return AVIF_TRUE;
134
1.58k
    } else {
135
10.3k
        for (int i = 0; i < avifMatrixCoefficientsTableSize; ++i) {
136
8.97k
            const struct avifMatrixCoefficientsTable * const table = &matrixCoefficientsTables[i];
137
8.97k
            if (table->matrixCoefficientsEnum == image->matrixCoefficients) {
138
188
                coeffs[0] = table->kr;
139
188
                coeffs[2] = table->kb;
140
188
                coeffs[1] = 1.0f - coeffs[0] - coeffs[2];
141
188
                return AVIF_TRUE;
142
188
            }
143
8.97k
        }
144
1.58k
    }
145
1.39k
    return AVIF_FALSE;
146
1.61k
}
147
148
void avifCalcYUVCoefficients(const avifImage * image, float * outR, float * outG, float * outB)
149
1.61k
{
150
    // (As of ISO/IEC 23000-22:2019 Amendment 2)
151
    // MIAF Section 7.3.6.4 "Colour information property":
152
    //
153
    // If a coded image has no associated colour property, the default property is defined as having
154
    // colour_type equal to 'nclx' with properties as follows:
155
    // -   colour_primaries equal to 1,
156
    // -   transfer_characteristics equal to 13,
157
    // -   matrix_coefficients equal to 5 or 6 (which are functionally identical), and
158
    // -   full_range_flag equal to 1.
159
    // Only if the colour information property of the image matches these default values, the colour
160
    // property may be omitted; all other images shall have an explicitly declared colour space via
161
    // association with a property of this type.
162
    //
163
    // See here for the discussion: https://github.com/AOMediaCodec/av1-avif/issues/77#issuecomment-676526097
164
165
    // matrix_coefficients of [5,6] == BT.601:
166
1.61k
    float kr = 0.299f;
167
1.61k
    float kb = 0.114f;
168
1.61k
    float kg = 1.0f - kr - kb;
169
170
1.61k
    float coeffs[3];
171
1.61k
    if (calcYUVInfoFromCICP(image, coeffs)) {
172
224
        kr = coeffs[0];
173
224
        kg = coeffs[1];
174
224
        kb = coeffs[2];
175
224
    }
176
177
1.61k
    *outR = kr;
178
1.61k
    *outG = kg;
179
1.61k
    *outB = kb;
180
1.61k
}
181
182
// ---------------------------------------------------------------------------
183
// Transfer characteristics
184
//
185
// Transfer characteristics are defined in ITU-T H.273 https://www.itu.int/rec/T-REC-H.273-201612-S/en
186
// with formulas for linear to gamma conversion in Table 3.
187
// This is based on tongyuantongyu's implementation in https://github.com/AOMediaCodec/libavif/pull/444
188
// with some fixes/changes in the first commit:
189
// - Fixed 5 transfer curves where toLinear and toGamma functions were swapped (470M, 470BG, Log100,
190
//   Log100Sqrt10 and SMPTE428)
191
// - 'avifToLinearLog100' and 'avifToLinearLog100Sqrt10' were modified to return the middle of the
192
//   range of linear values that are gamma-encoded to 0.0 in order to reduce the max round trip error,
193
//   based on vrabaud's change in
194
//   https://chromium.googlesource.com/webm/libwebp/+/25d94f473b10882b8bee9288d00539001b692042
195
// - In this file, PQ and HLG return "extended SDR" linear values in [0.0, 10000/203] and
196
//   [0.0, 1000/203] respectively, where a value of 1.0 means SDR white brightness (203 nits), and any
197
//   value above 1.0 is brigther.
198
// See git history for further changes.
199
200
struct avifTransferCharacteristicsTable
201
{
202
    avifTransferCharacteristics transferCharacteristicsEnum;
203
    const char * name;
204
    avifTransferFunction toLinear;
205
    avifTransferFunction toGamma;
206
};
207
208
static float avifToLinear709(float gamma)
209
0
{
210
0
    if (gamma < 0.0f) {
211
0
        return 0.0f;
212
0
    } else if (gamma < 4.5f * 0.018053968510807f) {
213
0
        return gamma / 4.5f;
214
0
    } else if (gamma < 1.0f) {
215
0
        return powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.0f / 0.45f);
216
0
    } else {
217
0
        return 1.0f;
218
0
    }
219
0
}
220
221
static float avifToGamma709(float linear)
222
0
{
223
0
    if (linear < 0.0f) {
224
0
        return 0.0f;
225
0
    } else if (linear < 0.018053968510807f) {
226
0
        return linear * 4.5f;
227
0
    } else if (linear < 1.0f) {
228
0
        return 1.09929682680944f * powf(linear, 0.45f) - 0.09929682680944f;
229
0
    } else {
230
0
        return 1.0f;
231
0
    }
232
0
}
233
234
static float avifToLinear470M(float gamma)
235
0
{
236
0
    return powf(AVIF_CLAMP(gamma, 0.0f, 1.0f), 2.2f);
237
0
}
238
239
static float avifToGamma470M(float linear)
240
0
{
241
0
    return powf(AVIF_CLAMP(linear, 0.0f, 1.0f), 1.0f / 2.2f);
242
0
}
243
244
static float avifToLinear470BG(float gamma)
245
0
{
246
0
    return powf(AVIF_CLAMP(gamma, 0.0f, 1.0f), 2.8f);
247
0
}
248
249
static float avifToGamma470BG(float linear)
250
0
{
251
0
    return powf(AVIF_CLAMP(linear, 0.0f, 1.0f), 1.0f / 2.8f);
252
0
}
253
254
static float avifToLinearSMPTE240(float gamma)
255
0
{
256
0
    if (gamma < 0.0f) {
257
0
        return 0.0f;
258
0
    } else if (gamma < 4.0f * 0.022821585529445f) {
259
0
        return gamma / 4.0f;
260
0
    } else if (gamma < 1.0f) {
261
0
        return powf((gamma + 0.111572195921731f) / 1.111572195921731f, 1.0f / 0.45f);
262
0
    } else {
263
0
        return 1.0f;
264
0
    }
265
0
}
266
267
static float avifToGammaSMPTE240(float linear)
268
0
{
269
0
    if (linear < 0.0f) {
270
0
        return 0.0f;
271
0
    } else if (linear < 0.022821585529445f) {
272
0
        return linear * 4.0f;
273
0
    } else if (linear < 1.0f) {
274
0
        return 1.111572195921731f * powf(linear, 0.45f) - 0.111572195921731f;
275
0
    } else {
276
0
        return 1.0f;
277
0
    }
278
0
}
279
280
static float avifToGammaLinear(float gamma)
281
0
{
282
0
    return AVIF_CLAMP(gamma, 0.0f, 1.0f);
283
0
}
284
285
static float avifToLinearLog100(float gamma)
286
0
{
287
    // The function is non-bijective so choose the middle of [0, 0.01].
288
0
    const float mid_interval = 0.01f / 2.f;
289
0
    return (gamma <= 0.0f) ? mid_interval : powf(10.0f, 2.f * (AVIF_MIN(gamma, 1.f) - 1.0f));
290
0
}
291
292
static float avifToGammaLog100(float linear)
293
0
{
294
0
    return linear <= 0.01f ? 0.0f : 1.0f + log10f(AVIF_MIN(linear, 1.0f)) / 2.0f;
295
0
}
296
297
static float avifToLinearLog100Sqrt10(float gamma)
298
0
{
299
    // The function is non-bijective so choose the middle of [0, 0.00316227766f].
300
0
    const float mid_interval = 0.00316227766f / 2.f;
301
0
    return (gamma <= 0.0f) ? mid_interval : powf(10.0f, 2.5f * (AVIF_MIN(gamma, 1.f) - 1.0f));
302
0
}
303
304
static float avifToGammaLog100Sqrt10(float linear)
305
0
{
306
0
    return linear <= 0.00316227766f ? 0.0f : 1.0f + log10f(AVIF_MIN(linear, 1.0f)) / 2.5f;
307
0
}
308
309
static float avifToLinearIEC61966(float gamma)
310
0
{
311
0
    if (gamma < -4.5f * 0.018053968510807f) {
312
0
        return powf((-gamma + 0.09929682680944f) / -1.09929682680944f, 1.0f / 0.45f);
313
0
    } else if (gamma < 4.5f * 0.018053968510807f) {
314
0
        return gamma / 4.5f;
315
0
    } else {
316
0
        return powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.0f / 0.45f);
317
0
    }
318
0
}
319
320
static float avifToGammaIEC61966(float linear)
321
0
{
322
0
    if (linear < -0.018053968510807f) {
323
0
        return -1.09929682680944f * powf(-linear, 0.45f) + 0.09929682680944f;
324
0
    } else if (linear < 0.018053968510807f) {
325
0
        return linear * 4.5f;
326
0
    } else {
327
0
        return 1.09929682680944f * powf(linear, 0.45f) - 0.09929682680944f;
328
0
    }
329
0
}
330
331
static float avifToLinearBT1361(float gamma)
332
0
{
333
0
    if (gamma < -0.25f) {
334
0
        return -0.25f;
335
0
    } else if (gamma < 0.0f) {
336
0
        return powf((gamma - 0.02482420670236f) / -0.27482420670236f, 1.0f / 0.45f) / -4.0f;
337
0
    } else if (gamma < 4.5f * 0.018053968510807f) {
338
0
        return gamma / 4.5f;
339
0
    } else if (gamma < 1.0f) {
340
0
        return powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.0f / 0.45f);
341
0
    } else {
342
0
        return 1.0f;
343
0
    }
344
0
}
345
346
static float avifToGammaBT1361(float linear)
347
0
{
348
0
    if (linear < -0.25f) {
349
0
        return -0.25f;
350
0
    } else if (linear < 0.0f) {
351
0
        return -0.27482420670236f * powf(-4.0f * linear, 0.45f) + 0.02482420670236f;
352
0
    } else if (linear < 0.018053968510807f) {
353
0
        return linear * 4.5f;
354
0
    } else if (linear < 1.0f) {
355
0
        return 1.09929682680944f * powf(linear, 0.45f) - 0.09929682680944f;
356
0
    } else {
357
0
        return 1.0f;
358
0
    }
359
0
}
360
361
static float avifToLinearSRGB(float gamma)
362
0
{
363
0
    if (gamma < 0.0f) {
364
0
        return 0.0f;
365
0
    } else if (gamma < 12.92f * 0.0030412825601275209f) {
366
0
        return gamma / 12.92f;
367
0
    } else if (gamma < 1.0f) {
368
0
        return powf((gamma + 0.0550107189475866f) / 1.0550107189475866f, 2.4f);
369
0
    } else {
370
0
        return 1.0f;
371
0
    }
372
0
}
373
374
static float avifToGammaSRGB(float linear)
375
0
{
376
0
    if (linear < 0.0f) {
377
0
        return 0.0f;
378
0
    } else if (linear < 0.0030412825601275209f) {
379
0
        return linear * 12.92f;
380
0
    } else if (linear < 1.0f) {
381
0
        return 1.0550107189475866f * powf(linear, 1.0f / 2.4f) - 0.0550107189475866f;
382
0
    } else {
383
0
        return 1.0f;
384
0
    }
385
0
}
386
387
0
#define PQ_MAX_NITS 10000.0f
388
0
#define HLG_PEAK_LUMINANCE_NITS 1000.0f
389
0
#define SDR_WHITE_NITS 203.0f
390
391
static float avifToLinearPQ(float gamma)
392
0
{
393
0
    if (gamma > 0.0f) {
394
0
        const float powGamma = powf(gamma, 1.0f / 78.84375f);
395
0
        const float num = AVIF_MAX(powGamma - 0.8359375f, 0.0f);
396
0
        const float den = AVIF_MAX(18.8515625f - 18.6875f * powGamma, FLT_MIN);
397
0
        const float linear = powf(num / den, 1.0f / 0.1593017578125f);
398
        // Scale so that SDR white is 1.0 (extended SDR).
399
0
        return linear * PQ_MAX_NITS / SDR_WHITE_NITS;
400
0
    } else {
401
0
        return 0.0f;
402
0
    }
403
0
}
404
405
static float avifToGammaPQ(float linear)
406
0
{
407
0
    if (linear > 0.0f) {
408
        // Scale from extended SDR range to [0.0, 1.0].
409
0
        linear = AVIF_CLAMP(linear * SDR_WHITE_NITS / PQ_MAX_NITS, 0.0f, 1.0f);
410
0
        const float powLinear = powf(linear, 0.1593017578125f);
411
0
        const float num = 0.1640625f * powLinear - 0.1640625f;
412
0
        const float den = 1.0f + 18.6875f * powLinear;
413
0
        return powf(1.0f + num / den, 78.84375f);
414
0
    } else {
415
0
        return 0.0f;
416
0
    }
417
0
}
418
419
static float avifToLinearSMPTE428(float gamma)
420
0
{
421
0
    return powf(AVIF_MAX(gamma, 0.0f), 2.6f) / 0.91655527974030934f;
422
0
}
423
424
static float avifToGammaSMPTE428(float linear)
425
0
{
426
0
    return powf(0.91655527974030934f * AVIF_MAX(linear, 0.0f), 1.0f / 2.6f);
427
0
}
428
429
// Formula from ITU-R BT.2100-2
430
// Assumes Lw=1000 (max display luminance in nits).
431
// For simplicity, approximates Ys (which should be 0.2627*r+0.6780*g+0.0593*b)
432
// to the input value (r, g, or b depending on the current channel).
433
static float avifToLinearHLG(float gamma)
434
0
{
435
    // Inverse OETF followed by the OOTF, see Table 5 in ITU-R BT.2100-2 page 7.
436
    // Note that this differs slightly from  ITU-T H.273 which doesn't use the OOTF.
437
0
    if (gamma < 0.0f) {
438
0
        return 0.0f;
439
0
    }
440
0
    float linear = 0.0f;
441
0
    if (gamma <= 0.5f) {
442
0
        linear = powf((gamma * gamma) * (1.0f / 3.0f), 1.2f);
443
0
    } else {
444
0
        linear = powf((expf((gamma - 0.55991073f) / 0.17883277f) + 0.28466892f) / 12.0f, 1.2f);
445
0
    }
446
    // Scale so that SDR white is 1.0 (extended SDR).
447
0
    return linear * HLG_PEAK_LUMINANCE_NITS / SDR_WHITE_NITS;
448
0
}
449
450
static float avifToGammaHLG(float linear)
451
0
{
452
    // Scale from extended SDR range to [0.0, 1.0].
453
0
    linear = AVIF_CLAMP(linear * SDR_WHITE_NITS / HLG_PEAK_LUMINANCE_NITS, 0.0f, 1.0f);
454
    // Inverse OOTF followed by OETF see Table 5 and Note 5i in ITU-R BT.2100-2 page 7-8.
455
0
    linear = powf(linear, 1.0f / 1.2f);
456
0
    if (linear < 0.0f) {
457
0
        return 0.0f;
458
0
    } else if (linear <= (1.0f / 12.0f)) {
459
0
        return sqrtf(3.0f * linear);
460
0
    } else {
461
0
        return 0.17883277f * logf(12.0f * linear - 0.28466892f) + 0.55991073f;
462
0
    }
463
0
}
464
465
static const struct avifTransferCharacteristicsTable transferCharacteristicsTables[] = {
466
    { AVIF_TRANSFER_CHARACTERISTICS_BT709, "BT.709", avifToLinear709, avifToGamma709 },
467
    { AVIF_TRANSFER_CHARACTERISTICS_BT470M, "BT.470-6 System M", avifToLinear470M, avifToGamma470M },
468
    { AVIF_TRANSFER_CHARACTERISTICS_BT470BG, "BT.470-6 System BG", avifToLinear470BG, avifToGamma470BG },
469
    { AVIF_TRANSFER_CHARACTERISTICS_BT601, "BT.601", avifToLinear709, avifToGamma709 },
470
    { AVIF_TRANSFER_CHARACTERISTICS_SMPTE240, "SMPTE 240M", avifToLinearSMPTE240, avifToGammaSMPTE240 },
471
    { AVIF_TRANSFER_CHARACTERISTICS_LINEAR, "Linear", avifToGammaLinear, avifToGammaLinear },
472
    { AVIF_TRANSFER_CHARACTERISTICS_LOG100, "100:1 Log", avifToLinearLog100, avifToGammaLog100 },
473
    { AVIF_TRANSFER_CHARACTERISTICS_LOG100_SQRT10, "100sqrt(10):1 Log", avifToLinearLog100Sqrt10, avifToGammaLog100Sqrt10 },
474
    { AVIF_TRANSFER_CHARACTERISTICS_IEC61966, "IEC 61966-2-4", avifToLinearIEC61966, avifToGammaIEC61966 },
475
    { AVIF_TRANSFER_CHARACTERISTICS_BT1361, "BT.1361", avifToLinearBT1361, avifToGammaBT1361 },
476
    { AVIF_TRANSFER_CHARACTERISTICS_SRGB, "sRGB", avifToLinearSRGB, avifToGammaSRGB },
477
    { AVIF_TRANSFER_CHARACTERISTICS_BT2020_10BIT, "10bit BT.2020", avifToLinear709, avifToGamma709 },
478
    { AVIF_TRANSFER_CHARACTERISTICS_BT2020_12BIT, "12bit BT.2020", avifToLinear709, avifToGamma709 },
479
    { AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084, "SMPTE ST 2084 (PQ)", avifToLinearPQ, avifToGammaPQ },
480
    { AVIF_TRANSFER_CHARACTERISTICS_SMPTE428, "SMPTE ST 428-1", avifToLinearSMPTE428, avifToGammaSMPTE428 },
481
    { AVIF_TRANSFER_CHARACTERISTICS_HLG, "ARIB STD-B67 (HLG)", avifToLinearHLG, avifToGammaHLG }
482
};
483
484
static const int avifTransferCharacteristicsTableSize =
485
    sizeof(transferCharacteristicsTables) / sizeof(transferCharacteristicsTables[0]);
486
487
avifTransferFunction avifTransferCharacteristicsGetGammaToLinearFunction(avifTransferCharacteristics atc)
488
0
{
489
0
    for (int i = 0; i < avifTransferCharacteristicsTableSize; ++i) {
490
0
        const struct avifTransferCharacteristicsTable * const table = &transferCharacteristicsTables[i];
491
0
        if (table->transferCharacteristicsEnum == atc) {
492
0
            return table->toLinear;
493
0
        }
494
0
    }
495
0
    return avifToLinear709; // Provide a reasonable default.
496
0
}
497
498
avifTransferFunction avifTransferCharacteristicsGetLinearToGammaFunction(avifTransferCharacteristics atc)
499
0
{
500
0
    for (int i = 0; i < avifTransferCharacteristicsTableSize; ++i) {
501
0
        const struct avifTransferCharacteristicsTable * const table = &transferCharacteristicsTables[i];
502
0
        if (table->transferCharacteristicsEnum == atc) {
503
0
            return table->toGamma;
504
0
        }
505
0
    }
506
0
    return avifToGamma709; // Provide a reasonable default.
507
0
}
508
509
void avifColorPrimariesComputeYCoeffs(avifColorPrimaries colorPrimaries, float coeffs[3])
510
36
{
511
36
    float primaries[8];
512
36
    avifColorPrimariesGetValues(colorPrimaries, primaries);
513
36
    float const rX = primaries[0];
514
36
    float const rY = primaries[1];
515
36
    float const gX = primaries[2];
516
36
    float const gY = primaries[3];
517
36
    float const bX = primaries[4];
518
36
    float const bY = primaries[5];
519
36
    float const wX = primaries[6];
520
36
    float const wY = primaries[7];
521
36
    float const rZ = 1.0f - (rX + rY); // (Eq. 34)
522
36
    float const gZ = 1.0f - (gX + gY); // (Eq. 35)
523
36
    float const bZ = 1.0f - (bX + bY); // (Eq. 36)
524
36
    float const wZ = 1.0f - (wX + wY); // (Eq. 37)
525
36
    float const kr = (rY * (wX * (gY * bZ - bY * gZ) + wY * (bX * gZ - gX * bZ) + wZ * (gX * bY - bX * gY))) /
526
36
                     (wY * (rX * (gY * bZ - bY * gZ) + gX * (bY * rZ - rY * bZ) + bX * (rY * gZ - gY * rZ)));
527
    // (Eq. 32)
528
36
    float const kb = (bY * (wX * (rY * gZ - gY * rZ) + wY * (gX * rZ - rX * gZ) + wZ * (rX * gY - gX * rY))) /
529
36
                     (wY * (rX * (gY * bZ - bY * gZ) + gX * (bY * rZ - rY * bZ) + bX * (rY * gZ - gY * rZ)));
530
    // (Eq. 33)
531
36
    coeffs[0] = kr;
532
36
    coeffs[2] = kb;
533
36
    coeffs[1] = 1.0f - coeffs[0] - coeffs[2];
534
36
}