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.7 by greg, Fri Oct 20 15:26:11 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          16      /* Number of MacBeth unsaturated colors */
84 < short   mbmod[NMBMOD] = {
82 <                DarkSkin,LightSkin,BlueSky,Foliage,BlueFlower,BluishGreen,
83 <                PurplishBlue,ModerateRed,YellowGreen,OrangeYellow,
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,Purple
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  REQFLGS        (1L<<White|1L<<Neutral8|1L<<Neutral65| \
91 <                                1L<<Neutral5|1L<<Neutral35|1L<<Black)
90 > #define  UNSFLGS        (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \
91 >                        1L<<BluishGreen|1L<<ModerateRed)
92  
93 < #define  CENTCVG        0.3     /* measured coverage of square sample */
94 < #define  FULLCVG        0.9     /* coverage of entire square */
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   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  
112 extern char     *malloc();
126  
114
127   main(argc, argv)
128   int     argc;
129   char    **argv;
130   {
119        int     inpispic = 1;
131          int     i;
132  
133          progname = argv[0];
# Line 147 | Line 158 | char   **argv;
158                          bounds[2][1] = atoi(argv[++i]);
159                          bounds[3][0] = atoi(argv[++i]);
160                          bounds[3][1] = atoi(argv[++i]);
161 <                        inpispic = 2;
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 <                        inpispic = 0;
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[1]);
183 >                perror(argv[i]);
184                  exit(1);
185          }
186          if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
187 <                perror(argv[2]);
187 >                perror(argv[i+1]);
188                  exit(1);
189          }
190 <        if (inpispic) {                 /* load input picture header */
190 >        if (scanning) {                 /* load input picture header */
191   #ifdef MSDOS
192                  setmode(fileno(stdin), O_BINARY);
193   #endif
# Line 173 | Line 196 | char   **argv;
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 +                        pickchartpos(argv[i]);
203 +                        scanning = 2;
204 +                }
205          } else {                        /* else set default xmax and ymax */
206                  xmax = 512;
207                  ymax = 2*512/3;
208          }
209 <        if (inpispic != 2) {            /* use default boundaries */
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 <        if (inpispic)                   /* 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:\n\t");
223 <        printargs(argc, argv, stdout);
224 <        printf("}\n");
225 <        putmapping();                   /* put out color mapping */
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 (inpispic)
250 >                if (scanning)
251                          picdebug();
252                  else
253                          clrdebug();
254          exit(0);
255   userr:
256          fprintf(stderr,
257 < "Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n",
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] -c [xyY.dat [output.cal]]\n",
259 >        fprintf(stderr, "   or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n",
260                          progname);
261          exit(1);
262   }
# Line 229 | Line 281 | init()                         /* initialize */
281                  exit(1);
282          }
283                                          /* map MacBeth colors to RGB space */
284 <        for (i = 0; i < 24; i++)
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, cvg)                     /* find color number for position */
292 > chartndx(x, y, np)                      /* find color number for position */
293   int     x, y;
294 < double  cvg;
294 > int     *np;
295   {
296          double  ipos[3], cpos[3];
297          int     ix, iy;
298          double  fx, fy;
245        double  cmin, cmax;
299  
300          ipos[0] = x;
301          ipos[1] = y;
# Line 251 | Line 304 | double cvg;
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 <        cmin = .5*(1.-cvg);
313 <        cmax = 1. - cmin;
314 <        if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax)
315 <                return(-1);
316 <        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  
# Line 270 | Line 326 | getpicture()                           /* load in picture colors */
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 288 | Line 344 | getpicture()                           /* load in picture colors */
344                                          progname);
345                          exit(1);
346                  }
347 <                for (x = 0; x < xmax; x++) {
348 <                        i = chartndx(x, y, CENTCVG);
293 <                        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(inpRGB[i], pval);
351                                  ccount[i]++;
352                          }
298                }
353          }
354          for (i = 0; i < 24; i++) {              /* compute averages */
355                  if (ccount[i] == 0)
# Line 304 | Line 358 | getpicture()                           /* load in picture colors */
358                  scalecolor(inpRGB[i], d);
359                  inpflags |= 1L<<i;
360          }
361 <        free((char *)scanln);
361 >        free((void *)scanln);
362   }
363  
364  
# Line 320 | Line 374 | getcolors()                    /* get xyY colors from standard input */
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[0] > 1. |
378 <                                xyYin[1] < 0. | xyYin[1] > 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);
# Line 359 | Line 413 | getcolors()                    /* get xyY colors from standard input */
413   bresp(y, x)             /* piecewise linear interpolation of primaries */
414   COLOR   y, x;
415   {
362        double  cv[3];
416          register int    i, n;
417  
418          for (i = 0; i < 3; i++) {
419                  for (n = 0; n < NMBNEU-2; n++)
420                          if (colval(x,i) < colval(bramp[n+1][0],i))
421                                  break;
422 <                cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) *
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));
374                if (cv[i] < 0.) cv[i] = 0.;
427          }
376        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          register int    i, n;
437                                          /* did we get what we need? */
438          if ((inpflags & REQFLGS) != REQFLGS) {
# Line 392 | Line 445 | compute()                      /* compute color mapping */
445                  copycolor(bramp[i][0], inpRGB[mbneu[i]]);
446                  copycolor(bramp[i][1], mbRGB[mbneu[i]]);
447          }
448 <                                        /* compute color matrix */
449 <        for (n = 0, i = 0; i < NMBMOD; i++)
450 <                if (inpflags & 1L<<mbmod[i]) {
451 <                        bresp(clrin[n], inpRGB[mbmod[i]]);
452 <                        copycolor(clrout[n], mbRGB[mbmod[i]]);
453 <                        n++;
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 <        compsoln(clrin, clrout, n);
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 417 | 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");
420                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 432 | 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 451 | 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: bad number of colors to match\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 501 | 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 + 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];
# Line 531 | Line 651 | register float xyYin[3];
651          ctmp[0] = xyYin[0] * d;
652          ctmp[1] = xyYin[2];
653          ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
654 <        cie_rgb(rgbout, ctmp);
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  
666          if (fseek(stdin, 0L, 0) == EOF) {
667                  fprintf(stderr, "%s: cannot seek on input picture\n", progname);
# Line 565 | Line 687 | picdebug()                     /* put out debugging picture */
687                          exit(1);
688                  }
689                  for (x = 0; x < xmax; x++) {
690 <                        i = chartndx(x, y, CENTCVG);
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 579 | Line 708 | picdebug()                     /* put out debugging picture */
708          }
709                                                  /* clean up */
710          fclose(debugfp);
711 <        free((char *)scan);
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];
718 >        COLR    mbclr[24], cvclr[24], orclr[24];
719          COLR    *scan;
720 <        COLOR   ctmp;
721 <        int     y;
722 <        register int    i, x;
720 >        COLOR   ctmp, ct2;
721 >        int     y, i;
722 >        register int    x, rg;
723                                                  /* convert colors */
724          for (i = 0; i < 24; i++) {
725 <                setcolr(mbclr[i], colval(mbRGB[i],RED),
726 <                                colval(mbRGB[i],GRN), colval(mbRGB[i],BLU));
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 <                        cvtcolor(ctmp, inpRGB[i]);
731 <                        setcolr(cvclr[i], colval(ctmp,RED),
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 */
# Line 613 | Line 756 | clrdebug()                     /* put out debug picture from color input
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 <                        if ((i = chartndx(x, y, CENTCVG)) >= 0)
761 <                                copycolr(scan[x], mbclr[i]);
762 <                        else if ((i = chartndx(x, y, FULLCVG)) >= 0 &&
763 <                                        inpflags & 1L<<i)
764 <                                copycolr(scan[x], cvclr[i]);
765 <                        else
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);
# Line 629 | Line 778 | clrdebug()                     /* put out debug picture from color input
778          }
779                                                  /* clean up */
780          fclose(debugfp);
781 <        free((char *)scan);
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