| 1 | 
greg | 
2.1 | 
#ifndef lint | 
| 2 | 
schorsch | 
2.19 | 
static const char       RCSid[] = "$Id: macbethcal.c,v 2.18 2003/06/05 19:29:34 schorsch Exp $"; | 
| 3 | 
greg | 
2.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * Calibrate a scanned MacBeth Color Checker Chart | 
| 6 | 
  | 
  | 
 * | 
| 7 | 
greg | 
2.14 | 
 * Produce a .cal file suitable for use with pcomb, | 
| 8 | 
  | 
  | 
 * or .cwp file suitable for use with pcwarp. | 
| 9 | 
  | 
  | 
 * | 
| 10 | 
  | 
  | 
 * Warping code depends on conformance of COLOR and W3VEC types. | 
| 11 | 
greg | 
2.1 | 
 */ | 
| 12 | 
  | 
  | 
 | 
| 13 | 
  | 
  | 
#include <stdio.h> | 
| 14 | 
greg | 
2.14 | 
#include <math.h> | 
| 15 | 
schorsch | 
2.18 | 
#include <time.h> | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
#include "platform.h" | 
| 18 | 
greg | 
2.1 | 
#include "color.h" | 
| 19 | 
  | 
  | 
#include "resolu.h" | 
| 20 | 
  | 
  | 
#include "pmap.h" | 
| 21 | 
greg | 
2.14 | 
#include "warp3d.h" | 
| 22 | 
greg | 
2.1 | 
 | 
| 23 | 
greg | 
2.4 | 
                                /* MacBeth colors */ | 
| 24 | 
  | 
  | 
#define DarkSkin        0 | 
| 25 | 
  | 
  | 
#define LightSkin       1 | 
| 26 | 
  | 
  | 
#define BlueSky         2 | 
| 27 | 
  | 
  | 
#define Foliage         3 | 
| 28 | 
  | 
  | 
#define BlueFlower      4 | 
| 29 | 
  | 
  | 
#define BluishGreen     5 | 
| 30 | 
  | 
  | 
#define Orange          6 | 
| 31 | 
  | 
  | 
#define PurplishBlue    7 | 
| 32 | 
  | 
  | 
#define ModerateRed     8 | 
| 33 | 
  | 
  | 
#define Purple          9 | 
| 34 | 
  | 
  | 
#define YellowGreen     10 | 
| 35 | 
  | 
  | 
#define OrangeYellow    11 | 
| 36 | 
  | 
  | 
#define Blue            12 | 
| 37 | 
  | 
  | 
#define Green           13 | 
| 38 | 
  | 
  | 
#define Red             14 | 
| 39 | 
  | 
  | 
#define Yellow          15 | 
| 40 | 
  | 
  | 
#define Magenta         16 | 
| 41 | 
  | 
  | 
#define Cyan            17 | 
| 42 | 
  | 
  | 
#define White           18 | 
| 43 | 
  | 
  | 
#define Neutral8        19 | 
| 44 | 
  | 
  | 
#define Neutral65       20 | 
| 45 | 
  | 
  | 
#define Neutral5        21 | 
| 46 | 
  | 
  | 
#define Neutral35       22 | 
| 47 | 
  | 
  | 
#define Black           23 | 
| 48 | 
  | 
  | 
                                /* computed from 5nm spectral measurements */ | 
| 49 | 
  | 
  | 
                                /* CIE 1931 2 degree obs, equal-energy white */ | 
| 50 | 
greg | 
2.1 | 
float   mbxyY[24][3] = { | 
| 51 | 
  | 
  | 
                {0.462, 0.3769, 0.0932961},     /* DarkSkin */ | 
| 52 | 
  | 
  | 
                {0.4108, 0.3542, 0.410348},     /* LightSkin */ | 
| 53 | 
  | 
  | 
                {0.2626, 0.267, 0.181554},      /* BlueSky */ | 
| 54 | 
  | 
  | 
                {0.36, 0.4689, 0.108447},       /* Foliage */ | 
| 55 | 
  | 
  | 
                {0.2977, 0.2602, 0.248407},     /* BlueFlower */ | 
| 56 | 
  | 
  | 
                {0.2719, 0.3485, 0.401156},     /* BluishGreen */ | 
| 57 | 
  | 
  | 
                {0.52, 0.4197, 0.357899},       /* Orange */ | 
| 58 | 
  | 
  | 
                {0.229, 0.1866, 0.103911},      /* PurplishBlue */ | 
| 59 | 
  | 
  | 
                {0.4909, 0.3262, 0.242615},     /* ModerateRed */ | 
| 60 | 
  | 
  | 
                {0.3361, 0.2249, 0.0600102},    /* Purple */ | 
| 61 | 
  | 
  | 
                {0.3855, 0.4874, 0.42963},      /* YellowGreen */ | 
| 62 | 
  | 
  | 
                {0.4853, 0.4457, 0.476343},     /* OrangeYellow */ | 
| 63 | 
  | 
  | 
                {0.2026, 0.1369, 0.0529249},    /* Blue */ | 
| 64 | 
  | 
  | 
                {0.3007, 0.4822, 0.221226},     /* Green */ | 
| 65 | 
  | 
  | 
                {0.5805, 0.3238, 0.162167},     /* Red */ | 
| 66 | 
  | 
  | 
                {0.4617, 0.472, 0.64909},       /* Yellow */ | 
| 67 | 
  | 
  | 
                {0.4178, 0.2625, 0.233662},     /* Magenta */ | 
| 68 | 
  | 
  | 
                {0.2038, 0.2508, 0.167275},     /* Cyan */ | 
| 69 | 
  | 
  | 
                {0.3358, 0.337, 0.916877},      /* White */ | 
| 70 | 
  | 
  | 
                {0.3338, 0.3348, 0.604678},     /* Neutral.8 */ | 
| 71 | 
  | 
  | 
                {0.3333, 0.3349, 0.364566},     /* Neutral.65 */ | 
| 72 | 
  | 
  | 
                {0.3353, 0.3359, 0.200238},     /* Neutral.5 */ | 
| 73 | 
  | 
  | 
                {0.3363, 0.336, 0.0878721},     /* Neutral.35 */ | 
| 74 | 
  | 
  | 
                {0.3346, 0.3349, 0.0308383}     /* Black */ | 
| 75 | 
  | 
  | 
        }; | 
| 76 | 
  | 
  | 
 | 
| 77 | 
  | 
  | 
COLOR   mbRGB[24];              /* MacBeth RGB values */ | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
#define NMBNEU          6       /* Number of MacBeth neutral colors */ | 
| 80 | 
greg | 
2.4 | 
short   mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; | 
| 81 | 
greg | 
2.1 | 
 | 
| 82 | 
greg | 
2.8 | 
#define  NEUFLGS        (1L<<White|1L<<Neutral8|1L<<Neutral65| \ | 
| 83 | 
  | 
  | 
                                1L<<Neutral5|1L<<Neutral35|1L<<Black) | 
| 84 | 
greg | 
2.4 | 
 | 
| 85 | 
greg | 
2.8 | 
#define  SATFLGS        (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \ | 
| 86 | 
  | 
  | 
                        1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \ | 
| 87 | 
  | 
  | 
                        1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower) | 
| 88 | 
greg | 
2.4 | 
 | 
| 89 | 
greg | 
2.8 | 
#define  UNSFLGS        (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ | 
| 90 | 
  | 
  | 
                        1L<<BluishGreen|1L<<ModerateRed) | 
| 91 | 
greg | 
2.7 | 
 | 
| 92 | 
greg | 
2.8 | 
#define  REQFLGS        NEUFLGS                 /* need these colors */ | 
| 93 | 
  | 
  | 
#define  MODFLGS        (NEUFLGS|UNSFLGS)       /* should be in gamut */ | 
| 94 | 
  | 
  | 
 | 
| 95 | 
greg | 
2.9 | 
#define  RG_BORD        0       /* patch border */ | 
| 96 | 
  | 
  | 
#define  RG_CENT        01      /* central region of patch */ | 
| 97 | 
  | 
  | 
#define  RG_ORIG        02      /* original color region */ | 
| 98 | 
  | 
  | 
#define  RG_CORR        04      /* corrected color region */ | 
| 99 | 
greg | 
2.7 | 
 | 
| 100 | 
gwlarson | 
2.16 | 
#ifndef  DISPCOM | 
| 101 | 
  | 
  | 
#define  DISPCOM        "ximage -op %s" | 
| 102 | 
  | 
  | 
#endif | 
| 103 | 
  | 
  | 
 | 
| 104 | 
greg | 
2.11 | 
int     scanning = 1;           /* scanned input (or recorded output)? */ | 
| 105 | 
greg | 
2.14 | 
double  irrad = 1.0;            /* irradiance multiplication factor */ | 
| 106 | 
  | 
  | 
int     rawmap = 0;             /* put out raw color mapping? */ | 
| 107 | 
greg | 
2.11 | 
 | 
| 108 | 
greg | 
2.1 | 
int     xmax, ymax;             /* input image dimensions */ | 
| 109 | 
  | 
  | 
int     bounds[4][2];           /* image coordinates of chart corners */ | 
| 110 | 
  | 
  | 
double  imgxfm[3][3];           /* coordinate transformation matrix */ | 
| 111 | 
  | 
  | 
 | 
| 112 | 
greg | 
2.7 | 
COLOR   inpRGB[24];             /* measured or scanned input colors */ | 
| 113 | 
  | 
  | 
long    inpflags = 0;           /* flags of which colors were input */ | 
| 114 | 
greg | 
2.8 | 
long    gmtflags = 0;           /* flags of out-of-gamut colors */ | 
| 115 | 
greg | 
2.1 | 
 | 
| 116 | 
greg | 
2.2 | 
COLOR   bramp[NMBNEU][2];       /* brightness ramp (per primary) */ | 
| 117 | 
greg | 
2.13 | 
COLORMAT        solmat;         /* color mapping matrix */ | 
| 118 | 
  | 
  | 
COLOR   colmin, colmax;         /* gamut limits */ | 
| 119 | 
greg | 
2.1 | 
 | 
| 120 | 
greg | 
2.14 | 
WARP3D  *wcor = NULL;           /* color space warp */ | 
| 121 | 
  | 
  | 
 | 
| 122 | 
greg | 
2.4 | 
FILE    *debugfp = NULL;        /* debug output picture */ | 
| 123 | 
greg | 
2.1 | 
char    *progname; | 
| 124 | 
  | 
  | 
 | 
| 125 | 
  | 
  | 
 | 
| 126 | 
  | 
  | 
main(argc, argv) | 
| 127 | 
  | 
  | 
int     argc; | 
| 128 | 
  | 
  | 
char    **argv; | 
| 129 | 
  | 
  | 
{ | 
| 130 | 
  | 
  | 
        int     i; | 
| 131 | 
  | 
  | 
 | 
| 132 | 
  | 
  | 
        progname = argv[0]; | 
| 133 | 
greg | 
2.7 | 
        for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 134 | 
  | 
  | 
                switch (argv[i][1]) { | 
| 135 | 
  | 
  | 
                case 'd':                               /* debug output */ | 
| 136 | 
  | 
  | 
                        i++; | 
| 137 | 
  | 
  | 
                        if (badarg(argc-i, argv+i, "s")) | 
| 138 | 
  | 
  | 
                                goto userr; | 
| 139 | 
  | 
  | 
                        if ((debugfp = fopen(argv[i], "w")) == NULL) { | 
| 140 | 
  | 
  | 
                                perror(argv[i]); | 
| 141 | 
  | 
  | 
                                exit(1); | 
| 142 | 
  | 
  | 
                        } | 
| 143 | 
schorsch | 
2.18 | 
                        SET_FILE_BINARY(debugfp); | 
| 144 | 
greg | 
2.7 | 
                        newheader("RADIANCE", debugfp);         /* start */ | 
| 145 | 
  | 
  | 
                        printargs(argc, argv, debugfp);         /* header */ | 
| 146 | 
  | 
  | 
                        break; | 
| 147 | 
  | 
  | 
                case 'p':                               /* picture position */ | 
| 148 | 
  | 
  | 
                        if (badarg(argc-i-1, argv+i+1, "iiiiiiii")) | 
| 149 | 
  | 
  | 
                                goto userr; | 
| 150 | 
  | 
  | 
                        bounds[0][0] = atoi(argv[++i]); | 
| 151 | 
  | 
  | 
                        bounds[0][1] = atoi(argv[++i]); | 
| 152 | 
  | 
  | 
                        bounds[1][0] = atoi(argv[++i]); | 
| 153 | 
  | 
  | 
                        bounds[1][1] = atoi(argv[++i]); | 
| 154 | 
  | 
  | 
                        bounds[2][0] = atoi(argv[++i]); | 
| 155 | 
  | 
  | 
                        bounds[2][1] = atoi(argv[++i]); | 
| 156 | 
  | 
  | 
                        bounds[3][0] = atoi(argv[++i]); | 
| 157 | 
  | 
  | 
                        bounds[3][1] = atoi(argv[++i]); | 
| 158 | 
greg | 
2.11 | 
                        scanning = 2; | 
| 159 | 
greg | 
2.7 | 
                        break; | 
| 160 | 
gwlarson | 
2.16 | 
                case 'P':                               /* pick position */ | 
| 161 | 
  | 
  | 
                        scanning = 3; | 
| 162 | 
  | 
  | 
                        break; | 
| 163 | 
greg | 
2.14 | 
                case 'i':                               /* irradiance factor */ | 
| 164 | 
  | 
  | 
                        i++; | 
| 165 | 
  | 
  | 
                        if (badarg(argc-i, argv+i, "f")) | 
| 166 | 
  | 
  | 
                                goto userr; | 
| 167 | 
  | 
  | 
                        irrad = atof(argv[i]); | 
| 168 | 
  | 
  | 
                        break; | 
| 169 | 
  | 
  | 
                case 'm':                               /* raw map output */ | 
| 170 | 
  | 
  | 
                        rawmap = 1; | 
| 171 | 
  | 
  | 
                        break; | 
| 172 | 
greg | 
2.7 | 
                case 'c':                               /* color input */ | 
| 173 | 
greg | 
2.11 | 
                        scanning = 0; | 
| 174 | 
greg | 
2.7 | 
                        break; | 
| 175 | 
  | 
  | 
                default: | 
| 176 | 
  | 
  | 
                        goto userr; | 
| 177 | 
  | 
  | 
                } | 
| 178 | 
  | 
  | 
                                                        /* open files */ | 
| 179 | 
  | 
  | 
        if (i < argc && freopen(argv[i], "r", stdin) == NULL) { | 
| 180 | 
greg | 
2.13 | 
                perror(argv[i]); | 
| 181 | 
greg | 
2.1 | 
                exit(1); | 
| 182 | 
  | 
  | 
        } | 
| 183 | 
greg | 
2.7 | 
        if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { | 
| 184 | 
greg | 
2.13 | 
                perror(argv[i+1]); | 
| 185 | 
greg | 
2.1 | 
                exit(1); | 
| 186 | 
  | 
  | 
        } | 
| 187 | 
greg | 
2.11 | 
        if (scanning) {                 /* load input picture header */ | 
| 188 | 
schorsch | 
2.18 | 
                SET_FILE_BINARY(stdin); | 
| 189 | 
greg | 
2.7 | 
                if (checkheader(stdin, COLRFMT, NULL) < 0 || | 
| 190 | 
  | 
  | 
                                fgetresolu(&xmax, &ymax, stdin) < 0) { | 
| 191 | 
  | 
  | 
                        fprintf(stderr, "%s: bad input picture\n", progname); | 
| 192 | 
  | 
  | 
                        exit(1); | 
| 193 | 
  | 
  | 
                } | 
| 194 | 
gwlarson | 
2.16 | 
                if (scanning == 3) { | 
| 195 | 
  | 
  | 
                        if (i >= argc) | 
| 196 | 
  | 
  | 
                                goto userr; | 
| 197 | 
  | 
  | 
                        pickchartpos(argv[i]); | 
| 198 | 
  | 
  | 
                        scanning = 2; | 
| 199 | 
  | 
  | 
                } | 
| 200 | 
greg | 
2.7 | 
        } else {                        /* else set default xmax and ymax */ | 
| 201 | 
  | 
  | 
                xmax = 512; | 
| 202 | 
  | 
  | 
                ymax = 2*512/3; | 
| 203 | 
greg | 
2.1 | 
        } | 
| 204 | 
greg | 
2.11 | 
        if (scanning != 2) {            /* use default boundaries */ | 
| 205 | 
greg | 
2.1 | 
                bounds[0][0] = bounds[2][0] = .029*xmax + .5; | 
| 206 | 
  | 
  | 
                bounds[0][1] = bounds[1][1] = .956*ymax + .5; | 
| 207 | 
  | 
  | 
                bounds[1][0] = bounds[3][0] = .971*xmax + .5; | 
| 208 | 
  | 
  | 
                bounds[2][1] = bounds[3][1] = .056*ymax + .5; | 
| 209 | 
  | 
  | 
        } | 
| 210 | 
  | 
  | 
        init();                         /* initialize */ | 
| 211 | 
greg | 
2.11 | 
        if (scanning)                   /* get picture colors */ | 
| 212 | 
greg | 
2.7 | 
                getpicture(); | 
| 213 | 
  | 
  | 
        else | 
| 214 | 
  | 
  | 
                getcolors(); | 
| 215 | 
greg | 
2.2 | 
        compute();                      /* compute color mapping */ | 
| 216 | 
greg | 
2.14 | 
        if (rawmap) {                   /* print out raw correspondence */ | 
| 217 | 
  | 
  | 
                register int    j; | 
| 218 | 
  | 
  | 
 | 
| 219 | 
  | 
  | 
                printf("# Color correspondence produced by:\n#\t\t"); | 
| 220 | 
  | 
  | 
                printargs(argc, argv, stdout); | 
| 221 | 
greg | 
2.15 | 
                printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", | 
| 222 | 
greg | 
2.11 | 
                                i+1 < argc ? argv[i+1] : "{this_file}"); | 
| 223 | 
greg | 
2.14 | 
                printf("#\t   Or: pcond [options] -m %s orig.pic > output.pic\n", | 
| 224 | 
  | 
  | 
                                i+1 < argc ? argv[i+1] : "{this_file}"); | 
| 225 | 
  | 
  | 
                for (j = 0; j < 24; j++) | 
| 226 | 
  | 
  | 
                        printf("%f %f %f    %f %f %f\n", | 
| 227 | 
  | 
  | 
                                colval(inpRGB[j],RED), colval(inpRGB[j],GRN), | 
| 228 | 
  | 
  | 
                                colval(inpRGB[j],BLU), colval(mbRGB[j],RED), | 
| 229 | 
  | 
  | 
                                colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); | 
| 230 | 
  | 
  | 
                if (scanning && debugfp != NULL) | 
| 231 | 
  | 
  | 
                        cwarp();                /* color warp for debugging */ | 
| 232 | 
  | 
  | 
        } else {                        /* print color mapping */ | 
| 233 | 
  | 
  | 
                                                /* print header */ | 
| 234 | 
  | 
  | 
                printf("{\n\tColor correction file computed by:\n\t\t"); | 
| 235 | 
  | 
  | 
                printargs(argc, argv, stdout); | 
| 236 | 
  | 
  | 
                printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", | 
| 237 | 
  | 
  | 
                                i+1 < argc ? argv[i+1] : "{this_file}"); | 
