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
22
24
26 {
29 }
30
32 {
33 memset(
s, 0,
sizeof(*
s));
35 s->ctx = cmsCreateContext(
NULL,
s);
38
39 cmsSetLogErrorHandlerTHR(
s->ctx,
log_cb);
40 return 0;
41 }
42
44 {
46 cmsFreeToneCurve(
s->curves[
i]);
47 cmsDeleteContext(
s->ctx);
48 memset(
s, 0,
sizeof(*
s));
49 }
50
52 cmsToneCurve **out_curve)
53 {
56
58 goto done;
59
60 switch (trc) {
62 s->curves[trc] = cmsBuildGamma(
s->ctx, 1.0);
63 break;
65 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.2);
66 break;
68 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.8);
69 break;
74 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
75 /* γ = */ 1/0.45,
76 /* a = */ 1/1.099296826809442,
77 /* b = */ 1 - 1/1.099296826809442,
78 /* c = */ 1/4.5,
79 /* d = */ 4.5 * 0.018053968510807,
80 });
81 break;
83 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
84 /* γ = */ 1/0.45,
85 /* a = */ 1/1.1115,
86 /* b = */ 1 - 1/1.1115,
87 /* c = */ 1/4.0,
88 /* d = */ 4.0 * 0.0228,
89 });
90 break;
92 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
93 /* a = */ 1.0,
94 /* b = */ 10.0,
95 /* c = */ 2.0,
96 /* d = */ -1.0,
97 /* e = */ 0.0
98 });
99 break;
101 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
102 /* a = */ 1.0,
103 /* b = */ 10.0,
104 /* c = */ 2.5,
105 /* d = */ -1.0,
106 /* e = */ 0.0
107 });
108 break;
110 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
111 /* γ = */ 2.4,
112 /* a = */ 1/1.055,
113 /* b = */ 1 - 1/1.055,
114 /* c = */ 1/12.92,
115 /* d = */ 12.92 * 0.0031308,
116 });
117 break;
119 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 2, (
double[3]) {
120 /* γ = */ 2.6,
121 /* a = */ pow(52.37/48.0, 1/2.6),
122 /* b = */ 0.0
123 });
124 break;
125
126 /* Can't be represented using the existing parametric tone curves.
127 * FIXME: use cmsBuildTabulatedToneCurveFloat instead */
133
134 default:
136 }
137
140
141 done:
142 *out_curve =
s->curves[trc];
143 return 0;
144 }
145
149 cmsHPROFILE *out_profile)
150 {
151 cmsToneCurve *tonecurve;
154
159
160 *out_profile = cmsCreateRGBProfileTHR(
s->ctx,
161 &(cmsCIExyY) { av_q2d(prim->wp.x), av_q2d(prim->wp.y), 1.0 },
162 &(cmsCIExyYTRIPLE) {
163 .Red = { av_q2d(prim->prim.r.x), av_q2d(prim->prim.r.y), 1.0 },
164 .Green = { av_q2d(prim->prim.g.x), av_q2d(prim->prim.g.y), 1.0 },
165 .Blue = { av_q2d(prim->prim.b.x), av_q2d(prim->prim.b.y), 1.0 },
166 },
167 (cmsToneCurve *[3]) { tonecurve, tonecurve, tonecurve }
168 );
169
171 }
172
174 {
175 cmsUInt32Number
size;
177
180
182 if (!buf)
184
188 }
189
193 }
194
195 return 0;
196 }
197
199 {
200 double k = 1.0 / (XYZ.X + XYZ.Y + XYZ.Z);
201 xy->
x =
av_d2q(k * XYZ.X, 100000);
202 xy->
y =
av_d2q(k * XYZ.Y, 100000);
203 }
204
207 {
208 static const uint8_t testprimaries[4][3] = {
209 { 0xFF, 0, 0 }, /* red */
210 { 0, 0xFF, 0 }, /* green */
211 { 0, 0, 0xFF }, /* blue */
212 { 0xFF, 0xFF, 0xFF }, /* white */
213 };
214
217 cmsFloat64Number prev_adapt;
218 cmsHPROFILE xyz;
220 cmsCIEXYZ dst[4];
221
222 xyz = cmsCreateXYZProfileTHR(
s->ctx);
223 if (!xyz)
225
226 /* We need to use an unadapted observer to get the raw values */
227 prev_adapt = cmsSetAdaptationStateTHR(
s->ctx, 0.0);
228 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8, xyz, TYPE_XYZ_DBL,
229 INTENT_ABSOLUTE_COLORIMETRIC,
230 /* Note: These flags mostly don't do anything
231 * anyway, but specify them regardless */
232 cmsFLAGS_NOCACHE |
233 cmsFLAGS_NOOPTIMIZE |
234 cmsFLAGS_LOWRESPRECALC |
235 cmsFLAGS_GRIDPOINTS(2));
236 cmsSetAdaptationStateTHR(
s->ctx, prev_adapt);
237 cmsCloseProfile(xyz);
241 }
242
243 cmsDoTransform(
tf, testprimaries, dst, 4);
244 cmsDeleteTransform(
tf);
249 return 0;
250 }
251
254 {
255 /* 8-bit linear grayscale ramp */
256 static const uint8_t testramp[16][3] = {
257 { 1, 1, 1}, /* avoid exact zero due to log100 etc. */
258 { 17, 17, 17},
259 { 34, 34, 34},
260 { 51, 51, 51},
261 { 68, 68, 68},
262 { 85, 85, 85},
263 { 02, 02, 02},
264 {119, 119, 119},
265 {136, 136, 136},
266 {153, 153, 153},
267 {170, 170, 170},
268 {187, 187, 187},
269 {204, 204, 204},
270 {221, 221, 221},
271 {238, 238, 238},
272 {255, 255, 255},
273 };
274
276
278 cmsToneCurve *tonecurve;
283 continue;
284
285 ref = cmsCreateGrayProfileTHR(
s->ctx, cmsD50_xyY(), tonecurve);
288
289 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8,
ref, TYPE_GRAY_DBL,
290 INTENT_RELATIVE_COLORIMETRIC,
291 cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE);
292 cmsCloseProfile(
ref);
296 }
297
299 cmsDeleteTransform(
tf);
300
304 *out_trc = trc;
305 return 0;
306 }
307 }
308
310 return 0;
311 }