--- ray/src/px/macbethcal.c 1995/11/01 09:04:16 2.10 +++ ray/src/px/macbethcal.c 1997/02/06 11:36:53 2.15 @@ -1,4 +1,4 @@ -/* Copyright (c) 1995 Regents of the University of California */ +/* Copyright (c) 1997 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -7,16 +7,21 @@ static char SCCSid[] = "$SunId$ LBL"; /* * Calibrate a scanned MacBeth Color Checker Chart * - * Produce a .cal file suitable for use with pcomb. + * Produce a .cal file suitable for use with pcomb, + * or .cwp file suitable for use with pcwarp. + * + * Warping code depends on conformance of COLOR and W3VEC types. */ #include +#include #ifdef MSDOS #include #endif #include "color.h" #include "resolu.h" #include "pmap.h" +#include "warp3d.h" /* MacBeth colors */ #define DarkSkin 0 @@ -95,6 +100,10 @@ short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutra #define RG_ORIG 02 /* original color region */ #define RG_CORR 04 /* corrected color region */ +int scanning = 1; /* scanned input (or recorded output)? */ +double irrad = 1.0; /* irradiance multiplication factor */ +int rawmap = 0; /* put out raw color mapping? */ + int xmax, ymax; /* input image dimensions */ int bounds[4][2]; /* image coordinates of chart corners */ double imgxfm[3][3]; /* coordinate transformation matrix */ @@ -104,8 +113,11 @@ long inpflags = 0; /* flags of which colors were inpu long gmtflags = 0; /* flags of out-of-gamut colors */ COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ -double solmat[3][3]; /* color mapping matrix */ +COLORMAT solmat; /* color mapping matrix */ +COLOR colmin, colmax; /* gamut limits */ +WARP3D *wcor = NULL; /* color space warp */ + FILE *debugfp = NULL; /* debug output picture */ char *progname; @@ -116,7 +128,6 @@ main(argc, argv) int argc; char **argv; { - int inpispic = 1; int i; progname = argv[0]; @@ -147,24 +158,33 @@ char **argv; bounds[2][1] = atoi(argv[++i]); bounds[3][0] = atoi(argv[++i]); bounds[3][1] = atoi(argv[++i]); - inpispic = 2; + scanning = 2; break; + case 'i': /* irradiance factor */ + i++; + if (badarg(argc-i, argv+i, "f")) + goto userr; + irrad = atof(argv[i]); + break; + case 'm': /* raw map output */ + rawmap = 1; + break; case 'c': /* color input */ - inpispic = 0; + scanning = 0; break; default: goto userr; } /* open files */ if (i < argc && freopen(argv[i], "r", stdin) == NULL) { - perror(argv[1]); + perror(argv[i]); exit(1); } if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { - perror(argv[2]); + perror(argv[i+1]); exit(1); } - if (inpispic) { /* load input picture header */ + if (scanning) { /* load input picture header */ #ifdef MSDOS setmode(fileno(stdin), O_BINARY); #endif @@ -177,36 +197,57 @@ char **argv; xmax = 512; ymax = 2*512/3; } - if (inpispic != 2) { /* use default boundaries */ + if (scanning != 2) { /* use default boundaries */ bounds[0][0] = bounds[2][0] = .029*xmax + .5; bounds[0][1] = bounds[1][1] = .956*ymax + .5; bounds[1][0] = bounds[3][0] = .971*xmax + .5; bounds[2][1] = bounds[3][1] = .056*ymax + .5; } init(); /* initialize */ - if (inpispic) /* get picture colors */ + if (scanning) /* get picture colors */ getpicture(); else getcolors(); compute(); /* compute color mapping */ - /* print comment */ - printf("{\n\tColor correction file computed by:\n\t\t"); - printargs(argc, argv, stdout); - printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", - i+1 < argc ? argv[i+1] : "{this_file}"); - printf("}\n"); - putmapping(); /* put out color mapping */ + if (rawmap) { /* print out raw correspondence */ + register int j; + + printf("# Color correspondence produced by:\n#\t\t"); + printargs(argc, argv, stdout); + printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", + i+1 < argc ? argv[i+1] : "{this_file}"); + printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n", + i+1 < argc ? argv[i+1] : "{this_file}"); + for (j = 0; j < 24; j++) + printf("%f %f %f %f %f %f\n", + colval(inpRGB[j],RED), colval(inpRGB[j],GRN), + colval(inpRGB[j],BLU), colval(mbRGB[j],RED), + colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); + if (scanning && debugfp != NULL) + cwarp(); /* color warp for debugging */ + } else { /* print color mapping */ + /* print header */ + printf("{\n\tColor correction file computed by:\n\t\t"); + printargs(argc, argv, stdout); + printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", + i+1 < argc ? argv[i+1] : "{this_file}"); + if (!scanning) + printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", + i+1 < argc ? argv[i+1] : "{this_file}"); + printf("}\n"); + putmapping(); /* put out color mapping */ + } if (debugfp != NULL) /* put out debug picture */ - if (inpispic) + if (scanning) picdebug(); else clrdebug(); exit(0); userr: fprintf(stderr, -"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", +"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", progname); - fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", + fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", progname); exit(1); } @@ -231,8 +272,10 @@ init() /* initialize */ exit(1); } /* map MacBeth colors to RGB space */ - for (i = 0; i < 24; i++) + for (i = 0; i < 24; i++) { xyY2RGB(mbRGB[i], mbxyY[i]); + scalecolor(mbRGB[i], irrad); + } } @@ -322,8 +365,8 @@ getcolors() /* get xyY colors from standard input */ fgetval(stdin, 'f', &xyYin[0]) != 1 || fgetval(stdin, 'f', &xyYin[1]) != 1 || fgetval(stdin, 'f', &xyYin[2]) != 1 || - xyYin[0] < 0. | xyYin[0] > 1. | - xyYin[1] < 0. | xyYin[1] > 1.) { + xyYin[0] < 0. | xyYin[1] < 0. || + xyYin[0] + xyYin[1] > 1.) { fprintf(stderr, "%s: bad color input data\n", progname); exit(1); @@ -381,7 +424,7 @@ compute() /* compute color mapping */ COLOR clrin[24], clrout[24]; long cflags; COLOR ctmp; - register int i, j, n; + register int i, n; /* did we get what we need? */ if ((inpflags & REQFLGS) != REQFLGS) { fprintf(stderr, "%s: missing required input colors\n", @@ -393,6 +436,24 @@ compute() /* compute color mapping */ copycolor(bramp[i][0], inpRGB[mbneu[i]]); copycolor(bramp[i][1], mbRGB[mbneu[i]]); } + /* compute color space gamut */ + if (scanning) { + copycolor(colmin, cblack); + copycolor(colmax, cwhite); + scalecolor(colmax, irrad); + } else + for (i = 0; i < 3; i++) { + colval(colmin,i) = colval(bramp[0][0],i) - + colval(bramp[0][1],i) * + (colval(bramp[1][0],i)-colval(bramp[0][0],i)) / + (colval(bramp[1][1],i)-colval(bramp[1][0],i)); + colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) + + (1.-colval(bramp[NMBNEU-2][1],i)) * + (colval(bramp[NMBNEU-1][0],i) - + colval(bramp[NMBNEU-2][0],i)) / + (colval(bramp[NMBNEU-1][1],i) - + colval(bramp[NMBNEU-2][1],i)); + } /* compute color mapping */ do { cflags = inpflags & ~gmtflags; @@ -404,17 +465,10 @@ compute() /* compute color mapping */ n++; } compsoln(clrin, clrout, n); - /* check out-of-gamut colors */ - for (i = 0; i < 24; i++) - if (cflags & 1L<= 1.) { - gmtflags |= 1L< 0.99 && irrad < 1.01) /* check gamut */ + for (i = 0; i < 24; i++) + if (cflags & 1L<