summaryrefslogtreecommitdiff
path: root/xicc/cam02plot.c
blob: f397e0930df497ea0e1871e9872bb216909e1dd6 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

/* 
 * International Color Consortium color transform expanded support
 *
 * Author:  Graeme W. Gill
 * Date:    11/11/2004
 * Version: 1.00
 *
 * Copyright 2000-2004 Graeme W. Gill
 * All rights reserved.
 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 *
 */

/*
 * This is some test code to test the CIECAM02 continuity. 
 * This test creates file "cam02plot.tif" containing values that
 * have been converted to and back from CIECAM02 space. 
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "copyright.h"
#include "aconfig.h"
#include "numlib.h"
#include "icc.h"
#include "xcam.h"
#include "cam02.h"
#include "tiffio.h"
#include "cam02ref.h"

#define DEFRES 100		/* Default cube resolution */


#define NORMAL_XYZ2RGB
#undef NIEVE_XYZ2RGB
#undef COMPLEX_XYZ2RGB

#define SCALE 1.0		/* Compress RGB by scale towards grey */

#ifndef _isnan
#define _isnan(x) ((x) != (x))
#define _finite(x) ((x) == 0.0 || (x) * 1.0000001 != (x))
#endif

static void
Lab2XYZ(double *out, double *in) {
	double L = in[0], a = in[1], b = in[2];
	double x,y,z,fx,fy,fz;

	if (L > 8.0) {
		fy = (L + 16.0)/116.0;
		y = pow(fy,3.0);
	} else {
		y = L/903.2963058;
		fy = 7.787036979 * y + 16.0/116.0;
	}

	fx = a/500.0 + fy;
	if (fx > 24.0/116.0)
		x = pow(fx,3.0);
	else
		x = (fx - 16.0/116.0)/7.787036979;

	fz = fy - b/200.0;
	if (fz > 24.0/116.0)
		z = pow(fz,3.0);
	else
		z = (fz - 16.0/116.0)/7.787036979;

	out[0] = x * 0.9642;
	out[1] = y * 1.0000;
	out[2] = z * 0.8249;
}

/* CIE XYZ to perceptual Lab */
static void
XYZ2Lab(double *out, double *in) {
	double X = in[0], Y = in[1], Z = in[2];
	double x,y,z,fx,fy,fz;

	x = X/0.9642;
	y = Y/1.0000;
	z = Z/0.8249;

	if (x > 0.008856451586)
		fx = pow(x,1.0/3.0);
	else
		fx = 7.787036979 * x + 16.0/116.0;

	if (y > 0.008856451586)
		fy = pow(y,1.0/3.0);
	else
		fy = 7.787036979 * y + 16.0/116.0;

	if (z > 0.008856451586)
		fz = pow(z,1.0/3.0);
	else
		fz = 7.787036979 * z + 16.0/116.0;

	out[0] = 116.0 * fy - 16.0;
	out[1] = 500.0 * (fx - fy);
	out[2] = 200.0 * (fy - fz);
}

#ifdef NORMAL_XYZ2RGB

static double sign(double x) {
	if (x < 0.0)
		return -1.0;
	else
		return 1.0;
}

