summaryrefslogtreecommitdiff
path: root/xicc/bt1886.c
blob: 2dad1a6184d6925aec31aebafdb2e3028a218e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

/* 
 * Author:  Graeme W. Gill
 * Date:    16/8/13
 * Version: 1.00
 *
 * Copyright 2013, 2014 Graeme W. Gill
 * All rights reserved.
 *
 * This material is licenced under the GNU AFFERO GENERAL PUB LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 *
 */

/* BT.1886 type input offset transfer curve, */
/* + general gamma + input + output offset curve support. */

#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#ifdef __sun
#include <unistd.h>
#endif
#if defined(__IBMC__) && defined(_M_IX86)
#include <float.h>
#endif
#include "numlib.h"
#include "icc.h"		/* definitions for this library */
#include "bt1886.h"		/* definitions for this library */

#undef DEBUG

/* BT.1886 support */
/* This is both a EOTF curve, and a white point */
/* adjustment. */

/* Compute technical gamma from effective gamma in BT.1886 style */

/* Info for optimization */
typedef struct {
	double wp;			/* 100% input target */ 
	double thyr;		/* 50% input target */
	double bp;			/* 0% input target */
} gam_fits;

/* gamma + input offset function handed to powell() */
static double gam_fit(void *dd, double *v) {
	gam_fits *gf = (gam_fits *)dd;
	double gamma = v[0];
	double a, b;
	double rv = 0.0;
	double t1, t2;

	if (gamma < 0.0) {
		rv += 100.0 * -gamma;
		gamma = 1e-4;
	}

	t1 = pow(gf->bp, 1.0/gamma);
	t2 = pow(gf->wp, 1.0/gamma);
	b = t1/(t2 - t1);						/* Offset */
	a = pow(t2 - t1, gamma);				/* Gain */

	/* Comput 50% output for this technical gamma */
	/* (All values are without output offset being added in) */
	t1 = a * pow((0.5 + b), gamma);
	t1 = t1 - gf->thyr;
	rv += t1 * t1;
	
	return rv;
}

/* Given the effective gamma and the output offset Y, */
/* return the technical gamma needed for the correct 50% response. */
static double xicc_tech_gamma(
	double egamma,			/* effective gamma needed */
	double off,				/* Output offset required */
	double outoprop			/* Prop. of offset to be accounted for on output */
) {
	gam_fits gf;
	double outo;
	double op[1], sa[1], rv;

	if (off <= 0.0) {
		return egamma;
	}

	/* We set up targets without outo being added */
	outo = off * outoprop; 		/* Offset acounted for in output */
	gf.bp = off - outo;			/* Black value for 0 % input */
	gf.wp = 1.0 - outo;			/* White value for 100% input */
	gf.thyr = pow(0.5, egamma) - outo;	/* Advetised 50% target */

	op[0] = egamma;
	sa[0] = 0.1;

	if (powell(&rv, 1, op, sa, 1e-6, 500, gam_fit, (void *)&gf, NULL, NULL) != 0)
		warning("Computing effective gamma and input offset is inaccurate");

	return op[0];
}

/* Set the bt1886_info to a default do nothing state */
void bt1886_setnop(bt1886_info *p) {
	icmXYZ2XYZ(p->w, icmD50);
	p->ingo = 0.0;
	p->outsc = 1.0;
	p->outo = 0.0;
	p->outL = 0.0;
	p->tab[0] = 0.0;
	p->tab[1] = 0.0;
}

/* Setup the bt1886_info for the given target black point, proportion of */
/* offset to be accounted for on output, and gamma. */
/* wp XYZ simply sets the L*a*b* reference */
/* Pure BT.1886 will have outopro = 0.0 and gamma = 2.4 */
void bt1886_setup(
bt1886_info *p,
icmXYZNumber *w,	/* wp used for L*a*b* conversion */
double *XYZbp,		/* normalised bp used for black offset and black point hue "bend" */
double outoprop,	/* 0..1 proportion of output black point compensation */
double gamma,		/* technical or effective gamma */
int effg			/* nz if effective gamma, z if technical gamma */
) {
	double Lab[3], ino, bkipow, wtipow;

	icmXYZ2XYZ(p->w, *w);

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886_setup wp.Y %f, bp.Y %f, outprop %f, gamma %f, effg %d", p->w.Y, XYZbp[1], outoprop, gamma, effg);
#endif

	if (effg) {
		p->gamma = xicc_tech_gamma(gamma, XYZbp[1], outoprop);
#ifdef DEBUG
		a1logd(g_log, 2, "bt1886_setup tgamma %f", p->gamma);
#endif
	} else {
		p->gamma = gamma;
	}

	icmXYZ2Lab(&p->w, Lab, XYZbp);

	p->outL   = Lab[0];		/* For bp blend comp. */
	p->tab[0] = Lab[1];		/* a* b* correction needed */
	p->tab[1] = Lab[2];
#ifdef DEBUG
	a1logd(g_log, 2, "bt1886_setup bend Lab = %f %f %f", p->outL, p->tab[0], p->tab[1]);
#endif

	if (XYZbp[1] < 0.0)
		XYZbp[1] = 0.0;


	p->outo = XYZbp[1] * outoprop; 		/* Offset acounted for in output */
	ino = XYZbp[1] - p->outo;			/* Balance of offset accounted for in input */

	bkipow = pow(ino, 1.0/p->gamma);				/* Input offset black to 1/pow */
	wtipow = pow((1.0 - p->outo), 1.0/p->gamma);	/* Input offset white to 1/pow */

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886_setup outo %f, ino %f, bkipow %f, wtipow %f", p->outo, ino, bkipow, wtipow);
#endif

	p->ingo = bkipow/(wtipow - bkipow);			/* non-linear Y that makes input offset */
												/* proportion of black point */
	p->outsc = pow(wtipow - bkipow, p->gamma);	/* Scale to make input of 1 map to */
												/* 1.0 - p->outo */

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886_setup ingo %f, outsc %f", p->ingo, p->outsc);
#endif
}