| 238 | 
  | 
  | 
                if (!scanning) | 
| 239 | 
  | 
  | 
                        printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n", | 
| 240 | 
  | 
  | 
                                        i+1 < argc ? argv[i+1] : "{this_file}"); | 
| 241 | 
  | 
  | 
                printf("}\n"); | 
| 242 | 
  | 
  | 
                putmapping();                   /* put out color mapping */ | 
| 243 | 
  | 
  | 
        } | 
| 244 | 
schorsch | 
2.19 | 
        if (debugfp != NULL) {          /* put out debug picture */ | 
| 245 | 
greg | 
2.11 | 
                if (scanning) | 
| 246 | 
greg | 
2.7 | 
                        picdebug(); | 
| 247 | 
  | 
  | 
                else | 
| 248 | 
  | 
  | 
                        clrdebug(); | 
| 249 | 
schorsch | 
2.19 | 
        } | 
| 250 | 
greg | 
2.1 | 
        exit(0); | 
| 251 | 
  | 
  | 
userr: | 
| 252 | 
greg | 
2.7 | 
        fprintf(stderr, | 
| 253 | 
gwlarson | 
2.16 | 
"Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", | 
| 254 | 
greg | 
2.1 | 
                        progname); | 
| 255 | 
greg | 
2.14 | 
        fprintf(stderr, "   or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", | 
| 256 | 
greg | 
2.7 | 
                        progname); | 
| 257 | 
greg | 
2.1 | 
        exit(1); | 
| 258 | 
  | 
  | 
} | 
| 259 | 
  | 
  | 
 | 
| 260 | 
  | 
  | 
 | 
| 261 | 
  | 
  | 
init()                          /* initialize */ | 
| 262 | 
  | 
  | 
{ | 
| 263 | 
  | 
  | 
        double  quad[4][2]; | 
| 264 | 
greg | 
2.7 | 
        register int    i; | 
| 265 | 
greg | 
2.1 | 
                                        /* make coordinate transformation */ | 
| 266 | 
  | 
  | 
        quad[0][0] = bounds[0][0]; | 
| 267 | 
  | 
  | 
        quad[0][1] = bounds[0][1]; | 
| 268 | 
  | 
  | 
        quad[1][0] = bounds[1][0]; | 
| 269 | 
  | 
  | 
        quad[1][1] = bounds[1][1]; | 
| 270 | 
  | 
  | 
        quad[2][0] = bounds[3][0]; | 
| 271 | 
  | 
  | 
        quad[2][1] = bounds[3][1]; | 
| 272 | 
  | 
  | 
        quad[3][0] = bounds[2][0]; | 
| 273 | 
  | 
  | 
        quad[3][1] = bounds[2][1]; | 
| 274 | 
  | 
  | 
 | 
| 275 | 
  | 
  | 
        if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) { | 
| 276 | 
  | 
  | 
                fprintf(stderr, "%s: bad chart boundaries\n", progname); | 
| 277 | 
  | 
  | 
                exit(1); | 
| 278 | 
  | 
  | 
        } | 
| 279 | 
greg | 
2.7 | 
                                        /* map MacBeth colors to RGB space */ | 
| 280 | 
greg | 
2.14 | 
        for (i = 0; i < 24; i++) { | 
| 281 | 
greg | 
2.7 | 
                xyY2RGB(mbRGB[i], mbxyY[i]); | 
| 282 | 
greg | 
2.14 | 
                scalecolor(mbRGB[i], irrad); | 
| 283 | 
  | 
  | 
        } | 
| 284 | 
greg | 
2.1 | 
} | 
| 285 | 
  | 
  | 
 | 
| 286 | 
  | 
  | 
 | 
| 287 | 
  | 
  | 
int | 
| 288 | 
greg | 
2.9 | 
chartndx(x, y, np)                      /* find color number for position */ | 
| 289 | 
greg | 
2.1 | 
int     x, y; | 
| 290 | 
greg | 
2.9 | 
int     *np; | 
| 291 | 
greg | 
2.1 | 
{ | 
| 292 | 
  | 
  | 
        double  ipos[3], cpos[3]; | 
| 293 | 
  | 
  | 
        int     ix, iy; | 
| 294 | 
  | 
  | 
        double  fx, fy; | 
| 295 | 
  | 
  | 
 | 
| 296 | 
  | 
  | 
        ipos[0] = x; | 
| 297 | 
  | 
  | 
        ipos[1] = y; | 
| 298 | 
  | 
  | 
        ipos[2] = 1; | 
| 299 | 
  | 
  | 
        mx3d_transform(ipos, imgxfm, cpos); | 
| 300 | 
  | 
  | 
        cpos[0] /= cpos[2]; | 
| 301 | 
  | 
  | 
        cpos[1] /= cpos[2]; | 
| 302 | 
  | 
  | 
        if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) | 
| 303 | 
greg | 
2.9 | 
                return(RG_BORD); | 
| 304 | 
greg | 
2.1 | 
        ix = cpos[0]; | 
| 305 | 
  | 
  | 
        iy = cpos[1]; | 
| 306 | 
  | 
  | 
        fx = cpos[0] - ix; | 
| 307 | 
  | 
  | 
        fy = cpos[1] - iy; | 
| 308 | 
greg | 
2.9 | 
        *np = iy*6 + ix; | 
| 309 | 
  | 
  | 
        if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) | 
| 310 | 
  | 
  | 
                return(RG_CENT); | 
| 311 | 
  | 
  | 
        if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) | 
| 312 | 
  | 
  | 
                return(RG_BORD); | 
| 313 | 
  | 
  | 
        if (fx >= 0.5)                  /* right side is corrected */ | 
| 314 | 
  | 
  | 
                return(RG_CORR); | 
| 315 | 
  | 
  | 
        return(RG_ORIG);                /* left side is original */ | 
| 316 | 
greg | 
2.1 | 
} | 
| 317 | 
  | 
  | 
 | 
| 318 | 
  | 
  | 
 | 
| 319 | 
greg | 
2.7 | 
getpicture()                            /* load in picture colors */ | 
| 320 | 
greg | 
2.1 | 
{ | 
| 321 | 
  | 
  | 
        COLR    *scanln; | 
| 322 | 
  | 
  | 
        COLOR   pval; | 
| 323 | 
  | 
  | 
        int     ccount[24]; | 
| 324 | 
  | 
  | 
        double  d; | 
| 325 | 
greg | 
2.9 | 
        int     y, i; | 
| 326 | 
  | 
  | 
        register int    x; | 
| 327 | 
greg | 
2.1 | 
 | 
| 328 | 
  | 
  | 
        scanln = (COLR *)malloc(xmax*sizeof(COLR)); | 
| 329 | 
  | 
  | 
        if (scanln == NULL) { | 
| 330 | 
  | 
  | 
                perror(progname); | 
| 331 | 
  | 
  | 
                exit(1); | 
| 332 | 
  | 
  | 
        } | 
| 333 | 
  | 
  | 
        for (i = 0; i < 24; i++) { | 
| 334 | 
greg | 
2.7 | 
                setcolor(inpRGB[i], 0., 0., 0.); | 
| 335 | 
greg | 
2.1 | 
                ccount[i] = 0; | 
| 336 | 
  | 
  | 
        } | 
| 337 | 
  | 
  | 
        for (y = ymax-1; y >= 0; y--) { | 
| 338 | 
  | 
  | 
                if (freadcolrs(scanln, xmax, stdin) < 0) { | 
| 339 | 
  | 
  | 
                        fprintf(stderr, "%s: error reading input picture\n", | 
| 340 | 
  | 
  | 
                                        progname); | 
| 341 | 
  | 
  | 
                        exit(1); | 
| 342 | 
  | 
  | 
                } | 
| 343 | 
greg | 
2.9 | 
                for (x = 0; x < xmax; x++) | 
| 344 | 
  | 
  | 
                        if (chartndx(x, y, &i) == RG_CENT) { | 
| 345 | 
greg | 
2.1 | 
                                colr_color(pval, scanln[x]); | 
| 346 | 
greg | 
2.7 | 
                                addcolor(inpRGB[i], pval); | 
| 347 | 
greg | 
2.1 | 
                                ccount[i]++; | 
| 348 | 
  | 
  | 
                        } | 
| 349 | 
  | 
  | 
        } | 
| 350 | 
greg | 
2.7 | 
        for (i = 0; i < 24; i++) {              /* compute averages */ | 
| 351 | 
  | 
  | 
                if (ccount[i] == 0) | 
| 352 | 
  | 
  | 
                        continue; | 
| 353 | 
  | 
  | 
                d = 1./ccount[i]; | 
| 354 | 
  | 
  | 
                scalecolor(inpRGB[i], d); | 
| 355 | 
  | 
  | 
                inpflags |= 1L<<i; | 
| 356 | 
  | 
  | 
        } | 
| 357 | 
greg | 
2.17 | 
        free((void *)scanln); | 
| 358 | 
greg | 
2.7 | 
} | 
| 359 | 
  | 
  | 
 | 
| 360 | 
  | 
  | 
 | 
| 361 | 
  | 
  | 
getcolors()                     /* get xyY colors from standard input */ | 
| 362 | 
  | 
  | 
{ | 
| 363 | 
  | 
  | 
        int     gotwhite = 0; | 
| 364 | 
  | 
  | 
        COLOR   whiteclr; | 
| 365 | 
  | 
  | 
        int     n; | 
| 366 | 
  | 
  | 
        float   xyYin[3]; | 
| 367 | 
  | 
  | 
 | 
| 368 | 
  | 
  | 
        while (fgetval(stdin, 'i', &n) == 1) {          /* read colors */ | 
| 369 | 
  | 
  | 
                if (n < 0 | n > 24 || | 
| 370 | 
  | 
  | 
                                fgetval(stdin, 'f', &xyYin[0]) != 1 || | 
| 371 | 
  | 
  | 
                                fgetval(stdin, 'f', &xyYin[1]) != 1 || | 
| 372 | 
  | 
  | 
                                fgetval(stdin, 'f', &xyYin[2]) != 1 || | 
| 373 | 
greg | 
2.12 | 
                                xyYin[0] < 0. | xyYin[1] < 0. || | 
| 374 | 
  | 
  | 
                                xyYin[0] + xyYin[1] > 1.) { | 
| 375 | 
greg | 
2.7 | 
                        fprintf(stderr, "%s: bad color input data\n", | 
| 376 | 
greg | 
2.1 | 
                                        progname); | 
| 377 | 
  | 
  | 
                        exit(1); | 
| 378 | 
  | 
  | 
                } | 
| 379 | 
greg | 
2.7 | 
                if (n == 0) {                           /* calibration white */ | 
| 380 | 
  | 
  | 
                        xyY2RGB(whiteclr, xyYin); | 
| 381 | 
  | 
  | 
                        gotwhite++; | 
| 382 | 
  | 
  | 
                } else {                                /* standard color */ | 
| 383 | 
  | 
  | 
                        n--; | 
| 384 | 
  | 
  | 
                        xyY2RGB(inpRGB[n], xyYin); | 
| 385 | 
  | 
  | 
                        inpflags |= 1L<<n; | 
| 386 | 
  | 
  | 
                } | 
| 387 | 
greg | 
2.1 | 
        } | 
| 388 | 
greg | 
2.7 | 
                                        /* normalize colors */ | 
| 389 | 
  | 
  | 
        if (!gotwhite) { | 
| 390 | 
  | 
  | 
                if (!(inpflags & 1L<<White)) { | 
| 391 | 
  | 
  | 
                        fprintf(stderr, "%s: missing input for White\n", | 
| 392 | 
  | 
  | 
                                        progname); | 
| 393 | 
  | 
  | 
                        exit(1); | 
| 394 | 
  | 
  | 
                } | 
| 395 | 
  | 
  | 
                setcolor(whiteclr, | 
| 396 | 
  | 
  | 
                        colval(inpRGB[White],RED)/colval(mbRGB[White],RED), | 
| 397 | 
  | 
  | 
                        colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN), | 
| 398 | 
  | 
  | 
                        colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU)); | 
| 399 | 
  | 
  | 
        } | 
| 400 | 
  | 
  | 
        for (n = 0; n < 24; n++) | 
| 401 | 
  | 
  | 
                if (inpflags & 1L<<n) | 
| 402 | 
  | 
  | 
                        setcolor(inpRGB[n], | 
| 403 | 
  | 
  | 
                                colval(inpRGB[n],RED)/colval(whiteclr,RED), | 
| 404 | 
  | 
  | 
                                colval(inpRGB[n],GRN)/colval(whiteclr,GRN), | 
| 405 | 
  | 
  | 
                                colval(inpRGB[n],BLU)/colval(whiteclr,BLU)); | 
