1 |
< |
/* Copyright (c) 1997 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1999 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
8 |
|
* Calibrate a scanned MacBeth Color Checker Chart |
9 |
|
* |
10 |
< |
* Produce a .cal file suitable for use with pcomb. |
10 |
> |
* Produce a .cal file suitable for use with pcomb, |
11 |
> |
* or .cwp file suitable for use with pcwarp. |
12 |
> |
* |
13 |
> |
* Warping code depends on conformance of COLOR and W3VEC types. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include <stdio.h> |
17 |
+ |
#include <math.h> |
18 |
|
#ifdef MSDOS |
19 |
|
#include <fcntl.h> |
20 |
|
#endif |
21 |
|
#include "color.h" |
22 |
|
#include "resolu.h" |
23 |
|
#include "pmap.h" |
24 |
+ |
#include "warp3d.h" |
25 |
|
|
26 |
|
/* MacBeth colors */ |
27 |
|
#define DarkSkin 0 |
100 |
|
#define RG_ORIG 02 /* original color region */ |
101 |
|
#define RG_CORR 04 /* corrected color region */ |
102 |
|
|
103 |
+ |
#ifndef DISPCOM |
104 |
+ |
#define DISPCOM "ximage -op %s" |
105 |
+ |
#endif |
106 |
+ |
|
107 |
|
int scanning = 1; /* scanned input (or recorded output)? */ |
108 |
+ |
double irrad = 1.0; /* irradiance multiplication factor */ |
109 |
+ |
int rawmap = 0; /* put out raw color mapping? */ |
110 |
|
|
111 |
|
int xmax, ymax; /* input image dimensions */ |
112 |
|
int bounds[4][2]; /* image coordinates of chart corners */ |
120 |
|
COLORMAT solmat; /* color mapping matrix */ |
121 |
|
COLOR colmin, colmax; /* gamut limits */ |
122 |
|
|
123 |
+ |
WARP3D *wcor = NULL; /* color space warp */ |
124 |
+ |
|
125 |
|
FILE *debugfp = NULL; /* debug output picture */ |
126 |
|
char *progname; |
127 |
|
|
164 |
|
bounds[3][1] = atoi(argv[++i]); |
165 |
|
scanning = 2; |
166 |
|
break; |
167 |
+ |
case 'P': /* pick position */ |
168 |
+ |
scanning = 3; |
169 |
+ |
break; |
170 |
+ |
case 'i': /* irradiance factor */ |
171 |
+ |
i++; |
172 |
+ |
if (badarg(argc-i, argv+i, "f")) |
173 |
+ |
goto userr; |
174 |
+ |
irrad = atof(argv[i]); |
175 |
+ |
break; |
176 |
+ |
case 'm': /* raw map output */ |
177 |
+ |
rawmap = 1; |
178 |
+ |
break; |
179 |
|
case 'c': /* color input */ |
180 |
|
scanning = 0; |
181 |
|
break; |
200 |
|
fprintf(stderr, "%s: bad input picture\n", progname); |
201 |
|
exit(1); |
202 |
|
} |
203 |
+ |
if (scanning == 3) { |
204 |
+ |
if (i >= argc) |
205 |
+ |
goto userr; |
206 |
+ |
pickchartpos(argv[i]); |
207 |
+ |
scanning = 2; |
208 |
+ |
} |
209 |
|
} else { /* else set default xmax and ymax */ |
210 |
|
xmax = 512; |
211 |
|
ymax = 2*512/3; |
222 |
|
else |
223 |
|
getcolors(); |
224 |
|
compute(); /* compute color mapping */ |
225 |
< |
/* print comment */ |
226 |
< |
printf("{\n\tColor correction file computed by:\n\t\t"); |
227 |
< |
printargs(argc, argv, stdout); |
228 |
< |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
229 |
< |
i+1 < argc ? argv[i+1] : "{this_file}"); |
230 |
< |
if (!scanning) |
200 |
< |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
225 |
> |
if (rawmap) { /* print out raw correspondence */ |
226 |
> |
register int j; |
227 |
> |
|
228 |
> |
printf("# Color correspondence produced by:\n#\t\t"); |
229 |
> |
printargs(argc, argv, stdout); |
230 |
> |
printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", |
231 |
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
232 |
< |
printf("}\n"); |
233 |
< |
putmapping(); /* put out color mapping */ |
232 |
> |
printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n", |
233 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
234 |
> |
for (j = 0; j < 24; j++) |
235 |
> |
printf("%f %f %f %f %f %f\n", |
236 |
> |
colval(inpRGB[j],RED), colval(inpRGB[j],GRN), |
237 |
> |
colval(inpRGB[j],BLU), colval(mbRGB[j],RED), |
238 |
> |
colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); |
239 |
> |
if (scanning && debugfp != NULL) |
240 |
> |
cwarp(); /* color warp for debugging */ |
241 |
> |
} else { /* print color mapping */ |
242 |
> |
/* print header */ |
243 |
> |
printf("{\n\tColor correction file computed by:\n\t\t"); |
244 |
> |
printargs(argc, argv, stdout); |
245 |
> |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
246 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
247 |
> |
if (!scanning) |
248 |
> |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
249 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
250 |
> |
printf("}\n"); |
251 |
> |
putmapping(); /* put out color mapping */ |
252 |
> |
} |
253 |
|
if (debugfp != NULL) /* put out debug picture */ |
254 |
|
if (scanning) |
255 |
|
picdebug(); |
258 |
|
exit(0); |
259 |
|
userr: |
260 |
|
fprintf(stderr, |
261 |
< |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
261 |
> |
"Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", |
262 |
|
progname); |
263 |
< |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
263 |
> |
fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", |
264 |
|
progname); |
265 |
|
exit(1); |
266 |
|
} |
285 |
|
exit(1); |
286 |
|
} |
287 |
|
/* map MacBeth colors to RGB space */ |
288 |
< |
for (i = 0; i < 24; i++) |
288 |
> |
for (i = 0; i < 24; i++) { |
289 |
|
xyY2RGB(mbRGB[i], mbxyY[i]); |
290 |
+ |
scalecolor(mbRGB[i], irrad); |
291 |
+ |
} |
292 |
|
} |
293 |
|
|
294 |
|
|
453 |
|
if (scanning) { |
454 |
|
copycolor(colmin, cblack); |
455 |
|
copycolor(colmax, cwhite); |
456 |
+ |
scalecolor(colmax, irrad); |
457 |
|
} else |
458 |
|
for (i = 0; i < 3; i++) { |
459 |
|
colval(colmin,i) = colval(bramp[0][0],i) - |
478 |
|
n++; |
479 |
|
} |
480 |
|
compsoln(clrin, clrout, n); |
481 |
< |
/* check out-of-gamut colors */ |
482 |
< |
for (i = 0; i < 24; i++) |
483 |
< |
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
484 |
< |
gmtflags |= 1L<<i; |
481 |
> |
if (irrad > 0.99 && irrad < 1.01) /* check gamut */ |
482 |
> |
for (i = 0; i < 24; i++) |
483 |
> |
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
484 |
> |
gmtflags |= 1L<<i; |
485 |
|
} while (cflags & gmtflags); |
486 |
|
if (gmtflags & MODFLGS) |
487 |
|
fprintf(stderr, |
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
< |
putmapping() /* put out color mapping for pcomb -f */ |
493 |
> |
putmapping() /* put out color mapping */ |
494 |
|
{ |
495 |
|
static char cchar[3] = {'r', 'g', 'b'}; |
496 |
|
register int i, j; |
598 |
|
} |
599 |
|
|
600 |
|
|
601 |
+ |
cwarp() /* compute color warp map */ |
602 |
+ |
{ |
603 |
+ |
register int i; |
604 |
+ |
|
605 |
+ |
if ((wcor = new3dw(W3EXACT)) == NULL) |
606 |
+ |
goto memerr; |
607 |
+ |
for (i = 0; i < 24; i++) |
608 |
+ |
if (!add3dpt(wcor, inpRGB[i], mbRGB[i])) |
609 |
+ |
goto memerr; |
610 |
+ |
return; |
611 |
+ |
memerr: |
612 |
+ |
perror(progname); |
613 |
+ |
exit(1); |
614 |
+ |
} |
615 |
+ |
|
616 |
+ |
|
617 |
|
int |
618 |
|
cvtcolor(cout, cin) /* convert color according to our mapping */ |
619 |
|
COLOR cout, cin; |
621 |
|
COLOR ctmp; |
622 |
|
int clipped; |
623 |
|
|
624 |
< |
if (scanning) { |
624 |
> |
if (wcor != NULL) { |
625 |
> |
clipped = warp3d(cout, cin, wcor); |
626 |
> |
clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax); |
627 |
> |
} else if (scanning) { |
628 |
|
bresp(ctmp, cin); |
629 |
|
clipped = cresp(cout, ctmp); |
630 |
|
} else { |
696 |
|
if (!(1L<<i & gmtflags) || (x+y)&07) { |
697 |
|
copycolor(scan[x], mbRGB[i]); |
698 |
|
clipgamut(scan[x], bright(scan[x]), |
699 |
< |
CGAMUT, cblack, cwhite); |
699 |
> |
CGAMUT, colmin, colmax); |
700 |
|
} else |
701 |
|
copycolor(scan[x], blkcol); |
702 |
|
} else if (rg == RG_CORR) |
735 |
|
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
736 |
|
setcolr(orclr[i], colval(ctmp,RED), |
737 |
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
738 |
< |
bresp(ctmp, inpRGB[i]); |
739 |
< |
colortrans(ct2, solmat, ctmp); |
740 |
< |
clipgamut(ct2, bright(ct2), CGAMUT, cblack, cwhite); |
741 |
< |
setcolr(cvclr[i], colval(ct2,RED), |
742 |
< |
colval(ct2,GRN), colval(ct2,BLU)); |
738 |
> |
if (rawmap) |
739 |
> |
copycolr(cvclr[i], mbclr[i]); |
740 |
> |
else { |
741 |
> |
bresp(ctmp, inpRGB[i]); |
742 |
> |
colortrans(ct2, solmat, ctmp); |
743 |
> |
clipgamut(ct2, bright(ct2), CGAMUT, |
744 |
> |
cblack, cwhite); |
745 |
> |
setcolr(cvclr[i], colval(ct2,RED), |
746 |
> |
colval(ct2,GRN), |
747 |
> |
colval(ct2,BLU)); |
748 |
> |
} |
749 |
|
} |
750 |
|
} |
751 |
|
/* allocate scanline */ |
783 |
|
/* clean up */ |
784 |
|
fclose(debugfp); |
785 |
|
free((char *)scan); |
786 |
+ |
} |
787 |
+ |
|
788 |
+ |
|
789 |
+ |
getpos(name, bnds, fp) /* get boundary position */ |
790 |
+ |
char *name; |
791 |
+ |
int bnds[2]; |
792 |
+ |
FILE *fp; |
793 |
+ |
{ |
794 |
+ |
char buf[64]; |
795 |
+ |
|
796 |
+ |
fprintf(stderr, "\tSelect corner: %s\n", name); |
797 |
+ |
if (fgets(buf, sizeof(buf), fp) == NULL || |
798 |
+ |
sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) { |
799 |
+ |
fprintf(stderr, "%s: read error from display process\n", |
800 |
+ |
progname); |
801 |
+ |
exit(1); |
802 |
+ |
} |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
|
806 |
+ |
pickchartpos(pfn) /* display picture and pick chart location */ |
807 |
+ |
char *pfn; |
808 |
+ |
{ |
809 |
+ |
char combuf[512]; |
810 |
+ |
FILE *pfp; |
811 |
+ |
|
812 |
+ |
sprintf(combuf, DISPCOM, pfn); |
813 |
+ |
if ((pfp = popen(combuf, "r")) == NULL) { |
814 |
+ |
perror(combuf); |
815 |
+ |
exit(1); |
816 |
+ |
} |
817 |
+ |
fputs("Use middle mouse button to select chart corners:\n", stderr); |
818 |
+ |
getpos("upper left (dark skin)", bounds[0], pfp); |
819 |
+ |
getpos("upper right (bluish green)", bounds[1], pfp); |
820 |
+ |
getpos("lower left (white)", bounds[2], pfp); |
821 |
+ |
getpos("lower right (black)", bounds[3], pfp); |
822 |
+ |
fputs("Got it -- quit display program.\n", stderr); |
823 |
+ |
pclose(pfp); |
824 |
|
} |