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 |
+ |
#include <math.h> |
15 |
|
#ifdef MSDOS |
16 |
|
#include <fcntl.h> |
17 |
|
#endif |
18 |
+ |
#include <time.h> |
19 |
|
#include "color.h" |
20 |
|
#include "resolu.h" |
21 |
|
#include "pmap.h" |
22 |
+ |
#include "warp3d.h" |
23 |
|
|
24 |
|
/* MacBeth colors */ |
25 |
|
#define DarkSkin 0 |
98 |
|
#define RG_ORIG 02 /* original color region */ |
99 |
|
#define RG_CORR 04 /* corrected color region */ |
100 |
|
|
101 |
+ |
#ifndef DISPCOM |
102 |
+ |
#define DISPCOM "ximage -op %s" |
103 |
+ |
#endif |
104 |
+ |
|
105 |
|
int scanning = 1; /* scanned input (or recorded output)? */ |
106 |
+ |
double irrad = 1.0; /* irradiance multiplication factor */ |
107 |
+ |
int rawmap = 0; /* put out raw color mapping? */ |
108 |
|
|
109 |
|
int xmax, ymax; /* input image dimensions */ |
110 |
|
int bounds[4][2]; /* image coordinates of chart corners */ |
118 |
|
COLORMAT solmat; /* color mapping matrix */ |
119 |
|
COLOR colmin, colmax; /* gamut limits */ |
120 |
|
|
121 |
+ |
WARP3D *wcor = NULL; /* color space warp */ |
122 |
+ |
|
123 |
|
FILE *debugfp = NULL; /* debug output picture */ |
124 |
|
char *progname; |
125 |
|
|
115 |
– |
extern char *malloc(); |
126 |
|
|
117 |
– |
|
127 |
|
main(argc, argv) |
128 |
|
int argc; |
129 |
|
char **argv; |
160 |
|
bounds[3][1] = atoi(argv[++i]); |
161 |
|
scanning = 2; |
162 |
|
break; |
163 |
+ |
case 'P': /* pick position */ |
164 |
+ |
scanning = 3; |
165 |
+ |
break; |
166 |
+ |
case 'i': /* irradiance factor */ |
167 |
+ |
i++; |
168 |
+ |
if (badarg(argc-i, argv+i, "f")) |
169 |
+ |
goto userr; |
170 |
+ |
irrad = atof(argv[i]); |
171 |
+ |
break; |
172 |
+ |
case 'm': /* raw map output */ |
173 |
+ |
rawmap = 1; |
174 |
+ |
break; |
175 |
|
case 'c': /* color input */ |
176 |
|
scanning = 0; |
177 |
|
break; |
196 |
|
fprintf(stderr, "%s: bad input picture\n", progname); |
197 |
|
exit(1); |
198 |
|
} |
199 |
+ |
if (scanning == 3) { |
200 |
+ |
if (i >= argc) |
201 |
+ |
goto userr; |
202 |
+ |
pickchartpos(argv[i]); |
203 |
+ |
scanning = 2; |
204 |
+ |
} |
205 |
|
} else { /* else set default xmax and ymax */ |
206 |
|
xmax = 512; |
207 |
|
ymax = 2*512/3; |
218 |
|
else |
219 |
|
getcolors(); |
220 |
|
compute(); /* compute color mapping */ |
221 |
< |
/* print comment */ |
222 |
< |
printf("{\n\tColor correction file computed by:\n\t\t"); |
223 |
< |
printargs(argc, argv, stdout); |
224 |
< |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
225 |
< |
i+1 < argc ? argv[i+1] : "{this_file}"); |
226 |
< |
if (!scanning) |
200 |
< |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
221 |
> |
if (rawmap) { /* print out raw correspondence */ |
222 |
> |
register int j; |
223 |
> |
|
224 |
> |
printf("# Color correspondence produced by:\n#\t\t"); |
225 |
> |
printargs(argc, argv, stdout); |
226 |
> |
printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", |
227 |
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
228 |
< |
printf("}\n"); |
229 |
< |
putmapping(); /* put out color mapping */ |
228 |
> |
printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n", |
229 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
230 |
> |
for (j = 0; j < 24; j++) |
231 |
> |
printf("%f %f %f %f %f %f\n", |
232 |
> |
colval(inpRGB[j],RED), colval(inpRGB[j],GRN), |
233 |
> |
colval(inpRGB[j],BLU), colval(mbRGB[j],RED), |
234 |
> |
colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); |
235 |
> |
if (scanning && debugfp != NULL) |
236 |
> |
cwarp(); /* color warp for debugging */ |
237 |
> |
} else { /* print color mapping */ |
238 |
> |
/* print header */ |
239 |
> |
printf("{\n\tColor correction file computed by:\n\t\t"); |
240 |
> |
printargs(argc, argv, stdout); |
241 |
> |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
242 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
243 |
> |
if (!scanning) |
244 |
> |
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
245 |
> |
i+1 < argc ? argv[i+1] : "{this_file}"); |
246 |
> |
printf("}\n"); |
247 |
> |
putmapping(); /* put out color mapping */ |
248 |
> |
} |
249 |
|
if (debugfp != NULL) /* put out debug picture */ |
250 |
|
if (scanning) |
251 |
|
picdebug(); |
254 |
|
exit(0); |
255 |
|
userr: |
256 |
|
fprintf(stderr, |
257 |
< |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
257 |
> |
"Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", |
258 |
|
progname); |
259 |
< |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
259 |
> |
fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", |
260 |
|
progname); |
261 |
|
exit(1); |
262 |
|
} |
281 |
|
exit(1); |
282 |
|
} |
283 |
|
/* map MacBeth colors to RGB space */ |
284 |
< |
for (i = 0; i < 24; i++) |
284 |
> |
for (i = 0; i < 24; i++) { |
285 |
|
xyY2RGB(mbRGB[i], mbxyY[i]); |
286 |
+ |
scalecolor(mbRGB[i], irrad); |
287 |
+ |
} |
288 |
|
} |
289 |
|
|
290 |
|
|
358 |
|
scalecolor(inpRGB[i], d); |
359 |
|
inpflags |= 1L<<i; |
360 |
|
} |
361 |
< |
free((char *)scanln); |
361 |
> |
free((void *)scanln); |
362 |
|
} |
363 |
|
|
364 |
|
|
449 |
|
if (scanning) { |
450 |
|
copycolor(colmin, cblack); |
451 |
|
copycolor(colmax, cwhite); |
452 |
+ |
scalecolor(colmax, irrad); |
453 |
|
} else |
454 |
|
for (i = 0; i < 3; i++) { |
455 |
|
colval(colmin,i) = colval(bramp[0][0],i) - |
474 |
|
n++; |
475 |
|
} |
476 |
|
compsoln(clrin, clrout, n); |
477 |
< |
/* check out-of-gamut colors */ |
478 |
< |
for (i = 0; i < 24; i++) |
479 |
< |
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
480 |
< |
gmtflags |= 1L<<i; |
477 |
> |
if (irrad > 0.99 && irrad < 1.01) /* check gamut */ |
478 |
> |
for (i = 0; i < 24; i++) |
479 |
> |
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
480 |
> |
gmtflags |= 1L<<i; |
481 |
|
} while (cflags & gmtflags); |
482 |
|
if (gmtflags & MODFLGS) |
483 |
|
fprintf(stderr, |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
< |
putmapping() /* put out color mapping for pcomb -f */ |
489 |
> |
putmapping() /* put out color mapping */ |
490 |
|
{ |
491 |
|
static char cchar[3] = {'r', 'g', 'b'}; |
492 |
|
register int i, j; |
594 |
|
} |
595 |
|
|
596 |
|
|
597 |
+ |
cwarp() /* compute color warp map */ |
598 |
+ |
{ |
599 |
+ |
register int i; |
600 |
+ |
|
601 |
+ |
if ((wcor = new3dw(W3EXACT)) == NULL) |
602 |
+ |
goto memerr; |
603 |
+ |
for (i = 0; i < 24; i++) |
604 |
+ |
if (!add3dpt(wcor, inpRGB[i], mbRGB[i])) |
605 |
+ |
goto memerr; |
606 |
+ |
return; |
607 |
+ |
memerr: |
608 |
+ |
perror(progname); |
609 |
+ |
exit(1); |
610 |
+ |
} |
611 |
+ |
|
612 |
+ |
|
613 |
|
int |
614 |
|
cvtcolor(cout, cin) /* convert color according to our mapping */ |
615 |
|
COLOR cout, cin; |
617 |
|
COLOR ctmp; |
618 |
|
int clipped; |
619 |
|
|
620 |
< |
if (scanning) { |
620 |
> |
if (wcor != NULL) { |
621 |
> |
clipped = warp3d(cout, cin, wcor); |
622 |
> |
clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax); |
623 |
> |
} else if (scanning) { |
624 |
|
bresp(ctmp, cin); |
625 |
|
clipped = cresp(cout, ctmp); |
626 |
|
} else { |
692 |
|
if (!(1L<<i & gmtflags) || (x+y)&07) { |
693 |
|
copycolor(scan[x], mbRGB[i]); |
694 |
|
clipgamut(scan[x], bright(scan[x]), |
695 |
< |
CGAMUT, cblack, cwhite); |
695 |
> |
CGAMUT, colmin, colmax); |
696 |
|
} else |
697 |
|
copycolor(scan[x], blkcol); |
698 |
|
} else if (rg == RG_CORR) |
708 |
|
} |
709 |
|
/* clean up */ |
710 |
|
fclose(debugfp); |
711 |
< |
free((char *)scan); |
711 |
> |
free((void *)scan); |
712 |
|
} |
713 |
|
|
714 |
|
|
731 |
|
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
732 |
|
setcolr(orclr[i], colval(ctmp,RED), |
733 |
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
734 |
< |
bresp(ctmp, inpRGB[i]); |
735 |
< |
colortrans(ct2, solmat, ctmp); |
736 |
< |
clipgamut(ct2, bright(ct2), CGAMUT, cblack, cwhite); |
737 |
< |
setcolr(cvclr[i], colval(ct2,RED), |
738 |
< |
colval(ct2,GRN), colval(ct2,BLU)); |
734 |
> |
if (rawmap) |
735 |
> |
copycolr(cvclr[i], mbclr[i]); |
736 |
> |
else { |
737 |
> |
bresp(ctmp, inpRGB[i]); |
738 |
> |
colortrans(ct2, solmat, ctmp); |
739 |
> |
clipgamut(ct2, bright(ct2), CGAMUT, |
740 |
> |
cblack, cwhite); |
741 |
> |
setcolr(cvclr[i], colval(ct2,RED), |
742 |
> |
colval(ct2,GRN), |
743 |
> |
colval(ct2,BLU)); |
744 |
> |
} |
745 |
|
} |
746 |
|
} |
747 |
|
/* allocate scanline */ |
778 |
|
} |
779 |
|
/* clean up */ |
780 |
|
fclose(debugfp); |
781 |
< |
free((char *)scan); |
781 |
> |
free((void *)scan); |
782 |
> |
} |
783 |
> |
|
784 |
> |
|
785 |
> |
getpos(name, bnds, fp) /* get boundary position */ |
786 |
> |
char *name; |
787 |
> |
int bnds[2]; |
788 |
> |
FILE *fp; |
789 |
> |
{ |
790 |
> |
char buf[64]; |
791 |
> |
|
792 |
> |
fprintf(stderr, "\tSelect corner: %s\n", name); |
793 |
> |
if (fgets(buf, sizeof(buf), fp) == NULL || |
794 |
> |
sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) { |
795 |
> |
fprintf(stderr, "%s: read error from display process\n", |
796 |
> |
progname); |
797 |
> |
exit(1); |
798 |
> |
} |
799 |
> |
} |
800 |
> |
|
801 |
> |
|
802 |
> |
pickchartpos(pfn) /* display picture and pick chart location */ |
803 |
> |
char *pfn; |
804 |
> |
{ |
805 |
> |
char combuf[512]; |
806 |
> |
FILE *pfp; |
807 |
> |
|
808 |
> |
sprintf(combuf, DISPCOM, pfn); |
809 |
> |
if ((pfp = popen(combuf, "r")) == NULL) { |
810 |
> |
perror(combuf); |
811 |
> |
exit(1); |
812 |
> |
} |
813 |
> |
fputs("Use middle mouse button to select chart corners:\n", stderr); |
814 |
> |
getpos("upper left (dark skin)", bounds[0], pfp); |
815 |
> |
getpos("upper right (bluish green)", bounds[1], pfp); |
816 |
> |
getpos("lower left (white)", bounds[2], pfp); |
817 |
> |
getpos("lower right (black)", bounds[3], pfp); |
818 |
> |
fputs("Got it -- quit display program.\n", stderr); |
819 |
> |
pclose(pfp); |
820 |
|
} |