/* Version with reasonable clipping. Converts to Lab, */
/* clips the Lab an then converts to sRGB */
/* Convert from XYZ to sRGB */
void XYZ2sRGB(double *out, double *in, int trace) {
	double lab[3];
	double tmp[3];
	double scale = SCALE;		/* Scale RGB towards grey */
	double mat[3][3] = 
		{
			{  3.2410, -1.5374, -0.4986 },
			{ -0.9692,  1.8760,  0.0416 },
			{  0.0556, -0.2040,  1.0570 }
		};
	int i;

// ~~999
	trace = 0;

	/* Copy from input */
	for(i = 0; i < 3; i++)
		tmp[i] = in[i];

	if (trace) printf("XYZ to RGB\n");
	if (trace) printf("input XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
	/* Clip to reasonable range */
//	if (tmp[1] <= 0.0)
//		tmp[0] = tmp[2] = 0.0;
	XYZ2Lab(lab, tmp);
	if (trace) printf("Lab %f %f %f\n",tmp[0],tmp[1],tmp[2]);
	icmClipLab(lab, lab);
	if (trace) printf("Clipped Lab %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* We're going to assume that L == 0 is always black */
	if (lab[0] <= 0.0) {
		lab[1] = lab[2] = 0.0;
	}
	if (trace) printf("Clipped Lab2 %f %f %f\n",tmp[0],tmp[1],tmp[2]);
	Lab2XYZ(tmp, lab);
	if (trace) printf("XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);
	icmClipXYZ(tmp, tmp);
	if (trace) printf("clipped XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Now convert to sRGB assuming D50 (??) white point */
	icmMulBy3x3(tmp, mat, tmp);

	/* Scale */
	for(i = 0; i < 3; i++) {
		tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
	}

	if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Apply gamma */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.00304) {
			tmp[i] = tmp[i] * 12.92;
		} else {
			tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
		}
	}

	if (trace) printf("gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

#ifndef NEVER
	/* Clip to nearest */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.0)
			tmp[i] = 0.0;
		else if (tmp[i] > 1.0)
			tmp[i] = 1.0;
	}
#else
	/* Clip towards center */
	{
		double biggest = -100.0;
		for(i = 0; i < 3; i++) {
			tmp[i] -= 0.5;
			if (fabs(tmp[i]) > biggest)
				biggest = fabs(tmp[i]);
		}
		for(i = 0; i < 3; i++) {
			if (biggest > 0.5)
				tmp[i] *= 0.5/biggest; 
			tmp[i] += 0.5;
		}
	}
#endif
	if (trace) printf("output clipped RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Copy to output */
	for(i = 0; i < 3; i++)
		out[i] = tmp[i];

}

#endif /* NORMAL_XYZ2RGB */

#ifdef NIEVE_XYZ2RGB

/* Nieve version with RGB clipping */
/* Convert from XYZ to sRGB */
void XYZ2sRGB(double *out, double *in, int trace) {
	double tmp[3];
	double scale = SCALE;		/* Scale RGB towards grey */
	double mat[3][3] = 
		{
			{  3.2410, -1.5374, -0.4986 },
			{ -0.9692,  1.8760,  0.0416 },
			{  0.0556, -0.2040,  1.0570 }
		};
	int i;

	if (trace) printf("XYZ to RGB\n");
	if (trace) printf("input XYZ %f %f %f\n",in[0],in[1],in[2]);

	/* Now convert to sRGB assuming D50 (??) white point */
	icmMulBy3x3(tmp, mat, in);

	/* Scale */
	for(i = 0; i < 3; i++) {
		tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
	}

	if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Clip to nearest */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.0)
			tmp[i] = 0.0;
		else if (tmp[i] > 1.0)
			tmp[i] = 1.0;
	}

	if (trace) printf("clipped RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Apply gamma */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.00304) {
			tmp[i] = tmp[i] * 12.92;
		} else {
			tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
		}
	}

	if (trace) printf("output gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Copy to output */
	for(i = 0; i < 3; i++)
		out[i] = tmp[i];
}

#endif /* NIEVE_XYZ2RGB */

#ifdef COMPLEX_XYZ2RGB

/* Complex version using powell to ensure good clipping */
/* Callback context */
typedef struct {
	double scale;
	double lab[3];
	double imat[3][3];
} ctx;