| 406 | 
greg | 
2.1 | 
} | 
| 407 | 
  | 
  | 
 | 
| 408 | 
  | 
  | 
 | 
| 409 | 
greg | 
2.2 | 
bresp(y, x)             /* piecewise linear interpolation of primaries */ | 
| 410 | 
  | 
  | 
COLOR   y, x; | 
| 411 | 
greg | 
2.1 | 
{ | 
| 412 | 
greg | 
2.2 | 
        register int    i, n; | 
| 413 | 
greg | 
2.1 | 
 | 
| 414 | 
greg | 
2.2 | 
        for (i = 0; i < 3; i++) { | 
| 415 | 
greg | 
2.5 | 
                for (n = 0; n < NMBNEU-2; n++) | 
| 416 | 
  | 
  | 
                        if (colval(x,i) < colval(bramp[n+1][0],i)) | 
| 417 | 
  | 
  | 
                                break; | 
| 418 | 
greg | 
2.8 | 
                colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * | 
| 419 | 
greg | 
2.2 | 
                                                colval(bramp[n][1],i) + | 
| 420 | 
  | 
  | 
                                (colval(x,i) - colval(bramp[n][0],i)) * | 
| 421 | 
  | 
  | 
                                                colval(bramp[n+1][1],i)) / | 
| 422 | 
  | 
  | 
                        (colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); | 
| 423 | 
  | 
  | 
        } | 
| 424 | 
greg | 
2.1 | 
} | 
| 425 | 
  | 
  | 
 | 
| 426 | 
  | 
  | 
 | 
| 427 | 
greg | 
2.2 | 
compute()                       /* compute color mapping */ | 
| 428 | 
greg | 
2.1 | 
{ | 
| 429 | 
greg | 
2.8 | 
        COLOR   clrin[24], clrout[24]; | 
| 430 | 
  | 
  | 
        long    cflags; | 
| 431 | 
  | 
  | 
        COLOR   ctmp; | 
| 432 | 
greg | 
2.13 | 
        register int    i, n; | 
| 433 | 
greg | 
2.7 | 
                                        /* did we get what we need? */ | 
| 434 | 
  | 
  | 
        if ((inpflags & REQFLGS) != REQFLGS) { | 
| 435 | 
  | 
  | 
                fprintf(stderr, "%s: missing required input colors\n", | 
| 436 | 
  | 
  | 
                                progname); | 
| 437 | 
  | 
  | 
                exit(1); | 
| 438 | 
greg | 
2.2 | 
        } | 
| 439 | 
greg | 
2.1 | 
                                        /* compute piecewise luminance curve */ | 
| 440 | 
  | 
  | 
        for (i = 0; i < NMBNEU; i++) { | 
| 441 | 
greg | 
2.7 | 
                copycolor(bramp[i][0], inpRGB[mbneu[i]]); | 
| 442 | 
greg | 
2.2 | 
                copycolor(bramp[i][1], mbRGB[mbneu[i]]); | 
| 443 | 
greg | 
2.1 | 
        } | 
| 444 | 
greg | 
2.13 | 
                                        /* compute color space gamut */ | 
| 445 | 
  | 
  | 
        if (scanning) { | 
| 446 | 
  | 
  | 
                copycolor(colmin, cblack); | 
| 447 | 
  | 
  | 
                copycolor(colmax, cwhite); | 
| 448 | 
greg | 
2.14 | 
                scalecolor(colmax, irrad); | 
| 449 | 
greg | 
2.13 | 
        } else | 
| 450 | 
  | 
  | 
                for (i = 0; i < 3; i++) { | 
| 451 | 
  | 
  | 
                        colval(colmin,i) = colval(bramp[0][0],i) - | 
| 452 | 
  | 
  | 
                                colval(bramp[0][1],i) * | 
| 453 | 
  | 
  | 
                                (colval(bramp[1][0],i)-colval(bramp[0][0],i)) / | 
| 454 | 
  | 
  | 
                                (colval(bramp[1][1],i)-colval(bramp[1][0],i)); | 
| 455 | 
  | 
  | 
                        colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) + | 
| 456 | 
  | 
  | 
                                (1.-colval(bramp[NMBNEU-2][1],i)) * | 
| 457 | 
  | 
  | 
                                (colval(bramp[NMBNEU-1][0],i) - | 
| 458 | 
  | 
  | 
                                        colval(bramp[NMBNEU-2][0],i)) / | 
| 459 | 
  | 
  | 
                                (colval(bramp[NMBNEU-1][1],i) - | 
| 460 | 
  | 
  | 
                                        colval(bramp[NMBNEU-2][1],i)); | 
| 461 | 
  | 
  | 
                } | 
| 462 | 
greg | 
2.8 | 
                                        /* compute color mapping */ | 
| 463 | 
  | 
  | 
        do { | 
| 464 | 
  | 
  | 
                cflags = inpflags & ~gmtflags; | 
| 465 | 
  | 
  | 
                n = 0;                          /* compute transform matrix */ | 
| 466 | 
  | 
  | 
                for (i = 0; i < 24; i++) | 
| 467 | 
  | 
  | 
                        if (cflags & 1L<<i) { | 
| 468 | 
  | 
  | 
                                bresp(clrin[n], inpRGB[i]); | 
| 469 | 
  | 
  | 
                                copycolor(clrout[n], mbRGB[i]); | 
| 470 | 
  | 
  | 
                                n++; | 
| 471 | 
  | 
  | 
                        } | 
| 472 | 
  | 
  | 
                compsoln(clrin, clrout, n); | 
| 473 | 
greg | 
2.14 | 
                if (irrad > 0.99 && irrad < 1.01)       /* check gamut */ | 
| 474 | 
  | 
  | 
                        for (i = 0; i < 24; i++) | 
| 475 | 
  | 
  | 
                                if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) | 
| 476 | 
  | 
  | 
                                        gmtflags |= 1L<<i; | 
| 477 | 
greg | 
2.8 | 
        } while (cflags & gmtflags); | 
| 478 | 
  | 
  | 
        if (gmtflags & MODFLGS) | 
| 479 | 
  | 
  | 
                fprintf(stderr, | 
| 480 | 
  | 
  | 
                "%s: warning - some moderate colors are out of gamut\n", | 
| 481 | 
  | 
  | 
                                progname); | 
| 482 | 
greg | 
2.2 | 
} | 
| 483 | 
  | 
  | 
 | 
| 484 | 
  | 
  | 
 | 
| 485 | 
greg | 
2.14 | 
putmapping()                    /* put out color mapping */ | 
| 486 | 
greg | 
2.2 | 
{ | 
| 487 | 
  | 
  | 
        static char     cchar[3] = {'r', 'g', 'b'}; | 
| 488 | 
  | 
  | 
        register int    i, j; | 
| 489 | 
greg | 
2.1 | 
                                        /* print brightness mapping */ | 
| 490 | 
greg | 
2.2 | 
        for (j = 0; j < 3; j++) { | 
| 491 | 
  | 
  | 
                printf("%cxa(i) : select(i", cchar[j]); | 
| 492 | 
  | 
  | 
                for (i = 0; i < NMBNEU; i++) | 
| 493 | 
  | 
  | 
                        printf(",%g", colval(bramp[i][0],j)); | 
| 494 | 
  | 
  | 
                printf(");\n"); | 
| 495 | 
  | 
  | 
                printf("%cya(i) : select(i", cchar[j]); | 
| 496 | 
  | 
  | 
                for (i = 0; i < NMBNEU; i++) | 
| 497 | 
  | 
  | 
                        printf(",%g", colval(bramp[i][1],j)); | 
| 498 | 
  | 
  | 
                printf(");\n"); | 
| 499 | 
  | 
  | 
                printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", | 
| 500 | 
  | 
  | 
                                cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], | 
| 501 | 
  | 
  | 
                                cchar[j], cchar[j]); | 
| 502 | 
  | 
  | 
                printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); | 
| 503 | 
greg | 
2.11 | 
                printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", | 
| 504 | 
  | 
  | 
                                cchar[j], scanning?'n':'o', cchar[j], | 
| 505 | 
  | 
  | 
                                cchar[j], cchar[j], cchar[j], cchar[j]); | 