/* Apply BT.1886 eotf curve to the device RGB value */
/* to produce a linear light RGB. We pass xvYCC out of range values through. */
void bt1886_fwd_curve(bt1886_info *p, double *out, double *in) {
	int j;

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886    Dev RGB in %f %f %f, pow %f\n", in[0],in[1],in[2], p->gamma);
	a1logd(g_log, 2, "outo %f, outsc %f, pow %f\n", p->outo, p->outsc, 1.0/p->gamma);
	a1logd(g_log, 2, "ingo %f, pow %f, outsc %f, outo %f\n", p->ingo, p->gamma, p->outsc,p->outo);
#endif

	for (j = 0; j < 3; j++) {
		int neg = 0;
		double vv = in[j];
	
		if (vv < 0.0) {			/* Allow for xvYCC */
			neg = 1;
			vv = -vv;
		}
		/* Apply input offset */
		vv += p->ingo;
		
		/* Apply power and scale */
		if (vv > 0.0)
			vv = p->outsc * pow(vv, p->gamma);

		/* Apply output portion of offset */
		vv += p->outo;

		if (neg)
			vv = -vv;

		out[j] = vv;
	}

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 linear RGB out %f %f %f\n", out[0],out[1],out[2]);
#endif
}

/* Apply inverse BT.1886 eotf curve to the linear light RGB to produce */
/* device RGB values. We pass xvYCC out of range values through. */
void bt1886_bwd_curve(bt1886_info *p, double *out, double *in) {
	int j;

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 linear RGB in %f %f %f\n", in[0],in[1],in[2]);
	a1logd(g_log, 2, "outo %f, outsc %f, pow %f, ingo %f\n", p->outo, p->outsc, 1.0/p->gamma,p->ingo);
#endif

	for (j = 0; j < 3; j++) {
		int neg = 0;
		double vv = in[j];
	
		if (vv < 0.0) {			/* Allow for xvYCC */
			neg = 1;
			vv = -vv;
		}

		/* Un-apply output portion of offset */
		vv -= p->outo;

		/* Un-apply power and scale */
		if (vv > 0.0)
			vv = pow(vv/p->outsc, 1.0/p->gamma);

		/* Un-apply input offset */
		vv -= p->ingo;
		
		if (neg)
			vv = -vv;

		out[j] = vv;
	}

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886   Dev RGB out %f %f %f\n", in[0],in[1],in[2]);
#endif
}

/* Apply BT.1886 processing black point hue adjustment to the XYZ value */
void bt1886_wp_adjust(bt1886_info *p, double *out, double *in) {
	double vv;

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 XYZ wp adj. in %f %f %f\n", in[0],in[1],in[2]);
#endif

	icmXYZ2Lab(&p->w, out, in);

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 Lab wp adj. in %f %f %f\n", out[0],out[1],out[2]);
#endif

	/* Blend ab to required black point offset p->tab[] as L approaches black. */
	vv = (out[0] - p->outL)/(100.0 - p->outL);	/* 0 at bp, 1 at wp */
	vv = 1.0 - vv;
	if (vv < 0.0)
		vv = 0.0;
	else if (vv > 1.0)
		vv = 1.0;
	vv = pow(vv, 40.0);

	out[1] += vv * p->tab[0];
	out[2] += vv * p->tab[1];

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 Lab after wp adj. %f %f %f\n", out[0],out[1],out[2]);
#endif

	icmLab2XYZ(&p->w, out, out);

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 XYZ after wp adj. %f %f %f\n", out[0],out[1],out[2]);
#endif
}


/* Apply inverse BT.1886 processing black point hue adjustment to the XYZ value */
void bt1886_inv_wp_adjust(bt1886_info *p, double *out, double *in) {
	double vv;

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 XYZ inv. wp adj. in %f %f %f\n", in[0],in[1],in[2]);
#endif

	icmXYZ2Lab(&p->w, out, in);

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 Lab inv. wp adj. in %f %f %f\n", out[0],out[1],out[2]);
#endif

	/* Blend ab to required black point offset p->tab[] as L approaches black. */
	vv = (out[0] - p->outL)/(100.0 - p->outL);	/* 0 at bp, 1 at wp */
	vv = 1.0 - vv;
	if (vv < 0.0)
		vv = 0.0;
	else if (vv > 1.0)
		vv = 1.0;
	vv = pow(vv, 40.0);

	out[1] -= vv * p->tab[0];
	out[2] -= vv * p->tab[1];

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 Lab after inv. wp adj. %f %f %f\n", out[0],out[1],out[2]);
#endif

	icmLab2XYZ(&p->w, out, out);

#ifdef DEBUG
	a1logd(g_log, 2, "bt1886 XYZ after inv. wp adj. %f %f %f\n", out[0],out[1],out[2]);
#endif
}