double clipf(void *fdata, double tp[]) {
	ctx *x = (ctx *)fdata;
	double lab[3], tmp[3], tt;
	int k;
	double rv = 0.0;

//printf("\n");
//printf("~1 clipf got %f %f %f\n",tp[0],tp[1],tp[2]);

	/* Penalize for being out of gamut */
	for (k = 0; k < 3; k++) {
		if (tp[k] < 0.0) {
			tt = 100000.0 * (0.0 - tp[k]);
			rv += tt * tt;
		} else if (tp[k] > 1.0) {
			tt = 100000.0 * (tp[k] - 1.0);
			rv += tt * tt;
		}
	}
//printf("~1 rv out of gamut = %f\n",rv);

	/* Unscale it */
	for(k = 0; k < 3; k++)
		tmp[k] = ((tp[k] - 0.5) / x->scale) + 0.5;
//printf("~1 unscale %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	icmMulBy3x3(tmp, x->imat, tmp);		/* To XYZ */
//printf("~1 xyz %f %f %f\n",tmp[0],tmp[1],tmp[2]);
	XYZ2Lab(lab, tmp);					/* To Lab */
//printf("~1 lab    = %f %f %f\n",lab[0],lab[1],lab[2]);
//printf("~1 target = %f %f %f\n",x->lab[0],x->lab[1],x->lab[2]);

	/* Compute an error to the goal */
	tt = 10000.0 * (lab[0] - x->lab[0]);
	rv += tt * tt;

	tt = 1.0 * (lab[1] - x->lab[1]);
	rv += tt * tt;
	tt = 1.0 * (lab[2] - x->lab[2]);
	rv += tt * tt;

//printf("~1 rv = %f\n",rv);

	return rv;
}

/* Convert from XYZ to sRGB */
void XYZ2sRGB(double *out, double *in, int trace) {
	double lab[3], tmp[3];
	double scale = SCALE;		/* Scale RGB towards grey */
	double mat[3][3] = 
		{
			{  3.2410, -1.5374, -0.4986 },
			{ -0.9692,  1.8760,  0.0416 },
			{  0.0556, -0.2040,  1.0570 }
		};
	int i;

	/* Copy from input */
	for(i = 0; i < 3; i++)
		tmp[i] = in[i];

	if (trace) printf("XYZ to RGB\n");

	if (trace) printf("input XYZ %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	XYZ2Lab(lab, tmp);

	if (trace) printf("Lab %f %f %f\n",lab[0],lab[1],lab[2]);

	icmClipLab(lab, lab);

	if (trace) printf("Clipped Lab %f %f %f\n",lab[0],lab[1],lab[2]);

	/* Now convert to sRGB assuming D50 (??) white point */
	Lab2XYZ(tmp, lab);
	icmMulBy3x3(tmp, mat, tmp);

	/* Scale */
	for(i = 0; i < 3; i++) {
		tmp[i] = ((tmp[i] - 0.5) * scale) + 0.5;
	}

	if (trace) printf("raw RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* See if it need clipping */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.0 || tmp[i] > 1.0)
			break;
	}
	/* It needs more clipping */
	if (i < 3) {
		ctx x;
		double ss[3] = { 0.1, 0.1, 0.1 };
		x.lab[0] = lab[0];
		x.lab[1] = lab[1];
		x.lab[2] = lab[2];
		x.scale = scale;
		icmInverse3x3(x.imat, mat);

		if (powell(NULL, 3, tmp, ss, 1e-6, 1000, clipf, (void *)&x) < 0.0)
			error("RGB clip failed");
	}
	if (trace) printf("RGB clip %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Clip to nearest */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.0)
			tmp[i] = 0.0;
		else if (tmp[i] > 1.0)
			tmp[i] = 1.0;
	}
	if (trace) printf("clip to nearest RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Apply gamma */
	for(i = 0; i < 3; i++) {
		if (tmp[i] < 0.00304) {
			tmp[i] = tmp[i] * 12.92;
		} else {
			tmp[i] = 1.055 * pow(tmp[i], 1.0/2.4) - 0.055;
		}
	}
	if (trace) printf("gamma corrected RGB %f %f %f\n",tmp[0],tmp[1],tmp[2]);

	/* Copy to output */
	for(i = 0; i < 3; i++)
		out[i] = tmp[i];

}
#endif 	/* COMPLEX_XYZ2RGB */

void usage(char *diag) {
	fprintf(stderr,"Create a 3D slice plot of XYZ/Lab <-> Jab, Version %s\n",ARGYLL_VERSION_STR);
	fprintf(stderr,"Author: Graeme W. Gill, licensed under the AGPL Version 3\n");
	if (diag != NULL)
		fprintf(stderr,"Diagnostic '%s'\n",diag);
	fprintf(stderr,"usage: cam02plot [-options] x y\n");
	fprintf(stderr,"'cam02plot -l -o' shows problem best\n");
	fprintf(stderr," -v level Verbosity level 0 - 2 (default = 1)\n");
	fprintf(stderr," -n       Don't use Helmholtz-Kohlraush flag\n"); 
	fprintf(stderr," -f       Use the reference CIECAM transform\n"); 
	fprintf(stderr," -r res   Resolution (default %d)\n",DEFRES); 
	fprintf(stderr," -l       Lab source cube (default XYZ source cube)\n");
	fprintf(stderr," -i       XYZ/Lab -> Jab (default XYZ/Lab -> Lab)\n");
	fprintf(stderr," -o       Jab -> XYZ -> RGB (defult Lab -> XYZ -> RGB\n");
	fprintf(stderr," -s       XYZ/Lab -> ab scale as RGB\n");
	fprintf(stderr," -x       Cross section plots\n");
	fprintf(stderr,"    x y   Return input color for this coordinate\n");
	exit(1);
}

int
main(int argc, char *argv[]) {
	int fa,nfa;				/* argument we're looking at */
	char *tiffname = "cam02plot.tif";
	TIFF *wh = NULL;
	uint16 resunits = RESUNIT_INCH;
	float resx = 75.0, resy = 75.0;
	tdata_t *obuf;
	unsigned char *ob;
	int verb = 0;
	int transl = 0;				/* Translate x,y into input color */
	int tx = 0, ty = 0;			/* coordinate to translate */
	int use_hk = 1;				/* Use Helmholtz-Kohlraush flag */
	int use_ref = 0;			/* Use reference transform rather than working code */
	int lab_plot = 0;			/* Lab <-> Jab plot, else XYZ <-> Jab */
	int to_jab = 0;				/* Convert to Jab, else convert to Lab */
	int from_jab = 0;			/* Convert from Jab, else convert from Lab */
	int to_ss = 0;				/* Convert from XYZ/Lab to abs scale ss */
	int xsect = 0;				/* Cross section plots */
	
	double white[9][3] = {
		{ 0.964242, 1.000000, 0.825124 },		/* 0: D50 */
		{ 1.098494, 1.000000, 0.355908 },		/* 1: A */
		{ 0.908731, 1.000000, 0.987233 },		/* 2: F5 */
		{ 1.128981, 1.000000, 0.305862 },		/* 3: D25 */
		{ 0.950471, 1.000000, 1.088828 },		/* 4: D65 */
		{ 0.949662, 1.000000, 1.160438 },		/* 5: D80 */
		{ 0.949662, 1.000000, 1.160438 },		/* 6: D80 */
		{ 0.951297, 1.000000, 1.338196 },		/* 7: D85 */
		{ 0.952480, 1.000000, 1.386693 }		/* 8: D90 */
	};

#ifdef NEVER
	double La[6] = { 10.0, 31.83, 100.0, 318.31, 1000.83, 3183.1 };

	ViewingCondition Vc[4] = {
		vc_average,
		vc_dark,
		vc_dim,
		vc_cut_sheet
	};
#endif /* NEVER */

	int i, j, k, m;
	double xyz[3], Jab[3], rgb[3];
	cam02 *cam1, *cam2;

	int res = DEFRES;	/* Resolution of scan through space */
	int ares;		/* Array of 2d sub-rasters */
	int w, h;		/* Width and height of raster */
	int x, y;
	int sp = 5;			/* Spacing beween resxres sub-images */

	error_program = argv[0];

	/* Process the arguments */
	for(fa = 1;fa < argc;fa++) {
		nfa = fa;					/* skip to nfa if next argument is used */
		if (argv[fa][0] == '-')	{	/* Look for any flags */
			char *na = NULL;		/* next argument after flag, null if none */

			if (argv[fa][2] != '\000')
				na = &argv[fa][2];		/* next is directly after flag */
			else {
				if ((fa+1) < argc) {
					if (argv[fa+1][0] != '-') {
						nfa = fa + 1;
						na = argv[nfa];		/* next is seperate non-flag argument */
					}
				}
			}

			if (argv[fa][1] == '?')
				usage("Requested usage");

			/* Verbosity */
			else if (argv[fa][1] == 'v' || argv[fa][1] == 'V') {
				fa = nfa;
				if (na == NULL)
					verb = 2;
				else {
					if (na[0] == '0')
						verb = 0;
					else if (na[0] == '1')
						verb = 1;
					else if (na[0] == '2')
						verb = 2;
					else
						usage("Illegal verbosity level");
				}
			}

			else if (argv[fa][1] == 'r' || argv[fa][1] == 'R') {
				fa = nfa;
				if (na == NULL)
					usage("Need resolution after -r");
				res = atoi(na);
				if (res < 2 || res > 1000)
					usage("Resolution is out of range");
			}
			else if (argv[fa][1] == 'n' || argv[fa][1] == 'N') {
				use_hk = 0;
			}
			else if (argv[fa][1] == 'f' || argv[fa][1] == 'F') {
				use_ref = 1;
			}
			else if (argv[fa][1] == 'l' || argv[fa][1] == 'L') {
				lab_plot = 1;
			}
			else if (argv[fa][1] == 'i' || argv[fa][1] == 'I') {
				to_jab = 1;
			}
			else if (argv[fa][1] == 'o' || argv[fa][1] == 'O') {
				from_jab = 1;
			}
			else if (argv[fa][1] == 's' || argv[fa][1] == 'S') {
				to_ss = 1;
			}
			else if (argv[fa][1] == 'x' || argv[fa][1] == 'X') {
				xsect = 1;
			}
			else 
				usage("Unknown flag");
		} else
			break;
	}

	if ((fa+1) < argc) {
		tx = atoi(argv[fa]);
		ty = atoi(argv[fa+1]);
		transl = 1;
	}


	/* Setup cam to convert to Jab */
	if (use_ref)
		cam1 = new_cam02ref();
	else
		cam1 = new_cam02();
	cam1->set_view(
		cam1,
		vc_average,	/* Enumerated Viewing Condition */
		white[4],	/* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) */
		1000.0,		/* Adapting/Surround Luminance cd/m^2 */
		0.20,		/* Relative Luminance of Background to reference white */
		0.0,		/* Luminance of white in image - not used */
		0.00,		/* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
		0.00,		/* Glare as a fraction of the ambient (Y range 0.0 .. 1.0) */
		white[4],	/* The Glare color coordinates (typically the Ambient color) */
		use_hk		/* use Helmholtz-Kohlraush flag */ 
	);
	
	/* Setup cam to convert from Jab */
	if (use_ref)
		cam2 = new_cam02ref();
	else
		cam2 = new_cam02();
	cam2->set_view(
		cam2,
		vc_average,	/* Enumerated Viewing Condition */
		white[4],	/* Reference/Adapted White XYZ (Y range 0.0 .. 1.0) */
		1000.0,		/* Adapting/Surround Luminance cd/m^2 */
		0.20,		/* Relative Luminance of Background to reference white */
		0.0,		/* Luminance of white in image - not used */
		0.00,		/* Flare as a fraction of the reference white (Y range 0.0 .. 1.0) */
		0.00,		/* Glare as a fraction of the ambient (Y range 0.0 .. 1.0) */
		white[4],	/* The Glare color coordinates (typically the Ambient color) */
		use_hk		/* use Helmholtz-Kohlraush flag */ 
	);
	
	/* Figure out the size of the raster */
	ares = (int)ceil(sqrt((double)res));
	if (verb)
		printf("For res %d, ares = %d\n",res,ares);
	
	w = ares * res + sp * (ares+1);
	h = ares * res + sp * (ares+1);

	if (to_ss) {
		cam1->retss = 1;
	}

	if (transl) {
		int ix,iy,iz;
		double dx,dy,dz;
		double tmp[3], lab[3];

		cam1->trace = 1;
		cam2->trace = 1;

//printf("~1 translate %d %d\n",tx,ty);
		tx -= sp;		/* Border at base */
		ty -= sp;

//printf("~1 offset %d %d\n",tx,ty);
		ix = tx % (res + sp);
		iy = ty % (res + sp);
		iz = (ty / (res + sp)) * ares + (tx / (res + sp));
		
//printf("~1 x y z = %d %d %d\n", ix,iy,iz);

		dx = ix/(res-1.0);
		dy = iy/(res-1.0);
		dz = iz/((ares * ares)-1.0);
		
//printf("~1 X Y Z = %f %f %f\n", dx,dy,dz);

		/* Source range is extended L*a*b* type */
		if (lab_plot) {
			if (xsect) {
				lab[0] = 180.0 * dz - 40.0;
				lab[1] = 300.0 * dy - 150.0;
				lab[2] = 300.0 * dx - 150.0;
			} else {
				lab[0] = 180.0 * dx - 40.0;
				lab[1] = 300.0 * dy - 150.0;
				lab[2] = 300.0 * dz - 150.0;
			}
//printf("~1 Lab = %f %f %f\n", lab[0],lab[1],lab[2]);
			Lab2XYZ(tmp, lab);
			printf("======================================================\n");
			printf("%d,%d -> Lab        %f %f %f\n", tx+sp, ty+sp, lab[0],lab[1],lab[2]);
			printf("      == XYZ        %f %f %f\n", tmp[0],tmp[1],tmp[2]);

		/* Else source range is extended XYZ type */
		} else {
			if (xsect) {
				tmp[0] = 1.4 * dz - 0.2;
				tmp[1] = 1.4 * dy - 0.2;
				tmp[2] = 1.6 * dx - 0.2;
			} else {
				tmp[0] = 1.4 * dx - 0.2;
				tmp[1] = 1.4 * dy - 0.2;
				tmp[2] = 1.6 * dz - 0.2;
			}
			printf("%d,%d -> Input color XYZ %f %f %f\n", tx, ty, tmp[0],tmp[1],tmp[2]);
		}

		if (to_ss) {
			cam1->XYZ_to_cam(cam1, Jab, tmp);
			printf("         XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);

		/* Convert XYZ through cam and back, then to RGB */
		} else if (to_jab) {
			cam1->XYZ_to_cam(cam1, Jab, tmp);
			if (to_ss) {
				printf("         XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);
			} else {
				printf("         XYZ -> ss %f %f %f\n", Jab[0],Jab[1],Jab[2]);
			}

		/* Else convert XYZ to L*a*b* */
		} else {
			XYZ2Lab(Jab, tmp);
			printf("         XYZ -> Lab %f %f %f\n", Jab[0],Jab[1],Jab[2]);
		}

		/* Convert from Jab back to XYZ */
		if (from_jab) {
			cam2->cam_to_XYZ(cam2, rgb, Jab);
			printf("         Jab -> XYZ %f %f %f\n", rgb[0],rgb[1],rgb[2]);

		/* Else convert from Lab back to XYZ */
		} else {
			Lab2XYZ(rgb, Jab);
			printf("         Lab -> XYZ %f %f %f\n", rgb[0],rgb[1],rgb[2]);
		}

		XYZ2Lab(tmp, rgb);
		printf("      == Lab        %f %f %f\n", tmp[0],tmp[1],tmp[2]);

		/* Interpret XYZ as RGB color */
		XYZ2sRGB(rgb, rgb, 1);
		printf("         XYZ -> RGB %f %f %f\n", rgb[0],rgb[1],rgb[2]);

		cam1->trace = 0;
		cam2->trace = 0;

		printf("\n");
		exit(0);
	}

	if (verb)
		printf("Raster width = %d, height = %d\n",w,h);

	/* Setup the tiff file */
	if ((wh = TIFFOpen(tiffname, "w")) == NULL)
		error("Can\'t create TIFF file '%s'!",tiffname);
	
	TIFFSetField(wh, TIFFTAG_IMAGEWIDTH,  w);
	TIFFSetField(wh, TIFFTAG_IMAGELENGTH, h);
	TIFFSetField(wh, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
	TIFFSetField(wh, TIFFTAG_SAMPLESPERPIXEL, 3);
	TIFFSetField(wh, TIFFTAG_BITSPERSAMPLE, 8);
	TIFFSetField(wh, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(wh, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
	TIFFSetField(wh, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
	TIFFSetField(wh, TIFFTAG_RESOLUTIONUNIT, resunits);
	TIFFSetField(wh, TIFFTAG_XRESOLUTION, resx);
	TIFFSetField(wh, TIFFTAG_YRESOLUTION, resy);
	TIFFSetField(wh, TIFFTAG_IMAGEDESCRIPTION, "cam02plot");

	obuf = _TIFFmalloc(TIFFScanlineSize(wh));
	ob = (unsigned char *)obuf;

	y = 0;

	/* Fill sp lines with black */
	for (x = 0, m = 0; m < w; m++) {
		ob[x] = ob[x+1] = ob[x+2] = 0;
		x += 3;
	}
	for (j = 0; j < sp; j++, y++) {
		TIFFWriteScanline(wh, ob, y, 0);
	}

	for (i = 0; i < ares; i++) {				/* Vertical blocks (rows) */
		for (j = 0; j < res; j++, y++) {		/* Vertical in block (y = Y/a*) */
			xyz[1] = j/(res-1.0);
			x = 0;

			/* Fill sp pixels with black */
			for (m = 0; m < sp; m++) {
				ob[x] = ob[x+1] = ob[x+2] = 0;
				x += 3;
			}
			for (k = 0; k < ares; k++) {		/* Horizontal blocks (columns) */
				int zv = i * ares + k;
				if (zv >= res) {
					/* Fill res pixels with black */
					for (m = 0; m < res; m++) {
						ob[x] = ob[x+1] = ob[x+2] = 0;
						x += 3;
					}
				} else {
					xyz[2] = zv/(res-1.0);		/* z = block = Z/b* */
					for (m = 0; m < res; m++) {	/* Horizontal in block (x = X/L*) */
						double tmp[3], xyz2[3];
						xyz[0] = m/(res-1.0);

						if (lab_plot) {
							if (xsect) {
								tmp[0] = 180.0 * xyz[2] - 40.0;
								tmp[1] = 300.0 * xyz[1] - 150.0;
								tmp[2] = 300.0 * xyz[0] - 150.0;
							} else {
								tmp[0] = 180.0 * xyz[0] - 40.0;
								tmp[1] = 300.0 * xyz[1] - 150.0;
								tmp[2] = 300.0 * xyz[2] - 150.0;
							}
							Lab2XYZ(tmp, tmp);

						} else {
							if (xsect) {
								tmp[0] = 1.4 * xyz[2] - 0.2;
								tmp[1] = 1.4 * xyz[1] - 0.2;
								tmp[2] = 1.6 * xyz[0] - 0.2;
							} else {
								tmp[0] = 1.4 * xyz[0] - 0.2;
								tmp[1] = 1.4 * xyz[1] - 0.2;
								tmp[2] = 1.6 * xyz[2] - 0.2;
							}
						}

						/* Convert XYZ through cam and back, then to RGB */
						if (to_jab || to_ss)
							cam1->XYZ_to_cam(cam1, Jab, tmp);
						else
							XYZ2Lab(Jab, tmp);

						if (to_ss) {
							rgb[0] = Jab[0];
							rgb[1] = Jab[1];
							rgb[2] = Jab[2];
						} else {
							if (from_jab)
								cam2->cam_to_XYZ(cam2, xyz2, Jab);
							else
								Lab2XYZ(xyz2, Jab);
	
							XYZ2sRGB(rgb, xyz2, 0);
						}

						/* Fill with pixel value */
						ob[x+0] = (int)(rgb[0] * 255.0 + 0.5);
						ob[x+1] = (int)(rgb[1] * 255.0 + 0.5);
						ob[x+2] = (int)(rgb[2] * 255.0 + 0.5);
						x += 3;
					}
				}
				/* Fill sp pixels with black */
				for (m = 0; m < sp; m++) {
					ob[x] = ob[x+1] = ob[x+2] = 0;
					x += 3;
				}
			}
			TIFFWriteScanline(wh, ob, y, 0);
		}
		/* Fill sp lines with black */
		for (x = m = 0; m < w; m++) {
			ob[x] = ob[x+1] = ob[x+2] = 0;
			x += 3;
		}
		for (j = 0; j < sp; j++, y++) {
			TIFFWriteScanline(wh, ob, y, 0);
		}
	}

	cam1->del(cam1);
	cam2->del(cam2);

	/* Write TIFF file */
	TIFFClose(wh);

	return 0;
}