| 506 | 
greg | 
2.2 | 
                printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", | 
| 507 | 
  | 
  | 
                                cchar[j], cchar[j], cchar[j], | 
| 508 | 
  | 
  | 
                                cchar[j], cchar[j]); | 
| 509 | 
  | 
  | 
                printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n", | 
| 510 | 
  | 
  | 
                                cchar[j], cchar[j], cchar[j], cchar[j]); | 
| 511 | 
  | 
  | 
        } | 
| 512 | 
greg | 
2.1 | 
                                        /* print color mapping */ | 
| 513 | 
greg | 
2.11 | 
        if (scanning) { | 
| 514 | 
  | 
  | 
                printf("r = ri(1); g = gi(1); b = bi(1);\n"); | 
| 515 | 
  | 
  | 
                printf("ro = %g*rn + %g*gn + %g*bn ;\n", | 
| 516 | 
  | 
  | 
                                solmat[0][0], solmat[0][1], solmat[0][2]); | 
| 517 | 
  | 
  | 
                printf("go = %g*rn + %g*gn + %g*bn ;\n", | 
| 518 | 
  | 
  | 
                                solmat[1][0], solmat[1][1], solmat[1][2]); | 
| 519 | 
  | 
  | 
                printf("bo = %g*rn + %g*gn + %g*bn ;\n", | 
| 520 | 
  | 
  | 
                                solmat[2][0], solmat[2][1], solmat[2][2]); | 
| 521 | 
  | 
  | 
        } else { | 
| 522 | 
  | 
  | 
                printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n"); | 
| 523 | 
  | 
  | 
                printf("r = %g*r1 + %g*g1 + %g*b1 ;\n", | 
| 524 | 
  | 
  | 
                                solmat[0][0], solmat[0][1], solmat[0][2]); | 
| 525 | 
  | 
  | 
                printf("g = %g*r1 + %g*g1 + %g*b1 ;\n", | 
| 526 | 
  | 
  | 
                                solmat[1][0], solmat[1][1], solmat[1][2]); | 
| 527 | 
  | 
  | 
                printf("b = %g*r1 + %g*g1 + %g*b1 ;\n", | 
| 528 | 
  | 
  | 
                                solmat[2][0], solmat[2][1], solmat[2][2]); | 
| 529 | 
  | 
  | 
        } | 
| 530 | 
greg | 
2.1 | 
} | 
| 531 | 
  | 
  | 
 | 
| 532 | 
  | 
  | 
 | 
| 533 | 
greg | 
2.4 | 
compsoln(cin, cout, n)          /* solve 3xN system using least-squares */ | 
| 534 | 
greg | 
2.2 | 
COLOR   cin[], cout[]; | 
| 535 | 
greg | 
2.1 | 
int     n; | 
| 536 | 
  | 
  | 
{ | 
| 537 | 
  | 
  | 
        extern double   mx3d_adjoint(), fabs(); | 
| 538 | 
  | 
  | 
        double  mat[3][3], invmat[3][3]; | 
| 539 | 
  | 
  | 
        double  det; | 
| 540 | 
  | 
  | 
        double  colv[3], rowv[3]; | 
| 541 | 
greg | 
2.4 | 
        register int    i, j, k; | 
| 542 | 
greg | 
2.1 | 
 | 
| 543 | 
greg | 
2.8 | 
        if (n < 3) { | 
| 544 | 
  | 
  | 
                fprintf(stderr, "%s: too few colors to match!\n", progname); | 
| 545 | 
greg | 
2.1 | 
                exit(1); | 
| 546 | 
  | 
  | 
        } | 
| 547 | 
greg | 
2.4 | 
        if (n == 3) | 
| 548 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 549 | 
  | 
  | 
                        for (j = 0; j < 3; j++) | 
| 550 | 
  | 
  | 
                                mat[i][j] = colval(cin[j],i); | 
| 551 | 
  | 
  | 
        else {                          /* compute A^t A */ | 
| 552 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 553 | 
  | 
  | 
                        for (j = i; j < 3; j++) { | 
| 554 | 
  | 
  | 
                                mat[i][j] = 0.; | 
| 555 | 
  | 
  | 
                                for (k = 0; k < n; k++) | 
| 556 | 
  | 
  | 
                                        mat[i][j] += colval(cin[k],i) * | 
| 557 | 
  | 
  | 
                                                        colval(cin[k],j); | 
| 558 | 
  | 
  | 
                        } | 
| 559 | 
  | 
  | 
                for (i = 1; i < 3; i++)         /* using symmetry */ | 
| 560 | 
  | 
  | 
                        for (j = 0; j < i; j++) | 
| 561 | 
  | 
  | 
                                mat[i][j] = mat[j][i]; | 
| 562 | 
  | 
  | 
        } | 
| 563 | 
greg | 
2.1 | 
        det = mx3d_adjoint(mat, invmat); | 
| 564 | 
  | 
  | 
        if (fabs(det) < 1e-4) { | 
| 565 | 
  | 
  | 
                fprintf(stderr, "%s: cannot compute color mapping\n", | 
| 566 | 
  | 
  | 
                                progname); | 
| 567 | 
  | 
  | 
                solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.; | 
| 568 | 
  | 
  | 
                solmat[0][1] = solmat[0][2] = solmat[1][0] = | 
| 569 | 
  | 
  | 
                solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.; | 
| 570 | 
  | 
  | 
                return; | 
| 571 | 
  | 
  | 
        } | 
| 572 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 573 | 
  | 
  | 
                for (j = 0; j < 3; j++) | 
| 574 | 
  | 
  | 
                        invmat[i][j] /= det; | 
| 575 | 
  | 
  | 
        for (i = 0; i < 3; i++) { | 
| 576 | 
greg | 
2.4 | 
                if (n == 3) | 
| 577 | 
  | 
  | 
                        for (j = 0; j < 3; j++) | 
| 578 | 
  | 
  | 
                                colv[j] = colval(cout[j],i); | 
| 579 | 
  | 
  | 
                else | 
| 580 | 
  | 
  | 
                        for (j = 0; j < 3; j++) { | 
| 581 | 
  | 
  | 
                                colv[j] = 0.; | 
| 582 | 
  | 
  | 
                                for (k = 0; k < n; k++) | 
| 583 | 
  | 
  | 
                                        colv[j] += colval(cout[k],i) * | 
| 584 | 
  | 
  | 
                                                        colval(cin[k],j); | 
| 585 | 
  | 
  | 
                        } | 
| 586 | 
greg | 
2.3 | 
                mx3d_transform(colv, invmat, rowv); | 
| 587 | 
greg | 
2.1 | 
                for (j = 0; j < 3; j++) | 
| 588 | 
greg | 
2.3 | 
                        solmat[i][j] = rowv[j]; | 
| 589 | 
greg | 
2.1 | 
        } | 
| 590 | 
  | 
  | 
} | 
| 591 | 
  | 
  | 
 | 
| 592 | 
greg | 
2.3 | 
 | 
| 593 | 
greg | 
2.14 | 
cwarp()                         /* compute color warp map */ | 
| 594 | 
  | 
  | 
{ | 
| 595 | 
  | 
  | 
        register int    i; | 
| 596 | 
  | 
  | 
 | 
| 597 | 
  | 
  | 
        if ((wcor = new3dw(W3EXACT)) == NULL) | 
| 598 | 
  | 
  | 
                goto memerr; | 
| 599 | 
  | 
  | 
        for (i = 0; i < 24; i++) | 
| 600 | 
  | 
  | 
                if (!add3dpt(wcor, inpRGB[i], mbRGB[i])) | 
| 601 | 
  | 
  | 
                        goto memerr; | 
| 602 | 
  | 
  | 
        return; | 
| 603 | 
  | 
  | 
memerr: | 
| 604 | 
  | 
  | 
        perror(progname); | 
| 605 | 
  | 
  | 
        exit(1); | 
| 606 | 
  | 
  | 
} | 
| 607 | 
  | 
  | 
 | 
| 608 | 
  | 
  | 
 | 
