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.12 by greg, Thu Jan 30 19:14:11 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 Regents of the University of California */
1 > /* Copyright (c) 1997 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 77 | Line 77 | COLOR  mbRGB[24];              /* MacBeth RGB values */
77   #define NMBNEU          6       /* Number of MacBeth neutral colors */
78   short   mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White};
79  
80 < #define NMBMOD          16      /* Number of MacBeth unsaturated colors */
81 < short   mbmod[NMBMOD] = {
82 <                DarkSkin,LightSkin,BlueSky,Foliage,BlueFlower,BluishGreen,
83 <                PurplishBlue,ModerateRed,YellowGreen,OrangeYellow,
84 <                Black,Neutral35,Neutral5,Neutral65,Neutral8,White
85 <        };
80 > #define  NEUFLGS        (1L<<White|1L<<Neutral8|1L<<Neutral65| \
81 >                                1L<<Neutral5|1L<<Neutral35|1L<<Black)
82  
83 < #define NMBSAT          8       /* Number of MacBeth saturated colors */
84 < short   mbsat[NMBSAT] = {
85 <                Red,Green,Blue,Magenta,Yellow,Cyan,
90 <                Orange,Purple
91 <        };
83 > #define  SATFLGS        (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \
84 >                        1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \
85 >                        1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower)
86  
87 < #define  REQFLGS        (1L<<White|1L<<Neutral8|1L<<Neutral65| \
88 <                                1L<<Neutral5|1L<<Neutral35|1L<<Black)
87 > #define  UNSFLGS        (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \
88 >                        1L<<BluishGreen|1L<<ModerateRed)
89  
90 < #define  CENTCVG        0.3     /* measured coverage of square sample */
91 < #define  FULLCVG        0.9     /* coverage of entire square */
90 > #define  REQFLGS        NEUFLGS                 /* need these colors */
91 > #define  MODFLGS        (NEUFLGS|UNSFLGS)       /* should be in gamut */
92  
93 + #define  RG_BORD        0       /* patch border */
94 + #define  RG_CENT        01      /* central region of patch */
95 + #define  RG_ORIG        02      /* original color region */
96 + #define  RG_CORR        04      /* corrected color region */
97 +
98 + int     scanning = 1;           /* scanned input (or recorded output)? */
99 +
100   int     xmax, ymax;             /* input image dimensions */
101   int     bounds[4][2];           /* image coordinates of chart corners */
102   double  imgxfm[3][3];           /* coordinate transformation matrix */
103  
104   COLOR   inpRGB[24];             /* measured or scanned input colors */
105   long    inpflags = 0;           /* flags of which colors were input */
106 + long    gmtflags = 0;           /* flags of out-of-gamut colors */
107  
108   COLOR   bramp[NMBNEU][2];       /* brightness ramp (per primary) */
109   double  solmat[3][3];           /* color mapping matrix */
# Line 116 | Line 118 | main(argc, argv)
118   int     argc;
119   char    **argv;
120   {
119        int     inpispic = 1;
121          int     i;
122  
123          progname = argv[0];
# Line 147 | Line 148 | char   **argv;
148                          bounds[2][1] = atoi(argv[++i]);
149                          bounds[3][0] = atoi(argv[++i]);
150                          bounds[3][1] = atoi(argv[++i]);
151 <                        inpispic = 2;
151 >                        scanning = 2;
152                          break;
153                  case 'c':                               /* color input */
154 <                        inpispic = 0;
154 >                        scanning = 0;
155                          break;
156                  default:
157                          goto userr;
# Line 164 | Line 165 | char   **argv;
165                  perror(argv[2]);
166                  exit(1);
167          }
168 <        if (inpispic) {                 /* load input picture header */
168 >        if (scanning) {                 /* load input picture header */
169   #ifdef MSDOS
170                  setmode(fileno(stdin), O_BINARY);
171   #endif
# Line 177 | Line 178 | char   **argv;
178                  xmax = 512;
179                  ymax = 2*512/3;
180          }
181 <        if (inpispic != 2) {            /* use default boundaries */
181 >        if (scanning != 2) {            /* use default boundaries */
182                  bounds[0][0] = bounds[2][0] = .029*xmax + .5;
183                  bounds[0][1] = bounds[1][1] = .956*ymax + .5;
184                  bounds[1][0] = bounds[3][0] = .971*xmax + .5;
185                  bounds[2][1] = bounds[3][1] = .056*ymax + .5;
186          }
187          init();                         /* initialize */
188 <        if (inpispic)                   /* get picture colors */
188 >        if (scanning)                   /* get picture colors */
189                  getpicture();
190          else
191                  getcolors();
192          compute();                      /* compute color mapping */
193                                          /* print comment */
194 <        printf("{ Color correction file computed by:\n\t");
194 >        printf("{\n\tColor correction file computed by:\n\t\t");
195          printargs(argc, argv, stdout);
196 +        printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
197 +                        i+1 < argc ? argv[i+1] : "{this_file}");
198 +        if (!scanning)
199 +                printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n",
200 +                                i+1 < argc ? argv[i+1] : "{this_file}");
201          printf("}\n");
202          putmapping();                   /* put out color mapping */
203          if (debugfp != NULL)            /* put out debug picture */
204 <                if (inpispic)
204 >                if (scanning)
205                          picdebug();
206                  else
207                          clrdebug();
# Line 235 | Line 241 | init()                         /* initialize */
241  
242  
243   int
244 < chartndx(x, y, cvg)                     /* find color number for position */
244 > chartndx(x, y, np)                      /* find color number for position */
245   int     x, y;
246 < double  cvg;
246 > int     *np;
247   {
248          double  ipos[3], cpos[3];
249          int     ix, iy;
250          double  fx, fy;
245        double  cmin, cmax;
251  
252          ipos[0] = x;
253          ipos[1] = y;
# Line 251 | Line 256 | double cvg;
256          cpos[0] /= cpos[2];
257          cpos[1] /= cpos[2];
258          if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
259 <                return(-1);
259 >                return(RG_BORD);
260          ix = cpos[0];
261          iy = cpos[1];
262          fx = cpos[0] - ix;
263          fy = cpos[1] - iy;
264 <        cmin = .5*(1.-cvg);
265 <        cmax = 1. - cmin;
266 <        if (fx < cmin || fx >= cmax || fy < cmin || fy >= cmax)
267 <                return(-1);
268 <        return(iy*6 + ix);
264 >        *np = iy*6 + ix;
265 >        if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65)
266 >                return(RG_CENT);
267 >        if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95)
268 >                return(RG_BORD);
269 >        if (fx >= 0.5)                  /* right side is corrected */
270 >                return(RG_CORR);
271 >        return(RG_ORIG);                /* left side is original */
272   }
273  
274  
# Line 270 | Line 278 | getpicture()                           /* load in picture colors */
278          COLOR   pval;
279          int     ccount[24];
280          double  d;
281 <        int     y;
282 <        register int    x, i;
281 >        int     y, i;
282 >        register int    x;
283  
284          scanln = (COLR *)malloc(xmax*sizeof(COLR));
285          if (scanln == NULL) {
# Line 288 | Line 296 | getpicture()                           /* load in picture colors */
296                                          progname);
297                          exit(1);
298                  }
299 <                for (x = 0; x < xmax; x++) {
300 <                        i = chartndx(x, y, CENTCVG);
293 <                        if (i >= 0) {
299 >                for (x = 0; x < xmax; x++)
300 >                        if (chartndx(x, y, &i) == RG_CENT) {
301                                  colr_color(pval, scanln[x]);
302                                  addcolor(inpRGB[i], pval);
303                                  ccount[i]++;
304                          }
298                }
305          }
306          for (i = 0; i < 24; i++) {              /* compute averages */
307                  if (ccount[i] == 0)
# Line 320 | Line 326 | getcolors()                    /* get xyY colors from standard input */
326                                  fgetval(stdin, 'f', &xyYin[0]) != 1 ||
327                                  fgetval(stdin, 'f', &xyYin[1]) != 1 ||
328                                  fgetval(stdin, 'f', &xyYin[2]) != 1 ||
329 <                                xyYin[0] < 0. | xyYin[0] > 1. |
330 <                                xyYin[1] < 0. | xyYin[1] > 1.) {
329 >                                xyYin[0] < 0. | xyYin[1] < 0. ||
330 >                                xyYin[0] + xyYin[1] > 1.) {
331                          fprintf(stderr, "%s: bad color input data\n",
332                                          progname);
333                          exit(1);
# Line 359 | Line 365 | getcolors()                    /* get xyY colors from standard input */
365   bresp(y, x)             /* piecewise linear interpolation of primaries */
366   COLOR   y, x;
367   {
362        double  cv[3];
368          register int    i, n;
369  
370          for (i = 0; i < 3; i++) {
371                  for (n = 0; n < NMBNEU-2; n++)
372                          if (colval(x,i) < colval(bramp[n+1][0],i))
373                                  break;
374 <                cv[i] = ((colval(bramp[n+1][0],i) - colval(x,i)) *
374 >                colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
375                                                  colval(bramp[n][1],i) +
376                                  (colval(x,i) - colval(bramp[n][0],i)) *
377                                                  colval(bramp[n+1][1],i)) /
378                          (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
374                if (cv[i] < 0.) cv[i] = 0.;
379          }
376        setcolor(y, cv[0], cv[1], cv[2]);
380   }
381  
382  
383   compute()                       /* compute color mapping */
384   {
385 <        COLOR   clrin[NMBMOD], clrout[NMBMOD];
386 <        register int    i, n;
385 >        COLOR   clrin[24], clrout[24];
386 >        long    cflags;
387 >        COLOR   ctmp;
388 >        register int    i, j, n;
389                                          /* did we get what we need? */
390          if ((inpflags & REQFLGS) != REQFLGS) {
391                  fprintf(stderr, "%s: missing required input colors\n",
# Line 392 | Line 397 | compute()                      /* compute color mapping */
397                  copycolor(bramp[i][0], inpRGB[mbneu[i]]);
398                  copycolor(bramp[i][1], mbRGB[mbneu[i]]);
399          }
400 <                                        /* compute color matrix */
401 <        for (n = 0, i = 0; i < NMBMOD; i++)
402 <                if (inpflags & 1L<<mbmod[i]) {
403 <                        bresp(clrin[n], inpRGB[mbmod[i]]);
404 <                        copycolor(clrout[n], mbRGB[mbmod[i]]);
405 <                        n++;
406 <                }
407 <        compsoln(clrin, clrout, n);
400 >                                        /* compute color mapping */
401 >        do {
402 >                cflags = inpflags & ~gmtflags;
403 >                n = 0;                          /* compute transform matrix */
404 >                for (i = 0; i < 24; i++)
405 >                        if (cflags & 1L<<i) {
406 >                                bresp(clrin[n], inpRGB[i]);
407 >                                copycolor(clrout[n], mbRGB[i]);
408 >                                n++;
409 >                        }
410 >                compsoln(clrin, clrout, n);
411 >                                                /* check out-of-gamut colors */
412 >                for (i = 0; i < 24; i++)
413 >                        if (cflags & 1L<<i) {
414 >                                cvtcolor(ctmp, mbRGB[i]);
415 >                                for (j = 0; j < 3; j++)
416 >                                        if (colval(ctmp,j) <= 1e-6 ||
417 >                                                colval(ctmp,j) >= 1.-1e-6) {
418 >                                                gmtflags |= 1L<<i;
419 >                                                break;
420 >                                        }
421 >                        }
422 >        } while (cflags & gmtflags);
423 >        if (gmtflags & MODFLGS)
424 >                fprintf(stderr,
425 >                "%s: warning - some moderate colors are out of gamut\n",
426 >                                progname);
427   }
428  
429  
# Line 417 | Line 441 | putmapping()                   /* put out color mapping for pcomb -f *
441                  for (i = 0; i < NMBNEU; i++)
442                          printf(",%g", colval(bramp[i][1],j));
443                  printf(");\n");
420                printf("%c = %ci(1);\n", cchar[j], cchar[j]);
444                  printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
445                                  cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
446                                  cchar[j], cchar[j]);
447                  printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
448 <                printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
449 <                                cchar[j], cchar[j], cchar[j],
450 <                                cchar[j], cchar[j], cchar[j]);
448 >                printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
449 >                                cchar[j], scanning?'n':'o', cchar[j],
450 >                                cchar[j], cchar[j], cchar[j], cchar[j]);
451                  printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
452                                  cchar[j], cchar[j], cchar[j],
453                                  cchar[j], cchar[j]);
# Line 432 | Line 455 | putmapping()                   /* put out color mapping for pcomb -f *
455                                  cchar[j], cchar[j], cchar[j], cchar[j]);
456          }
457                                          /* print color mapping */
458 <        printf("ro = %g*rn + %g*gn + %g*bn ;\n",
459 <                        solmat[0][0], solmat[0][1], solmat[0][2]);
460 <        printf("go = %g*rn + %g*gn + %g*bn ;\n",
461 <                        solmat[1][0], solmat[1][1], solmat[1][2]);
462 <        printf("bo = %g*rn + %g*gn + %g*bn ;\n",
463 <                        solmat[2][0], solmat[2][1], solmat[2][2]);
458 >        if (scanning) {
459 >                printf("r = ri(1); g = gi(1); b = bi(1);\n");
460 >                printf("ro = %g*rn + %g*gn + %g*bn ;\n",
461 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
462 >                printf("go = %g*rn + %g*gn + %g*bn ;\n",
463 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
464 >                printf("bo = %g*rn + %g*gn + %g*bn ;\n",
465 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
466 >        } else {
467 >                printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n");
468 >                printf("r = %g*r1 + %g*g1 + %g*b1 ;\n",
469 >                                solmat[0][0], solmat[0][1], solmat[0][2]);
470 >                printf("g = %g*r1 + %g*g1 + %g*b1 ;\n",
471 >                                solmat[1][0], solmat[1][1], solmat[1][2]);
472 >                printf("b = %g*r1 + %g*g1 + %g*b1 ;\n",
473 >                                solmat[2][0], solmat[2][1], solmat[2][2]);
474 >        }
475   }
476  
477  
# Line 451 | Line 485 | int    n;
485          double  colv[3], rowv[3];
486          register int    i, j, k;
487  
488 <        if (n < 3 | n > NMBMOD) {
489 <                fprintf(stderr, "%s: bad number of colors to match\n", progname);
488 >        if (n < 3) {
489 >                fprintf(stderr, "%s: too few colors to match!\n", progname);
490                  exit(1);
491          }
492          if (n == 3)
# Line 504 | Line 538 | int    n;
538   cvtcolor(cout, cin)             /* convert color according to our mapping */
539   COLOR   cout, cin;
540   {
541 +        COLOR   ctmp;
542 +
543 +        if (scanning) {
544 +                bresp(ctmp, cin);
545 +                cresp(cout, ctmp);
546 +        } else {
547 +                cresp(ctmp, cin);
548 +                bresp(cout, ctmp);
549 +        }
550 +        if (colval(cout,RED) < 0.)
551 +                colval(cout,RED) = 0.;
552 +        if (colval(cout,GRN) < 0.)
553 +                colval(cout,GRN) = 0.;
554 +        if (colval(cout,BLU) < 0.)
555 +                colval(cout,BLU) = 0.;
556 + }
557 +
558 +
559 + cresp(cout, cin)                /* transform color according to matrix */
560 + COLOR   cout, cin;
561 + {
562          double  r, g, b;
563  
564 <        bresp(cout, cin);
565 <        r = colval(cout,0)*solmat[0][0] + colval(cout,1)*solmat[0][1]
566 <                        + colval(cout,2)*solmat[0][2];
567 <        if (r < 0) r = 0;
568 <        g = colval(cout,0)*solmat[1][0] + colval(cout,1)*solmat[1][1]
569 <                        + colval(cout,2)*solmat[1][2];
515 <        if (g < 0) g = 0;
516 <        b = colval(cout,0)*solmat[2][0] + colval(cout,1)*solmat[2][1]
517 <                        + colval(cout,2)*solmat[2][2];
518 <        if (b < 0) b = 0;
564 >        r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1]
565 >                        + colval(cin,2)*solmat[0][2];
566 >        g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1]
567 >                        + colval(cin,2)*solmat[1][2];
568 >        b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1]
569 >                        + colval(cin,2)*solmat[2][2];
570          setcolor(cout, r, g, b);
571   }
572  
# Line 532 | Line 583 | register float xyYin[3];
583          ctmp[1] = xyYin[2];
584          ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
585          cie_rgb(rgbout, ctmp);
586 +                                /* allow negative values */
587 +        colortrans(rgbout, xyz2rgbmat, ctmp, 0);
588   }
589  
590  
591   picdebug()                      /* put out debugging picture */
592   {
593 +        static COLOR    blkcol = BLKCOLOR;
594          COLOR   *scan;
595 <        int     y;
596 <        register int    x, i;
595 >        int     y, i;
596 >        register int    x, rg;
597  
598          if (fseek(stdin, 0L, 0) == EOF) {
599                  fprintf(stderr, "%s: cannot seek on input picture\n", progname);
# Line 565 | Line 619 | picdebug()                     /* put out debugging picture */
619                          exit(1);
620                  }
621                  for (x = 0; x < xmax; x++) {
622 <                        i = chartndx(x, y, CENTCVG);
623 <                        if (i < 0)
622 >                        rg = chartndx(x, y, &i);
623 >                        if (rg == RG_CENT) {
624 >                                if (!(1L<<i & gmtflags) || (x+y)&07)
625 >                                        copycolor(scan[x], mbRGB[i]);
626 >                                else
627 >                                        copycolor(scan[x], blkcol);
628 >                        } else if (rg == RG_CORR)
629                                  cvtcolor(scan[x], scan[x]);
630 <                        else
631 <                                copycolor(scan[x], mbRGB[i]);
630 >                        else if (rg != RG_ORIG)
631 >                                copycolor(scan[x], blkcol);
632                  }
633                  if (fwritescan(scan, xmax, debugfp) < 0) {
634                          fprintf(stderr, "%s: error writing debugging picture\n",
# Line 586 | Line 645 | picdebug()                     /* put out debugging picture */
645   clrdebug()                      /* put out debug picture from color input */
646   {
647          static COLR     blkclr = BLKCOLR;
648 <        COLR    mbclr[24], cvclr[24];
648 >        COLR    mbclr[24], cvclr[24], orclr[24];
649          COLR    *scan;
650          COLOR   ctmp;
651 <        int     y;
652 <        register int    i, x;
651 >        int     y, i;
652 >        register int    x, rg;
653                                                  /* convert colors */
654          for (i = 0; i < 24; i++) {
655                  setcolr(mbclr[i], colval(mbRGB[i],RED),
656                                  colval(mbRGB[i],GRN), colval(mbRGB[i],BLU));
657                  if (inpflags & 1L<<i) {
658 +                        setcolr(orclr[i], colval(inpRGB[i],RED),
659 +                                        colval(inpRGB[i],GRN),
660 +                                        colval(inpRGB[i],BLU));
661                          cvtcolor(ctmp, inpRGB[i]);
662                          setcolr(cvclr[i], colval(ctmp,RED),
663                                          colval(ctmp,GRN), colval(ctmp,BLU));
# Line 613 | Line 675 | clrdebug()                     /* put out debug picture from color input
675          fprtresolu(xmax, ymax, debugfp);
676                                                  /* write debug picture */
677          for (y = ymax-1; y >= 0; y--) {
678 <                for (x = 0; x < xmax; x++)
679 <                        if ((i = chartndx(x, y, CENTCVG)) >= 0)
680 <                                copycolr(scan[x], mbclr[i]);
681 <                        else if ((i = chartndx(x, y, FULLCVG)) >= 0 &&
682 <                                        inpflags & 1L<<i)
683 <                                copycolr(scan[x], cvclr[i]);
684 <                        else
678 >                for (x = 0; x < xmax; x++) {
679 >                        rg = chartndx(x, y, &i);
680 >                        if (rg == RG_CENT) {
681 >                                if (!(1L<<i & gmtflags) || (x+y)&07)
682 >                                        copycolr(scan[x], mbclr[i]);
683 >                                else
684 >                                        copycolr(scan[x], blkclr);
685 >                        } else if (rg == RG_BORD || !(1L<<i & inpflags))
686                                  copycolr(scan[x], blkclr);
687 +                        else if (rg == RG_ORIG)
688 +                                copycolr(scan[x], orclr[i]);
689 +                        else /* rg == RG_CORR */
690 +                                copycolr(scan[x], cvclr[i]);
691 +                }
692                  if (fwritecolrs(scan, xmax, debugfp) < 0) {
693                          fprintf(stderr, "%s: error writing debugging picture\n",
694                                          progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines