ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
Revision: 2.13
Committed: Fri Jan 31 15:56:17 1997 UTC (27 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +50 -42 lines
Log Message:
added gamut mapping for display devices (doesn't work w/ pcomb)

File Contents

# User Rev Content
1 greg 2.12 /* Copyright (c) 1997 Regents of the University of California */
2 greg 2.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Calibrate a scanned MacBeth Color Checker Chart
9     *
10     * Produce a .cal file suitable for use with pcomb.
11     */
12    
13     #include <stdio.h>
14     #ifdef MSDOS
15     #include <fcntl.h>
16     #endif
17     #include "color.h"
18     #include "resolu.h"
19     #include "pmap.h"
20    
21 greg 2.4 /* MacBeth colors */
22     #define DarkSkin 0
23     #define LightSkin 1
24     #define BlueSky 2
25     #define Foliage 3
26     #define BlueFlower 4
27     #define BluishGreen 5
28     #define Orange 6
29     #define PurplishBlue 7
30     #define ModerateRed 8
31     #define Purple 9
32     #define YellowGreen 10
33     #define OrangeYellow 11
34     #define Blue 12
35     #define Green 13
36     #define Red 14
37     #define Yellow 15
38     #define Magenta 16
39     #define Cyan 17
40     #define White 18
41     #define Neutral8 19
42     #define Neutral65 20
43     #define Neutral5 21
44     #define Neutral35 22
45     #define Black 23
46     /* computed from 5nm spectral measurements */
47     /* CIE 1931 2 degree obs, equal-energy white */
48 greg 2.1 float mbxyY[24][3] = {
49     {0.462, 0.3769, 0.0932961}, /* DarkSkin */
50     {0.4108, 0.3542, 0.410348}, /* LightSkin */
51     {0.2626, 0.267, 0.181554}, /* BlueSky */
52     {0.36, 0.4689, 0.108447}, /* Foliage */
53     {0.2977, 0.2602, 0.248407}, /* BlueFlower */
54     {0.2719, 0.3485, 0.401156}, /* BluishGreen */
55     {0.52, 0.4197, 0.357899}, /* Orange */
56     {0.229, 0.1866, 0.103911}, /* PurplishBlue */
57     {0.4909, 0.3262, 0.242615}, /* ModerateRed */
58     {0.3361, 0.2249, 0.0600102}, /* Purple */
59     {0.3855, 0.4874, 0.42963}, /* YellowGreen */
60     {0.4853, 0.4457, 0.476343}, /* OrangeYellow */
61     {0.2026, 0.1369, 0.0529249}, /* Blue */
62     {0.3007, 0.4822, 0.221226}, /* Green */
63     {0.5805, 0.3238, 0.162167}, /* Red */
64     {0.4617, 0.472, 0.64909}, /* Yellow */
65     {0.4178, 0.2625, 0.233662}, /* Magenta */
66     {0.2038, 0.2508, 0.167275}, /* Cyan */
67     {0.3358, 0.337, 0.916877}, /* White */
68     {0.3338, 0.3348, 0.604678}, /* Neutral.8 */
69     {0.3333, 0.3349, 0.364566}, /* Neutral.65 */
70     {0.3353, 0.3359, 0.200238}, /* Neutral.5 */
71     {0.3363, 0.336, 0.0878721}, /* Neutral.35 */
72     {0.3346, 0.3349, 0.0308383} /* Black */
73     };
74    
75     COLOR mbRGB[24]; /* MacBeth RGB values */
76    
77     #define NMBNEU 6 /* Number of MacBeth neutral colors */
78 greg 2.4 short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White};
79 greg 2.1
80 greg 2.8 #define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \
81     1L<<Neutral5|1L<<Neutral35|1L<<Black)
82 greg 2.4
83 greg 2.8 #define SATFLGS (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \
84     1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \
85     1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower)
86 greg 2.4
87 greg 2.8 #define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \
88     1L<<BluishGreen|1L<<ModerateRed)
89 greg 2.7
90 greg 2.8 #define REQFLGS NEUFLGS /* need these colors */
91     #define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */
92    
93 greg 2.9 #define RG_BORD 0 /* patch border */
94     #define RG_CENT 01 /* central region of patch */
95     #define RG_ORIG 02 /* original color region */
96     #define RG_CORR 04 /* corrected color region */
97 greg 2.7
98 greg 2.11 int scanning = 1; /* scanned input (or recorded output)? */
99    
100 greg 2.1 int xmax, ymax; /* input image dimensions */
101     int bounds[4][2]; /* image coordinates of chart corners */
102     double imgxfm[3][3]; /* coordinate transformation matrix */
103    
104 greg 2.7 COLOR inpRGB[24]; /* measured or scanned input colors */
105     long inpflags = 0; /* flags of which colors were input */
106 greg 2.8 long gmtflags = 0; /* flags of out-of-gamut colors */
107 greg 2.1
108 greg 2.2 COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */
109 greg 2.13 COLORMAT solmat; /* color mapping matrix */
110     COLOR colmin, colmax; /* gamut limits */
111 greg 2.1
112 greg 2.4 FILE *debugfp = NULL; /* debug output picture */
113 greg 2.1 char *progname;
114    
115     extern char *malloc();
116    
117    
118     main(argc, argv)
119     int argc;
120     char **argv;
121     {
122     int i;
123    
124     progname = argv[0];
125 greg 2.7 for (i = 1; i < argc && argv[i][0] == '-'; i++)
126     switch (argv[i][1]) {
127     case 'd': /* debug output */
128     i++;
129     if (badarg(argc-i, argv+i, "s"))
130     goto userr;
131     if ((debugfp = fopen(argv[i], "w")) == NULL) {
132     perror(argv[i]);
133     exit(1);
134     }
135 greg 2.1 #ifdef MSDOS
136 greg 2.7 setmode(fileno(debugfp), O_BINARY);
137 greg 2.1 #endif
138 greg 2.7 newheader("RADIANCE", debugfp); /* start */
139     printargs(argc, argv, debugfp); /* header */
140     break;
141     case 'p': /* picture position */
142     if (badarg(argc-i-1, argv+i+1, "iiiiiiii"))
143     goto userr;
144     bounds[0][0] = atoi(argv[++i]);
145     bounds[0][1] = atoi(argv[++i]);
146     bounds[1][0] = atoi(argv[++i]);
147     bounds[1][1] = atoi(argv[++i]);
148     bounds[2][0] = atoi(argv[++i]);
149     bounds[2][1] = atoi(argv[++i]);
150     bounds[3][0] = atoi(argv[++i]);
151     bounds[3][1] = atoi(argv[++i]);
152 greg 2.11 scanning = 2;
153 greg 2.7 break;
154     case 'c': /* color input */
155 greg 2.11 scanning = 0;
156 greg 2.7 break;
157     default:
158     goto userr;
159     }
160     /* open files */
161     if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
162 greg 2.13 perror(argv[i]);
163 greg 2.1 exit(1);
164     }
165 greg 2.7 if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
166 greg 2.13 perror(argv[i+1]);
167 greg 2.1 exit(1);
168     }
169 greg 2.11 if (scanning) { /* load input picture header */
170 greg 2.1 #ifdef MSDOS
171 greg 2.7 setmode(fileno(stdin), O_BINARY);
172 greg 2.1 #endif
173 greg 2.7 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
174     fgetresolu(&xmax, &ymax, stdin) < 0) {
175     fprintf(stderr, "%s: bad input picture\n", progname);
176     exit(1);
177     }
178     } else { /* else set default xmax and ymax */
179     xmax = 512;
180     ymax = 2*512/3;
181 greg 2.1 }
182 greg 2.11 if (scanning != 2) { /* use default boundaries */
183 greg 2.1 bounds[0][0] = bounds[2][0] = .029*xmax + .5;
184     bounds[0][1] = bounds[1][1] = .956*ymax + .5;
185     bounds[1][0] = bounds[3][0] = .971*xmax + .5;
186     bounds[2][1] = bounds[3][1] = .056*ymax + .5;
187     }
188     init(); /* initialize */
189 greg 2.11 if (scanning) /* get picture colors */
190 greg 2.7 getpicture();
191     else
192     getcolors();
193 greg 2.2 compute(); /* compute color mapping */
194 greg 2.4 /* print comment */
195 greg 2.10 printf("{\n\tColor correction file computed by:\n\t\t");
196 greg 2.7 printargs(argc, argv, stdout);
197 greg 2.10 printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
198     i+1 < argc ? argv[i+1] : "{this_file}");
199 greg 2.11 if (!scanning)
200     printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n",
201     i+1 < argc ? argv[i+1] : "{this_file}");
202 greg 2.7 printf("}\n");
203 greg 2.1 putmapping(); /* put out color mapping */
204 greg 2.7 if (debugfp != NULL) /* put out debug picture */
205 greg 2.11 if (scanning)
206 greg 2.7 picdebug();
207     else
208     clrdebug();
209 greg 2.1 exit(0);
210     userr:
211 greg 2.7 fprintf(stderr,
212     "Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n",
213 greg 2.1 progname);
214 greg 2.7 fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n",
215     progname);
216 greg 2.1 exit(1);
217     }
218    
219    
220     init() /* initialize */
221     {
222     double quad[4][2];
223 greg 2.7 register int i;
224 greg 2.1 /* make coordinate transformation */
225     quad[0][0] = bounds[0][0];
226     quad[0][1] = bounds[0][1];
227     quad[1][0] = bounds[1][0];
228     quad[1][1] = bounds[1][1];
229     quad[2][0] = bounds[3][0];
230     quad[2][1] = bounds[3][1];
231     quad[3][0] = bounds[2][0];
232     quad[3][1] = bounds[2][1];
233    
234     if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) {
235     fprintf(stderr, "%s: bad chart boundaries\n", progname);
236     exit(1);
237     }
238 greg 2.7 /* map MacBeth colors to RGB space */
239     for (i = 0; i < 24; i++)
240     xyY2RGB(mbRGB[i], mbxyY[i]);
241 greg 2.1 }
242    
243    
244     int
245 greg 2.9 chartndx(x, y, np) /* find color number for position */
246 greg 2.1 int x, y;
247 greg 2.9 int *np;
248 greg 2.1 {
249     double ipos[3], cpos[3];
250     int ix, iy;
251     double fx, fy;
252    
253     ipos[0] = x;
254     ipos[1] = y;
255     ipos[2] = 1;
256     mx3d_transform(ipos, imgxfm, cpos);
257     cpos[0] /= cpos[2];
258     cpos[1] /= cpos[2];
259     if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
260 greg 2.9 return(RG_BORD);
261 greg 2.1 ix = cpos[0];
262     iy = cpos[1];
263     fx = cpos[0] - ix;
264     fy = cpos[1] - iy;
265 greg 2.9 *np = iy*6 + ix;
266     if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65)
267     return(RG_CENT);
268     if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95)
269     return(RG_BORD);
270     if (fx >= 0.5) /* right side is corrected */
271     return(RG_CORR);
272     return(RG_ORIG); /* left side is original */
273 greg 2.1 }
274    
275    
276 greg 2.7 getpicture() /* load in picture colors */
277 greg 2.1 {
278     COLR *scanln;
279     COLOR pval;
280     int ccount[24];
281     double d;
282 greg 2.9 int y, i;
283     register int x;
284 greg 2.1
285     scanln = (COLR *)malloc(xmax*sizeof(COLR));
286     if (scanln == NULL) {
287     perror(progname);
288     exit(1);
289     }
290     for (i = 0; i < 24; i++) {
291 greg 2.7 setcolor(inpRGB[i], 0., 0., 0.);
292 greg 2.1 ccount[i] = 0;
293     }
294     for (y = ymax-1; y >= 0; y--) {
295     if (freadcolrs(scanln, xmax, stdin) < 0) {
296     fprintf(stderr, "%s: error reading input picture\n",
297     progname);
298     exit(1);
299     }
300 greg 2.9 for (x = 0; x < xmax; x++)
301     if (chartndx(x, y, &i) == RG_CENT) {
302 greg 2.1 colr_color(pval, scanln[x]);
303 greg 2.7 addcolor(inpRGB[i], pval);
304 greg 2.1 ccount[i]++;
305     }
306     }
307 greg 2.7 for (i = 0; i < 24; i++) { /* compute averages */
308     if (ccount[i] == 0)
309     continue;
310     d = 1./ccount[i];
311     scalecolor(inpRGB[i], d);
312     inpflags |= 1L<<i;
313     }
314     free((char *)scanln);
315     }
316    
317    
318     getcolors() /* get xyY colors from standard input */
319     {
320     int gotwhite = 0;
321     COLOR whiteclr;
322     int n;
323     float xyYin[3];
324    
325     while (fgetval(stdin, 'i', &n) == 1) { /* read colors */
326     if (n < 0 | n > 24 ||
327     fgetval(stdin, 'f', &xyYin[0]) != 1 ||
328     fgetval(stdin, 'f', &xyYin[1]) != 1 ||
329     fgetval(stdin, 'f', &xyYin[2]) != 1 ||
330 greg 2.12 xyYin[0] < 0. | xyYin[1] < 0. ||
331     xyYin[0] + xyYin[1] > 1.) {
332 greg 2.7 fprintf(stderr, "%s: bad color input data\n",
333 greg 2.1 progname);
334     exit(1);
335     }
336 greg 2.7 if (n == 0) { /* calibration white */
337     xyY2RGB(whiteclr, xyYin);
338     gotwhite++;
339     } else { /* standard color */
340     n--;
341     xyY2RGB(inpRGB[n], xyYin);
342     inpflags |= 1L<<n;
343     }
344 greg 2.1 }
345 greg 2.7 /* normalize colors */
346     if (!gotwhite) {
347     if (!(inpflags & 1L<<White)) {
348     fprintf(stderr, "%s: missing input for White\n",
349     progname);
350     exit(1);
351     }
352     setcolor(whiteclr,
353     colval(inpRGB[White],RED)/colval(mbRGB[White],RED),
354     colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN),
355     colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU));
356     }
357     for (n = 0; n < 24; n++)
358     if (inpflags & 1L<<n)
359     setcolor(inpRGB[n],
360     colval(inpRGB[n],RED)/colval(whiteclr,RED),
361     colval(inpRGB[n],GRN)/colval(whiteclr,GRN),
362     colval(inpRGB[n],BLU)/colval(whiteclr,BLU));
363 greg 2.1 }
364    
365    
366 greg 2.2 bresp(y, x) /* piecewise linear interpolation of primaries */
367     COLOR y, x;
368 greg 2.1 {
369 greg 2.2 register int i, n;
370 greg 2.1
371 greg 2.2 for (i = 0; i < 3; i++) {
372 greg 2.5 for (n = 0; n < NMBNEU-2; n++)
373     if (colval(x,i) < colval(bramp[n+1][0],i))
374     break;
375 greg 2.8 colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
376 greg 2.2 colval(bramp[n][1],i) +
377     (colval(x,i) - colval(bramp[n][0],i)) *
378     colval(bramp[n+1][1],i)) /
379     (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
380     }
381 greg 2.1 }
382    
383    
384 greg 2.2 compute() /* compute color mapping */
385 greg 2.1 {
386 greg 2.8 COLOR clrin[24], clrout[24];
387     long cflags;
388     COLOR ctmp;
389 greg 2.13 register int i, n;
390 greg 2.7 /* did we get what we need? */
391     if ((inpflags & REQFLGS) != REQFLGS) {
392     fprintf(stderr, "%s: missing required input colors\n",
393     progname);
394     exit(1);
395 greg 2.2 }
396 greg 2.1 /* compute piecewise luminance curve */
397     for (i = 0; i < NMBNEU; i++) {
398 greg 2.7 copycolor(bramp[i][0], inpRGB[mbneu[i]]);
399 greg 2.2 copycolor(bramp[i][1], mbRGB[mbneu[i]]);
400 greg 2.1 }
401 greg 2.13 /* compute color space gamut */
402     if (scanning) {
403     copycolor(colmin, cblack);
404     copycolor(colmax, cwhite);
405     } else
406     for (i = 0; i < 3; i++) {
407     colval(colmin,i) = colval(bramp[0][0],i) -
408     colval(bramp[0][1],i) *
409     (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
410     (colval(bramp[1][1],i)-colval(bramp[1][0],i));
411     colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
412     (1.-colval(bramp[NMBNEU-2][1],i)) *
413     (colval(bramp[NMBNEU-1][0],i) -
414     colval(bramp[NMBNEU-2][0],i)) /
415     (colval(bramp[NMBNEU-1][1],i) -
416     colval(bramp[NMBNEU-2][1],i));
417     }
418 greg 2.8 /* compute color mapping */
419     do {
420     cflags = inpflags & ~gmtflags;
421     n = 0; /* compute transform matrix */
422     for (i = 0; i < 24; i++)
423     if (cflags & 1L<<i) {
424     bresp(clrin[n], inpRGB[i]);
425     copycolor(clrout[n], mbRGB[i]);
426     n++;
427     }
428     compsoln(clrin, clrout, n);
429     /* check out-of-gamut colors */
430     for (i = 0; i < 24; i++)
431 greg 2.13 if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
432     gmtflags |= 1L<<i;
433 greg 2.8 } while (cflags & gmtflags);
434     if (gmtflags & MODFLGS)
435     fprintf(stderr,
436     "%s: warning - some moderate colors are out of gamut\n",
437     progname);
438 greg 2.2 }
439    
440    
441     putmapping() /* put out color mapping for pcomb -f */
442     {
443     static char cchar[3] = {'r', 'g', 'b'};
444     register int i, j;
445 greg 2.1 /* print brightness mapping */
446 greg 2.2 for (j = 0; j < 3; j++) {
447     printf("%cxa(i) : select(i", cchar[j]);
448     for (i = 0; i < NMBNEU; i++)
449     printf(",%g", colval(bramp[i][0],j));
450     printf(");\n");
451     printf("%cya(i) : select(i", cchar[j]);
452     for (i = 0; i < NMBNEU; i++)
453     printf(",%g", colval(bramp[i][1],j));
454     printf(");\n");
455     printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
456     cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
457     cchar[j], cchar[j]);
458     printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
459 greg 2.11 printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
460     cchar[j], scanning?'n':'o', cchar[j],
461     cchar[j], cchar[j], cchar[j], cchar[j]);
462 greg 2.2 printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
463     cchar[j], cchar[j], cchar[j],
464     cchar[j], cchar[j]);
465     printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n",
466     cchar[j], cchar[j], cchar[j], cchar[j]);
467     }
468 greg 2.1 /* print color mapping */
469 greg 2.11 if (scanning) {
470     printf("r = ri(1); g = gi(1); b = bi(1);\n");
471     printf("ro = %g*rn + %g*gn + %g*bn ;\n",
472     solmat[0][0], solmat[0][1], solmat[0][2]);
473     printf("go = %g*rn + %g*gn + %g*bn ;\n",
474     solmat[1][0], solmat[1][1], solmat[1][2]);
475     printf("bo = %g*rn + %g*gn + %g*bn ;\n",
476     solmat[2][0], solmat[2][1], solmat[2][2]);
477     } else {
478     printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n");
479     printf("r = %g*r1 + %g*g1 + %g*b1 ;\n",
480     solmat[0][0], solmat[0][1], solmat[0][2]);
481     printf("g = %g*r1 + %g*g1 + %g*b1 ;\n",
482     solmat[1][0], solmat[1][1], solmat[1][2]);
483     printf("b = %g*r1 + %g*g1 + %g*b1 ;\n",
484     solmat[2][0], solmat[2][1], solmat[2][2]);
485     }
486 greg 2.1 }
487    
488    
489 greg 2.4 compsoln(cin, cout, n) /* solve 3xN system using least-squares */
490 greg 2.2 COLOR cin[], cout[];
491 greg 2.1 int n;
492     {
493     extern double mx3d_adjoint(), fabs();
494     double mat[3][3], invmat[3][3];
495     double det;
496     double colv[3], rowv[3];
497 greg 2.4 register int i, j, k;
498 greg 2.1
499 greg 2.8 if (n < 3) {
500     fprintf(stderr, "%s: too few colors to match!\n", progname);
501 greg 2.1 exit(1);
502     }
503 greg 2.4 if (n == 3)
504     for (i = 0; i < 3; i++)
505     for (j = 0; j < 3; j++)
506     mat[i][j] = colval(cin[j],i);
507     else { /* compute A^t A */
508     for (i = 0; i < 3; i++)
509     for (j = i; j < 3; j++) {
510     mat[i][j] = 0.;
511     for (k = 0; k < n; k++)
512     mat[i][j] += colval(cin[k],i) *
513     colval(cin[k],j);
514     }
515     for (i = 1; i < 3; i++) /* using symmetry */
516     for (j = 0; j < i; j++)
517     mat[i][j] = mat[j][i];
518     }
519 greg 2.1 det = mx3d_adjoint(mat, invmat);
520     if (fabs(det) < 1e-4) {
521     fprintf(stderr, "%s: cannot compute color mapping\n",
522     progname);
523     solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.;
524     solmat[0][1] = solmat[0][2] = solmat[1][0] =
525     solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.;
526     return;
527     }
528     for (i = 0; i < 3; i++)
529     for (j = 0; j < 3; j++)
530     invmat[i][j] /= det;
531     for (i = 0; i < 3; i++) {
532 greg 2.4 if (n == 3)
533     for (j = 0; j < 3; j++)
534     colv[j] = colval(cout[j],i);
535     else
536     for (j = 0; j < 3; j++) {
537     colv[j] = 0.;
538     for (k = 0; k < n; k++)
539     colv[j] += colval(cout[k],i) *
540     colval(cin[k],j);
541     }
542 greg 2.3 mx3d_transform(colv, invmat, rowv);
543 greg 2.1 for (j = 0; j < 3; j++)
544 greg 2.3 solmat[i][j] = rowv[j];
545 greg 2.1 }
546     }
547    
548 greg 2.3
549 greg 2.13 int
550 greg 2.1 cvtcolor(cout, cin) /* convert color according to our mapping */
551     COLOR cout, cin;
552     {
553 greg 2.8 COLOR ctmp;
554 greg 2.13 int clipped;
555 greg 2.8
556 greg 2.11 if (scanning) {
557     bresp(ctmp, cin);
558 greg 2.13 clipped = cresp(cout, ctmp);
559 greg 2.11 } else {
560 greg 2.13 clipped = cresp(ctmp, cin);
561 greg 2.11 bresp(cout, ctmp);
562     }
563 greg 2.13 return(clipped);
564 greg 2.8 }
565    
566    
567 greg 2.13 int
568 greg 2.8 cresp(cout, cin) /* transform color according to matrix */
569     COLOR cout, cin;
570     {
571 greg 2.13 colortrans(cout, solmat, cin);
572     return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
573 greg 2.1 }
574    
575    
576 greg 2.7 xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */
577     COLOR rgbout;
578     register float xyYin[3];
579 greg 2.1 {
580 greg 2.7 COLOR ctmp;
581     double d;
582    
583     d = xyYin[2] / xyYin[1];
584     ctmp[0] = xyYin[0] * d;
585     ctmp[1] = xyYin[2];
586     ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
587 greg 2.12 /* allow negative values */
588 greg 2.13 colortrans(rgbout, xyz2rgbmat, ctmp);
589 greg 2.7 }
590    
591    
592     picdebug() /* put out debugging picture */
593     {
594 greg 2.8 static COLOR blkcol = BLKCOLOR;
595 greg 2.1 COLOR *scan;
596 greg 2.9 int y, i;
597     register int x, rg;
598 greg 2.1
599     if (fseek(stdin, 0L, 0) == EOF) {
600     fprintf(stderr, "%s: cannot seek on input picture\n", progname);
601     exit(1);
602     }
603     getheader(stdin, NULL, NULL); /* skip input header */
604     fgetresolu(&xmax, &ymax, stdin);
605     /* allocate scanline */
606     scan = (COLOR *)malloc(xmax*sizeof(COLOR));
607     if (scan == NULL) {
608     perror(progname);
609     exit(1);
610     }
611     /* finish debug header */
612 greg 2.5 fputformat(COLRFMT, debugfp);
613 greg 2.1 putc('\n', debugfp);
614     fprtresolu(xmax, ymax, debugfp);
615 greg 2.7 /* write debug picture */
616 greg 2.1 for (y = ymax-1; y >= 0; y--) {
617     if (freadscan(scan, xmax, stdin) < 0) {
618     fprintf(stderr, "%s: error rereading input picture\n",
619     progname);
620     exit(1);
621     }
622     for (x = 0; x < xmax; x++) {
623 greg 2.9 rg = chartndx(x, y, &i);
624     if (rg == RG_CENT) {
625 greg 2.13 if (!(1L<<i & gmtflags) || (x+y)&07) {
626 greg 2.9 copycolor(scan[x], mbRGB[i]);
627 greg 2.13 clipgamut(scan[x], bright(scan[x]),
628     CGAMUT, cblack, cwhite);
629     } else
630 greg 2.9 copycolor(scan[x], blkcol);
631     } else if (rg == RG_CORR)
632 greg 2.1 cvtcolor(scan[x], scan[x]);
633 greg 2.9 else if (rg != RG_ORIG)
634 greg 2.8 copycolor(scan[x], blkcol);
635 greg 2.1 }
636     if (fwritescan(scan, xmax, debugfp) < 0) {
637     fprintf(stderr, "%s: error writing debugging picture\n",
638     progname);
639     exit(1);
640     }
641     }
642 greg 2.7 /* clean up */
643     fclose(debugfp);
644     free((char *)scan);
645     }
646    
647    
648     clrdebug() /* put out debug picture from color input */
649     {
650     static COLR blkclr = BLKCOLR;
651 greg 2.9 COLR mbclr[24], cvclr[24], orclr[24];
652 greg 2.7 COLR *scan;
653 greg 2.13 COLOR ctmp, ct2;
654 greg 2.9 int y, i;
655     register int x, rg;
656 greg 2.7 /* convert colors */
657     for (i = 0; i < 24; i++) {
658 greg 2.13 copycolor(ctmp, mbRGB[i]);
659     clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
660     setcolr(mbclr[i], colval(ctmp,RED),
661     colval(ctmp,GRN), colval(ctmp,BLU));
662 greg 2.7 if (inpflags & 1L<<i) {
663 greg 2.13 copycolor(ctmp, inpRGB[i]);
664     clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
665     setcolr(orclr[i], colval(ctmp,RED),
666 greg 2.7 colval(ctmp,GRN), colval(ctmp,BLU));
667 greg 2.13 bresp(ctmp, inpRGB[i]);
668     colortrans(ct2, solmat, ctmp);
669     clipgamut(ct2, bright(ct2), CGAMUT, cblack, cwhite);
670     setcolr(cvclr[i], colval(ct2,RED),
671     colval(ct2,GRN), colval(ct2,BLU));
672 greg 2.7 }
673     }
674     /* allocate scanline */
675     scan = (COLR *)malloc(xmax*sizeof(COLR));
676     if (scan == NULL) {
677     perror(progname);
678     exit(1);
679     }
680     /* finish debug header */
681     fputformat(COLRFMT, debugfp);
682     putc('\n', debugfp);
683     fprtresolu(xmax, ymax, debugfp);
684     /* write debug picture */
685     for (y = ymax-1; y >= 0; y--) {
686 greg 2.9 for (x = 0; x < xmax; x++) {
687     rg = chartndx(x, y, &i);
688     if (rg == RG_CENT) {
689 greg 2.8 if (!(1L<<i & gmtflags) || (x+y)&07)
690     copycolr(scan[x], mbclr[i]);
691     else
692     copycolr(scan[x], blkclr);
693 greg 2.9 } else if (rg == RG_BORD || !(1L<<i & inpflags))
694     copycolr(scan[x], blkclr);
695     else if (rg == RG_ORIG)
696     copycolr(scan[x], orclr[i]);
697     else /* rg == RG_CORR */
698 greg 2.7 copycolr(scan[x], cvclr[i]);
699 greg 2.9 }
700 greg 2.7 if (fwritecolrs(scan, xmax, debugfp) < 0) {
701     fprintf(stderr, "%s: error writing debugging picture\n",
702     progname);
703     exit(1);
704     }
705     }
706     /* clean up */
707     fclose(debugfp);
708 greg 2.1 free((char *)scan);
709     }