| 609 | 
greg | 
2.13 | 
int | 
| 610 | 
greg | 
2.1 | 
cvtcolor(cout, cin)             /* convert color according to our mapping */ | 
| 611 | 
  | 
  | 
COLOR   cout, cin; | 
| 612 | 
  | 
  | 
{ | 
| 613 | 
greg | 
2.8 | 
        COLOR   ctmp; | 
| 614 | 
greg | 
2.13 | 
        int     clipped; | 
| 615 | 
greg | 
2.8 | 
 | 
| 616 | 
greg | 
2.14 | 
        if (wcor != NULL) { | 
| 617 | 
  | 
  | 
                clipped = warp3d(cout, cin, wcor); | 
| 618 | 
  | 
  | 
                clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax); | 
| 619 | 
  | 
  | 
        } else if (scanning) { | 
| 620 | 
greg | 
2.11 | 
                bresp(ctmp, cin); | 
| 621 | 
greg | 
2.13 | 
                clipped = cresp(cout, ctmp); | 
| 622 | 
greg | 
2.11 | 
        } else { | 
| 623 | 
greg | 
2.13 | 
                clipped = cresp(ctmp, cin); | 
| 624 | 
greg | 
2.11 | 
                bresp(cout, ctmp); | 
| 625 | 
  | 
  | 
        } | 
| 626 | 
greg | 
2.13 | 
        return(clipped); | 
| 627 | 
greg | 
2.8 | 
} | 
| 628 | 
  | 
  | 
 | 
| 629 | 
  | 
  | 
 | 
| 630 | 
greg | 
2.13 | 
int | 
| 631 | 
greg | 
2.8 | 
cresp(cout, cin)                /* transform color according to matrix */ | 
| 632 | 
  | 
  | 
COLOR   cout, cin; | 
| 633 | 
  | 
  | 
{ | 
| 634 | 
greg | 
2.13 | 
        colortrans(cout, solmat, cin); | 
| 635 | 
  | 
  | 
        return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax)); | 
| 636 | 
greg | 
2.1 | 
} | 
| 637 | 
  | 
  | 
 | 
| 638 | 
  | 
  | 
 | 
| 639 | 
greg | 
2.7 | 
xyY2RGB(rgbout, xyYin)          /* convert xyY to RGB */ | 
| 640 | 
  | 
  | 
COLOR   rgbout; | 
| 641 | 
  | 
  | 
register float  xyYin[3]; | 
| 642 | 
greg | 
2.1 | 
{ | 
| 643 | 
greg | 
2.7 | 
        COLOR   ctmp; | 
| 644 | 
  | 
  | 
        double  d; | 
| 645 | 
  | 
  | 
 | 
| 646 | 
  | 
  | 
        d = xyYin[2] / xyYin[1]; | 
| 647 | 
  | 
  | 
        ctmp[0] = xyYin[0] * d; | 
| 648 | 
  | 
  | 
        ctmp[1] = xyYin[2]; | 
| 649 | 
  | 
  | 
        ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; | 
| 650 | 
greg | 
2.12 | 
                                /* allow negative values */ | 
| 651 | 
greg | 
2.13 | 
        colortrans(rgbout, xyz2rgbmat, ctmp); | 
| 652 | 
greg | 
2.7 | 
} | 
| 653 | 
  | 
  | 
 | 
| 654 | 
  | 
  | 
 | 
| 655 | 
  | 
  | 
picdebug()                      /* put out debugging picture */ | 
| 656 | 
  | 
  | 
{ | 
| 657 | 
greg | 
2.8 | 
        static COLOR    blkcol = BLKCOLOR; | 
| 658 | 
greg | 
2.1 | 
        COLOR   *scan; | 
| 659 | 
greg | 
2.9 | 
        int     y, i; | 
| 660 | 
  | 
  | 
        register int    x, rg; | 
| 661 | 
greg | 
2.1 | 
 | 
| 662 | 
  | 
  | 
        if (fseek(stdin, 0L, 0) == EOF) { | 
| 663 | 
  | 
  | 
                fprintf(stderr, "%s: cannot seek on input picture\n", progname); | 
| 664 | 
  | 
  | 
                exit(1); | 
| 665 | 
  | 
  | 
        } | 
| 666 | 
  | 
  | 
        getheader(stdin, NULL, NULL);           /* skip input header */ | 
| 667 | 
  | 
  | 
        fgetresolu(&xmax, &ymax, stdin); | 
| 668 | 
  | 
  | 
                                                /* allocate scanline */ | 
| 669 | 
  | 
  | 
        scan = (COLOR *)malloc(xmax*sizeof(COLOR)); | 
| 670 | 
  | 
  | 
        if (scan == NULL) { | 
| 671 | 
  | 
  | 
                perror(progname); | 
| 672 | 
  | 
  | 
                exit(1); | 
| 673 | 
  | 
  | 
        } | 
| 674 | 
  | 
  | 
                                                /* finish debug header */ | 
| 675 | 
greg | 
2.5 | 
        fputformat(COLRFMT, debugfp); | 
| 676 | 
greg | 
2.1 | 
        putc('\n', debugfp); | 
| 677 | 
  | 
  | 
        fprtresolu(xmax, ymax, debugfp); | 
| 678 | 
greg | 
2.7 | 
                                                /* write debug picture */ | 
| 679 | 
greg | 
2.1 | 
        for (y = ymax-1; y >= 0; y--) { | 
| 680 | 
  | 
  | 
                if (freadscan(scan, xmax, stdin) < 0) { | 
| 681 | 
  | 
  | 
                        fprintf(stderr, "%s: error rereading input picture\n", | 
| 682 | 
  | 
  | 
                                        progname); | 
| 683 | 
  | 
  | 
                        exit(1); | 
| 684 | 
  | 
  | 
                } | 
| 685 | 
  | 
  | 
                for (x = 0; x < xmax; x++) { | 
| 686 | 
greg | 
2.9 | 
                        rg = chartndx(x, y, &i); | 
| 687 | 
  | 
  | 
                        if (rg == RG_CENT) { | 
| 688 | 
greg | 
2.13 | 
                                if (!(1L<<i & gmtflags) || (x+y)&07) { | 
| 689 | 
greg | 
2.9 | 
                                        copycolor(scan[x], mbRGB[i]); | 
| 690 | 
greg | 
2.13 | 
                                        clipgamut(scan[x], bright(scan[x]), | 
| 691 | 
greg | 
2.14 | 
                                                CGAMUT, colmin, colmax); | 
| 692 | 
greg | 
2.13 | 
                                } else | 
| 693 | 
greg | 
2.9 | 
                                        copycolor(scan[x], blkcol); | 
| 694 | 
  | 
  | 
                        } else if (rg == RG_CORR) | 
| 695 | 
greg | 
2.1 | 
                                cvtcolor(scan[x], scan[x]); | 
| 696 | 
greg | 
2.9 | 
                        else if (rg != RG_ORIG) | 
| 697 | 
greg | 
2.8 | 
                                copycolor(scan[x], blkcol); | 
| 698 | 
greg | 
2.1 | 
                } | 
| 699 | 
  | 
  | 
                if (fwritescan(scan, xmax, debugfp) < 0) { | 
| 700 | 
  | 
  | 
                        fprintf(stderr, "%s: error writing debugging picture\n", | 
| 701 | 
  | 
  | 
                                        progname); | 
| 702 | 
  | 
  | 
                        exit(1); | 
| 703 | 
  | 
  | 
                } | 
| 704 | 
  | 
  | 
        } | 
| 705 | 
greg | 
2.7 | 
                                                /* clean up */ | 
| 706 | 
  | 
  | 
        fclose(debugfp); | 
| 707 | 
greg | 
2.17 | 
        free((void *)scan); | 
| 708 | 
greg | 
2.7 | 
} | 
| 709 | 
  | 
  | 
 | 
| 710 | 
  | 
  | 
 | 
| 711 | 
  | 
  | 
