--- ray/src/px/macbethcal.c 1997/02/06 11:36:53 2.15 +++ ray/src/px/macbethcal.c 2004/12/15 06:30:36 2.24 @@ -1,9 +1,6 @@ -/* Copyright (c) 1997 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: macbethcal.c,v 2.24 2004/12/15 06:30:36 greg Exp $"; #endif - /* * Calibrate a scanned MacBeth Color Checker Chart * @@ -15,13 +12,16 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include -#ifdef MSDOS -#include -#endif +#include + +#include "platform.h" +#include "rtprocess.h" +#include "rtio.h" #include "color.h" #include "resolu.h" #include "pmap.h" #include "warp3d.h" +#include "mx3.h" /* MacBeth colors */ #define DarkSkin 0 @@ -100,6 +100,10 @@ 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? */ @@ -121,12 +125,29 @@ WARP3D *wcor = NULL; /* color space warp */ FILE *debugfp = NULL; /* debug output picture */ char *progname; -extern char *malloc(); +static void init(void); +static int chartndx(int x, int y, int *np); +static void getpicture(void); +static void getcolors(void); +static void bresp(COLOR y, COLOR x); +static void compute(void); +static void putmapping(void); +static void compsoln(COLOR cin[], COLOR cout[], int n); +static void cwarp(void); +static int cvtcolor(COLOR cout, COLOR cin); +static int cresp(COLOR cout, COLOR cin); +static void xyY2RGB(COLOR rgbout, float xyYin[3]); +static void picdebug(void); +static void clrdebug(void); +static void getpos(char *name, int bnds[2], FILE *fp); +static void pickchartpos(char *pfn); -main(argc, argv) -int argc; -char **argv; +int +main( + int argc, + char **argv +) { int i; @@ -141,9 +162,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; @@ -160,6 +179,9 @@ char **argv; bounds[3][1] = atoi(argv[++i]); scanning = 2; break; + case 'P': /* pick position */ + scanning = 3; + break; case 'i': /* irradiance factor */ i++; if (badarg(argc-i, argv+i, "f")) @@ -185,14 +207,18 @@ char **argv; exit(1); } if (scanning) { /* load input picture header */ -#ifdef MSDOS - setmode(fileno(stdin), O_BINARY); -#endif + 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; @@ -237,23 +263,26 @@ char **argv; printf("}\n"); putmapping(); /* put out color mapping */ } - if (debugfp != NULL) /* put out debug picture */ + 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][-i irrad][-m] input.pic [output.{cal|cwp}]\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][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", progname); exit(1); + return 1; /* pro forma return */ } -init() /* initialize */ +static void +init(void) /* initialize */ { double quad[4][2]; register int i; @@ -279,10 +308,12 @@ init() /* initialize */ } -int -chartndx(x, y, np) /* find color number for position */ -int x, y; -int *np; +static int +chartndx( /* find color number for position */ + int x, + int y, + int *np +) { double ipos[3], cpos[3]; int ix, iy; @@ -311,7 +342,8 @@ int *np; } -getpicture() /* load in picture colors */ +static void +getpicture(void) /* load in picture colors */ { COLR *scanln; COLOR pval; @@ -349,11 +381,12 @@ 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[1] < 0. || + (xyYin[0] < 0.) | (xyYin[1] < 0.) || xyYin[0] + xyYin[1] > 1.) { fprintf(stderr, "%s: bad color input data\n", progname); @@ -401,8 +434,11 @@ getcolors() /* get xyY colors from standard input */ } -bresp(y, x) /* piecewise linear interpolation of primaries */ -COLOR y, x; +static void +bresp( /* piecewise linear interpolation of primaries */ + COLOR y, + COLOR x +) { register int i, n; @@ -419,7 +455,8 @@ COLOR y, x; } -compute() /* compute color mapping */ +static void +compute(void) /* compute color mapping */ { COLOR clrin[24], clrout[24]; long cflags; @@ -434,6 +471,13 @@ compute() /* compute color mapping */ /* compute piecewise luminance curve */ for (i = 0; i < NMBNEU; i++) { copycolor(bramp[i][0], inpRGB[mbneu[i]]); + for (n = i ? 3 : 0; n--; ) + if (colval(bramp[i][0],n) <= + colval(bramp[i-1][0],n)+1e-7) { + fprintf(stderr, + "%s: non-increasing neutral patch\n", progname); + exit(1); + } copycolor(bramp[i][1], mbRGB[mbneu[i]]); } /* compute color space gamut */ @@ -477,7 +521,8 @@ compute() /* compute color mapping */ } -putmapping() /* put out color mapping */ +static void +putmapping(void) /* put out color mapping */ { static char cchar[3] = {'r', 'g', 'b'}; register int i, j; @@ -525,9 +570,12 @@ putmapping() /* put out color mapping */ } -compsoln(cin, cout, n) /* solve 3xN system using least-squares */ -COLOR cin[], cout[]; -int n; +static void +compsoln( /* solve 3xN system using least-squares */ + COLOR cin[], + COLOR cout[], + int n +) { extern double mx3d_adjoint(), fabs(); double mat[3][3], invmat[3][3]; @@ -585,7 +633,8 @@ int n; } -cwarp() /* compute color warp map */ +static void +cwarp(void) /* compute color warp map */ { register int i; @@ -601,9 +650,11 @@ memerr: } -int -cvtcolor(cout, cin) /* convert color according to our mapping */ -COLOR cout, cin; +static int +cvtcolor( /* convert color according to our mapping */ + COLOR cout, + COLOR cin +) { COLOR ctmp; int clipped; @@ -622,18 +673,22 @@ COLOR cout, cin; } -int -cresp(cout, cin) /* transform color according to matrix */ -COLOR cout, cin; +static int +cresp( /* transform color according to matrix */ + COLOR cout, + COLOR cin +) { colortrans(cout, solmat, cin); return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax)); } -xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ -COLOR rgbout; -register float xyYin[3]; +static void +xyY2RGB( /* convert xyY to RGB */ + COLOR rgbout, + register float xyYin[3] +) { COLOR ctmp; double d; @@ -647,7 +702,8 @@ register float xyYin[3]; } -picdebug() /* put out debugging picture */ +static void +picdebug(void) /* put out debugging picture */ { static COLOR blkcol = BLKCOLOR; COLOR *scan; @@ -699,11 +755,12 @@ picdebug() /* put out debugging picture */ } /* clean up */ fclose(debugfp); - free((char *)scan); + free((void *)scan); } -clrdebug() /* put out debug picture from color input */ +static void +clrdebug(void) /* put out debug picture from color input */ { static COLR blkclr = BLKCOLR; COLR mbclr[24], cvclr[24], orclr[24]; @@ -769,5 +826,47 @@ clrdebug() /* put out debug picture from color input } /* clean up */ fclose(debugfp); - free((char *)scan); + free((void *)scan); +} + + +static void +getpos( /* get boundary position */ + char *name, + int bnds[2], + FILE *fp +) +{ + char buf[64]; + + fprintf(stderr, "\tSelect corner: %s\n", name); + if (fgets(buf, sizeof(buf), fp) == NULL || + sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) { + fprintf(stderr, "%s: read error from display process\n", + progname); + exit(1); + } +} + + +static void +pickchartpos( /* display picture and pick chart location */ + char *pfn +) +{ + char combuf[PATH_MAX]; + FILE *pfp; + + sprintf(combuf, DISPCOM, pfn); + if ((pfp = popen(combuf, "r")) == NULL) { + perror(combuf); + exit(1); + } + fputs("Use middle mouse button to select chart corners:\n", stderr); + getpos("upper left (dark skin)", bounds[0], pfp); + getpos("upper right (bluish green)", bounds[1], pfp); + getpos("lower left (white)", bounds[2], pfp); + getpos("lower right (black)", bounds[3], pfp); + fputs("Got it -- quit display program.\n", stderr); + pclose(pfp); }