--- ray/src/px/macbethcal.c 1995/10/23 12:20:03 2.9 +++ ray/src/px/macbethcal.c 2003/10/27 10:24:51 2.21 @@ -1,22 +1,25 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: macbethcal.c,v 2.21 2003/10/27 10:24:51 schorsch Exp $"; #endif - /* * 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 -#ifdef MSDOS -#include -#endif +#include +#include + +#include "platform.h" +#include "rtprocess.h" #include "color.h" #include "resolu.h" #include "pmap.h" +#include "warp3d.h" /* MacBeth colors */ #define DarkSkin 0 @@ -95,6 +98,14 @@ short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutra #define RG_ORIG 02 /* original color region */ #define RG_CORR 04 /* corrected color region */ +#ifndef DISPCOM +#define DISPCOM "ximage -op %s" +#endif + +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,19 +115,19 @@ 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; -extern char *malloc(); - main(argc, argv) int argc; char **argv; { - int inpispic = 1; int i; progname = argv[0]; @@ -130,9 +141,7 @@ char **argv; perror(argv[i]); exit(1); } -#ifdef MSDOS - setmode(fileno(debugfp), O_BINARY); -#endif + SET_FILE_BINARY(debugfp); newheader("RADIANCE", debugfp); /* start */ printargs(argc, argv, debugfp); /* header */ break; @@ -147,64 +156,104 @@ 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 'P': /* pick position */ + scanning = 3; + 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 */ -#ifdef MSDOS - setmode(fileno(stdin), O_BINARY); -#endif + if (scanning) { /* load input picture header */ + SET_FILE_BINARY(stdin); if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) { fprintf(stderr, "%s: bad input picture\n", progname); exit(1); } + if (scanning == 3) { + if (i >= argc) + goto userr; + pickchartpos(argv[i]); + scanning = 2; + } } else { /* else set default xmax and ymax */ 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("{ Color correction file computed by:\n\t"); - printargs(argc, argv, stdout); - printf("}\n"); - putmapping(); /* put out color mapping */ - if (debugfp != NULL) /* put out debug picture */ - if (inpispic) + 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 (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 | -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); } @@ -229,8 +278,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); + } } @@ -304,7 +355,7 @@ getpicture() /* load in picture colors */ scalecolor(inpRGB[i], d); inpflags |= 1L< 24 || + if ((n < 0) | (n > 24) || 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); @@ -379,7 +430,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", @@ -391,6 +442,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; @@ -402,17 +471,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<