ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
Revision: 2.8
Committed: Mon Oct 23 10:19:40 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +77 -43 lines
Log Message:
added automatic detection of out-of-gamut colors

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1995 Regents of the University of California */
2    
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.7 #define CENTCVG 0.3 /* measured coverage of square sample */
94     #define FULLCVG 0.9 /* coverage of entire square */
95    
96 greg 2.1 int xmax, ymax; /* input image dimensions */
97     int bounds[4][2]; /* image coordinates of chart corners */
98     double imgxfm[3][3]; /* coordinate transformation matrix */
99    
100 greg 2.7 COLOR inpRGB[24]; /* measured or scanned input colors */
101     long inpflags = 0; /* flags of which colors were input */
102 greg 2.8 long gmtflags = 0; /* flags of out-of-gamut colors */
103 greg 2.1
104 greg 2.2 COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */
105 greg 2.1 double solmat[3][3]; /* color mapping matrix */
106    
107 greg 2.4 FILE *debugfp = NULL; /* debug output picture */
108 greg 2.1 char *progname;
109    
110     extern char *malloc();
111    
112    
113     main(argc, argv)
114     int argc;
115     char **argv;
116     {
117 greg 2.7 int inpispic = 1;
118 greg 2.1 int i;
119    
120     progname = argv[0];
121 greg 2.7 for (i = 1; i < argc && argv[i][0] == '-'; i++)
122     switch (argv[i][1]) {
123     case 'd': /* debug output */
124     i++;
125     if (badarg(argc-i, argv+i, "s"))
126     goto userr;
127     if ((debugfp = fopen(argv[i], "w")) == NULL) {
128     perror(argv[i]);
129     exit(1);
130     }
131 greg 2.1 #ifdef MSDOS
132 greg 2.7 setmode(fileno(debugfp), O_BINARY);
133 greg 2.1 #endif
134 greg 2.7 newheader("RADIANCE", debugfp); /* start */
135     printargs(argc, argv, debugfp); /* header */
136     break;
137     case 'p': /* picture position */
138     if (badarg(argc-i-1, argv+i+1, "iiiiiiii"))
139     goto userr;
140     bounds[0][0] = atoi(argv[++i]);
141     bounds[0][1] = atoi(argv[++i]);
142     bounds[1][0] = atoi(argv[++i]);
143     bounds[1][1] = atoi(argv[++i]);
144     bounds[2][0] = atoi(argv[++i]);
145     bounds[2][1] = atoi(argv[++i]);
146     bounds[3][0] = atoi(argv[++i]);
147     bounds[3][1] = atoi(argv[++i]);
148     inpispic = 2;
149     break;
150     case 'c': /* color input */
151     inpispic = 0;
152     break;
153     default:
154     goto userr;
155     }
156     /* open files */
157     if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
158 greg 2.1 perror(argv[1]);
159     exit(1);
160     }
161 greg 2.7 if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
162 greg 2.1 perror(argv[2]);
163     exit(1);
164     }
165 greg 2.7 if (inpispic) { /* load input picture header */
166 greg 2.1 #ifdef MSDOS
167 greg 2.7 setmode(fileno(stdin), O_BINARY);
168 greg 2.1 #endif
169 greg 2.7 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
170     fgetresolu(&xmax, &ymax, stdin) < 0) {
171     fprintf(stderr, "%s: bad input picture\n", progname);
172     exit(1);
173     }
174     } else { /* else set default xmax and ymax */
175     xmax = 512;
176     ymax = 2*512/3;
177 greg 2.1 }
178 greg 2.7 if (inpispic != 2) { /* use default boundaries */
179 greg 2.1 bounds[0][0] = bounds[2][0] = .029*xmax + .5;
180     bounds[0][1] = bounds[1][1] = .956*ymax + .5;
181     bounds[1][0] = bounds[3][0] = .971*xmax + .5;
182     bounds[2][1] = bounds[3][1] = .056*ymax + .5;
183     }
184     init(); /* initialize */
185 greg 2.7 if (inpispic) /* get picture colors */
186     getpicture();
187     else
188     getcolors();
189 greg 2.2 compute(); /* compute color mapping */
190 greg 2.4 /* print comment */
191 greg 2.7 printf("{ Color correction file computed by:\n\t");
192     printargs(argc, argv, stdout);
193     printf("}\n");
194 greg 2.1 putmapping(); /* put out color mapping */
195 greg 2.7 if (debugfp != NULL) /* put out debug picture */
196     if (inpispic)
197     picdebug();
198     else
199     clrdebug();
200 greg 2.1 exit(0);
201     userr:
202 greg 2.7 fprintf(stderr,
203     "Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n",
204 greg 2.1 progname);
205 greg 2.7 fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n",
206     progname);
207 greg 2.1 exit(1);
208     }
209    
210    
211     init() /* initialize */
212     {
213     double quad[4][2];
214 greg 2.7 register int i;
215 greg 2.1 /* make coordinate transformation */
216     quad[0][0] = bounds[0][0];
217     quad[0][1] = bounds[0][1];
218     quad[1][0] = bounds[1][0];
219     quad[1][1] = bounds[1][1];
220     quad[2][0] = bounds[3][0];
221     quad[2][1] = bounds[3][1];
222     quad[3][0] = bounds[2][0];
223     quad[3][1] = bounds[2][1];
224    
225     if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) {
226     fprintf(stderr, "%s: bad chart boundaries\n", progname);
227     exit(1);
228     }
229 greg 2.7 /* map MacBeth colors to RGB space */
230     for (i = 0; i < 24; i++)
231     xyY2RGB(mbRGB[i], mbxyY[i]);
232 greg 2.1 }
233    
234    
235     int
236 greg 2.7 chartndx(x, y, cvg) /* find color number for position */
237 greg 2.1 int x, y;
238 greg 2.7 double cvg;
239 greg 2.1 {
240     double ipos[3], cpos[3];
241     int ix, iy;
242     double fx, fy;
243 greg 2.7 double cmin, cmax;
244 greg 2.1
245     ipos[0] = x;
246     ipos[1] = y;
247     ipos[2] = 1;
248     mx3d_transform(ipos, imgxfm, cpos);
249     cpos[0] /= cpos[2];
250     cpos[1] /= cpos[2];
251     if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
252     return(-1);
253     ix = cpos[0];
254     iy = cpos[1];
255     fx = cpos[0] - ix;
256     fy = cpos[1] - iy;
257 greg 2.7 cmin = .5*(1.-cvg);
258     cmax = 1. - cmin;
259     if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax)
260 greg 2.1 return(-1);
261     return(iy*6 + ix);
262     }
263    
264    
265 greg 2.7 getpicture() /* load in picture colors */
266 greg 2.1 {
267     COLR *scanln;
268     COLOR pval;
269     int ccount[24];
270     double d;
271     int y;
272     register int x, i;
273    
274     scanln = (COLR *)malloc(xmax*sizeof(COLR));
275     if (scanln == NULL) {
276     perror(progname);
277     exit(1);
278     }
279     for (i = 0; i < 24; i++) {
280 greg 2.7 setcolor(inpRGB[i], 0., 0., 0.);
281 greg 2.1 ccount[i] = 0;
282     }
283     for (y = ymax-1; y >= 0; y--) {
284     if (freadcolrs(scanln, xmax, stdin) < 0) {
285     fprintf(stderr, "%s: error reading input picture\n",
286     progname);
287     exit(1);
288     }
289     for (x = 0; x < xmax; x++) {
290 greg 2.7 i = chartndx(x, y, CENTCVG);
291 greg 2.1 if (i >= 0) {
292     colr_color(pval, scanln[x]);
293 greg 2.7 addcolor(inpRGB[i], pval);
294 greg 2.1 ccount[i]++;
295     }
296     }
297     }
298 greg 2.7 for (i = 0; i < 24; i++) { /* compute averages */
299     if (ccount[i] == 0)
300     continue;
301     d = 1./ccount[i];
302     scalecolor(inpRGB[i], d);
303     inpflags |= 1L<<i;
304     }
305     free((char *)scanln);
306     }
307    
308    
309     getcolors() /* get xyY colors from standard input */
310     {
311     int gotwhite = 0;
312     COLOR whiteclr;
313     int n;
314     float xyYin[3];
315    
316     while (fgetval(stdin, 'i', &n) == 1) { /* read colors */
317     if (n < 0 | n > 24 ||
318     fgetval(stdin, 'f', &xyYin[0]) != 1 ||
319     fgetval(stdin, 'f', &xyYin[1]) != 1 ||
320     fgetval(stdin, 'f', &xyYin[2]) != 1 ||
321     xyYin[0] < 0. | xyYin[0] > 1. |
322     xyYin[1] < 0. | xyYin[1] > 1.) {
323     fprintf(stderr, "%s: bad color input data\n",
324 greg 2.1 progname);
325     exit(1);
326     }
327 greg 2.7 if (n == 0) { /* calibration white */
328     xyY2RGB(whiteclr, xyYin);
329     gotwhite++;
330     } else { /* standard color */
331     n--;
332     xyY2RGB(inpRGB[n], xyYin);
333     inpflags |= 1L<<n;
334     }
335 greg 2.1 }
336 greg 2.7 /* normalize colors */
337     if (!gotwhite) {
338     if (!(inpflags & 1L<<White)) {
339     fprintf(stderr, "%s: missing input for White\n",
340     progname);
341     exit(1);
342     }
343     setcolor(whiteclr,
344     colval(inpRGB[White],RED)/colval(mbRGB[White],RED),
345     colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN),
346     colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU));
347     }
348     for (n = 0; n < 24; n++)
349     if (inpflags & 1L<<n)
350     setcolor(inpRGB[n],
351     colval(inpRGB[n],RED)/colval(whiteclr,RED),
352     colval(inpRGB[n],GRN)/colval(whiteclr,GRN),
353     colval(inpRGB[n],BLU)/colval(whiteclr,BLU));
354 greg 2.1 }
355    
356    
357 greg 2.2 bresp(y, x) /* piecewise linear interpolation of primaries */
358     COLOR y, x;
359 greg 2.1 {
360 greg 2.2 register int i, n;
361 greg 2.1
362 greg 2.2 for (i = 0; i < 3; i++) {
363 greg 2.5 for (n = 0; n < NMBNEU-2; n++)
364     if (colval(x,i) < colval(bramp[n+1][0],i))
365     break;
366 greg 2.8 colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
367 greg 2.2 colval(bramp[n][1],i) +
368     (colval(x,i) - colval(bramp[n][0],i)) *
369     colval(bramp[n+1][1],i)) /
370     (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
371     }
372 greg 2.1 }
373    
374    
375 greg 2.2 compute() /* compute color mapping */
376 greg 2.1 {
377 greg 2.8 COLOR clrin[24], clrout[24];
378     long cflags;
379     COLOR ctmp;
380     register int i, j, n;
381 greg 2.7 /* did we get what we need? */
382     if ((inpflags & REQFLGS) != REQFLGS) {
383     fprintf(stderr, "%s: missing required input colors\n",
384     progname);
385     exit(1);
386 greg 2.2 }
387 greg 2.1 /* compute piecewise luminance curve */
388     for (i = 0; i < NMBNEU; i++) {
389 greg 2.7 copycolor(bramp[i][0], inpRGB[mbneu[i]]);
390 greg 2.2 copycolor(bramp[i][1], mbRGB[mbneu[i]]);
391 greg 2.1 }
392 greg 2.8 /* compute color mapping */
393     do {
394     cflags = inpflags & ~gmtflags;
395     n = 0; /* compute transform matrix */
396     for (i = 0; i < 24; i++)
397     if (cflags & 1L<<i) {
398     bresp(clrin[n], inpRGB[i]);
399     copycolor(clrout[n], mbRGB[i]);
400     n++;
401     }
402     compsoln(clrin, clrout, n);
403     /* check out-of-gamut colors */
404     for (i = 0; i < 24; i++)
405     if (cflags & 1L<<i) {
406     cresp(ctmp, mbRGB[i]);
407     for (j = 0; j < 3; j++)
408     if (colval(ctmp,j) <= 0. ||
409     colval(ctmp,j) >= 1.) {
410     gmtflags |= 1L<<i;
411     break;
412     }
413     }
414     } while (cflags & gmtflags);
415     if (gmtflags & MODFLGS)
416     fprintf(stderr,
417     "%s: warning - some moderate colors are out of gamut\n",
418     progname);
419 greg 2.2 }
420    
421    
422     putmapping() /* put out color mapping for pcomb -f */
423     {
424     static char cchar[3] = {'r', 'g', 'b'};
425     register int i, j;
426 greg 2.1 /* print brightness mapping */
427 greg 2.2 for (j = 0; j < 3; j++) {
428     printf("%cxa(i) : select(i", cchar[j]);
429     for (i = 0; i < NMBNEU; i++)
430     printf(",%g", colval(bramp[i][0],j));
431     printf(");\n");
432     printf("%cya(i) : select(i", cchar[j]);
433     for (i = 0; i < NMBNEU; i++)
434     printf(",%g", colval(bramp[i][1],j));
435     printf(");\n");
436     printf("%c = %ci(1);\n", cchar[j], cchar[j]);
437     printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
438     cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
439     cchar[j], cchar[j]);
440     printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
441     printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
442     cchar[j], cchar[j], cchar[j],
443     cchar[j], cchar[j], cchar[j]);
444     printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
445     cchar[j], cchar[j], cchar[j],
446     cchar[j], cchar[j]);
447     printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n",
448     cchar[j], cchar[j], cchar[j], cchar[j]);
449     }
450 greg 2.1 /* print color mapping */
451 greg 2.2 printf("ro = %g*rn + %g*gn + %g*bn ;\n",
452 greg 2.3 solmat[0][0], solmat[0][1], solmat[0][2]);
453 greg 2.2 printf("go = %g*rn + %g*gn + %g*bn ;\n",
454 greg 2.3 solmat[1][0], solmat[1][1], solmat[1][2]);
455 greg 2.2 printf("bo = %g*rn + %g*gn + %g*bn ;\n",
456 greg 2.3 solmat[2][0], solmat[2][1], solmat[2][2]);
457 greg 2.1 }
458    
459    
460 greg 2.4 compsoln(cin, cout, n) /* solve 3xN system using least-squares */
461 greg 2.2 COLOR cin[], cout[];
462 greg 2.1 int n;
463     {
464     extern double mx3d_adjoint(), fabs();
465     double mat[3][3], invmat[3][3];
466     double det;
467     double colv[3], rowv[3];
468 greg 2.4 register int i, j, k;
469 greg 2.1
470 greg 2.8 if (n < 3) {
471     fprintf(stderr, "%s: too few colors to match!\n", progname);
472 greg 2.1 exit(1);
473     }
474 greg 2.4 if (n == 3)
475     for (i = 0; i < 3; i++)
476     for (j = 0; j < 3; j++)
477     mat[i][j] = colval(cin[j],i);
478     else { /* compute A^t A */
479     for (i = 0; i < 3; i++)
480     for (j = i; j < 3; j++) {
481     mat[i][j] = 0.;
482     for (k = 0; k < n; k++)
483     mat[i][j] += colval(cin[k],i) *
484     colval(cin[k],j);
485     }
486     for (i = 1; i < 3; i++) /* using symmetry */
487     for (j = 0; j < i; j++)
488     mat[i][j] = mat[j][i];
489     }
490 greg 2.1 det = mx3d_adjoint(mat, invmat);
491     if (fabs(det) < 1e-4) {
492     fprintf(stderr, "%s: cannot compute color mapping\n",
493     progname);
494     solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.;
495     solmat[0][1] = solmat[0][2] = solmat[1][0] =
496     solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.;
497     return;
498     }
499     for (i = 0; i < 3; i++)
500     for (j = 0; j < 3; j++)
501     invmat[i][j] /= det;
502     for (i = 0; i < 3; i++) {
503 greg 2.4 if (n == 3)
504     for (j = 0; j < 3; j++)
505     colv[j] = colval(cout[j],i);
506     else
507     for (j = 0; j < 3; j++) {
508     colv[j] = 0.;
509     for (k = 0; k < n; k++)
510     colv[j] += colval(cout[k],i) *
511     colval(cin[k],j);
512     }
513 greg 2.3 mx3d_transform(colv, invmat, rowv);
514 greg 2.1 for (j = 0; j < 3; j++)
515 greg 2.3 solmat[i][j] = rowv[j];
516 greg 2.1 }
517     }
518    
519 greg 2.3
520 greg 2.1 cvtcolor(cout, cin) /* convert color according to our mapping */
521     COLOR cout, cin;
522     {
523 greg 2.8 COLOR ctmp;
524    
525     bresp(ctmp, cin);
526     cresp(cout, ctmp);
527     if (colval(cout,RED) < 0.)
528     colval(cout,RED) = 0.;
529     if (colval(cout,GRN) < 0.)
530     colval(cout,GRN) = 0.;
531     if (colval(cout,BLU) < 0.)
532     colval(cout,BLU) = 0.;
533     }
534    
535    
536     cresp(cout, cin) /* transform color according to matrix */
537     COLOR cout, cin;
538     {
539 greg 2.1 double r, g, b;
540    
541 greg 2.8 r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1]
542     + colval(cin,2)*solmat[0][2];
543     g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1]
544     + colval(cin,2)*solmat[1][2];
545     b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1]
546     + colval(cin,2)*solmat[2][2];
547 greg 2.2 setcolor(cout, r, g, b);
548 greg 2.1 }
549    
550    
551 greg 2.7 xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */
552     COLOR rgbout;
553     register float xyYin[3];
554 greg 2.1 {
555 greg 2.7 COLOR ctmp;
556     double d;
557    
558     d = xyYin[2] / xyYin[1];
559     ctmp[0] = xyYin[0] * d;
560     ctmp[1] = xyYin[2];
561     ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
562     cie_rgb(rgbout, ctmp);
563     }
564    
565    
566     picdebug() /* put out debugging picture */
567     {
568 greg 2.8 static COLOR blkcol = BLKCOLOR;
569 greg 2.1 COLOR *scan;
570     int y;
571     register int x, i;
572    
573     if (fseek(stdin, 0L, 0) == EOF) {
574     fprintf(stderr, "%s: cannot seek on input picture\n", progname);
575     exit(1);
576     }
577     getheader(stdin, NULL, NULL); /* skip input header */
578     fgetresolu(&xmax, &ymax, stdin);
579     /* allocate scanline */
580     scan = (COLOR *)malloc(xmax*sizeof(COLOR));
581     if (scan == NULL) {
582     perror(progname);
583     exit(1);
584     }
585     /* finish debug header */
586 greg 2.5 fputformat(COLRFMT, debugfp);
587 greg 2.1 putc('\n', debugfp);
588     fprtresolu(xmax, ymax, debugfp);
589 greg 2.7 /* write debug picture */
590 greg 2.1 for (y = ymax-1; y >= 0; y--) {
591     if (freadscan(scan, xmax, stdin) < 0) {
592     fprintf(stderr, "%s: error rereading input picture\n",
593     progname);
594     exit(1);
595     }
596     for (x = 0; x < xmax; x++) {
597 greg 2.7 i = chartndx(x, y, CENTCVG);
598 greg 2.1 if (i < 0)
599     cvtcolor(scan[x], scan[x]);
600 greg 2.8 else if (!(1L<<i & gmtflags) || (x+y)&07)
601     copycolor(scan[x], mbRGB[i]);
602 greg 2.1 else
603 greg 2.8 copycolor(scan[x], blkcol);
604 greg 2.1 }
605     if (fwritescan(scan, xmax, debugfp) < 0) {
606     fprintf(stderr, "%s: error writing debugging picture\n",
607     progname);
608     exit(1);
609     }
610     }
611 greg 2.7 /* clean up */
612     fclose(debugfp);
613     free((char *)scan);
614     }
615    
616    
617     clrdebug() /* put out debug picture from color input */
618     {
619     static COLR blkclr = BLKCOLR;
620     COLR mbclr[24], cvclr[24];
621     COLR *scan;
622     COLOR ctmp;
623     int y;
624     register int i, x;
625     /* convert colors */
626     for (i = 0; i < 24; i++) {
627     setcolr(mbclr[i], colval(mbRGB[i],RED),
628     colval(mbRGB[i],GRN), colval(mbRGB[i],BLU));
629     if (inpflags & 1L<<i) {
630     cvtcolor(ctmp, inpRGB[i]);
631     setcolr(cvclr[i], colval(ctmp,RED),
632     colval(ctmp,GRN), colval(ctmp,BLU));
633     }
634     }
635     /* allocate scanline */
636     scan = (COLR *)malloc(xmax*sizeof(COLR));
637     if (scan == NULL) {
638     perror(progname);
639     exit(1);
640     }
641     /* finish debug header */
642     fputformat(COLRFMT, debugfp);
643     putc('\n', debugfp);
644     fprtresolu(xmax, ymax, debugfp);
645     /* write debug picture */
646     for (y = ymax-1; y >= 0; y--) {
647     for (x = 0; x < xmax; x++)
648 greg 2.8 if ((i = chartndx(x, y, CENTCVG)) >= 0) {
649     if (!(1L<<i & gmtflags) || (x+y)&07)
650     copycolr(scan[x], mbclr[i]);
651     else
652     copycolr(scan[x], blkclr);
653     } else if ((i = chartndx(x, y, FULLCVG)) >= 0 &&
654 greg 2.7 inpflags & 1L<<i)
655     copycolr(scan[x], cvclr[i]);
656     else
657     copycolr(scan[x], blkclr);
658     if (fwritecolrs(scan, xmax, debugfp) < 0) {
659     fprintf(stderr, "%s: error writing debugging picture\n",
660     progname);
661     exit(1);
662     }
663     }
664     /* clean up */
665     fclose(debugfp);
666 greg 2.1 free((char *)scan);
667     }