--- ray/src/px/macbethcal.c 1997/01/30 19:14:11 2.12 +++ ray/src/px/macbethcal.c 2003/06/05 19:29:34 2.18 @@ -1,22 +1,24 @@ -/* 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.18 2003/06/05 19:29:34 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 "color.h" #include "resolu.h" #include "pmap.h" +#include "warp3d.h" /* MacBeth colors */ #define DarkSkin 0 @@ -95,7 +97,13 @@ 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 */ @@ -106,14 +114,15 @@ 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; @@ -131,9 +140,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; @@ -150,6 +157,18 @@ 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")) + goto userr; + irrad = atof(argv[i]); + break; + case 'm': /* raw map output */ + rawmap = 1; + break; case 'c': /* color input */ scanning = 0; break; @@ -158,22 +177,26 @@ char **argv; } /* 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 (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; @@ -190,16 +213,34 @@ char **argv; 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}"); - if (!scanning) - printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", + 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("}\n"); - putmapping(); /* put out color mapping */ + 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(); @@ -208,9 +249,9 @@ char **argv; 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); } @@ -235,8 +276,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); + } } @@ -310,7 +353,7 @@ getpicture() /* load in picture colors */ scalecolor(inpRGB[i], d); inpflags |= 1L<= 1.-1e-6) { - gmtflags |= 1L< 0.99 && irrad < 1.01) /* check gamut */ + for (i = 0; i < 24; i++) + if (cflags & 1L<