Debian Codegraph @

xicc/cam97test.c

raw symbols SCIP
1
2 /*
3 * International Color Consortium color transform expanded support
4 *
5 * Author: Graeme W. Gill
6 * Date: 1/5/01
7 * Version: 1.20
8 *
9 * Copyright 2000-2004 Graeme W. Gill
10 * All rights reserved.
11 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
12 * see the License.txt file for licencing details.
13 *
14 */
15
16 /*
17 * This is some test code to test the CIECAM97s3 functionality.
18 */
19
20
21 #include <stdio.h>
22 #include <math.h>
23 #include "xcam.h"
24 #include "cam97s3.h"
25
26 #define DIAG /* Print internal value diagnostics for each conversion */
27 #define SPOTTEST /* Test known spot colors */
28 #undef INVTEST /* Lab cube to XYZ to Jab to XYZ */
29 #undef INVTEST2 /* Jab cube to XYZ to Jab */
30 #define TRES 33
31 #define USE_HK 0 /* Use Helmholtz-Kohlraush */
32 #define NOCAMCLIP 1 /* Don't clip before XYZ2Jab */
33
34 #ifndef _isnan
35 #define _isnan(x) ((x) != (x))
36 #define _finite(x) ((x) == (x))
37 #endif
38
39 static void
40 Lab2XYZ(double *out, double *in) {
41 double L = in[0], a = in[1], b = in[2];
42 double x,y,z,fx,fy,fz;
43
44 if (L > 8.0) {
45 fy = (L + 16.0)/116.0;
46 y = pow(fy,3.0);
47 } else {
48 y = L/903.2963058;
49 fy = 7.787036979 * y + 16.0/116.0;
50 }
51
52 fx = a/500.0 + fy;
53 if (fx > 24.0/116.0)
54 x = pow(fx,3.0);
55 else
56 x = (fx - 16.0/116.0)/7.787036979;
57
58 fz = fy - b/200.0;
59 if (fz > 24.0/116.0)
60 z = pow(fz,3.0);
61 else
62 z = (fz - 16.0/116.0)/7.787036979;
63
64 out[0] = x * 0.9642;
65 out[1] = y * 1.0000;
66 out[2] = z * 0.8249;
67 }
68
69 /* CIE XYZ to perceptual Lab */
70 static void
71 XYZ2Lab(double *out, double *in) {
72 double X = in[0], Y = in[1], Z = in[2];
73 double x,y,z,fx,fy,fz;
74
75 x = X/0.9642;
76 y = Y/1.0000;
77 z = Z/0.8249;
78
79 if (x > 0.008856451586)
80 fx = pow(x,1.0/3.0);
81 else
82 fx = 7.787036979 * x + 16.0/116.0;
83
84 if (y > 0.008856451586)
85 fy = pow(y,1.0/3.0);
86 else
87 fy = 7.787036979 * y + 16.0/116.0;
88
89 if (z > 0.008856451586)
90 fz = pow(z,1.0/3.0);
91 else
92 fz = 7.787036979 * z + 16.0/116.0;
93
94 out[0] = 116.0 * fy - 16.0;
95 out[1] = 500.0 * (fx - fy);
96 out[2] = 200.0 * (fy - fz);
97 }
98
99
100 /* Return maximum difference */
101 double maxdiff(double in1[3], double in2[3]) {
102 int i;
103 double tt, rv = 0.0;
104
105 /* Deal with the possibility that we have nan's */
106 for (i = 0; i < 3; i++) {
107 tt = fabs(in1[i] - in2[i]);
108 if (_isnan(tt))
109 return tt;
110 if (tt > rv)
111 rv = tt;
112 }
113 return rv;
114 }
115
116 /* Return absolute difference */
117 double absdiff(double in1[3], double in2[3]) {
118 double tt, rv = 0.0;
119 tt = in1[0] - in2[0];
120 rv += tt * tt;
121 tt = in1[1] - in2[1];
122 rv += tt * tt;
123 tt = in1[2] - in2[2];
124 rv += tt * tt;
125 return sqrt(rv);
126 }
127
128 /* Return maximum Lab difference of XYZ */
129 double maxxyzdiff(double i1[3], double i2[3]) {
130 int i;
131 double tt, rv = 0.0;
132 double in1[3], in2[3];
133
134 XYZ2Lab(in1, i1);
135 XYZ2Lab(in2, i2);
136
137 /* Deal with the possibility that we have nan's */
138 for (i = 0; i < 3; i++) {
139 tt = fabs(in1[i] - in2[i]);
140 if (_isnan(tt))
141 return tt;
142 if (tt > rv)
143 rv = tt;
144 }
145 return rv;
146 }
147
148 int
149 main(void) {
150 int ok = 1;
151 double white[6][3] = {
152 { 0.9505, 1.000, 1.0888 },
153 { 0.9505, 1.000, 1.0888 },
154 { 1.0985, 1.000, 0.3558 },
155 { 1.0985, 1.000, 0.3558 },
156 { 0.9505, 1.000, 1.0888 },
157 { 0.9642, 1.000, 0.8249 } /* D50 for inversion tests */
158 };
159 double La[6] = { 318.31, 31.83, 318.31, 31.83, 318.31, 150.0 };
160 double sample[5][3] = {
161 { 0.1901, 0.2000, 0.2178 },
162 { 0.5706, 0.4306, 0.3196 },
163 { 0.0353, 0.0656, 0.0214 },
164 { 0.1901, 0.2000, 0.2178 },
165 { 0.9505, 1.000, 1.0888 } /* Check white */
166 };
167
168 #ifdef CIECAM97S3_HK
169 #ifdef CIECAM97S3_SPLINE_E
170 double correct[5][4] = { /* Hue range is last two */
171 { 41.14, 0.06, 225.3, 251.9 },
172 { 69.04, 71.04, 19.4, 19.4 },
173 { 35.09, 87.15, 175.3, 175.3 },
174 { 55.64, 82.43, 252.5, 252.5 },
175 { 100.00000, 0.0, 0.0, 360.0 }
176 };
177 #else
178 double correct[5][4] = { /* Hue range is last two */
179 { 41.14, 0.06, 225.3, 251.9 },
180 { 69.056048, 71.20763, 19.4, 19.4 },
181 { 35.365009, 88.65238, 175.3, 175.3 },
182 { 55.266184, 80.55128, 252.5, 252.5 },
183 { 100.00000, 0.0, 0.0, 360.0 }
184 };
185 #endif
186 #else
187 double correct[5][4] = { /* Hue range is last two */
188 { 41.13, 0.05, 225.3, 251.9 },
189 { 64.14, 71.22, 19.4, 19.4 },
190 { 19.18, 88.64, 175.3, 175.3 },
191 { 39.11, 80.55, 252.5, 252.5 },
192 { 100.0, 0.00, 0.0, 360.0 } /* Check white */
193 };
194 #endif
195
196 double Jab[3];
197 double res[3];
198 cam97s3 *cam;
199 int c;
200
201 cam = new_cam97s3();
202
203 #ifdef SPOTTEST
204 for (c = 0; c < 5; c++) {
205
206 #ifdef DIAG
207 printf("Case %d:\n",c);
208 #endif /* DIAG */
209 cam->set_view(
210 cam,
211 vc_average, /* Enumerated Viewing Condition */
212 white[c], /* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) */
213 La[c], /* Adapting/Surround Luminance cd/m^2 */
214 0.20, /* Relative Luminance of Background to reference white */
215 0.0, /* Luminance of white in image - not used */
216 0.00, /* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
217 white[c], /* The Flare color coordinates (typically the Ambient color) */
218 USE_HK
219 );
220 #ifdef DIAG
221 printf("\n");
222 #endif /* DIAG */
223
224 {
225 double JCh[3], target[3];
226 cam->XYZ_to_cam(cam, Jab, sample[c]);
227
228 /* Convert to JCh for checking */
229 JCh[0] = Jab[0];
230
231 /* Compute hue angle */
232 JCh[2] = (180.0/3.14159265359) * atan2(Jab[2], Jab[1]);
233 JCh[2] = (JCh[2] < 0.0) ? JCh[2] + 360.0 : JCh[2];
234
235 /* Compute chroma value */
236 JCh[1] = sqrt(Jab[1] * Jab[1] + Jab[2] * Jab[2]);
237
238 target[0] = correct[c][0];
239 target[1] = correct[c][1];
240 if (JCh[2] >= correct[c][2] && JCh[2] <= correct[c][3]) {
241 target[2] = JCh[2];
242 } else {
243 if (fabs(JCh[2] - correct[c][2]) < fabs(JCh[2] - correct[c][3]))
244 target[2] = correct[c][2];
245 else
246 target[2] = correct[c][3];
247 }
248
249 cam->cam_to_XYZ(cam, res, Jab);
250
251 if (maxdiff(JCh, target) > 0.05) {
252 printf("Spottest: Excesive error in conversion to CAM %f\n",maxdiff(JCh, target));
253 ok = 0;
254 }
255 if (maxdiff(sample[c], res) > 1e-5) {
256 printf("Spottest: Excessive error in inversion %f\n",maxdiff(sample[c], res));
257 ok = 0;
258 }
259 #ifdef DIAG
260 printf("Jab is %f %f %f, Jch is %f %f %f\n",
261 Jab[0], Jab[1], Jab[2],
262 JCh[0], JCh[1], JCh[2]);
263
264 printf("Error to expected value = %f\n",maxdiff(JCh, target));
265
266 printf("XYZ is %f %f %f\n",res[0], res[1], res[2]);
267 printf("Inversion error = %f\n",maxdiff(sample[c], res));
268 #endif /* DIAG */
269 }
270 }
271 #endif /* SPOTTEST */
272
273 #ifdef INVTEST
274 for (c = 5; c < 6; c++) {
275 /* Get the range of Jab values */
276 double min[3] = { 1e6, 1e6, 1e6 };
277 double max[3] = { -1e6, -1e6, -1e6 };
278
279 cam->set_view(
280 cam,
281 vc_average, /* Enumerated Viewing Condition */
282 white[c], /* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) = D50 */
283 34.0, /* Adapting/Surround Luminance cd/m^2 */
284 0.20, /* Relative Luminance of Background to reference white */
285 0.0, /* Luminance of white in image - not used */
286 0.01, /* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
287 white[c], /* The Flare color coordinates (typically the Ambient color) */
288 USE_HK
289 );
290
291 {
292 int co0, co1, co2; /* (using co[3] triggers compiler bug) */
293 double merr = 0.0;
294 double xyz[3], Lab[3], Jab[3], checkxyz[3];
295
296
297 #ifdef NEVER
298 /* Test case */
299 Lab[0] = 0.0;
300 Lab[1] = -180.0;
301 Lab[2] = -180.0;
302 Lab2XYZ(xyz, Lab);
303 cam->XYZ_to_cam(cam, Jab, xyz);
304 cam->cam_to_XYZ(cam, checkxyz, Jab);
305
306 #else /* !NEVER */
307
308 /* itterate through Lab space */
309 for (co0 = 0; co0 < TRES; co0++) {
310 Lab[0] = co0/(TRES-1.0);
311 Lab[0] = Lab[0] * 100.0;
312 for (co1 = 0; co1 < TRES; co1++) {
313 Lab[1] = co1/(TRES-1.0);
314 Lab[1] = (Lab[1] - 0.5) * 256.0;
315 for (co2 = 0; co2 < TRES; co2++) {
316 double mxd;
317 int i;
318 Lab[2] = co2/(TRES-1.0);
319 Lab[2] = (Lab[2] - 0.5) * 256.0;
320
321 Lab2XYZ(xyz, Lab);
322 cam->XYZ_to_cam(cam, Jab, xyz);
323 for (i = 0; i < 3; i++) {
324 if (Jab[i] < min[i])
325 min[i] = Jab[i];
326 if (Jab[i] > max[i])
327 max[i] = Jab[i];
328 }
329 cam->cam_to_XYZ(cam, checkxyz, Jab);
330
331 /* Check the result */
332 mxd = maxxyzdiff(checkxyz, xyz);
333 if (_finite(merr) && (_isnan(mxd) || mxd > merr))
334 merr = mxd;
335 #ifdef DIAG
336 if (_isnan(mxd) || mxd > 0.1) {
337 printf("\n");
338 printf("Lab = %f %f %f\n",Lab[0], Lab[1], Lab[2]);
339 printf("%f %f %f -> %f %f %f -> %f %f %f [%f]\n",
340 xyz[0],xyz[1],xyz[2],Jab[0],Jab[1],Jab[2],
341 checkxyz[0],checkxyz[1],checkxyz[2],
342 maxxyzdiff(checkxyz, xyz));
343 }
344 #endif /* DIAG */
345 }
346 }
347 }
348 if (_isnan(merr) || merr > 0.1) {
349 printf("Excessive error in inversion check %f\n",merr);
350 ok = 0;
351 }
352 #ifdef DIAG
353 printf("Inversion check complete, peak error = %f\n",merr);
354 #endif /* !NEVER */
355
356 #endif /* DIAG */
357 printf("\n");
358 printf("Range of Jab values was:\n");
359 printf("J: %f -> %f\n", min[0], max[0]);
360 printf("a: %f -> %f\n", min[1], max[1]);
361 printf("b: %f -> %f\n", min[2], max[2]);
362 }
363 }
364 #endif /* INVTEST */
365
366 #ifdef INVTEST2
367 for (c = 5; c < 6; c++) {
368 cam->set_view(
369 cam,
370 vc_average, /* Enumerated Viewing Condition */
371 white[c], /* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) = D50 */
372 34.0, /* Adapting/Surround Luminance cd/m^2 */
373 0.20, /* Relative Luminance of Background to reference white */
374 0.0, /* Luminance of white in image - not used */
375 0.01, /* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
376 white[c], /* The Flare color coordinates (typically the Ambient color) */
377 USE_HK
378 );
379
380 {
381 int co0, co1, co2; /* (using co[3] triggers compiler bug) */
382 double merr = 0.0;
383 double xyz[3], Jab[3], checkJab[3];
384
385 #ifdef NEVER
386 /* Test case */
387 #ifdef NEVER
388 Jab[0] = 0.0;
389 Jab[1] = -180.0;
390 Jab[2] = -180.0;
391 cam->cam_to_XYZ(cam, xyz, Jab);
392 cam->XYZ_to_cam(cam, checkJab, xyz);
393 #else
394 xyz[0] = 555.365115;
395 xyz[1] = -518.091908;
396 xyz[2] = -1591.731701;
397 cam->XYZ_to_cam(cam, Jab, xyz);
398 cam->cam_to_XYZ(cam, xyz, Jab);
399 #endif
400
401 #else /* !NEVER */
402
403 /* itterate through Jab space */
404 for (co0 = 0; co0 < TRES; co0++) {
405 Jab[0] = co0/(TRES-1.0);
406 Jab[0] = 0.0 + Jab[0] * 100.0;
407 for (co1 = 0; co1 < TRES; co1++) {
408 Jab[1] = co1/(TRES-1.0);
409 Jab[1] = (Jab[1] - 0.5) * 256.0;
410 for (co2 = 0; co2 < TRES; co2++) {
411 double mxd;
412 Jab[2] = co2/(TRES-1.0);
413 Jab[2] = (Jab[2] - 0.5) * 256.0;
414
415 cam->cam_to_XYZ(cam, xyz, Jab);
416 cam->XYZ_to_cam(cam, checkJab, xyz);
417
418 /* Check the result */
419 mxd = maxdiff(checkJab, Jab);
420 if (_finite(merr) && (_isnan(mxd) || mxd > merr))
421 merr = mxd;
422 #ifdef DIAG
423 if (_isnan(mxd) || mxd > 0.1) {
424 printf("\n");
425 printf("Jab = %f %f %f\n",Jab[0], Jab[1], Jab[2]);
426 printf("%f %f %f -> %f %f %f -> %f %f %f [%f]\n",
427 Jab[0],Jab[1],Jab[2], xyz[0],xyz[1],xyz[2],
428 checkJab[0],checkJab[1],checkJab[2],
429 maxdiff(checkJab, Jab));
430 }
431 #endif /* DIAG */
432 }
433 }
434 }
435 if (_isnan(merr) || merr > 1.0) {
436 printf("Excessive error in inversion check 2 %f\n",merr);
437 ok = 0;
438 }
439 #ifdef DIAG
440 printf("Inversion check 2 complete, peak error = %f\n",merr);
441 #endif /* DIAG */
442 #endif /* !NEVER */
443 }
444 }
445 #endif /* INVTEST2 */
446
447 printf("\n");
448 if (ok == 0) {
449 printf("Cam testing FAILED\n");
450 } else {
451 printf("Cam testing OK\n");
452 }
453
454 return 0;
455 }
456