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.21 by schorsch, Mon Oct 27 10:24:51 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 < #ifdef MSDOS
15 < #include <fcntl.h>
16 < #endif
14 > #include <math.h>
15 > #include <time.h>
16 >
17 > #include "platform.h"
18 > #include "rtprocess.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 130 | Line 141 | char   **argv;
141                                  perror(argv[i]);
142                                  exit(1);
143                          }
144 < #ifdef MSDOS
134 <                        setmode(fileno(debugfp), O_BINARY);
135 < #endif
144 >                        SET_FILE_BINARY(debugfp);
145                          newheader("RADIANCE", debugfp);         /* start */
146                          printargs(argc, argv, debugfp);         /* header */
147                          break;
# Line 147 | Line 156 | char   **argv;
156                          bounds[2][1] = atoi(argv[++i]);
157                          bounds[3][0] = atoi(argv[++i]);
158                          bounds[3][1] = atoi(argv[++i]);
159 <                        inpispic = 2;
159 >                        scanning = 2;
160                          break;
161 +                case 'P':                               /* pick position */
162 +                        scanning = 3;
163 +                        break;
164 +                case 'i':                               /* irradiance factor */
165 +                        i++;
166 +                        if (badarg(argc-i, argv+i, "f"))
167 +                                goto userr;
168 +                        irrad = atof(argv[i]);
169 +                        break;
170 +                case 'm':                               /* raw map output */
171 +                        rawmap = 1;
172 +                        break;
173                  case 'c':                               /* color input */
174 <                        inpispic = 0;
174 >                        scanning = 0;
175                          break;
176                  default:
177                          goto userr;
178                  }
179                                                          /* open files */
180          if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
181 <                perror(argv[1]);
181 >                perror(argv[i]);
182                  exit(1);
183          }
184          if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
185 <                perror(argv[2]);
185 >                perror(argv[i+1]);
186                  exit(1);
187          }
188 <        if (inpispic) {                 /* load input picture header */
189 < #ifdef MSDOS
169 <                setmode(fileno(stdin), O_BINARY);
170 < #endif
188 >        if (scanning) {                 /* load input picture header */
189 >                SET_FILE_BINARY(stdin);
190                  if (checkheader(stdin, COLRFMT, NULL) < 0 ||
191                                  fgetresolu(&xmax, &ymax, stdin) < 0) {
192                          fprintf(stderr, "%s: bad input picture\n", progname);
193                          exit(1);
194                  }
195 +                if (scanning == 3) {
196 +                        if (i >= argc)
197 +                                goto userr;
198 +                        pickchartpos(argv[i]);
199 +                        scanning = 2;
200 +                }
201          } else {                        /* else set default xmax and ymax */
202                  xmax = 512;
203                  ymax = 2*512/3;
204          }
205 <        if (inpispic != 2) {            /* use default boundaries */
205 >        if (scanning != 2) {            /* use default boundaries */
206                  bounds[0][0] = bounds[2][0] = .029*xmax + .5;
207                  bounds[0][1] = bounds[1][1] = .956*ymax + .5;
208                  bounds[1][0] = bounds[3][0] = .971*xmax + .5;
209                  bounds[2][1] = bounds[3][1] = .056*ymax + .5;
210          }
211          init();                         /* initialize */
212 <        if (inpispic)                   /* get picture colors */
212 >        if (scanning)                   /* get picture colors */
213                  getpicture();
214          else
215                  getcolors();
216          compute();                      /* compute color mapping */
217 <                                        /* print comment */
218 <        printf("{ Color correction file computed by:\n\t");
219 <        printargs(argc, argv, stdout);
220 <        printf("}\n");
221 <        putmapping();                   /* put out color mapping */
222 <        if (debugfp != NULL)            /* put out debug picture */
223 <                if (inpispic)
217 >        if (rawmap) {                   /* print out raw correspondence */
218 >                register int    j;
219 >
220 >                printf("# Color correspondence produced by:\n#\t\t");
221 >                printargs(argc, argv, stdout);
222 >                printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n",
223 >                                i+1 < argc ? argv[i+1] : "{this_file}");
224 >                printf("#\t   Or: pcond [options] -m %s orig.pic > output.pic\n",
225 >                                i+1 < argc ? argv[i+1] : "{this_file}");
226 >                for (j = 0; j < 24; j++)
227 >                        printf("%f %f %f    %f %f %f\n",
228 >                                colval(inpRGB[j],RED), colval(inpRGB[j],GRN),
229 >                                colval(inpRGB[j],BLU), colval(mbRGB[j],RED),
230 >                                colval(mbRGB[j],GRN), colval(mbRGB[j],BLU));
231 >                if (scanning && debugfp != NULL)
232 >                        cwarp();                /* color warp for debugging */
233 >        } else {                        /* print color mapping */
234 >                                                /* print header */
235 >                printf("{\n\tColor correction file computed by:\n\t\t");
236 >                printargs(argc, argv, stdout);
237 >                printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
238 >                                i+1 < argc ? argv[i+1] : "{this_file}");
239 >                if (!scanning)
240 >                        printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n",
241 >                                        i+1 < argc ? argv[i+1] : "{this_file}");
242 >                printf("}\n");
243 >                putmapping();                   /* put out color mapping */
244 >        }
245 >        if (debugfp != NULL) {          /* put out debug picture */
246 >                if (scanning)
247                          picdebug();
248                  else
249                          clrdebug();
250 +        }
251          exit(0);
252   userr:
253          fprintf(stderr,
254 < "Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n",
254 > "Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n",
255                          progname);
256 <        fprintf(stderr, "   or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n",
256 >        fprintf(stderr, "   or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n",
257                          progname);
258          exit(1);
259   }
# Line 229 | Line 278 | init()                         /* initialize */
278                  exit(1);
279          }
280                                          /* map MacBeth colors to RGB space */
281 <        for (i = 0; i < 24; i++)
281 >        for (i = 0; i < 24; i++) {
282                  xyY2RGB(mbRGB[i], mbxyY[i]);
283 +                scalecolor(mbRGB[i], irrad);
284 +        }
285   }
286  
287  
288   int
289 < chartndx(x, y, cvg)                     /* find color number for position */
289 > chartndx(x, y, np)                      /* find color number for position */
290   int     x, y;
291 < double  cvg;
291 > int     *np;
292   {
293          double  ipos[3], cpos[3];
294          int     ix, iy;
295          double  fx, fy;
245        double  cmin, cmax;
296  
297          ipos[0] = x;
298          ipos[1] = y;
# Line 251 | Line 301 | double cvg;
301          cpos[0] /= cpos[2];
302          cpos[1] /= cpos[2];
303          if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
304 <                return(-1);
304 >                return(RG_BORD);
305          ix = cpos[0];
306          iy = cpos[1];
307          fx = cpos[0] - ix;
308          fy = cpos[1] - iy;
309 <        cmin = .5*(1.-cvg);
310 <        cmax = 1. - cmin;
311 <        if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax)
312 <                return(-1);
313 <        return(iy*6 + ix);
309 >        *np = iy*6 + ix;
310 >        if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65)
311 >                return(RG_CENT);
312 >        if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95)
313 >                return(RG_BORD);
314 >        if (fx >= 0.5)                  /* right side is corrected */
315 >                return(RG_CORR);
316 >        return(RG_ORIG);                /* left side is original */
317   }
318  
319  
# Line 270 | Line 323 | getpicture()                           /* load in picture colors */
323          COLOR   pval;
324          int     ccount[24];
325          double  d;
326 <        int     y;
327 <        register int    x, i;
326 >        int     y, i;
327 >        register int    x;
328  
329          scanln = (COLR *)malloc(xmax*sizeof(COLR));
330          if (scanln == NULL) {
# Line 288 | Line 341 | getpicture()                           /* load in picture colors */
341                                          progname);
342                          exit(1);
343                  }
344 <                for (x = 0; x < xmax; x++) {
345 <                        i = chartndx(x, y, CENTCVG);
293 <                        if (i >= 0) {
344 >                for (x = 0; x < xmax; x++)
345 >                        if (chartndx(x, y, &i) == RG_CENT) {
346                                  colr_color(pval, scanln[x]);
347                                  addcolor(inpRGB[i], pval);
348                                  ccount[i]++;
349                          }
298                }
350          }
351          for (i = 0; i < 24; i++) {              /* compute averages */
352                  if (ccount[i] == 0)
# Line 304 | Line 355 | getpicture()                           /* load in picture colors */
355                  scalecolor(inpRGB[i], d);
356                  inpflags |= 1L<<i;
357          }
358 <        free((char *)scanln);
358 >        free((void *)scanln);
359   }
360  
361  
# Line 316 | Line 367 | getcolors()                    /* get xyY colors from standard input */
367          float   xyYin[3];
368  
369          while (fgetval(stdin, 'i', &n) == 1) {          /* read colors */
370 <                if (n < 0 | n > 24 ||
370 >                if ((n < 0) | (n > 24) ||
371                                  fgetval(stdin, 'f', &xyYin[0]) != 1 ||
372                                  fgetval(stdin, 'f', &xyYin[1]) != 1 ||
373                                  fgetval(stdin, 'f', &xyYin[2]) != 1 ||
374 <                                xyYin[0] < 0. | xyYin[0] > 1. |
375 <                                xyYin[1] < 0. | xyYin[1] > 1.) {
374 >                                (xyYin[0] < 0.) | (xyYin[1] < 0.) ||
375 >                                xyYin[0] + xyYin[1] > 1.) {
376                          fprintf(stderr, "%s: bad color input data\n",
377                                          progname);
378                          exit(1);
# Line 359 | Line 410 | getcolors()                    /* get xyY colors from standard input */
410   bresp(y, x)             /* piecewise linear interpolation of primaries */
411   COLOR   y, x;
412   {
362        double  cv[3];
413          register int    i, n;
414  
415          for (i = 0; i < 3; i++) {
416                  for (n = 0; n < NMBNEU-2; n++)
417                          if (colval(x,i) < colval(bramp[n+1][0],i))
418                                  break;
419 <                cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) *
419 >                colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
420                                                  colval(bramp[n][1],i) +
421                                  (colval(x,i) - colval(bramp[n][0],i)) *
422                                                  colval(bramp[n+1][1],i)) /
423                          (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
374                if (cv[i] < 0.) cv[i] = 0.;
424          }
376        setcolor(y, cv[0], cv[1], cv[2]);
425   }
426  
427  
428   compute()                       /* compute color mapping */
429   {
430 <        COLOR   clrin[NMBMOD], clrout[NMBMOD];
430 >        COLOR   clrin[24], clrout[24];
431 >        long    cflags;
432 >        COLOR   ctmp;
433          register int    i, n;
434                                          /* did we get what we need? */
435          if ((inpflags & REQFLGS) != REQFLGS) {
# Line 392 | Line 442 | compute()                      /* compute color mapping */
442                  copycolor(bramp[i][0], inpRGB[mbneu[i]]);
443                  copycolor(bramp[i][1], mbRGB[mbneu[i]]);
444          }
445 <                                        /* compute color matrix */
446 <        for (n = 0, i = 0; i < NMBMOD; i++)
447 <                if (inpflags & 1L<<mbmod[i]) {
448 <                        bresp(clrin[n], inpRGB[mbmod[i]]);
449 <                        copycolor(clrout[n], mbRGB[mbmod[i]]);
450 <                        n++;
445 >                                        /* compute color space gamut */
446 >        if (scanning) {
447 >                copycolor(colmin, cblack);
448 >                copycolor(colmax, cwhite);
449 >                scalecolor(colmax, irrad);
450 >        } else
451 >                for (i = 0; i < 3; i++) {
452 >                        colval(colmin,i) = colval(bramp[0][0],i) -
453 >                                colval(bramp[0][1],i) *
454 >                                (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
455 >                                (colval(bramp[1][1],i)-colval(bramp[1][0],i));
456 >                        colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
457 >                                (1.-colval(bramp[NMBNEU-2][1],i)) *
458 >                                (colval(bramp[NMBNEU-1][0],i) -
459 >                                        colval(bramp[NMBNEU-2][0],i)) /
460 >                                (colval(bramp[NMBNEU-1][1],i) -
461 >                                        colval(bramp[NMBNEU-2][1],i));
462                  }
463 <        compsoln(clrin, clrout, n);
463 >                                        /* compute color mapping */
464 >        do {
465 >                cflags = inpflags & ~gmtflags;
466 >                n = 0;                          /* compute transform matrix */
467 >                for (i = 0; i < 24; i++)
468 >                        if (cflags & 1L<<i) {
469 >                                bresp(clrin[n], inpRGB[i]);
470 >                                copycolor(clrout[n], mbRGB[i]);
471 >                                n++;
472 >                        }
473 >                compsoln(clrin, clrout, n);
474 >                if (irrad > 0.99 && irrad < 1.01)       /* check gamut */
475 >                        for (i = 0; i < 24; i++)
476 >                                if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
477 >                                        gmtflags |= 1L<<i;
478 >        } while (cflags & gmtflags);
479 >        if (gmtflags & MODFLGS)
480 >                fprintf(stderr,
481 >                "%s: warning - some moderate colors are out of gamut\n",
482 >                                progname);
483   }
484  
485  
486 < putmapping()                    /* put out color mapping for pcomb -f */
486 > putmapping()                    /* put out color mapping */
487   {
488          static char     cchar[3] = {'r', 'g', 'b'};
489          register int    i, j;
# Line 417 | Line 497 | putmapping()                   /* put out color mapping for pcomb -f *
497                  for (i = 0; i < NMBNEU; i++)
498                          printf(",%g", colval(bramp[i][1],j));
499                  printf(");\n");
420                printf("%c = %ci(1);\n", cchar[j], cchar[j]);
500                  printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
501                                  cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
502                                  cchar[j], cchar[j]);
503                  printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
504 <                printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
505 <                                cchar[j], cchar[j], cchar[j],
506 <                                cchar[j], cchar[j], cchar[j]);
504 >                printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
505 >                                cchar[j], scanning?'n':'o', cchar[j],
506 >                                cchar[j], cchar[j], cchar[j], cchar[j]);
507                  printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
508                                  cchar[j], cchar[j], cchar[j],
509                                  cchar[j], cchar[j]);
# Line 432 | Line 511 | putmapping()                   /* put out color mapping for pcomb -f *
511                                  cchar[j], cchar[j], cchar[j], cchar[j]);
512          }
513                                          /* print color mapping */
514 <        printf("ro = %g*rn + %g*gn + %g*bn ;\n",
515 <                        solmat[0][0], solmat[0][1], solmat[0][2]);
516 <        printf("go = %g*rn + %g*gn + %g*bn ;\n",
517 <                        solmat[1][0], solmat[1][1], solmat[1][2]);
518 <        printf("bo = %g*rn + %g*gn + %g*bn ;\n",
519 <                        solmat[2][0], solmat[2][1], solmat[2][2]);
514 >        if (scanning) {
515 >                printf("r = ri(1); g = gi(1); b = bi(1);\n");
516 >                printf("ro = %g*rn + %g*gn + %g*bn ;\n",
517 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
518 >                printf("go = %g*rn + %g*gn + %g*bn ;\n",
519 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
520 >                printf("bo = %g*rn + %g*gn + %g*bn ;\n",
521 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
522 >        } else {
523 >                printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n");
524 >                printf("r = %g*r1 + %g*g1 + %g*b1 ;\n",
525 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
526 >                printf("g = %g*r1 + %g*g1 + %g*b1 ;\n",
527 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
528 >                printf("b = %g*r1 + %g*g1 + %g*b1 ;\n",
529 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
530 >        }
531   }
532  
533  
# Line 451 | Line 541 | int    n;
541          double  colv[3], rowv[3];
542          register int    i, j, k;
543  
544 <        if (n < 3 | n > NMBMOD) {
545 <                fprintf(stderr, "%s: bad number of colors to match\n", progname);
544 >        if (n < 3) {
545 >                fprintf(stderr, "%s: too few colors to match!\n", progname);
546                  exit(1);
547          }
548          if (n == 3)
# Line 501 | Line 591 | int    n;
591   }
592  
593  
594 + cwarp()                         /* compute color warp map */
595 + {
596 +        register int    i;
597 +
598 +        if ((wcor = new3dw(W3EXACT)) == NULL)
599 +                goto memerr;
600 +        for (i = 0; i < 24; i++)
601 +                if (!add3dpt(wcor, inpRGB[i], mbRGB[i]))
602 +                        goto memerr;
603 +        return;
604 + memerr:
605 +        perror(progname);
606 +        exit(1);
607 + }
608 +
609 +
610 + int
611   cvtcolor(cout, cin)             /* convert color according to our mapping */
612   COLOR   cout, cin;
613   {
614 <        double  r, g, b;
614 >        COLOR   ctmp;
615 >        int     clipped;
616  
617 <        bresp(cout, cin);
618 <        r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1]
619 <                        + colval(cout,2)*solmat[0][2];
620 <        if (r < 0) r = 0;
621 <        g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1]
622 <                        + colval(cout,2)*solmat[1][2];
623 <        if (g < 0) g = 0;
624 <        b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1]
625 <                        + colval(cout,2)*solmat[2][2];
626 <        if (b < 0) b = 0;
627 <        setcolor(cout, r, g, b);
617 >        if (wcor != NULL) {
618 >                clipped = warp3d(cout, cin, wcor);
619 >                clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax);
620 >        } else if (scanning) {
621 >                bresp(ctmp, cin);
622 >                clipped = cresp(cout, ctmp);
623 >        } else {
624 >                clipped = cresp(ctmp, cin);
625 >                bresp(cout, ctmp);
626 >        }
627 >        return(clipped);
628   }
629  
630  
631 + int
632 + cresp(cout, cin)                /* transform color according to matrix */
633 + COLOR   cout, cin;
634 + {
635 +        colortrans(cout, solmat, cin);
636 +        return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
637 + }
638 +
639 +
640   xyY2RGB(rgbout, xyYin)          /* convert xyY to RGB */
641   COLOR   rgbout;
642   register float  xyYin[3];
# Line 531 | Line 648 | register float xyYin[3];
648          ctmp[0] = xyYin[0] * d;
649          ctmp[1] = xyYin[2];
650          ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
651 <        cie_rgb(rgbout, ctmp);
651 >                                /* allow negative values */
652 >        colortrans(rgbout, xyz2rgbmat, ctmp);
653   }
654  
655  
656   picdebug()                      /* put out debugging picture */
657   {
658 +        static COLOR    blkcol = BLKCOLOR;
659          COLOR   *scan;
660 <        int     y;
661 <        register int    x, i;
660 >        int     y, i;
661 >        register int    x, rg;
662  
663          if (fseek(stdin, 0L, 0) == EOF) {
664                  fprintf(stderr, "%s: cannot seek on input picture\n", progname);
# Line 565 | Line 684 | picdebug()                     /* put out debugging picture */
684                          exit(1);
685                  }
686                  for (x = 0; x < xmax; x++) {
687 <                        i = chartndx(x, y, CENTCVG);
688 <                        if (i < 0)
687 >                        rg = chartndx(x, y, &i);
688 >                        if (rg == RG_CENT) {
689 >                                if (!(1L<<i & gmtflags) || (x+y)&07) {
690 >                                        copycolor(scan[x], mbRGB[i]);
691 >                                        clipgamut(scan[x], bright(scan[x]),
692 >                                                CGAMUT, colmin, colmax);
693 >                                } else
694 >                                        copycolor(scan[x], blkcol);
695 >                        } else if (rg == RG_CORR)
696                                  cvtcolor(scan[x], scan[x]);
697 <                        else
698 <                                copycolor(scan[x], mbRGB[i]);
697 >                        else if (rg != RG_ORIG)
698 >                                copycolor(scan[x], blkcol);
699                  }
700                  if (fwritescan(scan, xmax, debugfp) < 0) {
701                          fprintf(stderr, "%s: error writing debugging picture\n",
# Line 579 | Line 705 | picdebug()                     /* put out debugging picture */
705          }
706                                                  /* clean up */
707          fclose(debugfp);
708 <        free((char *)scan);
708 >        free((void *)scan);
709   }
710  
711  
712   clrdebug()                      /* put out debug picture from color input */
713   {
714          static COLR     blkclr = BLKCOLR;
715 <        COLR    mbclr[24], cvclr[24];
715 >        COLR    mbclr[24], cvclr[24], orclr[24];
716          COLR    *scan;
717 <        COLOR   ctmp;
718 <        int     y;
719 <        register int    i, x;
717 >        COLOR   ctmp, ct2;
718 >        int     y, i;
719 >        register int    x, rg;
720                                                  /* convert colors */
721          for (i = 0; i < 24; i++) {
722 <                setcolr(mbclr[i], colval(mbRGB[i],RED),
723 <                                colval(mbRGB[i],GRN), colval(mbRGB[i],BLU));
722 >                copycolor(ctmp, mbRGB[i]);
723 >                clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
724 >                setcolr(mbclr[i], colval(ctmp,RED),
725 >                                colval(ctmp,GRN), colval(ctmp,BLU));
726                  if (inpflags & 1L<<i) {
727 <                        cvtcolor(ctmp, inpRGB[i]);
728 <                        setcolr(cvclr[i], colval(ctmp,RED),
727 >                        copycolor(ctmp, inpRGB[i]);
728 >                        clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
729 >                        setcolr(orclr[i], colval(ctmp,RED),
730                                          colval(ctmp,GRN), colval(ctmp,BLU));
731 +                        if (rawmap)
732 +                                copycolr(cvclr[i], mbclr[i]);
733 +                        else {
734 +                                bresp(ctmp, inpRGB[i]);
735 +                                colortrans(ct2, solmat, ctmp);
736 +                                clipgamut(ct2, bright(ct2), CGAMUT,
737 +                                                cblack, cwhite);
738 +                                setcolr(cvclr[i], colval(ct2,RED),
739 +                                                colval(ct2,GRN),
740 +                                                colval(ct2,BLU));
741 +                        }
742                  }
743          }
744                                                  /* allocate scanline */
# Line 613 | Line 753 | clrdebug()                     /* put out debug picture from color input
753          fprtresolu(xmax, ymax, debugfp);
754                                                  /* write debug picture */
755          for (y = ymax-1; y >= 0; y--) {
756 <                for (x = 0; x < xmax; x++)
757 <                        if ((i = chartndx(x, y, CENTCVG)) >= 0)
758 <                                copycolr(scan[x], mbclr[i]);
759 <                        else if ((i = chartndx(x, y, FULLCVG)) >= 0 &&
760 <                                        inpflags & 1L<<i)
761 <                                copycolr(scan[x], cvclr[i]);
762 <                        else
756 >                for (x = 0; x < xmax; x++) {
757 >                        rg = chartndx(x, y, &i);
758 >                        if (rg == RG_CENT) {
759 >                                if (!(1L<<i & gmtflags) || (x+y)&07)
760 >                                        copycolr(scan[x], mbclr[i]);
761 >                                else
762 >                                        copycolr(scan[x], blkclr);
763 >                        } else if (rg == RG_BORD || !(1L<<i & inpflags))
764                                  copycolr(scan[x], blkclr);
765 +                        else if (rg == RG_ORIG)
766 +                                copycolr(scan[x], orclr[i]);
767 +                        else /* rg == RG_CORR */
768 +                                copycolr(scan[x], cvclr[i]);
769 +                }
770                  if (fwritecolrs(scan, xmax, debugfp) < 0) {
771                          fprintf(stderr, "%s: error writing debugging picture\n",
772                                          progname);
# Line 629 | Line 775 | clrdebug()                     /* put out debug picture from color input
775          }
776                                                  /* clean up */
777          fclose(debugfp);
778 <        free((char *)scan);
778 >        free((void *)scan);
779 > }
780 >
781 >
782 > getpos(name, bnds, fp)          /* get boundary position */
783 > char    *name;
784 > int     bnds[2];
785 > FILE    *fp;
786 > {
787 >        char    buf[64];
788 >
789 >        fprintf(stderr, "\tSelect corner: %s\n", name);
790 >        if (fgets(buf, sizeof(buf), fp) == NULL ||
791 >                        sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) {
792 >                fprintf(stderr, "%s: read error from display process\n",
793 >                                progname);
794 >                exit(1);
795 >        }
796 > }
797 >
798 >
799 > pickchartpos(pfn)               /* display picture and pick chart location */
800 > char    *pfn;
801 > {
802 >        char    combuf[512];
803 >        FILE    *pfp;
804 >
805 >        sprintf(combuf, DISPCOM, pfn);
806 >        if ((pfp = popen(combuf, "r")) == NULL) {
807 >                perror(combuf);
808 >                exit(1);
809 >        }
810 >        fputs("Use middle mouse button to select chart corners:\n", stderr);
811 >        getpos("upper left (dark skin)", bounds[0], pfp);
812 >        getpos("upper right (bluish green)", bounds[1], pfp);
813 >        getpos("lower left (white)", bounds[2], pfp);
814 >        getpos("lower right (black)", bounds[3], pfp);
815 >        fputs("Got it -- quit display program.\n", stderr);
816 >        pclose(pfp);
817   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines