1 /*
2 * Copyright (c) 2022 Niklas Haas
3 * This file is part of FFmpeg.
4 *
5 * FFmpeg is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * FFmpeg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with FFmpeg; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
21
23
25 {
28 }
29
31 {
32 memset(
s, 0,
sizeof(*
s));
34 s->ctx = cmsCreateContext(
NULL,
s);
37
38 cmsSetLogErrorHandlerTHR(
s->ctx,
log_cb);
39 return 0;
40 }
41
43 {
45 cmsFreeToneCurve(
s->curves[
i]);
46 cmsDeleteContext(
s->ctx);
47 memset(
s, 0,
sizeof(*
s));
48 }
49
51 cmsToneCurve **out_curve)
52 {
55
57 goto done;
58
59 switch (trc) {
61 s->curves[trc] = cmsBuildGamma(
s->ctx, 1.0);
62 break;
64 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.2);
65 break;
67 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.8);
68 break;
73 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
74 /* γ = */ 1/0.45,
75 /* a = */ 1/1.099296826809442,
76 /* b = */ 1 - 1/1.099296826809442,
77 /* c = */ 1/4.5,
78 /* d = */ 4.5 * 0.018053968510807,
79 });
80 break;
82 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
83 /* γ = */ 1/0.45,
84 /* a = */ 1/1.1115,
85 /* b = */ 1 - 1/1.1115,
86 /* c = */ 1/4.0,
87 /* d = */ 4.0 * 0.0228,
88 });
89 break;
91 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
92 /* a = */ 1.0,
93 /* b = */ 10.0,
94 /* c = */ 2.0,
95 /* d = */ -1.0,
96 /* e = */ 0.0
97 });
98 break;
100 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
101 /* a = */ 1.0,
102 /* b = */ 10.0,
103 /* c = */ 2.5,
104 /* d = */ -1.0,
105 /* e = */ 0.0
106 });
107 break;
109 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
110 /* γ = */ 2.4,
111 /* a = */ 1/1.055,
112 /* b = */ 1 - 1/1.055,
113 /* c = */ 1/12.92,
114 /* d = */ 12.92 * 0.0031308,
115 });
116 break;
118 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 2, (
double[3]) {
119 /* γ = */ 2.6,
120 /* a = */ pow(52.37/48.0, 1/2.6),
121 /* b = */ 0.0
122 });
123 break;
124
125 /* Can't be represented using the existing parametric tone curves.
126 * FIXME: use cmsBuildTabulatedToneCurveFloat instead */
132
133 default:
135 }
136
139
140 done:
141 *out_curve =
s->curves[trc];
142 return 0;
143 }
144
148 cmsHPROFILE *out_profile)
149 {
150 cmsToneCurve *tonecurve;
153
158
159 *out_profile = cmsCreateRGBProfileTHR(
s->ctx,
160 &(cmsCIExyY) { av_q2d(prim->wp.x), av_q2d(prim->wp.y), 1.0 },
161 &(cmsCIExyYTRIPLE) {
162 .Red = { av_q2d(prim->prim.r.x), av_q2d(prim->prim.r.y), 1.0 },
163 .Green = { av_q2d(prim->prim.g.x), av_q2d(prim->prim.g.y), 1.0 },
164 .Blue = { av_q2d(prim->prim.b.x), av_q2d(prim->prim.b.y), 1.0 },
165 },
166 (cmsToneCurve *[3]) { tonecurve, tonecurve, tonecurve }
167 );
168
170 }
171
173 {
174 cmsUInt32Number
size;
176
179
181 if (!buf)
183
187 }
188
192 }
193
194 return 0;
195 }
196
198 {
199 double k = 1.0 / (
XYZ.X +
XYZ.Y +
XYZ.Z);
202 }
203
205 {
207 /* denominator assumed to be positive */
209 }
210
211 static const AVCIExy wp_d50 = { {3457, 10000}, {3585, 10000} };
/* CIE D50 */
212
214 {
215 cmsCIEXYZ *white,
fixed;
219 return 0;
220
221 if (cmsGetEncodedICCversion(
profile) >= 0x4000000) {
// ICC v4
222 switch (cmsGetHeaderRenderingIntent(
profile)) {
223 case INTENT_RELATIVE_COLORIMETRIC:
224 case INTENT_ABSOLUTE_COLORIMETRIC: ;
225 /* ICC v4 colorimetric profiles are specified to always use D50
226 * media white point, anything else is a violation of the spec.
227 * Sadly, such profiles are incredibly common (Apple...), so make
228 * an effort to fix them. */
229 if (!(white = cmsReadTag(
profile, cmsSigMediaWhitePointTag)))
235 "profile media white point tag (expected %.4f %.4f, "
236 "got %.4f %.4f)\n",
239 /* x+y+z = 1 */
244 if (!cmsWriteTag(
profile, cmsSigMediaWhitePointTag, &
fixed))
246 }
247 break;
248 default: break;
249 }
250 }
251
252 return 0;
253 }
254
257 {
258 static const uint8_t testprimaries[4][3] = {
259 { 0xFF, 0, 0 }, /* red */
260 { 0, 0xFF, 0 }, /* green */
261 { 0, 0, 0xFF }, /* blue */
262 { 0xFF, 0xFF, 0xFF }, /* white */
263 };
264
267 cmsFloat64Number prev_adapt;
268 cmsHPROFILE xyz;
269 cmsHTRANSFORM tf;
271
272 xyz = cmsCreateXYZProfileTHR(
s->ctx);
273 if (!xyz)
275
276 /* We need to use an unadapted observer to get the raw values */
277 prev_adapt = cmsSetAdaptationStateTHR(
s->ctx, 0.0);
278 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8, xyz, TYPE_XYZ_DBL,
279 INTENT_ABSOLUTE_COLORIMETRIC,
280 /* Note: These flags mostly don't do anything
281 * anyway, but specify them regardless */
282 cmsFLAGS_NOCACHE |
283 cmsFLAGS_NOOPTIMIZE |
284 cmsFLAGS_LOWRESPRECALC |
285 cmsFLAGS_GRIDPOINTS(2));
286 cmsSetAdaptationStateTHR(
s->ctx, prev_adapt);
287 cmsCloseProfile(xyz);
288 if (!tf) {
291 }
292
293 cmsDoTransform(tf, testprimaries,
dst, 4);
294 cmsDeleteTransform(tf);
299 return 0;
300 }
301
304 {
305 /* 8-bit linear grayscale ramp */
306 static const uint8_t testramp[16][3] = {
307 { 1, 1, 1}, /* avoid exact zero due to log100 etc. */
308 { 17, 17, 17},
309 { 34, 34, 34},
310 { 51, 51, 51},
311 { 68, 68, 68},
312 { 85, 85, 85},
313 { 02, 02, 02},
314 {119, 119, 119},
315 {136, 136, 136},
316 {153, 153, 153},
317 {170, 170, 170},
318 {187, 187, 187},
319 {204, 204, 204},
320 {221, 221, 221},
321 {238, 238, 238},
322 {255, 255, 255},
323 };
324
326
328 cmsToneCurve *tonecurve;
330 cmsHTRANSFORM tf;
333 continue;
334
335 ref = cmsCreateGrayProfileTHR(
s->ctx, cmsD50_xyY(), tonecurve);
338
339 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8,
ref, TYPE_GRAY_DBL,
340 INTENT_RELATIVE_COLORIMETRIC,
341 cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE);
342 cmsCloseProfile(
ref);
343 if (!tf) {
346 }
347
349 cmsDeleteTransform(tf);
350
354 *out_trc = trc;
355 return 0;
356 }
357 }
358
360 return 0;
361 }