ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
(Generate patch)

Comparing ray/src/px/macbethcal.c (file contents):
Revision 2.4 by greg, Wed Oct 11 20:48:17 1995 UTC vs.
Revision 2.16 by gwlarson, Fri Jul 16 16:12:20 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 Regents of the University of California */
1 > /* Copyright (c) 1999 Regents of the University of California */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
8   * Calibrate a scanned MacBeth Color Checker Chart
9   *
10 < * Produce a .cal file suitable for use with pcomb.
10 > * Produce a .cal file suitable for use with pcomb,
11 > * or .cwp file suitable for use with pcwarp.
12 > *
13 > * Warping code depends on conformance of COLOR and W3VEC types.
14   */
15  
16   #include <stdio.h>
17 + #include <math.h>
18   #ifdef MSDOS
19   #include <fcntl.h>
20   #endif
21   #include "color.h"
22   #include "resolu.h"
23   #include "pmap.h"
24 + #include "warp3d.h"
25  
26                                  /* MacBeth colors */
27   #define DarkSkin        0
# Line 77 | Line 82 | COLOR  mbRGB[24];              /* MacBeth RGB values */
82   #define NMBNEU          6       /* Number of MacBeth neutral colors */
83   short   mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White};
84  
85 < #define NMBMOD          17      /* Number of MacBeth unsaturated colors */
86 < short   mbmod[NMBMOD] = {
82 <                DarkSkin,LightSkin,BlueSky,Foliage,BlueFlower,BluishGreen,
83 <                Orange,PurplishBlue,ModerateRed,Purple,YellowGreen,
84 <                Black,Neutral35,Neutral5,Neutral65,Neutral8,White
85 <        };
85 > #define  NEUFLGS        (1L<<White|1L<<Neutral8|1L<<Neutral65| \
86 >                                1L<<Neutral5|1L<<Neutral35|1L<<Black)
87  
88 < #define NMBSAT          8       /* Number of MacBeth saturated colors */
89 < short   mbsat[NMBSAT] = {
90 <                Red,Green,Blue,Magenta,Yellow,Cyan,
90 <                Orange,OrangeYellow
91 <        };
88 > #define  SATFLGS        (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \
89 >                        1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \
90 >                        1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower)
91  
92 + #define  UNSFLGS        (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \
93 +                        1L<<BluishGreen|1L<<ModerateRed)
94 +
95 + #define  REQFLGS        NEUFLGS                 /* need these colors */
96 + #define  MODFLGS        (NEUFLGS|UNSFLGS)       /* should be in gamut */
97 +
98 + #define  RG_BORD        0       /* patch border */
99 + #define  RG_CENT        01      /* central region of patch */
100 + #define  RG_ORIG        02      /* original color region */
101 + #define  RG_CORR        04      /* corrected color region */
102 +
103 + #ifndef  DISPCOM
104 + #define  DISPCOM        "ximage -op %s"
105 + #endif
106 +
107 + int     scanning = 1;           /* scanned input (or recorded output)? */
108 + double  irrad = 1.0;            /* irradiance multiplication factor */
109 + int     rawmap = 0;             /* put out raw color mapping? */
110 +
111   int     xmax, ymax;             /* input image dimensions */
112   int     bounds[4][2];           /* image coordinates of chart corners */
113   double  imgxfm[3][3];           /* coordinate transformation matrix */
114  
115 < COLOR   picRGB[24];             /* picture colors */
115 > COLOR   inpRGB[24];             /* measured or scanned input colors */
116 > long    inpflags = 0;           /* flags of which colors were input */
117 > long    gmtflags = 0;           /* flags of out-of-gamut colors */
118  
119   COLOR   bramp[NMBNEU][2];       /* brightness ramp (per primary) */
120 < double  solmat[3][3];           /* color mapping matrix */
120 > COLORMAT        solmat;         /* color mapping matrix */
121 > COLOR   colmin, colmax;         /* gamut limits */
122  
123 + WARP3D  *wcor = NULL;           /* color space warp */
124 +
125   FILE    *debugfp = NULL;        /* debug output picture */
126   char    *progname;
127  
# Line 112 | Line 135 | char   **argv;
135          int     i;
136  
137          progname = argv[0];
138 <        if (argc > 2 && !strcmp(argv[1], "-d")) {       /* debug output */
139 <                if ((debugfp = fopen(argv[2], "w")) == NULL) {
140 <                        perror(argv[2]);
141 <                        exit(1);
142 <                }
138 >        for (i = 1; i < argc && argv[i][0] == '-'; i++)
139 >                switch (argv[i][1]) {
140 >                case 'd':                               /* debug output */
141 >                        i++;
142 >                        if (badarg(argc-i, argv+i, "s"))
143 >                                goto userr;
144 >                        if ((debugfp = fopen(argv[i], "w")) == NULL) {
145 >                                perror(argv[i]);
146 >                                exit(1);
147 >                        }
148   #ifdef MSDOS
149 <                setmode(fileno(debugfp), O_BINARY);
149 >                        setmode(fileno(debugfp), O_BINARY);
150   #endif
151 <                newheader("RADIANCE", debugfp);
152 <                printargs(argc, argv, debugfp);
153 <                argv += 2;
154 <                argc -= 2;
155 <        }
156 <        if (argc != 3 && argc != 11)
157 <                goto userr;
158 <        if (strcmp(argv[1], "-") && freopen(argv[1], "r", stdin) == NULL) {
159 <                perror(argv[1]);
151 >                        newheader("RADIANCE", debugfp);         /* start */
152 >                        printargs(argc, argv, debugfp);         /* header */
153 >                        break;
154 >                case 'p':                               /* picture position */
155 >                        if (badarg(argc-i-1, argv+i+1, "iiiiiiii"))
156 >                                goto userr;
157 >                        bounds[0][0] = atoi(argv[++i]);
158 >                        bounds[0][1] = atoi(argv[++i]);
159 >                        bounds[1][0] = atoi(argv[++i]);
160 >                        bounds[1][1] = atoi(argv[++i]);
161 >                        bounds[2][0] = atoi(argv[++i]);
162 >                        bounds[2][1] = atoi(argv[++i]);
163 >                        bounds[3][0] = atoi(argv[++i]);
164 >                        bounds[3][1] = atoi(argv[++i]);
165 >                        scanning = 2;
166 >                        break;
167 >                case 'P':                               /* pick position */
168 >                        scanning = 3;
169 >                        break;
170 >                case 'i':                               /* irradiance factor */
171 >                        i++;
172 >                        if (badarg(argc-i, argv+i, "f"))
173 >                                goto userr;
174 >                        irrad = atof(argv[i]);
175 >                        break;
176 >                case 'm':                               /* raw map output */
177 >                        rawmap = 1;
178 >                        break;
179 >                case 'c':                               /* color input */
180 >                        scanning = 0;
181 >                        break;
182 >                default:
183 >                        goto userr;
184 >                }
185 >                                                        /* open files */
186 >        if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
187 >                perror(argv[i]);
188                  exit(1);
189          }
190 <        if (strcmp(argv[2], "-") && freopen(argv[2], "w", stdout) == NULL) {
191 <                perror(argv[2]);
190 >        if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
191 >                perror(argv[i+1]);
192                  exit(1);
193          }
194 +        if (scanning) {                 /* load input picture header */
195   #ifdef MSDOS
196 <        setmode(fileno(stdin), O_BINARY);
196 >                setmode(fileno(stdin), O_BINARY);
197   #endif
198 <        if (checkheader(stdin, COLRFMT, NULL) < 0 ||
199 <                        fgetresolu(&xmax, &ymax, stdin) < 0) {
200 <                fprintf(stderr, "%s: bad input picture\n", progname);
201 <                exit(1);
202 <        }
203 <                                        /* get chart boundaries */
204 <        if (argc == 11) {
148 <                for (i = 0; i < 4; i++) {
149 <                        if (!isint(argv[2*i+3]) | !isint(argv[2*i+4]))
198 >                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
199 >                                fgetresolu(&xmax, &ymax, stdin) < 0) {
200 >                        fprintf(stderr, "%s: bad input picture\n", progname);
201 >                        exit(1);
202 >                }
203 >                if (scanning == 3) {
204 >                        if (i >= argc)
205                                  goto userr;
206 <                        bounds[i][0] = atoi(argv[2*i+3]);
207 <                        bounds[i][1] = atoi(argv[2*i+4]);
206 >                        pickchartpos(argv[i]);
207 >                        scanning = 2;
208                  }
209 <        } else {
209 >        } else {                        /* else set default xmax and ymax */
210 >                xmax = 512;
211 >                ymax = 2*512/3;
212 >        }
213 >        if (scanning != 2) {            /* use default boundaries */
214                  bounds[0][0] = bounds[2][0] = .029*xmax + .5;
215                  bounds[0][1] = bounds[1][1] = .956*ymax + .5;
216                  bounds[1][0] = bounds[3][0] = .971*xmax + .5;
217                  bounds[2][1] = bounds[3][1] = .056*ymax + .5;
218          }
219          init();                         /* initialize */
220 <        getcolors();                    /* get picture colors */
220 >        if (scanning)                   /* get picture colors */
221 >                getpicture();
222 >        else
223 >                getcolors();
224          compute();                      /* compute color mapping */
225 <                                        /* print comment */
226 <        printf("{ Color correction file computed by %s }\n", progname);
227 <        printf("{ from scanned MacBetch color chart %s }\n", argv[1]);
228 <        putmapping();                   /* put out color mapping */
229 <        putdebug();                     /* put out debug picture */
225 >        if (rawmap) {                   /* print out raw correspondence */
226 >                register int    j;
227 >
228 >                printf("# Color correspondence produced by:\n#\t\t");
229 >                printargs(argc, argv, stdout);
230 >                printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n",
231 >                                i+1 < argc ? argv[i+1] : "{this_file}");
232 >                printf("#\t   Or: pcond [options] -m %s orig.pic > output.pic\n",
233 >                                i+1 < argc ? argv[i+1] : "{this_file}");
234 >                for (j = 0; j < 24; j++)
235 >                        printf("%f %f %f    %f %f %f\n",
236 >                                colval(inpRGB[j],RED), colval(inpRGB[j],GRN),
237 >                                colval(inpRGB[j],BLU), colval(mbRGB[j],RED),
238 >                                colval(mbRGB[j],GRN), colval(mbRGB[j],BLU));
239 >                if (scanning && debugfp != NULL)
240 >                        cwarp();                /* color warp for debugging */
241 >        } else {                        /* print color mapping */
242 >                                                /* print header */
243 >                printf("{\n\tColor correction file computed by:\n\t\t");
244 >                printargs(argc, argv, stdout);
245 >                printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
246 >                                i+1 < argc ? argv[i+1] : "{this_file}");
247 >                if (!scanning)
248 >                        printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n",
249 >                                        i+1 < argc ? argv[i+1] : "{this_file}");
250 >                printf("}\n");
251 >                putmapping();                   /* put out color mapping */
252 >        }
253 >        if (debugfp != NULL)            /* put out debug picture */
254 >                if (scanning)
255 >                        picdebug();
256 >                else
257 >                        clrdebug();
258          exit(0);
259   userr:
260 <        fprintf(stderr, "Usage: %s [-d dbg.pic] input.pic output.cal [xul yul xur yur xll yll xlr ylr]\n",
260 >        fprintf(stderr,
261 > "Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n",
262                          progname);
263 +        fprintf(stderr, "   or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n",
264 +                        progname);
265          exit(1);
266   }
267  
# Line 176 | Line 269 | userr:
269   init()                          /* initialize */
270   {
271          double  quad[4][2];
272 +        register int    i;
273                                          /* make coordinate transformation */
274          quad[0][0] = bounds[0][0];
275          quad[0][1] = bounds[0][1];
# Line 190 | Line 284 | init()                         /* initialize */
284                  fprintf(stderr, "%s: bad chart boundaries\n", progname);
285                  exit(1);
286          }
287 +                                        /* map MacBeth colors to RGB space */
288 +        for (i = 0; i < 24; i++) {
289 +                xyY2RGB(mbRGB[i], mbxyY[i]);
290 +                scalecolor(mbRGB[i], irrad);
291 +        }
292   }
293  
294  
295   int
296 < chartndx(x, y)                          /* find color number for position */
296 > chartndx(x, y, np)                      /* find color number for position */
297   int     x, y;
298 + int     *np;
299   {
300          double  ipos[3], cpos[3];
301          int     ix, iy;
# Line 208 | Line 308 | int    x, y;
308          cpos[0] /= cpos[2];
309          cpos[1] /= cpos[2];
310          if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
311 <                return(-1);
311 >                return(RG_BORD);
312          ix = cpos[0];
313          iy = cpos[1];
314          fx = cpos[0] - ix;
315          fy = cpos[1] - iy;
316 <        if (fx < .35 || fx >= .65 || fy < .35 || fy >= .65)
317 <                return(-1);
318 <        return(iy*6 + ix);
316 >        *np = iy*6 + ix;
317 >        if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65)
318 >                return(RG_CENT);
319 >        if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95)
320 >                return(RG_BORD);
321 >        if (fx >= 0.5)                  /* right side is corrected */
322 >                return(RG_CORR);
323 >        return(RG_ORIG);                /* left side is original */
324   }
325  
326  
327 < getcolors()                             /* load in picture colors */
327 > getpicture()                            /* load in picture colors */
328   {
329          COLR    *scanln;
330          COLOR   pval;
331          int     ccount[24];
332          double  d;
333 <        int     y;
334 <        register int    x, i;
333 >        int     y, i;
334 >        register int    x;
335  
336          scanln = (COLR *)malloc(xmax*sizeof(COLR));
337          if (scanln == NULL) {
# Line 234 | Line 339 | getcolors()                            /* load in picture colors */
339                  exit(1);
340          }
341          for (i = 0; i < 24; i++) {
342 <                setcolor(picRGB[i], 0., 0., 0.);
342 >                setcolor(inpRGB[i], 0., 0., 0.);
343                  ccount[i] = 0;
344          }
345          for (y = ymax-1; y >= 0; y--) {
# Line 243 | Line 348 | getcolors()                            /* load in picture colors */
348                                          progname);
349                          exit(1);
350                  }
351 <                for (x = 0; x < xmax; x++) {
352 <                        i = chartndx(x, y);
248 <                        if (i >= 0) {
351 >                for (x = 0; x < xmax; x++)
352 >                        if (chartndx(x, y, &i) == RG_CENT) {
353                                  colr_color(pval, scanln[x]);
354 <                                addcolor(picRGB[i], pval);
354 >                                addcolor(inpRGB[i], pval);
355                                  ccount[i]++;
356                          }
357 +        }
358 +        for (i = 0; i < 24; i++) {              /* compute averages */
359 +                if (ccount[i] == 0)
360 +                        continue;
361 +                d = 1./ccount[i];
362 +                scalecolor(inpRGB[i], d);
363 +                inpflags |= 1L<<i;
364 +        }
365 +        free((char *)scanln);
366 + }
367 +
368 +
369 + getcolors()                     /* get xyY colors from standard input */
370 + {
371 +        int     gotwhite = 0;
372 +        COLOR   whiteclr;
373 +        int     n;
374 +        float   xyYin[3];
375 +
376 +        while (fgetval(stdin, 'i', &n) == 1) {          /* read colors */
377 +                if (n < 0 | n > 24 ||
378 +                                fgetval(stdin, 'f', &xyYin[0]) != 1 ||
379 +                                fgetval(stdin, 'f', &xyYin[1]) != 1 ||
380 +                                fgetval(stdin, 'f', &xyYin[2]) != 1 ||
381 +                                xyYin[0] < 0. | xyYin[1] < 0. ||
382 +                                xyYin[0] + xyYin[1] > 1.) {
383 +                        fprintf(stderr, "%s: bad color input data\n",
384 +                                        progname);
385 +                        exit(1);
386                  }
387 +                if (n == 0) {                           /* calibration white */
388 +                        xyY2RGB(whiteclr, xyYin);
389 +                        gotwhite++;
390 +                } else {                                /* standard color */
391 +                        n--;
392 +                        xyY2RGB(inpRGB[n], xyYin);
393 +                        inpflags |= 1L<<n;
394 +                }
395          }
396 <        for (i = 0; i < 24; i++) {
397 <                if (ccount[i] == 0) {
398 <                        fprintf(stderr, "%s: bad chart boundaries\n",
396 >                                        /* normalize colors */
397 >        if (!gotwhite) {
398 >                if (!(inpflags & 1L<<White)) {
399 >                        fprintf(stderr, "%s: missing input for White\n",
400                                          progname);
401                          exit(1);
402                  }
403 <                d = 1.0/ccount[i];
404 <                scalecolor(picRGB[i], d);
403 >                setcolor(whiteclr,
404 >                        colval(inpRGB[White],RED)/colval(mbRGB[White],RED),
405 >                        colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN),
406 >                        colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU));
407          }
408 <        free((char *)scanln);
408 >        for (n = 0; n < 24; n++)
409 >                if (inpflags & 1L<<n)
410 >                        setcolor(inpRGB[n],
411 >                                colval(inpRGB[n],RED)/colval(whiteclr,RED),
412 >                                colval(inpRGB[n],GRN)/colval(whiteclr,GRN),
413 >                                colval(inpRGB[n],BLU)/colval(whiteclr,BLU));
414   }
415  
416  
417   bresp(y, x)             /* piecewise linear interpolation of primaries */
418   COLOR   y, x;
419   {
271        double  cv[3];
420          register int    i, n;
421  
422          for (i = 0; i < 3; i++) {
423 <                n = NMBNEU;
424 <                while (n > 0 && colval(x,i) < colval(bramp[--n][0],i))
425 <                        ;
426 <                cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) *
423 >                for (n = 0; n < NMBNEU-2; n++)
424 >                        if (colval(x,i) < colval(bramp[n+1][0],i))
425 >                                break;
426 >                colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
427                                                  colval(bramp[n][1],i) +
428                                  (colval(x,i) - colval(bramp[n][0],i)) *
429                                                  colval(bramp[n+1][1],i)) /
430                          (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
283                if (cv[i] < 0.) cv[i] = 0.;
431          }
285        setcolor(y, cv[0], cv[1], cv[2]);
432   }
433  
434  
435   compute()                       /* compute color mapping */
436   {
437 <        COLOR   clrin[NMBMOD], clrout[NMBMOD];
437 >        COLOR   clrin[24], clrout[24];
438 >        long    cflags;
439          COLOR   ctmp;
440 <        double  d;
441 <        register int    i;
442 <                                        /* map MacBeth colors to RGB space */
443 <        for (i = 0; i < 24; i++) {
444 <                d = mbxyY[i][2] / mbxyY[i][1];
445 <                ctmp[0] = mbxyY[i][0] * d;
299 <                ctmp[1] = mbxyY[i][2];
300 <                ctmp[2] = (1. - mbxyY[i][0] - mbxyY[i][1]) * d;
301 <                cie_rgb(mbRGB[i], ctmp);
440 >        register int    i, n;
441 >                                        /* did we get what we need? */
442 >        if ((inpflags & REQFLGS) != REQFLGS) {
443 >                fprintf(stderr, "%s: missing required input colors\n",
444 >                                progname);
445 >                exit(1);
446          }
447                                          /* compute piecewise luminance curve */
448          for (i = 0; i < NMBNEU; i++) {
449 <                copycolor(bramp[i][0], picRGB[mbneu[i]]);
449 >                copycolor(bramp[i][0], inpRGB[mbneu[i]]);
450                  copycolor(bramp[i][1], mbRGB[mbneu[i]]);
451          }
452 <                                        /* compute color matrix */
453 <        for (i = 0; i < NMBMOD; i++) {
454 <                bresp(clrin[i], picRGB[mbmod[i]]);
455 <                copycolor(clrout[i], mbRGB[mbmod[i]]);
456 <        }
457 <        compsoln(clrin, clrout, NMBMOD);
452 >                                        /* compute color space gamut */
453 >        if (scanning) {
454 >                copycolor(colmin, cblack);
455 >                copycolor(colmax, cwhite);
456 >                scalecolor(colmax, irrad);
457 >        } else
458 >                for (i = 0; i < 3; i++) {
459 >                        colval(colmin,i) = colval(bramp[0][0],i) -
460 >                                colval(bramp[0][1],i) *
461 >                                (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
462 >                                (colval(bramp[1][1],i)-colval(bramp[1][0],i));
463 >                        colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
464 >                                (1.-colval(bramp[NMBNEU-2][1],i)) *
465 >                                (colval(bramp[NMBNEU-1][0],i) -
466 >                                        colval(bramp[NMBNEU-2][0],i)) /
467 >                                (colval(bramp[NMBNEU-1][1],i) -
468 >                                        colval(bramp[NMBNEU-2][1],i));
469 >                }
470 >                                        /* compute color mapping */
471 >        do {
472 >                cflags = inpflags & ~gmtflags;
473 >                n = 0;                          /* compute transform matrix */
474 >                for (i = 0; i < 24; i++)
475 >                        if (cflags & 1L<<i) {
476 >                                bresp(clrin[n], inpRGB[i]);
477 >                                copycolor(clrout[n], mbRGB[i]);
478 >                                n++;
479 >                        }
480 >                compsoln(clrin, clrout, n);
481 >                if (irrad > 0.99 && irrad < 1.01)       /* check gamut */
482 >                        for (i = 0; i < 24; i++)
483 >                                if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
484 >                                        gmtflags |= 1L<<i;
485 >        } while (cflags & gmtflags);
486 >        if (gmtflags & MODFLGS)
487 >                fprintf(stderr,
488 >                "%s: warning - some moderate colors are out of gamut\n",
489 >                                progname);
490   }
491  
492  
493 < putmapping()                    /* put out color mapping for pcomb -f */
493 > putmapping()                    /* put out color mapping */
494   {
495          static char     cchar[3] = {'r', 'g', 'b'};
496          register int    i, j;
# Line 328 | Line 504 | putmapping()                   /* put out color mapping for pcomb -f *
504                  for (i = 0; i < NMBNEU; i++)
505                          printf(",%g", colval(bramp[i][1],j));
506                  printf(");\n");
331                printf("%c = %ci(1);\n", cchar[j], cchar[j]);
507                  printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
508                                  cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
509                                  cchar[j], cchar[j]);
510                  printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
511 <                printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
512 <                                cchar[j], cchar[j], cchar[j],
513 <                                cchar[j], cchar[j], cchar[j]);
511 >                printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
512 >                                cchar[j], scanning?'n':'o', cchar[j],
513 >                                cchar[j], cchar[j], cchar[j], cchar[j]);
514                  printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
515                                  cchar[j], cchar[j], cchar[j],
516                                  cchar[j], cchar[j]);
# Line 343 | Line 518 | putmapping()                   /* put out color mapping for pcomb -f *
518                                  cchar[j], cchar[j], cchar[j], cchar[j]);
519          }
520                                          /* print color mapping */
521 <        printf("ro = %g*rn + %g*gn + %g*bn ;\n",
522 <                        solmat[0][0], solmat[0][1], solmat[0][2]);
523 <        printf("go = %g*rn + %g*gn + %g*bn ;\n",
524 <                        solmat[1][0], solmat[1][1], solmat[1][2]);
525 <        printf("bo = %g*rn + %g*gn + %g*bn ;\n",
526 <                        solmat[2][0], solmat[2][1], solmat[2][2]);
521 >        if (scanning) {
522 >                printf("r = ri(1); g = gi(1); b = bi(1);\n");
523 >                printf("ro = %g*rn + %g*gn + %g*bn ;\n",
524 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
525 >                printf("go = %g*rn + %g*gn + %g*bn ;\n",
526 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
527 >                printf("bo = %g*rn + %g*gn + %g*bn ;\n",
528 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
529 >        } else {
530 >                printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n");
531 >                printf("r = %g*r1 + %g*g1 + %g*b1 ;\n",
532 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
533 >                printf("g = %g*r1 + %g*g1 + %g*b1 ;\n",
534 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
535 >                printf("b = %g*r1 + %g*g1 + %g*b1 ;\n",
536 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
537 >        }
538   }
539  
540  
# Line 362 | Line 548 | int    n;
548          double  colv[3], rowv[3];
549          register int    i, j, k;
550  
551 <        if (n < 3 | n > NMBMOD) {
552 <                fprintf(stderr, "%s: inconsistent code!\n", progname);
551 >        if (n < 3) {
552 >                fprintf(stderr, "%s: too few colors to match!\n", progname);
553                  exit(1);
554          }
555          if (n == 3)
# Line 412 | Line 598 | int    n;
598   }
599  
600  
601 + cwarp()                         /* compute color warp map */
602 + {
603 +        register int    i;
604 +
605 +        if ((wcor = new3dw(W3EXACT)) == NULL)
606 +                goto memerr;
607 +        for (i = 0; i < 24; i++)
608 +                if (!add3dpt(wcor, inpRGB[i], mbRGB[i]))
609 +                        goto memerr;
610 +        return;
611 + memerr:
612 +        perror(progname);
613 +        exit(1);
614 + }
615 +
616 +
617 + int
618   cvtcolor(cout, cin)             /* convert color according to our mapping */
619   COLOR   cout, cin;
620   {
621 <        double  r, g, b;
621 >        COLOR   ctmp;
622 >        int     clipped;
623  
624 <        bresp(cout, cin);
625 <        r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1]
626 <                        + colval(cout,2)*solmat[0][2];
627 <        if (r < 0) r = 0;
628 <        g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1]
629 <                        + colval(cout,2)*solmat[1][2];
630 <        if (g < 0) g = 0;
631 <        b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1]
632 <                        + colval(cout,2)*solmat[2][2];
633 <        if (b < 0) b = 0;
634 <        setcolor(cout, r, g, b);
624 >        if (wcor != NULL) {
625 >                clipped = warp3d(cout, cin, wcor);
626 >                clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax);
627 >        } else if (scanning) {
628 >                bresp(ctmp, cin);
629 >                clipped = cresp(cout, ctmp);
630 >        } else {
631 >                clipped = cresp(ctmp, cin);
632 >                bresp(cout, ctmp);
633 >        }
634 >        return(clipped);
635   }
636  
637  
638 < putdebug()                      /* put out debugging picture */
638 > int
639 > cresp(cout, cin)                /* transform color according to matrix */
640 > COLOR   cout, cin;
641   {
642 +        colortrans(cout, solmat, cin);
643 +        return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
644 + }
645 +
646 +
647 + xyY2RGB(rgbout, xyYin)          /* convert xyY to RGB */
648 + COLOR   rgbout;
649 + register float  xyYin[3];
650 + {
651 +        COLOR   ctmp;
652 +        double  d;
653 +
654 +        d = xyYin[2] / xyYin[1];
655 +        ctmp[0] = xyYin[0] * d;
656 +        ctmp[1] = xyYin[2];
657 +        ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
658 +                                /* allow negative values */
659 +        colortrans(rgbout, xyz2rgbmat, ctmp);
660 + }
661 +
662 +
663 + picdebug()                      /* put out debugging picture */
664 + {
665 +        static COLOR    blkcol = BLKCOLOR;
666          COLOR   *scan;
667 <        int     y;
668 <        register int    x, i;
667 >        int     y, i;
668 >        register int    x, rg;
669  
440        if (debugfp == NULL)
441                return;
670          if (fseek(stdin, 0L, 0) == EOF) {
671                  fprintf(stderr, "%s: cannot seek on input picture\n", progname);
672                  exit(1);
# Line 452 | Line 680 | putdebug()                     /* put out debugging picture */
680                  exit(1);
681          }
682                                                  /* finish debug header */
683 +        fputformat(COLRFMT, debugfp);
684          putc('\n', debugfp);
685          fprtresolu(xmax, ymax, debugfp);
686 +                                                /* write debug picture */
687          for (y = ymax-1; y >= 0; y--) {
688                  if (freadscan(scan, xmax, stdin) < 0) {
689                          fprintf(stderr, "%s: error rereading input picture\n",
# Line 461 | Line 691 | putdebug()                     /* put out debugging picture */
691                          exit(1);
692                  }
693                  for (x = 0; x < xmax; x++) {
694 <                        i = chartndx(x, y);
695 <                        if (i < 0)
694 >                        rg = chartndx(x, y, &i);
695 >                        if (rg == RG_CENT) {
696 >                                if (!(1L<<i & gmtflags) || (x+y)&07) {
697 >                                        copycolor(scan[x], mbRGB[i]);
698 >                                        clipgamut(scan[x], bright(scan[x]),
699 >                                                CGAMUT, colmin, colmax);
700 >                                } else
701 >                                        copycolor(scan[x], blkcol);
702 >                        } else if (rg == RG_CORR)
703                                  cvtcolor(scan[x], scan[x]);
704 <                        else
705 <                                copycolor(scan[x], mbRGB[i]);
704 >                        else if (rg != RG_ORIG)
705 >                                copycolor(scan[x], blkcol);
706                  }
707                  if (fwritescan(scan, xmax, debugfp) < 0) {
708                          fprintf(stderr, "%s: error writing debugging picture\n",
# Line 473 | Line 710 | putdebug()                     /* put out debugging picture */
710                          exit(1);
711                  }
712          }
713 +                                                /* clean up */
714 +        fclose(debugfp);
715          free((char *)scan);
716 + }
717 +
718 +
719 + clrdebug()                      /* put out debug picture from color input */
720 + {
721 +        static COLR     blkclr = BLKCOLR;
722 +        COLR    mbclr[24], cvclr[24], orclr[24];
723 +        COLR    *scan;
724 +        COLOR   ctmp, ct2;
725 +        int     y, i;
726 +        register int    x, rg;
727 +                                                /* convert colors */
728 +        for (i = 0; i < 24; i++) {
729 +                copycolor(ctmp, mbRGB[i]);
730 +                clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
731 +                setcolr(mbclr[i], colval(ctmp,RED),
732 +                                colval(ctmp,GRN), colval(ctmp,BLU));
733 +                if (inpflags & 1L<<i) {
734 +                        copycolor(ctmp, inpRGB[i]);
735 +                        clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
736 +                        setcolr(orclr[i], colval(ctmp,RED),
737 +                                        colval(ctmp,GRN), colval(ctmp,BLU));
738 +                        if (rawmap)
739 +                                copycolr(cvclr[i], mbclr[i]);
740 +                        else {
741 +                                bresp(ctmp, inpRGB[i]);
742 +                                colortrans(ct2, solmat, ctmp);
743 +                                clipgamut(ct2, bright(ct2), CGAMUT,
744 +                                                cblack, cwhite);
745 +                                setcolr(cvclr[i], colval(ct2,RED),
746 +                                                colval(ct2,GRN),
747 +                                                colval(ct2,BLU));
748 +                        }
749 +                }
750 +        }
751 +                                                /* allocate scanline */
752 +        scan = (COLR *)malloc(xmax*sizeof(COLR));
753 +        if (scan == NULL) {
754 +                perror(progname);
755 +                exit(1);
756 +        }
757 +                                                /* finish debug header */
758 +        fputformat(COLRFMT, debugfp);
759 +        putc('\n', debugfp);
760 +        fprtresolu(xmax, ymax, debugfp);
761 +                                                /* write debug picture */
762 +        for (y = ymax-1; y >= 0; y--) {
763 +                for (x = 0; x < xmax; x++) {
764 +                        rg = chartndx(x, y, &i);
765 +                        if (rg == RG_CENT) {
766 +                                if (!(1L<<i & gmtflags) || (x+y)&07)
767 +                                        copycolr(scan[x], mbclr[i]);
768 +                                else
769 +                                        copycolr(scan[x], blkclr);
770 +                        } else if (rg == RG_BORD || !(1L<<i & inpflags))
771 +                                copycolr(scan[x], blkclr);
772 +                        else if (rg == RG_ORIG)
773 +                                copycolr(scan[x], orclr[i]);
774 +                        else /* rg == RG_CORR */
775 +                                copycolr(scan[x], cvclr[i]);
776 +                }
777 +                if (fwritecolrs(scan, xmax, debugfp) < 0) {
778 +                        fprintf(stderr, "%s: error writing debugging picture\n",
779 +                                        progname);
780 +                        exit(1);
781 +                }
782 +        }
783 +                                                /* clean up */
784 +        fclose(debugfp);
785 +        free((char *)scan);
786 + }
787 +
788 +
789 + getpos(name, bnds, fp)          /* get boundary position */
790 + char    *name;
791 + int     bnds[2];
792 + FILE    *fp;
793 + {
794 +        char    buf[64];
795 +
796 +        fprintf(stderr, "\tSelect corner: %s\n", name);
797 +        if (fgets(buf, sizeof(buf), fp) == NULL ||
798 +                        sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) {
799 +                fprintf(stderr, "%s: read error from display process\n",
800 +                                progname);
801 +                exit(1);
802 +        }
803 + }
804 +
805 +
806 + pickchartpos(pfn)               /* display picture and pick chart location */
807 + char    *pfn;
808 + {
809 +        char    combuf[512];
810 +        FILE    *pfp;
811 +
812 +        sprintf(combuf, DISPCOM, pfn);
813 +        if ((pfp = popen(combuf, "r")) == NULL) {
814 +                perror(combuf);
815 +                exit(1);
816 +        }
817 +        fputs("Use middle mouse button to select chart corners:\n", stderr);
818 +        getpos("upper left (dark skin)", bounds[0], pfp);
819 +        getpos("upper right (bluish green)", bounds[1], pfp);
820 +        getpos("lower left (white)", bounds[2], pfp);
821 +        getpos("lower right (black)", bounds[3], pfp);
822 +        fputs("Got it -- quit display program.\n", stderr);
823 +        pclose(pfp);
824   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines