| 1 |
< |
/* Copyright (c) 1995 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 |
| 82 |
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
| 83 |
|
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
| 84 |
|
|
| 85 |
< |
#define NMBMOD 16 /* Number of MacBeth unsaturated colors */ |
| 86 |
< |
short mbmod[NMBMOD] = { |
| 82 |
< |
DarkSkin,LightSkin,BlueSky,Foliage,BlueFlower,BluishGreen, |
| 83 |
< |
PurplishBlue,ModerateRed,YellowGreen,OrangeYellow, |
| 84 |
< |
Black,Neutral35,Neutral5,Neutral65,Neutral8,White |
| 85 |
< |
}; |
| 85 |
> |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
| 86 |
> |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
| 87 |
|
|
| 88 |
< |
#define NMBSAT 8 /* Number of MacBeth saturated colors */ |
| 89 |
< |
short mbsat[NMBSAT] = { |
| 90 |
< |
Red,Green,Blue,Magenta,Yellow,Cyan, |
| 90 |
< |
Orange,Purple |
| 91 |
< |
}; |
| 88 |
> |
#define SATFLGS (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \ |
| 89 |
> |
1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \ |
| 90 |
> |
1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower) |
| 91 |
|
|
| 92 |
< |
#define REQFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
| 93 |
< |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
| 92 |
> |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
| 93 |
> |
1L<<BluishGreen|1L<<ModerateRed) |
| 94 |
|
|
| 95 |
< |
#define CENTCVG 0.3 /* measured coverage of square sample */ |
| 96 |
< |
#define FULLCVG 0.9 /* coverage of entire square */ |
| 95 |
> |
#define REQFLGS NEUFLGS /* need these colors */ |
| 96 |
> |
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
| 97 |
|
|
| 98 |
+ |
#define RG_BORD 0 /* patch border */ |
| 99 |
+ |
#define RG_CENT 01 /* central region of patch */ |
| 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 */ |
| 113 |
|
double imgxfm[3][3]; /* coordinate transformation matrix */ |
| 114 |
|
|
| 115 |
|
COLOR inpRGB[24]; /* measured or scanned input colors */ |
| 116 |
|
long inpflags = 0; /* flags of which colors were input */ |
| 117 |
+ |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
| 118 |
|
|
| 119 |
|
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
| 120 |
< |
double solmat[3][3]; /* color mapping matrix */ |
| 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 |
|
|
| 132 |
|
int argc; |
| 133 |
|
char **argv; |
| 134 |
|
{ |
| 119 |
– |
int inpispic = 1; |
| 135 |
|
int i; |
| 136 |
|
|
| 137 |
|
progname = argv[0]; |
| 162 |
|
bounds[2][1] = atoi(argv[++i]); |
| 163 |
|
bounds[3][0] = atoi(argv[++i]); |
| 164 |
|
bounds[3][1] = atoi(argv[++i]); |
| 165 |
< |
inpispic = 2; |
| 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 |
< |
inpispic = 0; |
| 180 |
> |
scanning = 0; |
| 181 |
|
break; |
| 182 |
|
default: |
| 183 |
|
goto userr; |
| 184 |
|
} |
| 185 |
|
/* open files */ |
| 186 |
|
if (i < argc && freopen(argv[i], "r", stdin) == NULL) { |
| 187 |
< |
perror(argv[1]); |
| 187 |
> |
perror(argv[i]); |
| 188 |
|
exit(1); |
| 189 |
|
} |
| 190 |
|
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
| 191 |
< |
perror(argv[2]); |
| 191 |
> |
perror(argv[i+1]); |
| 192 |
|
exit(1); |
| 193 |
|
} |
| 194 |
< |
if (inpispic) { /* load input picture header */ |
| 194 |
> |
if (scanning) { /* load input picture header */ |
| 195 |
|
#ifdef MSDOS |
| 196 |
|
setmode(fileno(stdin), O_BINARY); |
| 197 |
|
#endif |
| 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; |
| 212 |
|
} |
| 213 |
< |
if (inpispic != 2) { /* use default boundaries */ |
| 213 |
> |
if (scanning != 2) { /* use default boundaries */ |
| 214 |
|
bounds[0][0] = bounds[2][0] = .029*xmax + .5; |
| 215 |
|
bounds[0][1] = bounds[1][1] = .956*ymax + .5; |
| 216 |
|
bounds[1][0] = bounds[3][0] = .971*xmax + .5; |
| 217 |
|
bounds[2][1] = bounds[3][1] = .056*ymax + .5; |
| 218 |
|
} |
| 219 |
|
init(); /* initialize */ |
| 220 |
< |
if (inpispic) /* get picture colors */ |
| 220 |
> |
if (scanning) /* get picture colors */ |
| 221 |
|
getpicture(); |
| 222 |
|
else |
| 223 |
|
getcolors(); |
| 224 |
|
compute(); /* compute color mapping */ |
| 225 |
< |
/* print comment */ |
| 226 |
< |
printf("{ Color correction file computed by:\n\t"); |
| 227 |
< |
printargs(argc, argv, stdout); |
| 228 |
< |
printf("}\n"); |
| 229 |
< |
putmapping(); /* put out color mapping */ |
| 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("#\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 (inpispic) |
| 254 |
> |
if (scanning) |
| 255 |
|
picdebug(); |
| 256 |
|
else |
| 257 |
|
clrdebug(); |
| 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 |
|
|
| 295 |
|
int |
| 296 |
< |
chartndx(x, y, cvg) /* find color number for position */ |
| 296 |
> |
chartndx(x, y, np) /* find color number for position */ |
| 297 |
|
int x, y; |
| 298 |
< |
double cvg; |
| 298 |
> |
int *np; |
| 299 |
|
{ |
| 300 |
|
double ipos[3], cpos[3]; |
| 301 |
|
int ix, iy; |
| 302 |
|
double fx, fy; |
| 245 |
– |
double cmin, cmax; |
| 303 |
|
|
| 304 |
|
ipos[0] = x; |
| 305 |
|
ipos[1] = y; |
| 308 |
|
cpos[0] /= cpos[2]; |
| 309 |
|
cpos[1] /= cpos[2]; |
| 310 |
|
if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) |
| 311 |
< |
return(-1); |
| 311 |
> |
return(RG_BORD); |
| 312 |
|
ix = cpos[0]; |
| 313 |
|
iy = cpos[1]; |
| 314 |
|
fx = cpos[0] - ix; |
| 315 |
|
fy = cpos[1] - iy; |
| 316 |
< |
cmin = .5*(1.-cvg); |
| 317 |
< |
cmax = 1. - cmin; |
| 318 |
< |
if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax) |
| 319 |
< |
return(-1); |
| 320 |
< |
return(iy*6 + ix); |
| 316 |
> |
*np = iy*6 + ix; |
| 317 |
> |
if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) |
| 318 |
> |
return(RG_CENT); |
| 319 |
> |
if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) |
| 320 |
> |
return(RG_BORD); |
| 321 |
> |
if (fx >= 0.5) /* right side is corrected */ |
| 322 |
> |
return(RG_CORR); |
| 323 |
> |
return(RG_ORIG); /* left side is original */ |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
|
| 330 |
|
COLOR pval; |
| 331 |
|
int ccount[24]; |
| 332 |
|
double d; |
| 333 |
< |
int y; |
| 334 |
< |
register int x, i; |
| 333 |
> |
int y, i; |
| 334 |
> |
register int x; |
| 335 |
|
|
| 336 |
|
scanln = (COLR *)malloc(xmax*sizeof(COLR)); |
| 337 |
|
if (scanln == NULL) { |
| 348 |
|
progname); |
| 349 |
|
exit(1); |
| 350 |
|
} |
| 351 |
< |
for (x = 0; x < xmax; x++) { |
| 352 |
< |
i = chartndx(x, y, CENTCVG); |
| 293 |
< |
if (i >= 0) { |
| 351 |
> |
for (x = 0; x < xmax; x++) |
| 352 |
> |
if (chartndx(x, y, &i) == RG_CENT) { |
| 353 |
|
colr_color(pval, scanln[x]); |
| 354 |
|
addcolor(inpRGB[i], pval); |
| 355 |
|
ccount[i]++; |
| 356 |
|
} |
| 298 |
– |
} |
| 357 |
|
} |
| 358 |
|
for (i = 0; i < 24; i++) { /* compute averages */ |
| 359 |
|
if (ccount[i] == 0) |
| 378 |
|
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
| 379 |
|
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
| 380 |
|
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
| 381 |
< |
xyYin[0] < 0. | xyYin[0] > 1. | |
| 382 |
< |
xyYin[1] < 0. | xyYin[1] > 1.) { |
| 381 |
> |
xyYin[0] < 0. | xyYin[1] < 0. || |
| 382 |
> |
xyYin[0] + xyYin[1] > 1.) { |
| 383 |
|
fprintf(stderr, "%s: bad color input data\n", |
| 384 |
|
progname); |
| 385 |
|
exit(1); |
| 417 |
|
bresp(y, x) /* piecewise linear interpolation of primaries */ |
| 418 |
|
COLOR y, x; |
| 419 |
|
{ |
| 362 |
– |
double cv[3]; |
| 420 |
|
register int i, n; |
| 421 |
|
|
| 422 |
|
for (i = 0; i < 3; i++) { |
| 423 |
|
for (n = 0; n < NMBNEU-2; n++) |
| 424 |
|
if (colval(x,i) < colval(bramp[n+1][0],i)) |
| 425 |
|
break; |
| 426 |
< |
cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
| 426 |
> |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
| 427 |
|
colval(bramp[n][1],i) + |
| 428 |
|
(colval(x,i) - colval(bramp[n][0],i)) * |
| 429 |
|
colval(bramp[n+1][1],i)) / |
| 430 |
|
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
| 374 |
– |
if (cv[i] < 0.) cv[i] = 0.; |
| 431 |
|
} |
| 376 |
– |
setcolor(y, cv[0], cv[1], cv[2]); |
| 432 |
|
} |
| 433 |
|
|
| 434 |
|
|
| 435 |
|
compute() /* compute color mapping */ |
| 436 |
|
{ |
| 437 |
< |
COLOR clrin[NMBMOD], clrout[NMBMOD]; |
| 437 |
> |
COLOR clrin[24], clrout[24]; |
| 438 |
> |
long cflags; |
| 439 |
> |
COLOR ctmp; |
| 440 |
|
register int i, n; |
| 441 |
|
/* did we get what we need? */ |
| 442 |
|
if ((inpflags & REQFLGS) != REQFLGS) { |
| 449 |
|
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
| 450 |
|
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
| 451 |
|
} |
| 452 |
< |
/* compute color matrix */ |
| 453 |
< |
for (n = 0, i = 0; i < NMBMOD; i++) |
| 454 |
< |
if (inpflags & 1L<<mbmod[i]) { |
| 455 |
< |
bresp(clrin[n], inpRGB[mbmod[i]]); |
| 456 |
< |
copycolor(clrout[n], mbRGB[mbmod[i]]); |
| 457 |
< |
n++; |
| 452 |
> |
/* compute color space gamut */ |
| 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) - |
| 460 |
> |
colval(bramp[0][1],i) * |
| 461 |
> |
(colval(bramp[1][0],i)-colval(bramp[0][0],i)) / |
| 462 |
> |
(colval(bramp[1][1],i)-colval(bramp[1][0],i)); |
| 463 |
> |
colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) + |
| 464 |
> |
(1.-colval(bramp[NMBNEU-2][1],i)) * |
| 465 |
> |
(colval(bramp[NMBNEU-1][0],i) - |
| 466 |
> |
colval(bramp[NMBNEU-2][0],i)) / |
| 467 |
> |
(colval(bramp[NMBNEU-1][1],i) - |
| 468 |
> |
colval(bramp[NMBNEU-2][1],i)); |
| 469 |
|
} |
| 470 |
< |
compsoln(clrin, clrout, n); |
| 470 |
> |
/* compute color mapping */ |
| 471 |
> |
do { |
| 472 |
> |
cflags = inpflags & ~gmtflags; |
| 473 |
> |
n = 0; /* compute transform matrix */ |
| 474 |
> |
for (i = 0; i < 24; i++) |
| 475 |
> |
if (cflags & 1L<<i) { |
| 476 |
> |
bresp(clrin[n], inpRGB[i]); |
| 477 |
> |
copycolor(clrout[n], mbRGB[i]); |
| 478 |
> |
n++; |
| 479 |
> |
} |
| 480 |
> |
compsoln(clrin, clrout, n); |
| 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, |
| 488 |
> |
"%s: warning - some moderate colors are out of gamut\n", |
| 489 |
> |
progname); |
| 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; |
| 504 |
|
for (i = 0; i < NMBNEU; i++) |
| 505 |
|
printf(",%g", colval(bramp[i][1],j)); |
| 506 |
|
printf(");\n"); |
| 420 |
– |
printf("%c = %ci(1);\n", cchar[j], cchar[j]); |
| 507 |
|
printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", |
| 508 |
|
cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], |
| 509 |
|
cchar[j], cchar[j]); |
| 510 |
|
printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); |
| 511 |
< |
printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
| 512 |
< |
cchar[j], cchar[j], cchar[j], |
| 513 |
< |
cchar[j], cchar[j], cchar[j]); |
| 511 |
> |
printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
| 512 |
> |
cchar[j], scanning?'n':'o', cchar[j], |
| 513 |
> |
cchar[j], cchar[j], cchar[j], cchar[j]); |
| 514 |
|
printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", |
| 515 |
|
cchar[j], cchar[j], cchar[j], |
| 516 |
|
cchar[j], cchar[j]); |
| 518 |
|
cchar[j], cchar[j], cchar[j], cchar[j]); |
| 519 |
|
} |
| 520 |
|
/* print color mapping */ |
| 521 |
< |
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
| 522 |
< |
solmat[0][0], solmat[0][1], solmat[0][2]); |
| 523 |
< |
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
| 524 |
< |
solmat[1][0], solmat[1][1], solmat[1][2]); |
| 525 |
< |
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
| 526 |
< |
solmat[2][0], solmat[2][1], solmat[2][2]); |
| 521 |
> |
if (scanning) { |
| 522 |
> |
printf("r = ri(1); g = gi(1); b = bi(1);\n"); |
| 523 |
> |
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
| 524 |
> |
solmat[0][0], solmat[0][1], solmat[0][2]); |
| 525 |
> |
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
| 526 |
> |
solmat[1][0], solmat[1][1], solmat[1][2]); |
| 527 |
> |
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
| 528 |
> |
solmat[2][0], solmat[2][1], solmat[2][2]); |
| 529 |
> |
} else { |
| 530 |
> |
printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n"); |
| 531 |
> |
printf("r = %g*r1 + %g*g1 + %g*b1 ;\n", |
| 532 |
> |
solmat[0][0], solmat[0][1], solmat[0][2]); |
| 533 |
> |
printf("g = %g*r1 + %g*g1 + %g*b1 ;\n", |
| 534 |
> |
solmat[1][0], solmat[1][1], solmat[1][2]); |
| 535 |
> |
printf("b = %g*r1 + %g*g1 + %g*b1 ;\n", |
| 536 |
> |
solmat[2][0], solmat[2][1], solmat[2][2]); |
| 537 |
> |
} |
| 538 |
|
} |
| 539 |
|
|
| 540 |
|
|
| 548 |
|
double colv[3], rowv[3]; |
| 549 |
|
register int i, j, k; |
| 550 |
|
|
| 551 |
< |
if (n < 3 | n > NMBMOD) { |
| 552 |
< |
fprintf(stderr, "%s: bad number of colors to match\n", progname); |
| 551 |
> |
if (n < 3) { |
| 552 |
> |
fprintf(stderr, "%s: too few colors to match!\n", progname); |
| 553 |
|
exit(1); |
| 554 |
|
} |
| 555 |
|
if (n == 3) |
| 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; |
| 620 |
|
{ |
| 621 |
< |
double r, g, b; |
| 621 |
> |
COLOR ctmp; |
| 622 |
> |
int clipped; |
| 623 |
|
|
| 624 |
< |
bresp(cout, cin); |
| 625 |
< |
r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1] |
| 626 |
< |
+ colval(cout,2)*solmat[0][2]; |
| 627 |
< |
if (r < 0) r = 0; |
| 628 |
< |
g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1] |
| 629 |
< |
+ colval(cout,2)*solmat[1][2]; |
| 630 |
< |
if (g < 0) g = 0; |
| 631 |
< |
b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1] |
| 632 |
< |
+ colval(cout,2)*solmat[2][2]; |
| 633 |
< |
if (b < 0) b = 0; |
| 634 |
< |
setcolor(cout, r, g, b); |
| 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 { |
| 631 |
> |
clipped = cresp(ctmp, cin); |
| 632 |
> |
bresp(cout, ctmp); |
| 633 |
> |
} |
| 634 |
> |
return(clipped); |
| 635 |
|
} |
| 636 |
|
|
| 637 |
|
|
| 638 |
+ |
int |
| 639 |
+ |
cresp(cout, cin) /* transform color according to matrix */ |
| 640 |
+ |
COLOR cout, cin; |
| 641 |
+ |
{ |
| 642 |
+ |
colortrans(cout, solmat, cin); |
| 643 |
+ |
return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax)); |
| 644 |
+ |
} |
| 645 |
+ |
|
| 646 |
+ |
|
| 647 |
|
xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ |
| 648 |
|
COLOR rgbout; |
| 649 |
|
register float xyYin[3]; |
| 655 |
|
ctmp[0] = xyYin[0] * d; |
| 656 |
|
ctmp[1] = xyYin[2]; |
| 657 |
|
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
| 658 |
< |
cie_rgb(rgbout, ctmp); |
| 658 |
> |
/* allow negative values */ |
| 659 |
> |
colortrans(rgbout, xyz2rgbmat, ctmp); |
| 660 |
|
} |
| 661 |
|
|
| 662 |
|
|
| 663 |
|
picdebug() /* put out debugging picture */ |
| 664 |
|
{ |
| 665 |
+ |
static COLOR blkcol = BLKCOLOR; |
| 666 |
|
COLOR *scan; |
| 667 |
< |
int y; |
| 668 |
< |
register int x, i; |
| 667 |
> |
int y, i; |
| 668 |
> |
register int x, rg; |
| 669 |
|
|
| 670 |
|
if (fseek(stdin, 0L, 0) == EOF) { |
| 671 |
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
| 691 |
|
exit(1); |
| 692 |
|
} |
| 693 |
|
for (x = 0; x < xmax; x++) { |
| 694 |
< |
i = chartndx(x, y, CENTCVG); |
| 695 |
< |
if (i < 0) |
| 694 |
> |
rg = chartndx(x, y, &i); |
| 695 |
> |
if (rg == RG_CENT) { |
| 696 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) { |
| 697 |
> |
copycolor(scan[x], mbRGB[i]); |
| 698 |
> |
clipgamut(scan[x], bright(scan[x]), |
| 699 |
> |
CGAMUT, colmin, colmax); |
| 700 |
> |
} else |
| 701 |
> |
copycolor(scan[x], blkcol); |
| 702 |
> |
} else if (rg == RG_CORR) |
| 703 |
|
cvtcolor(scan[x], scan[x]); |
| 704 |
< |
else |
| 705 |
< |
copycolor(scan[x], mbRGB[i]); |
| 704 |
> |
else if (rg != RG_ORIG) |
| 705 |
> |
copycolor(scan[x], blkcol); |
| 706 |
|
} |
| 707 |
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
| 708 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
| 719 |
|
clrdebug() /* put out debug picture from color input */ |
| 720 |
|
{ |
| 721 |
|
static COLR blkclr = BLKCOLR; |
| 722 |
< |
COLR mbclr[24], cvclr[24]; |
| 722 |
> |
COLR mbclr[24], cvclr[24], orclr[24]; |
| 723 |
|
COLR *scan; |
| 724 |
< |
COLOR ctmp; |
| 725 |
< |
int y; |
| 726 |
< |
register int i, x; |
| 724 |
> |
COLOR ctmp, ct2; |
| 725 |
> |
int y, i; |
| 726 |
> |
register int x, rg; |
| 727 |
|
/* convert colors */ |
| 728 |
|
for (i = 0; i < 24; i++) { |
| 729 |
< |
setcolr(mbclr[i], colval(mbRGB[i],RED), |
| 730 |
< |
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
| 729 |
> |
copycolor(ctmp, mbRGB[i]); |
| 730 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
| 731 |
> |
setcolr(mbclr[i], colval(ctmp,RED), |
| 732 |
> |
colval(ctmp,GRN), colval(ctmp,BLU)); |
| 733 |
|
if (inpflags & 1L<<i) { |
| 734 |
< |
cvtcolor(ctmp, inpRGB[i]); |
| 735 |
< |
setcolr(cvclr[i], colval(ctmp,RED), |
| 734 |
> |
copycolor(ctmp, inpRGB[i]); |
| 735 |
> |
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
| 736 |
> |
setcolr(orclr[i], colval(ctmp,RED), |
| 737 |
|
colval(ctmp,GRN), colval(ctmp,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 */ |
| 760 |
|
fprtresolu(xmax, ymax, debugfp); |
| 761 |
|
/* write debug picture */ |
| 762 |
|
for (y = ymax-1; y >= 0; y--) { |
| 763 |
< |
for (x = 0; x < xmax; x++) |
| 764 |
< |
if ((i = chartndx(x, y, CENTCVG)) >= 0) |
| 765 |
< |
copycolr(scan[x], mbclr[i]); |
| 766 |
< |
else if ((i = chartndx(x, y, FULLCVG)) >= 0 && |
| 767 |
< |
inpflags & 1L<<i) |
| 768 |
< |
copycolr(scan[x], cvclr[i]); |
| 769 |
< |
else |
| 763 |
> |
for (x = 0; x < xmax; x++) { |
| 764 |
> |
rg = chartndx(x, y, &i); |
| 765 |
> |
if (rg == RG_CENT) { |
| 766 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) |
| 767 |
> |
copycolr(scan[x], mbclr[i]); |
| 768 |
> |
else |
| 769 |
> |
copycolr(scan[x], blkclr); |
| 770 |
> |
} else if (rg == RG_BORD || !(1L<<i & inpflags)) |
| 771 |
|
copycolr(scan[x], blkclr); |
| 772 |
+ |
else if (rg == RG_ORIG) |
| 773 |
+ |
copycolr(scan[x], orclr[i]); |
| 774 |
+ |
else /* rg == RG_CORR */ |
| 775 |
+ |
copycolr(scan[x], cvclr[i]); |
| 776 |
+ |
} |
| 777 |
|
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
| 778 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
| 779 |
|
progname); |
| 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 |
|
} |