77 |
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
78 |
|
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
79 |
|
|
80 |
< |
#define NMBMOD 16 /* Number of MacBeth unsaturated colors */ |
81 |
< |
short mbmod[NMBMOD] = { |
82 |
< |
DarkSkin,LightSkin,BlueSky,Foliage,BlueFlower,BluishGreen, |
83 |
< |
PurplishBlue,ModerateRed,YellowGreen,OrangeYellow, |
84 |
< |
Black,Neutral35,Neutral5,Neutral65,Neutral8,White |
85 |
< |
}; |
80 |
> |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
81 |
> |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
82 |
|
|
83 |
< |
#define NMBSAT 8 /* Number of MacBeth saturated colors */ |
84 |
< |
short mbsat[NMBSAT] = { |
85 |
< |
Red,Green,Blue,Magenta,Yellow,Cyan, |
90 |
< |
Orange,Purple |
91 |
< |
}; |
83 |
> |
#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 |
|
|
87 |
< |
#define REQFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
88 |
< |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
87 |
> |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
88 |
> |
1L<<BluishGreen|1L<<ModerateRed) |
89 |
|
|
90 |
< |
#define CENTCVG 0.3 /* measured coverage of square sample */ |
91 |
< |
#define FULLCVG 0.9 /* coverage of entire square */ |
90 |
> |
#define REQFLGS NEUFLGS /* need these colors */ |
91 |
> |
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
92 |
|
|
93 |
+ |
#define RG_BORD 0 /* patch border */ |
94 |
+ |
#define RG_CENT 01 /* central region of patch */ |
95 |
+ |
#define RG_ORIG 02 /* original color region */ |
96 |
+ |
#define RG_CORR 04 /* corrected color region */ |
97 |
+ |
|
98 |
|
int xmax, ymax; /* input image dimensions */ |
99 |
|
int bounds[4][2]; /* image coordinates of chart corners */ |
100 |
|
double imgxfm[3][3]; /* coordinate transformation matrix */ |
101 |
|
|
102 |
|
COLOR inpRGB[24]; /* measured or scanned input colors */ |
103 |
|
long inpflags = 0; /* flags of which colors were input */ |
104 |
+ |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
105 |
|
|
106 |
|
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
107 |
|
double solmat[3][3]; /* color mapping matrix */ |
235 |
|
|
236 |
|
|
237 |
|
int |
238 |
< |
chartndx(x, y, cvg) /* find color number for position */ |
238 |
> |
chartndx(x, y, np) /* find color number for position */ |
239 |
|
int x, y; |
240 |
< |
double cvg; |
240 |
> |
int *np; |
241 |
|
{ |
242 |
|
double ipos[3], cpos[3]; |
243 |
|
int ix, iy; |
244 |
|
double fx, fy; |
245 |
– |
double cmin, cmax; |
245 |
|
|
246 |
|
ipos[0] = x; |
247 |
|
ipos[1] = y; |
250 |
|
cpos[0] /= cpos[2]; |
251 |
|
cpos[1] /= cpos[2]; |
252 |
|
if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) |
253 |
< |
return(-1); |
253 |
> |
return(RG_BORD); |
254 |
|
ix = cpos[0]; |
255 |
|
iy = cpos[1]; |
256 |
|
fx = cpos[0] - ix; |
257 |
|
fy = cpos[1] - iy; |
258 |
< |
cmin = .5*(1.-cvg); |
259 |
< |
cmax = 1. - cmin; |
260 |
< |
if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax) |
261 |
< |
return(-1); |
262 |
< |
return(iy*6 + ix); |
258 |
> |
*np = iy*6 + ix; |
259 |
> |
if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) |
260 |
> |
return(RG_CENT); |
261 |
> |
if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) |
262 |
> |
return(RG_BORD); |
263 |
> |
if (fx >= 0.5) /* right side is corrected */ |
264 |
> |
return(RG_CORR); |
265 |
> |
return(RG_ORIG); /* left side is original */ |
266 |
|
} |
267 |
|
|
268 |
|
|
272 |
|
COLOR pval; |
273 |
|
int ccount[24]; |
274 |
|
double d; |
275 |
< |
int y; |
276 |
< |
register int x, i; |
275 |
> |
int y, i; |
276 |
> |
register int x; |
277 |
|
|
278 |
|
scanln = (COLR *)malloc(xmax*sizeof(COLR)); |
279 |
|
if (scanln == NULL) { |
290 |
|
progname); |
291 |
|
exit(1); |
292 |
|
} |
293 |
< |
for (x = 0; x < xmax; x++) { |
294 |
< |
i = chartndx(x, y, CENTCVG); |
293 |
< |
if (i >= 0) { |
293 |
> |
for (x = 0; x < xmax; x++) |
294 |
> |
if (chartndx(x, y, &i) == RG_CENT) { |
295 |
|
colr_color(pval, scanln[x]); |
296 |
|
addcolor(inpRGB[i], pval); |
297 |
|
ccount[i]++; |
298 |
|
} |
298 |
– |
} |
299 |
|
} |
300 |
|
for (i = 0; i < 24; i++) { /* compute averages */ |
301 |
|
if (ccount[i] == 0) |
359 |
|
bresp(y, x) /* piecewise linear interpolation of primaries */ |
360 |
|
COLOR y, x; |
361 |
|
{ |
362 |
– |
double cv[3]; |
362 |
|
register int i, n; |
363 |
|
|
364 |
|
for (i = 0; i < 3; i++) { |
365 |
|
for (n = 0; n < NMBNEU-2; n++) |
366 |
|
if (colval(x,i) < colval(bramp[n+1][0],i)) |
367 |
|
break; |
368 |
< |
cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
368 |
> |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
369 |
|
colval(bramp[n][1],i) + |
370 |
|
(colval(x,i) - colval(bramp[n][0],i)) * |
371 |
|
colval(bramp[n+1][1],i)) / |
372 |
|
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
374 |
– |
if (cv[i] < 0.) cv[i] = 0.; |
373 |
|
} |
376 |
– |
setcolor(y, cv[0], cv[1], cv[2]); |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
compute() /* compute color mapping */ |
378 |
|
{ |
379 |
< |
COLOR clrin[NMBMOD], clrout[NMBMOD]; |
380 |
< |
register int i, n; |
379 |
> |
COLOR clrin[24], clrout[24]; |
380 |
> |
long cflags; |
381 |
> |
COLOR ctmp; |
382 |
> |
register int i, j, n; |
383 |
|
/* did we get what we need? */ |
384 |
|
if ((inpflags & REQFLGS) != REQFLGS) { |
385 |
|
fprintf(stderr, "%s: missing required input colors\n", |
391 |
|
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
392 |
|
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
393 |
|
} |
394 |
< |
/* compute color matrix */ |
395 |
< |
for (n = 0, i = 0; i < NMBMOD; i++) |
396 |
< |
if (inpflags & 1L<<mbmod[i]) { |
397 |
< |
bresp(clrin[n], inpRGB[mbmod[i]]); |
398 |
< |
copycolor(clrout[n], mbRGB[mbmod[i]]); |
399 |
< |
n++; |
400 |
< |
} |
401 |
< |
compsoln(clrin, clrout, n); |
394 |
> |
/* compute color mapping */ |
395 |
> |
do { |
396 |
> |
cflags = inpflags & ~gmtflags; |
397 |
> |
n = 0; /* compute transform matrix */ |
398 |
> |
for (i = 0; i < 24; i++) |
399 |
> |
if (cflags & 1L<<i) { |
400 |
> |
bresp(clrin[n], inpRGB[i]); |
401 |
> |
copycolor(clrout[n], mbRGB[i]); |
402 |
> |
n++; |
403 |
> |
} |
404 |
> |
compsoln(clrin, clrout, n); |
405 |
> |
/* check out-of-gamut colors */ |
406 |
> |
for (i = 0; i < 24; i++) |
407 |
> |
if (cflags & 1L<<i) { |
408 |
> |
cresp(ctmp, mbRGB[i]); |
409 |
> |
for (j = 0; j < 3; j++) |
410 |
> |
if (colval(ctmp,j) <= 0. || |
411 |
> |
colval(ctmp,j) >= 1.) { |
412 |
> |
gmtflags |= 1L<<i; |
413 |
> |
break; |
414 |
> |
} |
415 |
> |
} |
416 |
> |
} while (cflags & gmtflags); |
417 |
> |
if (gmtflags & MODFLGS) |
418 |
> |
fprintf(stderr, |
419 |
> |
"%s: warning - some moderate colors are out of gamut\n", |
420 |
> |
progname); |
421 |
|
} |
422 |
|
|
423 |
|
|
469 |
|
double colv[3], rowv[3]; |
470 |
|
register int i, j, k; |
471 |
|
|
472 |
< |
if (n < 3 | n > NMBMOD) { |
473 |
< |
fprintf(stderr, "%s: bad number of colors to match\n", progname); |
472 |
> |
if (n < 3) { |
473 |
> |
fprintf(stderr, "%s: too few colors to match!\n", progname); |
474 |
|
exit(1); |
475 |
|
} |
476 |
|
if (n == 3) |
522 |
|
cvtcolor(cout, cin) /* convert color according to our mapping */ |
523 |
|
COLOR cout, cin; |
524 |
|
{ |
525 |
+ |
COLOR ctmp; |
526 |
+ |
|
527 |
+ |
bresp(ctmp, cin); |
528 |
+ |
cresp(cout, ctmp); |
529 |
+ |
if (colval(cout,RED) < 0.) |
530 |
+ |
colval(cout,RED) = 0.; |
531 |
+ |
if (colval(cout,GRN) < 0.) |
532 |
+ |
colval(cout,GRN) = 0.; |
533 |
+ |
if (colval(cout,BLU) < 0.) |
534 |
+ |
colval(cout,BLU) = 0.; |
535 |
+ |
} |
536 |
+ |
|
537 |
+ |
|
538 |
+ |
cresp(cout, cin) /* transform color according to matrix */ |
539 |
+ |
COLOR cout, cin; |
540 |
+ |
{ |
541 |
|
double r, g, b; |
542 |
|
|
543 |
< |
bresp(cout, cin); |
544 |
< |
r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1] |
545 |
< |
+ colval(cout,2)*solmat[0][2]; |
546 |
< |
if (r < 0) r = 0; |
547 |
< |
g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1] |
548 |
< |
+ colval(cout,2)*solmat[1][2]; |
515 |
< |
if (g < 0) g = 0; |
516 |
< |
b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1] |
517 |
< |
+ colval(cout,2)*solmat[2][2]; |
518 |
< |
if (b < 0) b = 0; |
543 |
> |
r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1] |
544 |
> |
+ colval(cin,2)*solmat[0][2]; |
545 |
> |
g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1] |
546 |
> |
+ colval(cin,2)*solmat[1][2]; |
547 |
> |
b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1] |
548 |
> |
+ colval(cin,2)*solmat[2][2]; |
549 |
|
setcolor(cout, r, g, b); |
550 |
|
} |
551 |
|
|
567 |
|
|
568 |
|
picdebug() /* put out debugging picture */ |
569 |
|
{ |
570 |
+ |
static COLOR blkcol = BLKCOLOR; |
571 |
|
COLOR *scan; |
572 |
< |
int y; |
573 |
< |
register int x, i; |
572 |
> |
int y, i; |
573 |
> |
register int x, rg; |
574 |
|
|
575 |
|
if (fseek(stdin, 0L, 0) == EOF) { |
576 |
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
596 |
|
exit(1); |
597 |
|
} |
598 |
|
for (x = 0; x < xmax; x++) { |
599 |
< |
i = chartndx(x, y, CENTCVG); |
600 |
< |
if (i < 0) |
599 |
> |
rg = chartndx(x, y, &i); |
600 |
> |
if (rg == RG_CENT) { |
601 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) |
602 |
> |
copycolor(scan[x], mbRGB[i]); |
603 |
> |
else |
604 |
> |
copycolor(scan[x], blkcol); |
605 |
> |
} else if (rg == RG_CORR) |
606 |
|
cvtcolor(scan[x], scan[x]); |
607 |
< |
else |
608 |
< |
copycolor(scan[x], mbRGB[i]); |
607 |
> |
else if (rg != RG_ORIG) |
608 |
> |
copycolor(scan[x], blkcol); |
609 |
|
} |
610 |
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
611 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
622 |
|
clrdebug() /* put out debug picture from color input */ |
623 |
|
{ |
624 |
|
static COLR blkclr = BLKCOLR; |
625 |
< |
COLR mbclr[24], cvclr[24]; |
625 |
> |
COLR mbclr[24], cvclr[24], orclr[24]; |
626 |
|
COLR *scan; |
627 |
|
COLOR ctmp; |
628 |
< |
int y; |
629 |
< |
register int i, x; |
628 |
> |
int y, i; |
629 |
> |
register int x, rg; |
630 |
|
/* convert colors */ |
631 |
|
for (i = 0; i < 24; i++) { |
632 |
|
setcolr(mbclr[i], colval(mbRGB[i],RED), |
633 |
|
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
634 |
|
if (inpflags & 1L<<i) { |
635 |
+ |
setcolr(orclr[i], colval(inpRGB[i],RED), |
636 |
+ |
colval(inpRGB[i],GRN), |
637 |
+ |
colval(inpRGB[i],BLU)); |
638 |
|
cvtcolor(ctmp, inpRGB[i]); |
639 |
|
setcolr(cvclr[i], colval(ctmp,RED), |
640 |
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
652 |
|
fprtresolu(xmax, ymax, debugfp); |
653 |
|
/* write debug picture */ |
654 |
|
for (y = ymax-1; y >= 0; y--) { |
655 |
< |
for (x = 0; x < xmax; x++) |
656 |
< |
if ((i = chartndx(x, y, CENTCVG)) >= 0) |
657 |
< |
copycolr(scan[x], mbclr[i]); |
658 |
< |
else if ((i = chartndx(x, y, FULLCVG)) >= 0 && |
659 |
< |
inpflags & 1L<<i) |
660 |
< |
copycolr(scan[x], cvclr[i]); |
661 |
< |
else |
655 |
> |
for (x = 0; x < xmax; x++) { |
656 |
> |
rg = chartndx(x, y, &i); |
657 |
> |
if (rg == RG_CENT) { |
658 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) |
659 |
> |
copycolr(scan[x], mbclr[i]); |
660 |
> |
else |
661 |
> |
copycolr(scan[x], blkclr); |
662 |
> |
} else if (rg == RG_BORD || !(1L<<i & inpflags)) |
663 |
|
copycolr(scan[x], blkclr); |
664 |
+ |
else if (rg == RG_ORIG) |
665 |
+ |
copycolr(scan[x], orclr[i]); |
666 |
+ |
else /* rg == RG_CORR */ |
667 |
+ |
copycolr(scan[x], cvclr[i]); |
668 |
+ |
} |
669 |
|
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
670 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
671 |
|
progname); |