1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Calibrate a scanned MacBeth Color Checker Chart |
6 |
|
* |
7 |
< |
* Produce a .cal file suitable for use with pcomb. |
7 |
> |
* Produce a .cal file suitable for use with pcomb, |
8 |
> |
* or .cwp file suitable for use with pcwarp. |
9 |
> |
* |
10 |
> |
* Warping code depends on conformance of COLOR and W3VEC types. |
11 |
|
*/ |
12 |
|
|
13 |
|
#include <stdio.h> |
14 |
< |
#ifdef MSDOS |
15 |
< |
#include <fcntl.h> |
16 |
< |
#endif |
14 |
> |
#include <math.h> |
15 |
> |
#include <time.h> |
16 |
> |
|
17 |
> |
#include "platform.h" |
18 |
|
#include "color.h" |
19 |
|
#include "resolu.h" |
20 |
|
#include "pmap.h" |
21 |
+ |
#include "warp3d.h" |
22 |
|
|
23 |
|
/* MacBeth colors */ |
24 |
|
#define DarkSkin 0 |
97 |
|
#define RG_ORIG 02 /* original color region */ |
98 |
|
#define RG_CORR 04 /* corrected color region */ |
99 |
|
|
100 |
+ |
#ifndef DISPCOM |
101 |
+ |
#define DISPCOM "ximage -op %s" |
102 |
+ |
#endif |
103 |
+ |
|
104 |
|
int scanning = 1; /* scanned input (or recorded output)? */ |
105 |
+ |
double irrad = 1.0; /* irradiance multiplication factor */ |
106 |
+ |
int rawmap = 0; /* put out raw color mapping? */ |
107 |
|
|
108 |
|
int xmax, ymax; /* input image dimensions */ |
109 |
|
int bounds[4][2]; /* image coordinates of chart corners */ |
114 |
|
long gmtflags = 0; /* flags of out-of-gamut colors */ |
115 |
|
|
116 |
|
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
117 |
< |
double solmat[3][3]; /* color mapping matrix */ |
117 |
> |
COLORMAT solmat; /* color mapping matrix */ |
118 |
> |
COLOR colmin, colmax; /* gamut limits */ |
119 |
|
|
120 |
+ |
WARP3D *wcor = NULL; /* color space warp */ |
121 |
+ |
|
122 |
|
FILE *debugfp = NULL; /* debug output picture */ |
123 |
|
char *progname; |
124 |
|
|
114 |
– |
extern char *malloc(); |
125 |
|
|
116 |
– |
|
126 |
|
main(argc, argv) |
127 |
|
int argc; |
128 |
|
char **argv; |
140 |
|
perror(argv[i]); |
141 |
|
exit(1); |
142 |
|
} |
143 |
< |
#ifdef MSDOS |
135 |
< |
setmode(fileno(debugfp), O_BINARY); |
136 |
< |
#endif |
143 |
> |
SET_FILE_BINARY(debugfp); |
144 |
|
newheader("RADIANCE", debugfp); /* start */ |
145 |
|
printargs(argc, argv, debugfp); /* header */ |
146 |
|
break; |
157 |
|
bounds[3][1] = atoi(argv[++i]); |
158 |
|
scanning = 2; |
159 |
|
break; |
160 |
+ |
case 'P': /* pick position */ |
161 |
+ |
scanning = 3; |
162 |
+ |
break; |
163 |
+ |
case 'i': /* irradiance factor */ |
164 |
+ |
i++; |
165 |
+ |
if (badarg(argc-i, argv+i, "f")) |
166 |
+ |
goto userr; |
167 |
+ |
irrad = atof(argv[i]); |
168 |
+ |
break; |
169 |
+ |
case 'm': /* raw map output */ |
170 |
+ |
rawmap = 1; |
171 |
+ |
break; |
172 |
|
case 'c': /* color input */ |
173 |
|
scanning = 0; |
174 |
|
break; |
177 |
|
} |
178 |
|
/* open files */ |
179 |
|
if (i < argc && freopen(argv[i], "r", stdin) == NULL) { |
180 |
< |
perror(argv[1]); |
180 |
> |
perror(argv[i]); |
181 |
|
exit(1); |
182 |
|
} |
183 |
|
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
184 |
< |
perror(argv[2]); |
184 |
> |
perror(argv[i+1]); |
185 |
|
exit(1); |
186 |
|
} |
187 |
|
if (scanning) { /* load input picture header */ |
188 |
< |
#ifdef MSDOS |
170 |
< |
setmode(fileno(stdin), O_BINARY); |
171 |
< |
#endif |
188 |
> |
SET_FILE_BINARY(stdin); |
189 |
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
190 |
|
fgetresolu(&xmax, &ymax, stdin) < 0) { |
191 |
|
fprintf(stderr, "%s: bad input picture\n", progname); |
192 |
|
exit(1); |
193 |
|
} |
194 |
+ |
if (scanning == 3) { |
195 |
+ |
if (i >= argc) |
196 |
+ |
goto userr; |
197 |
+ |
pickchartpos(argv[i]); |
198 |
+ |
scanning = 2; |
199 |
+ |
} |
200 |
|
} else { /* else set default xmax and ymax */ |
201 |
|
xmax = 512; |
202 |
|
ymax = 2*512/3; |
213 |
|
else |
214 |
|
getcolors(); |
215 |
|
compute(); /* compute color mapping */ |
216 |
< |
/* print comment */ |
217 |
< |
printf("{\n\tColor correction file computed by:\n\t\t"); |
218 |
< |
printargs(argc, argv, stdout); |
219 |
< |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
220 |
< |
i+1 < argc ? argv[i+1] : "{this_file}"); |
221 |
< |
if (!scanning) |
199 |
< |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
216 |
> |
if (rawmap) { /* print out raw correspondence */ |
217 |
> |
register int j; |
218 |
> |
|
219 |
> |
printf("# Color correspondence produced by:\n#\t\t"); |
220 |
> |
printargs(argc, argv, stdout); |
221 |
> |
printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", |
222 |
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
223 |
< |
printf("}\n"); |
224 |
< |
putmapping(); /* put out color mapping */ |
225 |
< |
if (debugfp != NULL) /* put out debug picture */ |
223 |
> |
printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n", |
224 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
225 |
> |
for (j = 0; j < 24; j++) |
226 |
> |
printf("%f %f %f %f %f %f\n", |
227 |
> |
colval(inpRGB[j],RED), colval(inpRGB[j],GRN), |
228 |
> |
colval(inpRGB[j],BLU), colval(mbRGB[j],RED), |
229 |
> |
colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); |
230 |
> |
if (scanning && debugfp != NULL) |
231 |
> |
cwarp(); /* color warp for debugging */ |
232 |
> |
} else { /* print color mapping */ |
233 |
> |
/* print header */ |
234 |
> |
printf("{\n\tColor correction file computed by:\n\t\t"); |
235 |
> |
printargs(argc, argv, stdout); |
236 |
> |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
237 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
238 |
> |
if (!scanning) |
239 |
> |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
240 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
241 |
> |
printf("}\n"); |
242 |
> |
putmapping(); /* put out color mapping */ |
243 |
> |
} |
244 |
> |
if (debugfp != NULL) { /* put out debug picture */ |
245 |
|
if (scanning) |
246 |
|
picdebug(); |
247 |
|
else |
248 |
|
clrdebug(); |
249 |
+ |
} |
250 |
|
exit(0); |
251 |
|
userr: |
252 |
|
fprintf(stderr, |
253 |
< |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
253 |
> |
"Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", |
254 |
|
progname); |
255 |
< |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
255 |
> |
fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", |
256 |
|
progname); |
257 |
|
exit(1); |
258 |
|
} |
277 |
|
exit(1); |
278 |
|
} |
279 |
|
/* map MacBeth colors to RGB space */ |
280 |
< |
for (i = 0; i < 24; i++) |
280 |
> |
for (i = 0; i < 24; i++) { |
281 |
|
xyY2RGB(mbRGB[i], mbxyY[i]); |
282 |
+ |
scalecolor(mbRGB[i], irrad); |
283 |
+ |
} |
284 |
|
} |
285 |
|
|
286 |
|
|
354 |
|
scalecolor(inpRGB[i], d); |
355 |
|
inpflags |= 1L<<i; |
356 |
|
} |
357 |
< |
free((char *)scanln); |
357 |
> |
free((void *)scanln); |
358 |
|
} |
359 |
|
|
360 |
|
|
366 |
|
float xyYin[3]; |
367 |
|
|
368 |
|
while (fgetval(stdin, 'i', &n) == 1) { /* read colors */ |
369 |
< |
if (n < 0 | n > 24 || |
369 |
> |
if ((n < 0) | (n > 24) || |
370 |
|
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
371 |
|
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
372 |
|
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
373 |
< |
xyYin[0] < 0. | xyYin[1] < 0. || |
373 |
> |
(xyYin[0] < 0.) | (xyYin[1] < 0.) || |
374 |
|
xyYin[0] + xyYin[1] > 1.) { |
375 |
|
fprintf(stderr, "%s: bad color input data\n", |
376 |
|
progname); |
429 |
|
COLOR clrin[24], clrout[24]; |
430 |
|
long cflags; |
431 |
|
COLOR ctmp; |
432 |
< |
register int i, j, n; |
432 |
> |
register int i, n; |
433 |
|
/* did we get what we need? */ |
434 |
|
if ((inpflags & REQFLGS) != REQFLGS) { |
435 |
|
fprintf(stderr, "%s: missing required input colors\n", |
441 |
|
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
442 |
|
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
443 |
|
} |
444 |
+ |
/* compute color space gamut */ |
445 |
+ |
if (scanning) { |
446 |
+ |
copycolor(colmin, cblack); |
447 |
+ |
copycolor(colmax, cwhite); |
448 |
+ |
scalecolor(colmax, irrad); |
449 |
+ |
} else |
450 |
+ |
for (i = 0; i < 3; i++) { |
451 |
+ |
colval(colmin,i) = colval(bramp[0][0],i) - |
452 |
+ |
colval(bramp[0][1],i) * |
453 |
+ |
(colval(bramp[1][0],i)-colval(bramp[0][0],i)) / |
454 |
+ |
(colval(bramp[1][1],i)-colval(bramp[1][0],i)); |
455 |
+ |
colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) + |
456 |
+ |
(1.-colval(bramp[NMBNEU-2][1],i)) * |
457 |
+ |
(colval(bramp[NMBNEU-1][0],i) - |
458 |
+ |
colval(bramp[NMBNEU-2][0],i)) / |
459 |
+ |
(colval(bramp[NMBNEU-1][1],i) - |
460 |
+ |
colval(bramp[NMBNEU-2][1],i)); |
461 |
+ |
} |
462 |
|
/* compute color mapping */ |
463 |
|
do { |
464 |
|
cflags = inpflags & ~gmtflags; |
470 |
|
n++; |
471 |
|
} |
472 |
|
compsoln(clrin, clrout, n); |
473 |
< |
/* check out-of-gamut colors */ |
474 |
< |
for (i = 0; i < 24; i++) |
475 |
< |
if (cflags & 1L<<i) { |
476 |
< |
cvtcolor(ctmp, mbRGB[i]); |
415 |
< |
for (j = 0; j < 3; j++) |
416 |
< |
if (colval(ctmp,j) <= 1e-6 || |
417 |
< |
colval(ctmp,j) >= 1.-1e-6) { |
418 |
< |
gmtflags |= 1L<<i; |
419 |
< |
break; |
420 |
< |
} |
421 |
< |
} |
473 |
> |
if (irrad > 0.99 && irrad < 1.01) /* check gamut */ |
474 |
> |
for (i = 0; i < 24; i++) |
475 |
> |
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
476 |
> |
gmtflags |= 1L<<i; |
477 |
|
} while (cflags & gmtflags); |
478 |
|
if (gmtflags & MODFLGS) |
479 |
|
fprintf(stderr, |
482 |
|
} |
483 |
|
|
484 |
|
|
485 |
< |
putmapping() /* put out color mapping for pcomb -f */ |
485 |
> |
putmapping() /* put out color mapping */ |
486 |
|
{ |
487 |
|
static char cchar[3] = {'r', 'g', 'b'}; |
488 |
|
register int i, j; |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
+ |
cwarp() /* compute color warp map */ |
594 |
+ |
{ |
595 |
+ |
register int i; |
596 |
+ |
|
597 |
+ |
if ((wcor = new3dw(W3EXACT)) == NULL) |
598 |
+ |
goto memerr; |
599 |
+ |
for (i = 0; i < 24; i++) |
600 |
+ |
if (!add3dpt(wcor, inpRGB[i], mbRGB[i])) |
601 |
+ |
goto memerr; |
602 |
+ |
return; |
603 |
+ |
memerr: |
604 |
+ |
perror(progname); |
605 |
+ |
exit(1); |
606 |
+ |
} |
607 |
+ |
|
608 |
+ |
|
609 |
+ |
int |
610 |
|
cvtcolor(cout, cin) /* convert color according to our mapping */ |
611 |
|
COLOR cout, cin; |
612 |
|
{ |
613 |
|
COLOR ctmp; |
614 |
+ |
int clipped; |
615 |
|
|
616 |
< |
if (scanning) { |
616 |
> |
if (wcor != NULL) { |
617 |
> |
clipped = warp3d(cout, cin, wcor); |
618 |
> |
clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax); |
619 |
> |
} else if (scanning) { |
620 |
|
bresp(ctmp, cin); |
621 |
< |
cresp(cout, ctmp); |
621 |
> |
clipped = cresp(cout, ctmp); |
622 |
|
} else { |
623 |
< |
cresp(ctmp, cin); |
623 |
> |
clipped = cresp(ctmp, cin); |
624 |
|
bresp(cout, ctmp); |
625 |
|
} |
626 |
< |
if (colval(cout,RED) < 0.) |
551 |
< |
colval(cout,RED) = 0.; |
552 |
< |
if (colval(cout,GRN) < 0.) |
553 |
< |
colval(cout,GRN) = 0.; |
554 |
< |
if (colval(cout,BLU) < 0.) |
555 |
< |
colval(cout,BLU) = 0.; |
626 |
> |
return(clipped); |
627 |
|
} |
628 |
|
|
629 |
|
|
630 |
+ |
int |
631 |
|
cresp(cout, cin) /* transform color according to matrix */ |
632 |
|
COLOR cout, cin; |
633 |
|
{ |
634 |
< |
double r, g, b; |
635 |
< |
|
564 |
< |
r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1] |
565 |
< |
+ colval(cin,2)*solmat[0][2]; |
566 |
< |
g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1] |
567 |
< |
+ colval(cin,2)*solmat[1][2]; |
568 |
< |
b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1] |
569 |
< |
+ colval(cin,2)*solmat[2][2]; |
570 |
< |
setcolor(cout, r, g, b); |
634 |
> |
colortrans(cout, solmat, cin); |
635 |
> |
return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax)); |
636 |
|
} |
637 |
|
|
638 |
|
|
647 |
|
ctmp[0] = xyYin[0] * d; |
648 |
|
ctmp[1] = xyYin[2]; |
649 |
|
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
585 |
– |
cie_rgb(rgbout, ctmp); |
650 |
|
/* allow negative values */ |
651 |
< |
colortrans(rgbout, xyz2rgbmat, ctmp, 0); |
651 |
> |
colortrans(rgbout, xyz2rgbmat, ctmp); |
652 |
|
} |
653 |
|
|
654 |
|
|
685 |
|
for (x = 0; x < xmax; x++) { |
686 |
|
rg = chartndx(x, y, &i); |
687 |
|
if (rg == RG_CENT) { |
688 |
< |
if (!(1L<<i & gmtflags) || (x+y)&07) |
688 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) { |
689 |
|
copycolor(scan[x], mbRGB[i]); |
690 |
< |
else |
690 |
> |
clipgamut(scan[x], bright(scan[x]), |
691 |
> |
CGAMUT, colmin, colmax); |
692 |
> |
} else |
693 |
|
copycolor(scan[x], blkcol); |
694 |
|
} else if (rg == RG_CORR) |
695 |
|
cvtcolor(scan[x], scan[x]); |
704 |
|
} |
705 |
|
/* clean up */ |
706 |
|
fclose(debugfp); |
707 |
< |
free((char *)scan); |
707 |
> |
free((void *)scan); |
708 |
|
} |
709 |
|
|
710 |
|
|
713 |
|
static COLR blkclr = BLKCOLR; |
714 |
|
COLR mbclr[24], cvclr[24], orclr[24]; |
715 |
|
COLR *scan; |
716 |
< |
COLOR ctmp; |
716 |
> |
COLOR ctmp, ct2; |
717 |
|
int y, i; |
718 |
|
register int x, rg; |
719 |
|
/* convert colors */ |
720 |
|
for (i = 0; i < 24; i++) { |
721 |
< |
setcolr(mbclr[i], colval(mbRGB[i],RED), |
722 |
< |
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
721 |
> |
copycolor(ctmp, mbRGB[i]); |
722 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
723 |
> |
setcolr(mbclr[i], colval(ctmp,RED), |
724 |
> |
colval(ctmp,GRN), colval(ctmp,BLU)); |
725 |
|
if (inpflags & 1L<<i) { |
726 |
< |
setcolr(orclr[i], colval(inpRGB[i],RED), |
727 |
< |
colval(inpRGB[i],GRN), |
728 |
< |
colval(inpRGB[i],BLU)); |
661 |
< |
cvtcolor(ctmp, inpRGB[i]); |
662 |
< |
setcolr(cvclr[i], colval(ctmp,RED), |
726 |
> |
copycolor(ctmp, inpRGB[i]); |
727 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
728 |
> |
setcolr(orclr[i], colval(ctmp,RED), |
729 |
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
730 |
+ |
if (rawmap) |
731 |
+ |
copycolr(cvclr[i], mbclr[i]); |
732 |
+ |
else { |
733 |
+ |
bresp(ctmp, inpRGB[i]); |
734 |
+ |
colortrans(ct2, solmat, ctmp); |
735 |
+ |
clipgamut(ct2, bright(ct2), CGAMUT, |
736 |
+ |
cblack, cwhite); |
737 |
+ |
setcolr(cvclr[i], colval(ct2,RED), |
738 |
+ |
colval(ct2,GRN), |
739 |
+ |
colval(ct2,BLU)); |
740 |
+ |
} |
741 |
|
} |
742 |
|
} |
743 |
|
/* allocate scanline */ |
774 |
|
} |
775 |
|
/* clean up */ |
776 |
|
fclose(debugfp); |
777 |
< |
free((char *)scan); |
777 |
> |
free((void *)scan); |
778 |
> |
} |
779 |
> |
|
780 |
> |
|
781 |
> |
getpos(name, bnds, fp) /* get boundary position */ |
782 |
> |
char *name; |
783 |
> |
int bnds[2]; |
784 |
> |
FILE *fp; |
785 |
> |
{ |
786 |
> |
char buf[64]; |
787 |
> |
|
788 |
> |
fprintf(stderr, "\tSelect corner: %s\n", name); |
789 |
> |
if (fgets(buf, sizeof(buf), fp) == NULL || |
790 |
> |
sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) { |
791 |
> |
fprintf(stderr, "%s: read error from display process\n", |
792 |
> |
progname); |
793 |
> |
exit(1); |
794 |
> |
} |
795 |
> |
} |
796 |
> |
|
797 |
> |
|
798 |
> |
pickchartpos(pfn) /* display picture and pick chart location */ |
799 |
> |
char *pfn; |
800 |
> |
{ |
801 |
> |
char combuf[512]; |
802 |
> |
FILE *pfp; |
803 |
> |
|
804 |
> |
sprintf(combuf, DISPCOM, pfn); |
805 |
> |
if ((pfp = popen(combuf, "r")) == NULL) { |
806 |
> |
perror(combuf); |
807 |
> |
exit(1); |
808 |
> |
} |
809 |
> |
fputs("Use middle mouse button to select chart corners:\n", stderr); |
810 |
> |
getpos("upper left (dark skin)", bounds[0], pfp); |
811 |
> |
getpos("upper right (bluish green)", bounds[1], pfp); |
812 |
> |
getpos("lower left (white)", bounds[2], pfp); |
813 |
> |
getpos("lower right (black)", bounds[3], pfp); |
814 |
> |
fputs("Got it -- quit display program.\n", stderr); |
815 |
> |
pclose(pfp); |
816 |
|
} |