Debian Codegraph @

xicc/cam02plot.c

raw symbols SCIP
1
2 /*
3 * International Color Consortium color transform expanded support
4 *
5 * Author: Graeme W. Gill
6 * Date: 11/11/2004
7 * Version: 1.00
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 CIECAM02 continuity.
18 * This test creates file "cam02plot.tif" containing values that
19 * have been converted to and back from CIECAM02 space.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include "copyright.h"
26 #include "aconfig.h"
27 #include "numlib.h"
28 #include "icc.h"
29 #include "xcam.h"
30 #include "cam02.h"
31 #include "tiffio.h"
32 #include "cam02ref.h"
33
34 #define DEFRES 100 /* Default cube resolution */
35
36
37 #define NORMAL_XYZ2RGB
38 #undef NIEVE_XYZ2RGB
39 #undef COMPLEX_XYZ2RGB
40
41 #define SCALE 1.0 /* Compress RGB by scale towards grey */
42
43 #ifndef _isnan
44 #define _isnan(x) ((x) != (x))
45 #define _finite(x) ((x) == 0.0 || (x) * 1.0000001 != (x))
46 #endif
47
48 static void
49 Lab2XYZ(double *out, double *in) {
50 double L = in[0], a = in[1], b = in[2];
51 double x,y,z,fx,fy,fz;
52
53 if (L > 8.0) {
54 fy = (L + 16.0)/116.0;
55 y = pow(fy,3.0);
56 } else {
57 y = L/903.2963058;
58 fy = 7.787036979 * y + 16.0/116.0;
59 }
60
61 fx = a/500.0 + fy;
62 if (fx > 24.0/116.0)
63 x = pow(fx,3.0);
64 else
65 x = (fx - 16.0/116.0)/7.787036979;
66
67 fz = fy - b/200.0;
68 if (fz > 24.0/116.0)
69 z = pow(fz,3.0);
70 else
71 z = (fz - 16.0/116.0)/7.787036979;
72
73 out[0] = x * 0.9642;
74 out[1] = y * 1.0000;
75 out[2] = z * 0.8249;
76 }
77
78 /* CIE XYZ to perceptual Lab */
79 static void
80 XYZ2Lab(double *out, double *in) {
81 double X = in[0], Y = in[1], Z = in[2];
82 double x,y,z,fx,fy,fz;
83
84 x = X/0.9642;
85 y = Y/1.0000;
86 z = Z/0.8249;
87
88 if (x > 0.008856451586)
89 fx = pow(x,1.0/3.0);
90 else
91 fx = 7.787036979 * x + 16.0/116.0;
92
93 if (y > 0.008856451586)
94 fy = pow(y,1.0/3.0);
95 else
96 fy = 7.787036979 * y + 16.0/116.0;
97
98 if (z > 0.008856451586)
99 fz = pow(z,1.0/3.0);
100 else
101 fz = 7.787036979 * z + 16.0/116.0;
102
103 out[0] = 116.0 * fy - 16.0;
104 out[1] = 500.0 * (fx - fy);
105 out[2] = 200.0 * (fy - fz);
106 }
107
108 #ifdef NORMAL_XYZ2RGB
109
110 static double sign(double x) {
111 if (x < 0.0)
112 return -1.0;
113 else
114 return 1.0;
115 }
116
117 /* Version with reasonable clipping. Converts to Lab, */
118 /* clips the Lab an then converts to sRGB */
119 /* Convert from XYZ to sRGB */
120 void XYZ2sRGB(double *out, double *in, int trace) {
121 double lab[3];
122 double tmp[3];
123 double scale = SCALE; /* Scale RGB towards grey */
124 double mat[3][3] =
125 {
126 { 3.2410, -1.5374, -0.4986 },
127 { -0.9692, 1.8760, 0.0416 },
128 { 0.0556, -0.2040, 1.0570 }
129 };
130 int i;
131
132 // ~~999
133 trace = 0;
134
135 /* Copy from input */
136 for(i = 0; i < 3; i++)
137 tmp[i] = in[i];
138
139 if (trace) printf("XYZ to RGB\n");
140 if (trace) printf("input XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
141 /* Clip to reasonable range */
142 // if (tmp[1] <= 0.0)
143 // tmp[0] = tmp[2] = 0.0;
144 XYZ2Lab(lab, tmp);
145 if (trace) printf("Lab %f %f %f\n",tmp[0],tmp[1],tmp[2]);
146 icmClipLab(lab, lab);
147 if (trace) printf("Clipped Lab %f %f %f\n",tmp[0],tmp[1],tmp[2]);
148
149 /* We're going to assume that L == 0 is always black */
150 if (lab[0] <= 0.0) {
151 lab[1] = lab[2] = 0.0;
152 }
153 if (trace) printf("Clipped Lab2 %f %f %f\n",tmp[0],tmp[1],tmp[2]);
154 Lab2XYZ(tmp, lab);
155 if (trace) printf("XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
156 icmClipXYZ(tmp, tmp);
157 if (trace) printf("clipped XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
158
159 /* Now convert to sRGB assuming D50 (??) white point */
160 icmMulBy3x3(tmp, mat, tmp);
161
162 /* Scale */
163 for(i = 0; i < 3; i++) {
164 tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
165 }
166
167 if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
168
169 /* Apply gamma */
170 for(i = 0; i < 3; i++) {
171 if (tmp[i] < 0.00304) {
172 tmp[i] = tmp[i] * 12.92;
173 } else {
174 tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
175 }
176 }
177
178 if (trace) printf("gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
179
180 #ifndef NEVER
181 /* Clip to nearest */
182 for(i = 0; i < 3; i++) {
183 if (tmp[i] < 0.0)
184 tmp[i] = 0.0;
185 else if (tmp[i] > 1.0)
186 tmp[i] = 1.0;
187 }
188 #else
189 /* Clip towards center */
190 {
191 double biggest = -100.0;
192 for(i = 0; i < 3; i++) {
193 tmp[i] -= 0.5;
194 if (fabs(tmp[i]) > biggest)
195 biggest = fabs(tmp[i]);
196 }
197 for(i = 0; i < 3; i++) {
198 if (biggest > 0.5)
199 tmp[i] *= 0.5/biggest;
200 tmp[i] += 0.5;
201 }
202 }
203 #endif
204 if (trace) printf("output clipped RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
205
206 /* Copy to output */
207 for(i = 0; i < 3; i++)
208 out[i] = tmp[i];
209
210 }
211
212 #endif /* NORMAL_XYZ2RGB */
213
214 #ifdef NIEVE_XYZ2RGB
215
216 /* Nieve version with RGB clipping */
217 /* Convert from XYZ to sRGB */
218 void XYZ2sRGB(double *out, double *in, int trace) {
219 double tmp[3];
220 double scale = SCALE; /* Scale RGB towards grey */
221 double mat[3][3] =
222 {
223 { 3.2410, -1.5374, -0.4986 },
224 { -0.9692, 1.8760, 0.0416 },
225 { 0.0556, -0.2040, 1.0570 }
226 };
227 int i;
228
229 if (trace) printf("XYZ to RGB\n");
230 if (trace) printf("input XYZ %f %f %f\n",in[0],in[1],in[2]);
231
232 /* Now convert to sRGB assuming D50 (??) white point */
233 icmMulBy3x3(tmp, mat, in);
234
235 /* Scale */
236 for(i = 0; i < 3; i++) {
237 tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
238 }
239
240 if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
241
242 /* Clip to nearest */
243 for(i = 0; i < 3; i++) {
244 if (tmp[i] < 0.0)
245 tmp[i] = 0.0;
246 else if (tmp[i] > 1.0)
247 tmp[i] = 1.0;
248 }
249
250 if (trace) printf("clipped RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
251
252 /* Apply gamma */
253 for(i = 0; i < 3; i++) {
254 if (tmp[i] < 0.00304) {
255 tmp[i] = tmp[i] * 12.92;
256 } else {
257 tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
258 }
259 }
260
261 if (trace) printf("output gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
262
263 /* Copy to output */
264 for(i = 0; i < 3; i++)
265 out[i] = tmp[i];
266 }
267
268 #endif /* NIEVE_XYZ2RGB */
269
270 #ifdef COMPLEX_XYZ2RGB
271
272 /* Complex version using powell to ensure good clipping */
273 /* Callback context */
274 typedef struct {
275 double scale;
276 double lab[3];
277 double imat[3][3];
278 } ctx;
279
280 double clipf(void *fdata, double tp[]) {
281 ctx *x = (ctx *)fdata;
282 double lab[3], tmp[3], tt;
283 int k;
284 double rv = 0.0;
285
286 //printf("\n");
287 //printf("~1 clipf got %f %f %f\n",tp[0],tp[1],tp[2]);
288
289 /* Penalize for being out of gamut */
290 for (k = 0; k < 3; k++) {
291 if (tp[k] < 0.0) {
292 tt = 100000.0 * (0.0 - tp[k]);
293 rv += tt * tt;
294 } else if (tp[k] > 1.0) {
295 tt = 100000.0 * (tp[k] - 1.0);
296 rv += tt * tt;
297 }
298 }
299 //printf("~1 rv out of gamut = %f\n",rv);
300
301 /* Unscale it */
302 for(k = 0; k < 3; k++)
303 tmp[k] = ((tp[k] - 0.5) / x->scale) + 0.5;
304 //printf("~1 unscale %f %f %f\n",tmp[0],tmp[1],tmp[2]);
305
306 icmMulBy3x3(tmp, x->imat, tmp); /* To XYZ */
307 //printf("~1 xyz %f %f %f\n",tmp[0],tmp[1],tmp[2]);
308 XYZ2Lab(lab, tmp); /* To Lab */
309 //printf("~1 lab = %f %f %f\n",lab[0],lab[1],lab[2]);
310 //printf("~1 target = %f %f %f\n",x->lab[0],x->lab[1],x->lab[2]);
311
312 /* Compute an error to the goal */
313 tt = 10000.0 * (lab[0] - x->lab[0]);
314 rv += tt * tt;
315
316 tt = 1.0 * (lab[1] - x->lab[1]);
317 rv += tt * tt;
318 tt = 1.0 * (lab[2] - x->lab[2]);
319 rv += tt * tt;
320
321 //printf("~1 rv = %f\n",rv);
322
323 return rv;
324 }
325
326 /* Convert from XYZ to sRGB */
327 void XYZ2sRGB(double *out, double *in, int trace) {
328 double lab[3], tmp[3];
329 double scale = SCALE; /* Scale RGB towards grey */
330 double mat[3][3] =
331 {
332 { 3.2410, -1.5374, -0.4986 },
333 { -0.9692, 1.8760, 0.0416 },
334 { 0.0556, -0.2040, 1.0570 }
335 };
336 int i;
337
338 /* Copy from input */
339 for(i = 0; i < 3; i++)
340 tmp[i] = in[i];
341
342 if (trace) printf("XYZ to RGB\n");
343
344 if (trace) printf("input XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
345
346 XYZ2Lab(lab, tmp);
347
348 if (trace) printf("Lab %f %f %f\n",lab[0],lab[1],lab[2]);
349
350 icmClipLab(lab, lab);
351
352 if (trace) printf("Clipped Lab %f %f %f\n",lab[0],lab[1],lab[2]);
353
354 /* Now convert to sRGB assuming D50 (??) white point */
355 Lab2XYZ(tmp, lab);
356 icmMulBy3x3(tmp, mat, tmp);
357
358 /* Scale */
359 for(i = 0; i < 3; i++) {
360 tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
361 }
362
363 if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
364
365 /* See if it need clipping */
366 for(i = 0; i < 3; i++) {
367 if (tmp[i] < 0.0 || tmp[i] > 1.0)
368 break;
369 }
370 /* It needs more clipping */
371 if (i < 3) {
372 ctx x;
373 double ss[3] = { 0.1, 0.1, 0.1 };
374 x.lab[0] = lab[0];
375 x.lab[1] = lab[1];
376 x.lab[2] = lab[2];
377 x.scale = scale;
378 icmInverse3x3(x.imat, mat);
379
380 if (powell(NULL, 3, tmp, ss, 1e-6, 1000, clipf, (void *)&x) < 0.0)
381 error("RGB clip failed");
382 }
383 if (trace) printf("RGB clip %f %f %f\n",tmp[0],tmp[1],tmp[2]);
384
385 /* Clip to nearest */
386 for(i = 0; i < 3; i++) {
387 if (tmp[i] < 0.0)
388 tmp[i] = 0.0;
389 else if (tmp[i] > 1.0)
390 tmp[i] = 1.0;
391 }
392 if (trace) printf("clip to nearest RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
393
394 /* Apply gamma */
395 for(i = 0; i < 3; i++) {
396 if (tmp[i] < 0.00304) {
397 tmp[i] = tmp[i] * 12.92;
398 } else {
399 tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
400 }
401 }
402 if (trace) printf("gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);
403
404 /* Copy to output */
405 for(i = 0; i < 3; i++)
406 out[i] = tmp[i];
407
408 }
409 #endif /* COMPLEX_XYZ2RGB */
410
411 void usage(char *diag) {
412 fprintf(stderr,"Create a 3D slice plot of XYZ/Lab <-> Jab, Version %s\n",ARGYLL_VERSION_STR);
413 fprintf(stderr,"Author: Graeme W. Gill, licensed under the AGPL Version 3\n");
414 if (diag != NULL)
415 fprintf(stderr,"Diagnostic '%s'\n",diag);
416 fprintf(stderr,"usage: cam02plot [-options] x y\n");
417 fprintf(stderr,"'cam02plot -l -o' shows problem best\n");
418 fprintf(stderr," -v level Verbosity level 0 - 2 (default = 1)\n");
419 fprintf(stderr," -n Don't use Helmholtz-Kohlraush flag\n");
420 fprintf(stderr," -f Use the reference CIECAM transform\n");
421 fprintf(stderr," -r res Resolution (default %d)\n",DEFRES);
422 fprintf(stderr," -l Lab source cube (default XYZ source cube)\n");
423 fprintf(stderr," -i XYZ/Lab -> Jab (default XYZ/Lab -> Lab)\n");
424 fprintf(stderr," -o Jab -> XYZ -> RGB (defult Lab -> XYZ -> RGB\n");
425 fprintf(stderr," -s XYZ/Lab -> ab scale as RGB\n");
426 fprintf(stderr," -x Cross section plots\n");
427 fprintf(stderr," x y Return input color for this coordinate\n");
428 exit(1);
429 }
430
431 int
432 main(int argc, char *argv[]) {
433 int fa,nfa; /* argument we're looking at */
434 char *tiffname = "cam02plot.tif";
435 TIFF *wh = NULL;
436 uint16 resunits = RESUNIT_INCH;
437 float resx = 75.0, resy = 75.0;
438 tdata_t *obuf;
439 unsigned char *ob;
440 int verb = 0;
441 int transl = 0; /* Translate x,y into input color */
442 int tx = 0, ty = 0; /* coordinate to translate */
443 int use_hk = 1; /* Use Helmholtz-Kohlraush flag */
444 int use_ref = 0; /* Use reference transform rather than working code */
445 int lab_plot = 0; /* Lab <-> Jab plot, else XYZ <-> Jab */
446 int to_jab = 0; /* Convert to Jab, else convert to Lab */
447 int from_jab = 0; /* Convert from Jab, else convert from Lab */
448 int to_ss = 0; /* Convert from XYZ/Lab to abs scale ss */
449 int xsect = 0; /* Cross section plots */
450
451 double white[9][3] = {
452 { 0.964242, 1.000000, 0.825124 }, /* 0: D50 */
453 { 1.098494, 1.000000, 0.355908 }, /* 1: A */
454 { 0.908731, 1.000000, 0.987233 }, /* 2: F5 */
455 { 1.128981, 1.000000, 0.305862 }, /* 3: D25 */
456 { 0.950471, 1.000000, 1.088828 }, /* 4: D65 */
457 { 0.949662, 1.000000, 1.160438 }, /* 5: D80 */
458 { 0.949662, 1.000000, 1.160438 }, /* 6: D80 */
459 { 0.951297, 1.000000, 1.338196 }, /* 7: D85 */
460 { 0.952480, 1.000000, 1.386693 } /* 8: D90 */
461 };
462
463 #ifdef NEVER
464 double La[6] = { 10.0, 31.83, 100.0, 318.31, 1000.83, 3183.1 };
465
466 ViewingCondition Vc[4] = {
467 vc_average,
468 vc_dark,
469 vc_dim,
470 vc_cut_sheet
471 };
472 #endif /* NEVER */
473
474 int i, j, k, m;
475 double xyz[3], Jab[3], rgb[3];
476 cam02 *cam1, *cam2;
477
478 int res = DEFRES; /* Resolution of scan through space */
479 int ares; /* Array of 2d sub-rasters */
480 int w, h; /* Width and height of raster */
481 int x, y;
482 int sp = 5; /* Spacing beween resxres sub-images */
483
484 error_program = argv[0];
485
486 /* Process the arguments */
487 for(fa = 1;fa < argc;fa++) {
488 nfa = fa; /* skip to nfa if next argument is used */
489 if (argv[fa][0] == '-') { /* Look for any flags */
490 char *na = NULL; /* next argument after flag, null if none */
491
492 if (argv[fa][2] != '\000')
493 na = &argv[fa][2]; /* next is directly after flag */
494 else {
495 if ((fa+1) < argc) {
496 if (argv[fa+1][0] != '-') {
497 nfa = fa + 1;
498 na = argv[nfa]; /* next is seperate non-flag argument */
499 }
500 }
501 }
502
503 if (argv[fa][1] == '?')
504 usage("Requested usage");
505
506 /* Verbosity */
507 else if (argv[fa][1] == 'v' || argv[fa][1] == 'V') {
508 fa = nfa;
509 if (na == NULL)
510 verb = 2;
511 else {
512 if (na[0] == '0')
513 verb = 0;
514 else if (na[0] == '1')
515 verb = 1;
516 else if (na[0] == '2')
517 verb = 2;
518 else
519 usage("Illegal verbosity level");
520 }
521 }
522
523 else if (argv[fa][1] == 'r' || argv[fa][1] == 'R') {
524 fa = nfa;
525 if (na == NULL)
526 usage("Need resolution after -r");
527 res = atoi(na);
528 if (res < 2 || res > 1000)
529 usage("Resolution is out of range");
530 }
531 else if (argv[fa][1] == 'n' || argv[fa][1] == 'N') {
532 use_hk = 0;
533 }
534 else if (argv[fa][1] == 'f' || argv[fa][1] == 'F') {
535 use_ref = 1;
536 }
537 else if (argv[fa][1] == 'l' || argv[fa][1] == 'L') {
538 lab_plot = 1;
539 }
540 else if (argv[fa][1] == 'i' || argv[fa][1] == 'I') {
541 to_jab = 1;
542 }
543 else if (argv[fa][1] == 'o' || argv[fa][1] == 'O') {
544 from_jab = 1;
545 }
546 else if (argv[fa][1] == 's' || argv[fa][1] == 'S') {
547 to_ss = 1;
548 }
549 else if (argv[fa][1] == 'x' || argv[fa][1] == 'X') {
550 xsect = 1;
551 }
552 else
553 usage("Unknown flag");
554 } else
555 break;
556 }
557
558 if ((fa+1) < argc) {
559 tx = atoi(argv[fa]);
560 ty = atoi(argv[fa+1]);
561 transl = 1;
562 }
563
564
565 /* Setup cam to convert to Jab */
566 if (use_ref)
567 cam1 = new_cam02ref();
568 else
569 cam1 = new_cam02();
570 cam1->set_view(
571 cam1,
572 vc_average, /* Enumerated Viewing Condition */
573 white[4], /* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) */
574 1000.0, /* Adapting/Surround Luminance cd/m^2 */
575 0.20, /* Relative Luminance of Background to reference white */
576 0.0, /* Luminance of white in image - not used */
577 0.00, /* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
578 0.00, /* Glare as a fraction of the ambient (Y range 0.0 .. 1.0) */
579 white[4], /* The Glare color coordinates (typically the Ambient color) */
580 use_hk, /* use Helmholtz-Kohlraush flag */
581 1.0, /* HK scaling factor */
582 0.0, /* No mid-tone hack */
583 NULL
584 );
585
586 /* Setup cam to convert from Jab */
587 if (use_ref)
588 cam2 = new_cam02ref();
589 else
590 cam2 = new_cam02();
591 cam2->set_view(
592 cam2,
593 vc_average, /* Enumerated Viewing Condition */
594 white[4], /* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) */
595 1000.0, /* Adapting/Surround Luminance cd/m^2 */
596 0.20, /* Relative Luminance of Background to reference white */
597 0.0, /* Luminance of white in image - not used */
598 0.00, /* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
599 0.00, /* Glare as a fraction of the ambient (Y range 0.0 .. 1.0) */
600 white[4], /* The Glare color coordinates (typically the Ambient color) */
601 use_hk, /* use Helmholtz-Kohlraush flag */
602 1.0, /* HK scaling factor */
603 0.0, /* No mid-tone hack */
604 NULL
605 );
606
607 /* Figure out the size of the raster */
608 ares = (int)ceil(sqrt((double)res));
609 if (verb)
610 printf("For res %d, ares = %d\n",res,ares);
611
612 w = ares * res + sp * (ares+1);
613 h = ares * res + sp * (ares+1);
614
615 if (to_ss) {
616 cam1->retss = 1;
617 }
618
619 if (transl) {
620 int ix,iy,iz;
621 double dx,dy,dz;
622 double tmp[3], lab[3];
623
624 cam1->trace = 1;
625 cam2->trace = 1;
626
627 //printf("~1 translate %d %d\n",tx,ty);
628 tx -= sp; /* Border at base */
629 ty -= sp;
630
631 //printf("~1 offset %d %d\n",tx,ty);
632 ix = tx % (res + sp);
633 iy = ty % (res + sp);
634 iz = (ty / (res + sp)) * ares + (tx / (res + sp));
635
636 //printf("~1 x y z = %d %d %d\n", ix,iy,iz);
637
638 dx = ix/(res-1.0);
639 dy = iy/(res-1.0);
640 dz = iz/((ares * ares)-1.0);
641
642 //printf("~1 X Y Z = %f %f %f\n", dx,dy,dz);
643
644 /* Source range is extended L*a*b* type */
645 if (lab_plot) {
646 if (xsect) {
647 lab[0] = 180.0 * dz - 40.0;
648 lab[1] = 300.0 * dy - 150.0;
649 lab[2] = 300.0 * dx - 150.0;
650 } else {
651 lab[0] = 180.0 * dx - 40.0;
652 lab[1] = 300.0 * dy - 150.0;
653 lab[2] = 300.0 * dz - 150.0;
654 }
655 //printf("~1 Lab = %f %f %f\n", lab[0],lab[1],lab[2]);
656 Lab2XYZ(tmp, lab);
657 printf("======================================================\n");
658 printf("%d,%d -> Lab %f %f %f\n", tx+sp, ty+sp, lab[0],lab[1],lab[2]);
659 printf(" == XYZ %f %f %f\n", tmp[0],tmp[1],tmp[2]);
660
661 /* Else source range is extended XYZ type */
662 } else {
663 if (xsect) {
664 tmp[0] = 1.4 * dz - 0.2;
665 tmp[1] = 1.4 * dy - 0.2;
666 tmp[2] = 1.6 * dx - 0.2;
667 } else {
668 tmp[0] = 1.4 * dx - 0.2;
669 tmp[1] = 1.4 * dy - 0.2;
670 tmp[2] = 1.6 * dz - 0.2;
671 }
672 printf("%d,%d -> Input color XYZ %f %f %f\n", tx, ty, tmp[0],tmp[1],tmp[2]);
673 }
674
675 if (to_ss) {
676 cam1->XYZ_to_cam(cam1, Jab, tmp);
677 printf(" XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);
678
679 /* Convert XYZ through cam and back, then to RGB */
680 } else if (to_jab) {
681 cam1->XYZ_to_cam(cam1, Jab, tmp);
682 if (to_ss) {
683 printf(" XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);
684 } else {
685 printf(" XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);
686 }
687
688 /* Else convert XYZ to L*a*b* */
689 } else {
690 XYZ2Lab(Jab, tmp);
691 printf(" XYZ -> Lab %f %f %f\n", Jab[0],Jab[1],Jab[2]);
692 }
693
694 /* Convert from Jab back to XYZ */
695 if (from_jab) {
696 cam2->cam_to_XYZ(cam2, rgb, Jab);
697 printf(" Jab -> XYZ %f %f %f\n", rgb[0],rgb[1],rgb[2]);
698
699 /* Else convert from Lab back to XYZ */
700 } else {
701 Lab2XYZ(rgb, Jab);
702 printf(" Lab -> XYZ %f %f %f\n", rgb[0],rgb[1],rgb[2]);
703 }
704
705 XYZ2Lab(tmp, rgb);
706 printf(" == Lab %f %f %f\n", tmp[0],tmp[1],tmp[2]);
707
708 /* Interpret XYZ as RGB color */
709 XYZ2sRGB(rgb, rgb, 1);
710 printf(" XYZ -> RGB %f %f %f\n", rgb[0],rgb[1],rgb[2]);
711
712 cam1->trace = 0;
713 cam2->trace = 0;
714
715 printf("\n");
716 exit(0);
717 }
718
719 if (verb)
720 printf("Raster width = %d, height = %d\n",w,h);
721
722 /* Setup the tiff file */
723 if ((wh = TIFFOpen(tiffname, "w")) == NULL)
724 error("Can\'t create TIFF file '%s'!",tiffname);
725
726 TIFFSetField(wh, TIFFTAG_IMAGEWIDTH, w);
727 TIFFSetField(wh, TIFFTAG_IMAGELENGTH, h);
728 TIFFSetField(wh, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
729 TIFFSetField(wh, TIFFTAG_SAMPLESPERPIXEL, 3);
730 TIFFSetField(wh, TIFFTAG_BITSPERSAMPLE, 8);
731 TIFFSetField(wh, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
732 TIFFSetField(wh, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
733 TIFFSetField(wh, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
734 TIFFSetField(wh, TIFFTAG_RESOLUTIONUNIT, resunits);
735 TIFFSetField(wh, TIFFTAG_XRESOLUTION, resx);
736 TIFFSetField(wh, TIFFTAG_YRESOLUTION, resy);
737 TIFFSetField(wh, TIFFTAG_IMAGEDESCRIPTION, "cam02plot");
738
739 obuf = _TIFFmalloc(TIFFScanlineSize(wh));
740 ob = (unsigned char *)obuf;
741
742 y = 0;
743
744 /* Fill sp lines with black */
745 for (x = 0, m = 0; m < w; m++) {
746 ob[x] = ob[x+1] = ob[x+2] = 0;
747 x += 3;
748 }
749 for (j = 0; j < sp; j++, y++) {
750 TIFFWriteScanline(wh, ob, y, 0);
751 }
752
753 for (i = 0; i < ares; i++) { /* Vertical blocks (rows) */
754 for (j = 0; j < res; j++, y++) { /* Vertical in block (y = Y/a*) */
755 xyz[1] = j/(res-1.0);
756 x = 0;
757
758 /* Fill sp pixels with black */
759 for (m = 0; m < sp; m++) {
760 ob[x] = ob[x+1] = ob[x+2] = 0;
761 x += 3;
762 }
763 for (k = 0; k < ares; k++) { /* Horizontal blocks (columns) */
764 int zv = i * ares + k;
765 if (zv >= res) {
766 /* Fill res pixels with black */
767 for (m = 0; m < res; m++) {
768 ob[x] = ob[x+1] = ob[x+2] = 0;
769 x += 3;
770 }
771 } else {
772 xyz[2] = zv/(res-1.0); /* z = block = Z/b* */
773 for (m = 0; m < res; m++) { /* Horizontal in block (x = X/L*) */
774 double tmp[3], xyz2[3];
775 xyz[0] = m/(res-1.0);
776
777 if (lab_plot) {
778 if (xsect) {
779 tmp[0] = 180.0 * xyz[2] - 40.0;
780 tmp[1] = 300.0 * xyz[1] - 150.0;
781 tmp[2] = 300.0 * xyz[0] - 150.0;
782 } else {
783 tmp[0] = 180.0 * xyz[0] - 40.0;
784 tmp[1] = 300.0 * xyz[1] - 150.0;
785 tmp[2] = 300.0 * xyz[2] - 150.0;
786 }
787 Lab2XYZ(tmp, tmp);
788
789 } else {
790 if (xsect) {
791 tmp[0] = 1.4 * xyz[2] - 0.2;
792 tmp[1] = 1.4 * xyz[1] - 0.2;
793 tmp[2] = 1.6 * xyz[0] - 0.2;
794 } else {
795 tmp[0] = 1.4 * xyz[0] - 0.2;
796 tmp[1] = 1.4 * xyz[1] - 0.2;
797 tmp[2] = 1.6 * xyz[2] - 0.2;
798 }
799 }
800
801 /* Convert XYZ through cam and back, then to RGB */
802 if (to_jab || to_ss)
803 cam1->XYZ_to_cam(cam1, Jab, tmp);
804 else
805 XYZ2Lab(Jab, tmp);
806
807 if (to_ss) {
808 rgb[0] = Jab[0];
809 rgb[1] = Jab[1];
810 rgb[2] = Jab[2];
811 } else {
812 if (from_jab)
813 cam2->cam_to_XYZ(cam2, xyz2, Jab);
814 else
815 Lab2XYZ(xyz2, Jab);
816
817 XYZ2sRGB(rgb, xyz2, 0);
818 }
819
820 /* Fill with pixel value */
821 ob[x+0] = (int)(rgb[0] * 255.0 + 0.5);
822 ob[x+1] = (int)(rgb[1] * 255.0 + 0.5);
823 ob[x+2] = (int)(rgb[2] * 255.0 + 0.5);
824 x += 3;
825 }
826 }
827 /* Fill sp pixels with black */
828 for (m = 0; m < sp; m++) {
829 ob[x] = ob[x+1] = ob[x+2] = 0;
830 x += 3;
831 }
832 }
833 TIFFWriteScanline(wh, ob, y, 0);
834 }
835 /* Fill sp lines with black */
836 for (x = m = 0; m < w; m++) {
837 ob[x] = ob[x+1] = ob[x+2] = 0;
838 x += 3;
839 }
840 for (j = 0; j < sp; j++, y++) {
841 TIFFWriteScanline(wh, ob, y, 0);
842 }
843 }
844
845 cam1->del(cam1);
846 cam2->del(cam2);
847
848 /* Write TIFF file */
849 TIFFClose(wh);
850
851 return 0;
852 }
853