clrdebug()                      /* put out debug picture from color input */ | 
| 712 | 
  | 
  | 
{ | 
| 713 | 
  | 
  | 
        static COLR     blkclr = BLKCOLR; | 
| 714 | 
greg | 
2.9 | 
        COLR    mbclr[24], cvclr[24], orclr[24]; | 
| 715 | 
greg | 
2.7 | 
        COLR    *scan; | 
| 716 | 
greg | 
2.13 | 
        COLOR   ctmp, ct2; | 
| 717 | 
greg | 
2.9 | 
        int     y, i; | 
| 718 | 
  | 
  | 
        register int    x, rg; | 
| 719 | 
greg | 
2.7 | 
                                                /* convert colors */ | 
| 720 | 
  | 
  | 
        for (i = 0; i < 24; i++) { | 
| 721 | 
greg | 
2.13 | 
                copycolor(ctmp, mbRGB[i]); | 
| 722 | 
  | 
  | 
                clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); | 
| 723 | 
  | 
  | 
                setcolr(mbclr[i], colval(ctmp,RED), | 
| 724 | 
  | 
  | 
                                colval(ctmp,GRN), colval(ctmp,BLU)); | 
| 725 | 
greg | 
2.7 | 
                if (inpflags & 1L<<i) { | 
| 726 | 
greg | 
2.13 | 
                        copycolor(ctmp, inpRGB[i]); | 
| 727 | 
  | 
  | 
                        clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); | 
| 728 | 
  | 
  | 
                        setcolr(orclr[i], colval(ctmp,RED), | 
| 729 | 
greg | 
2.7 | 
                                        colval(ctmp,GRN), colval(ctmp,BLU)); | 
| 730 | 
greg | 
2.14 | 
                        if (rawmap) | 
| 731 | 
  | 
  | 
                                copycolr(cvclr[i], mbclr[i]); | 
| 732 | 
  | 
  | 
                        else { | 
| 733 | 
  | 
  | 
                                bresp(ctmp, inpRGB[i]); | 
| 734 | 
  | 
  | 
                                colortrans(ct2, solmat, ctmp); | 
| 735 | 
  | 
  | 
                                clipgamut(ct2, bright(ct2), CGAMUT, | 
| 736 | 
  | 
  | 
                                                cblack, cwhite); | 
| 737 | 
  | 
  | 
                                setcolr(cvclr[i], colval(ct2,RED), | 
| 738 | 
  | 
  | 
                                                colval(ct2,GRN), | 
| 739 | 
  | 
  | 
                                                colval(ct2,BLU)); | 
| 740 | 
  | 
  | 
                        } | 
| 741 | 
greg | 
2.7 | 
                } | 
| 742 | 
  | 
  | 
        } | 
| 743 | 
  | 
  | 
                                                /* allocate scanline */ | 
| 744 | 
  | 
  | 
        scan = (COLR *)malloc(xmax*sizeof(COLR)); | 
| 745 | 
  | 
  | 
        if (scan == NULL) { | 
| 746 | 
  | 
  | 
                perror(progname); | 
| 747 | 
  | 
  | 
                exit(1); | 
| 748 | 
  | 
  | 
        } | 
| 749 | 
  | 
  | 
                                                /* finish debug header */ | 
| 750 | 
  | 
  | 
        fputformat(COLRFMT, debugfp); | 
| 751 | 
  | 
  | 
        putc('\n', debugfp); | 
| 752 | 
  | 
  | 
        fprtresolu(xmax, ymax, debugfp); | 
| 753 | 
  | 
  | 
                                                /* write debug picture */ | 
| 754 | 
  | 
  | 
        for (y = ymax-1; y >= 0; y--) { | 
| 755 | 
greg | 
2.9 | 
                for (x = 0; x < xmax; x++) { | 
| 756 | 
  | 
  | 
                        rg = chartndx(x, y, &i); | 
| 757 | 
  | 
  | 
                        if (rg == RG_CENT) { | 
| 758 | 
greg | 
2.8 | 
                                if (!(1L<<i & gmtflags) || (x+y)&07) | 
| 759 | 
  | 
  | 
                                        copycolr(scan[x], mbclr[i]); | 
| 760 | 
  | 
  | 
                                else | 
| 761 | 
  | 
  | 
                                        copycolr(scan[x], blkclr); | 
| 762 | 
greg | 
2.9 | 
                        } else if (rg == RG_BORD || !(1L<<i & inpflags)) | 
| 763 | 
  | 
  | 
                                copycolr(scan[x], blkclr); | 
| 764 | 
  | 
  | 
                        else if (rg == RG_ORIG) | 
| 765 | 
  | 
  | 
                                copycolr(scan[x], orclr[i]); | 
| 766 | 
  | 
  | 
                        else /* rg == RG_CORR */ | 
| 767 | 
greg | 
2.7 | 
                                copycolr(scan[x], cvclr[i]); | 
| 768 | 
greg | 
2.9 | 
                } | 
| 769 | 
greg | 
2.7 | 
                if (fwritecolrs(scan, xmax, debugfp) < 0) { | 
| 770 | 
  | 
  | 
                        fprintf(stderr, "%s: error writing debugging picture\n", | 
| 771 | 
  | 
  | 
                                        progname); | 
| 772 | 
  | 
  | 
                        exit(1); | 
| 773 | 
  | 
  | 
                } | 
| 774 | 
  | 
  | 
        } | 
| 775 | 
  | 
  | 
                                                /* clean up */ | 
| 776 | 
  | 
  | 
        fclose(debugfp); | 
| 777 | 
greg | 
2.17 | 
        free((void *)scan); | 
| 778 | 
gwlarson | 
2.16 | 
} | 
| 779 | 
  | 
  | 
 | 
| 780 | 
  | 
  | 
 | 
| 781 | 
  | 
  | 
getpos(name, bnds, fp)          /* get boundary position */ | 
| 782 | 
  | 
  | 
char    *name; | 
| 783 | 
  | 
  | 
int     bnds[2]; | 
| 784 | 
  | 
  | 
FILE    *fp; | 
| 785 | 
  | 
  | 
{ | 
| 786 | 
  | 
  | 
        char    buf[64]; | 
| 787 | 
  | 
  | 
 | 
| 788 | 
  | 
  | 
        fprintf(stderr, "\tSelect corner: %s\n", name); | 
| 789 | 
  | 
  | 
        if (fgets(buf, sizeof(buf), fp) == NULL || | 
| 790 | 
  | 
  | 
                        sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) { | 
| 791 | 
  | 
  | 
                fprintf(stderr, "%s: read error from display process\n", | 
| 792 | 
  | 
  | 
                                progname); | 
| 793 | 
  | 
  | 
                exit(1); | 
| 794 | 
  | 
  | 
        } | 
| 795 | 
  | 
  | 
} | 
| 796 | 
  | 
  | 
 | 
| 797 | 
  | 
  | 
 | 
| 798 | 
  | 
  | 
pickchartpos(pfn)               /* display picture and pick chart location */ | 
| 799 | 
  | 
  | 
char    *pfn; | 
| 800 | 
  | 
  | 
{ | 
| 801 | 
  | 
  | 
        char    combuf[512]; | 
| 802 | 
  | 
  | 
        FILE    *pfp; | 
| 803 | 
  | 
  | 
 | 
| 804 | 
  | 
  | 
        sprintf(combuf, DISPCOM, pfn); | 
| 805 | 
  | 
  | 
        if ((pfp = popen(combuf, "r")) == NULL) { | 
| 806 | 
  | 
  | 
                perror(combuf); | 
| 807 | 
  | 
  | 
                exit(1); | 
| 808 | 
  | 
  | 
        } | 
| 809 | 
  | 
  | 
        fputs("Use middle mouse button to select chart corners:\n", stderr); | 
| 810 | 
  | 
  | 
        getpos("upper left (dark skin)", bounds[0], pfp); | 
| 811 | 
  | 
  | 
        getpos("upper right (bluish green)", bounds[1], pfp); | 
| 812 | 
  | 
  | 
        getpos("lower left (white)", bounds[2], pfp); | 
| 813 | 
  | 
  | 
        getpos("lower right (black)", bounds[3], pfp); | 
| 814 | 
  | 
  | 
        fputs("Got it -- quit display program.\n", stderr); | 
| 815 | 
  | 
  | 
        pclose(pfp); | 
| 816 | 
greg | 
2.1 | 
} |