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.17 by greg, Sat Feb 22 02:07:27 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines