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.11 by greg, Thu Jan 30 17:07:42 1997 UTC vs.
Revision 2.16 by gwlarson, Fri Jul 16 16:12:20 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 Regents of the University of California */
1 > /* Copyright (c) 1999 Regents of the University of California */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
8   * Calibrate a scanned MacBeth Color Checker Chart
9   *
10 < * Produce a .cal file suitable for use with pcomb.
10 > * Produce a .cal file suitable for use with pcomb,
11 > * or .cwp file suitable for use with pcwarp.
12 > *
13 > * Warping code depends on conformance of COLOR and W3VEC types.
14   */
15  
16   #include <stdio.h>
17 + #include <math.h>
18   #ifdef MSDOS
19   #include <fcntl.h>
20   #endif
21   #include "color.h"
22   #include "resolu.h"
23   #include "pmap.h"
24 + #include "warp3d.h"
25  
26                                  /* MacBeth colors */
27   #define DarkSkin        0
# Line 95 | Line 100 | short  mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutra
100   #define  RG_ORIG        02      /* original color region */
101   #define  RG_CORR        04      /* corrected color region */
102  
103 + #ifndef  DISPCOM
104 + #define  DISPCOM        "ximage -op %s"
105 + #endif
106 +
107   int     scanning = 1;           /* scanned input (or recorded output)? */
108 + double  irrad = 1.0;            /* irradiance multiplication factor */
109 + int     rawmap = 0;             /* put out raw color mapping? */
110  
111   int     xmax, ymax;             /* input image dimensions */
112   int     bounds[4][2];           /* image coordinates of chart corners */
# Line 106 | Line 117 | long   inpflags = 0;           /* flags of which colors were inpu
117   long    gmtflags = 0;           /* flags of out-of-gamut colors */
118  
119   COLOR   bramp[NMBNEU][2];       /* brightness ramp (per primary) */
120 < double  solmat[3][3];           /* color mapping matrix */
120 > COLORMAT        solmat;         /* color mapping matrix */
121 > COLOR   colmin, colmax;         /* gamut limits */
122  
123 + WARP3D  *wcor = NULL;           /* color space warp */
124 +
125   FILE    *debugfp = NULL;        /* debug output picture */
126   char    *progname;
127  
# Line 150 | Line 164 | char   **argv;
164                          bounds[3][1] = atoi(argv[++i]);
165                          scanning = 2;
166                          break;
167 +                case 'P':                               /* pick position */
168 +                        scanning = 3;
169 +                        break;
170 +                case 'i':                               /* irradiance factor */
171 +                        i++;
172 +                        if (badarg(argc-i, argv+i, "f"))
173 +                                goto userr;
174 +                        irrad = atof(argv[i]);
175 +                        break;
176 +                case 'm':                               /* raw map output */
177 +                        rawmap = 1;
178 +                        break;
179                  case 'c':                               /* color input */
180                          scanning = 0;
181                          break;
# Line 158 | Line 184 | char   **argv;
184                  }
185                                                          /* open files */
186          if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
187 <                perror(argv[1]);
187 >                perror(argv[i]);
188                  exit(1);
189          }
190          if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
191 <                perror(argv[2]);
191 >                perror(argv[i+1]);
192                  exit(1);
193          }
194          if (scanning) {                 /* load input picture header */
# Line 174 | Line 200 | char   **argv;
200                          fprintf(stderr, "%s: bad input picture\n", progname);
201                          exit(1);
202                  }
203 +                if (scanning == 3) {
204 +                        if (i >= argc)
205 +                                goto userr;
206 +                        pickchartpos(argv[i]);
207 +                        scanning = 2;
208 +                }
209          } else {                        /* else set default xmax and ymax */
210                  xmax = 512;
211                  ymax = 2*512/3;
# Line 190 | Line 222 | char   **argv;
222          else
223                  getcolors();
224          compute();                      /* compute color mapping */
225 <                                        /* print comment */
226 <        printf("{\n\tColor correction file computed by:\n\t\t");
227 <        printargs(argc, argv, stdout);
228 <        printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
229 <                        i+1 < argc ? argv[i+1] : "{this_file}");
230 <        if (!scanning)
199 <                printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n",
225 >        if (rawmap) {                   /* print out raw correspondence */
226 >                register int    j;
227 >
228 >                printf("# Color correspondence produced by:\n#\t\t");
229 >                printargs(argc, argv, stdout);
230 >                printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n",
231                                  i+1 < argc ? argv[i+1] : "{this_file}");
232 <        printf("}\n");
233 <        putmapping();                   /* put out color mapping */
232 >                printf("#\t   Or: pcond [options] -m %s orig.pic > output.pic\n",
233 >                                i+1 < argc ? argv[i+1] : "{this_file}");
234 >                for (j = 0; j < 24; j++)
235 >                        printf("%f %f %f    %f %f %f\n",
236 >                                colval(inpRGB[j],RED), colval(inpRGB[j],GRN),
237 >                                colval(inpRGB[j],BLU), colval(mbRGB[j],RED),
238 >                                colval(mbRGB[j],GRN), colval(mbRGB[j],BLU));
239 >                if (scanning && debugfp != NULL)
240 >                        cwarp();                /* color warp for debugging */
241 >        } else {                        /* print color mapping */
242 >                                                /* print header */
243 >                printf("{\n\tColor correction file computed by:\n\t\t");
244 >                printargs(argc, argv, stdout);
245 >                printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
246 >                                i+1 < argc ? argv[i+1] : "{this_file}");
247 >                if (!scanning)
248 >                        printf("\t   Or: pcond [options] -f %s orig.pic > output.pic\n",
249 >                                        i+1 < argc ? argv[i+1] : "{this_file}");
250 >                printf("}\n");
251 >                putmapping();                   /* put out color mapping */
252 >        }
253          if (debugfp != NULL)            /* put out debug picture */
254                  if (scanning)
255                          picdebug();
# Line 208 | Line 258 | char   **argv;
258          exit(0);
259   userr:
260          fprintf(stderr,
261 < "Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n",
261 > "Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n",
262                          progname);
263 <        fprintf(stderr, "   or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n",
263 >        fprintf(stderr, "   or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n",
264                          progname);
265          exit(1);
266   }
# Line 235 | Line 285 | init()                         /* initialize */
285                  exit(1);
286          }
287                                          /* map MacBeth colors to RGB space */
288 <        for (i = 0; i < 24; i++)
288 >        for (i = 0; i < 24; i++) {
289                  xyY2RGB(mbRGB[i], mbxyY[i]);
290 +                scalecolor(mbRGB[i], irrad);
291 +        }
292   }
293  
294  
# Line 326 | Line 378 | getcolors()                    /* get xyY colors from standard input */
378                                  fgetval(stdin, 'f', &xyYin[0]) != 1 ||
379                                  fgetval(stdin, 'f', &xyYin[1]) != 1 ||
380                                  fgetval(stdin, 'f', &xyYin[2]) != 1 ||
381 <                                xyYin[0] < 0. | xyYin[0] > 1. |
382 <                                xyYin[1] < 0. | xyYin[1] > 1.) {
381 >                                xyYin[0] < 0. | xyYin[1] < 0. ||
382 >                                xyYin[0] + xyYin[1] > 1.) {
383                          fprintf(stderr, "%s: bad color input data\n",
384                                          progname);
385                          exit(1);
# Line 385 | Line 437 | compute()                      /* compute color mapping */
437          COLOR   clrin[24], clrout[24];
438          long    cflags;
439          COLOR   ctmp;
440 <        register int    i, j, n;
440 >        register int    i, n;
441                                          /* did we get what we need? */
442          if ((inpflags & REQFLGS) != REQFLGS) {
443                  fprintf(stderr, "%s: missing required input colors\n",
# Line 397 | Line 449 | compute()                      /* compute color mapping */
449                  copycolor(bramp[i][0], inpRGB[mbneu[i]]);
450                  copycolor(bramp[i][1], mbRGB[mbneu[i]]);
451          }
452 +                                        /* compute color space gamut */
453 +        if (scanning) {
454 +                copycolor(colmin, cblack);
455 +                copycolor(colmax, cwhite);
456 +                scalecolor(colmax, irrad);
457 +        } else
458 +                for (i = 0; i < 3; i++) {
459 +                        colval(colmin,i) = colval(bramp[0][0],i) -
460 +                                colval(bramp[0][1],i) *
461 +                                (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
462 +                                (colval(bramp[1][1],i)-colval(bramp[1][0],i));
463 +                        colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
464 +                                (1.-colval(bramp[NMBNEU-2][1],i)) *
465 +                                (colval(bramp[NMBNEU-1][0],i) -
466 +                                        colval(bramp[NMBNEU-2][0],i)) /
467 +                                (colval(bramp[NMBNEU-1][1],i) -
468 +                                        colval(bramp[NMBNEU-2][1],i));
469 +                }
470                                          /* compute color mapping */
471          do {
472                  cflags = inpflags & ~gmtflags;
# Line 408 | Line 478 | compute()                      /* compute color mapping */
478                                  n++;
479                          }
480                  compsoln(clrin, clrout, n);
481 <                                                /* check out-of-gamut colors */
482 <                for (i = 0; i < 24; i++)
483 <                        if (cflags & 1L<<i) {
484 <                                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 <                        }
481 >                if (irrad > 0.99 && irrad < 1.01)       /* check gamut */
482 >                        for (i = 0; i < 24; i++)
483 >                                if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
484 >                                        gmtflags |= 1L<<i;
485          } while (cflags & gmtflags);
486          if (gmtflags & MODFLGS)
487                  fprintf(stderr,
# Line 427 | Line 490 | compute()                      /* compute color mapping */
490   }
491  
492  
493 < putmapping()                    /* put out color mapping for pcomb -f */
493 > putmapping()                    /* put out color mapping */
494   {
495          static char     cchar[3] = {'r', 'g', 'b'};
496          register int    i, j;
# Line 535 | Line 598 | int    n;
598   }
599  
600  
601 + cwarp()                         /* compute color warp map */
602 + {
603 +        register int    i;
604 +
605 +        if ((wcor = new3dw(W3EXACT)) == NULL)
606 +                goto memerr;
607 +        for (i = 0; i < 24; i++)
608 +                if (!add3dpt(wcor, inpRGB[i], mbRGB[i]))
609 +                        goto memerr;
610 +        return;
611 + memerr:
612 +        perror(progname);
613 +        exit(1);
614 + }
615 +
616 +
617 + int
618   cvtcolor(cout, cin)             /* convert color according to our mapping */
619   COLOR   cout, cin;
620   {
621          COLOR   ctmp;
622 +        int     clipped;
623  
624 <        if (scanning) {
624 >        if (wcor != NULL) {
625 >                clipped = warp3d(cout, cin, wcor);
626 >                clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax);
627 >        } else if (scanning) {
628                  bresp(ctmp, cin);
629 <                cresp(cout, ctmp);
629 >                clipped = cresp(cout, ctmp);
630          } else {
631 <                cresp(ctmp, cin);
631 >                clipped = cresp(ctmp, cin);
632                  bresp(cout, ctmp);
633          }
634 <        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.;
634 >        return(clipped);
635   }
636  
637  
638 + int
639   cresp(cout, cin)                /* transform color according to matrix */
640   COLOR   cout, cin;
641   {
642 <        double  r, g, b;
643 <
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);
642 >        colortrans(cout, solmat, cin);
643 >        return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
644   }
645  
646  
# Line 582 | Line 655 | register float xyYin[3];
655          ctmp[0] = xyYin[0] * d;
656          ctmp[1] = xyYin[2];
657          ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
658 <        cie_rgb(rgbout, ctmp);
658 >                                /* allow negative values */
659 >        colortrans(rgbout, xyz2rgbmat, ctmp);
660   }
661  
662  
# Line 619 | Line 693 | picdebug()                     /* put out debugging picture */
693                  for (x = 0; x < xmax; x++) {
694                          rg = chartndx(x, y, &i);
695                          if (rg == RG_CENT) {
696 <                                if (!(1L<<i & gmtflags) || (x+y)&07)
696 >                                if (!(1L<<i & gmtflags) || (x+y)&07) {
697                                          copycolor(scan[x], mbRGB[i]);
698 <                                else
698 >                                        clipgamut(scan[x], bright(scan[x]),
699 >                                                CGAMUT, colmin, colmax);
700 >                                } else
701                                          copycolor(scan[x], blkcol);
702                          } else if (rg == RG_CORR)
703                                  cvtcolor(scan[x], scan[x]);
# Line 645 | Line 721 | clrdebug()                     /* put out debug picture from color input
721          static COLR     blkclr = BLKCOLR;
722          COLR    mbclr[24], cvclr[24], orclr[24];
723          COLR    *scan;
724 <        COLOR   ctmp;
724 >        COLOR   ctmp, ct2;
725          int     y, i;
726          register int    x, rg;
727                                                  /* convert colors */
728          for (i = 0; i < 24; i++) {
729 <                setcolr(mbclr[i], colval(mbRGB[i],RED),
730 <                                colval(mbRGB[i],GRN), colval(mbRGB[i],BLU));
729 >                copycolor(ctmp, mbRGB[i]);
730 >                clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
731 >                setcolr(mbclr[i], colval(ctmp,RED),
732 >                                colval(ctmp,GRN), colval(ctmp,BLU));
733                  if (inpflags & 1L<<i) {
734 <                        setcolr(orclr[i], colval(inpRGB[i],RED),
735 <                                        colval(inpRGB[i],GRN),
736 <                                        colval(inpRGB[i],BLU));
659 <                        cvtcolor(ctmp, inpRGB[i]);
660 <                        setcolr(cvclr[i], colval(ctmp,RED),
734 >                        copycolor(ctmp, inpRGB[i]);
735 >                        clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
736 >                        setcolr(orclr[i], colval(ctmp,RED),
737                                          colval(ctmp,GRN), colval(ctmp,BLU));
738 +                        if (rawmap)
739 +                                copycolr(cvclr[i], mbclr[i]);
740 +                        else {
741 +                                bresp(ctmp, inpRGB[i]);
742 +                                colortrans(ct2, solmat, ctmp);
743 +                                clipgamut(ct2, bright(ct2), CGAMUT,
744 +                                                cblack, cwhite);
745 +                                setcolr(cvclr[i], colval(ct2,RED),
746 +                                                colval(ct2,GRN),
747 +                                                colval(ct2,BLU));
748 +                        }
749                  }
750          }
751                                                  /* allocate scanline */
# Line 696 | Line 783 | clrdebug()                     /* put out debug picture from color input
783                                                  /* clean up */
784          fclose(debugfp);
785          free((char *)scan);
786 + }
787 +
788 +
789 + getpos(name, bnds, fp)          /* get boundary position */
790 + char    *name;
791 + int     bnds[2];
792 + FILE    *fp;
793 + {
794 +        char    buf[64];
795 +
796 +        fprintf(stderr, "\tSelect corner: %s\n", name);
797 +        if (fgets(buf, sizeof(buf), fp) == NULL ||
798 +                        sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) {
799 +                fprintf(stderr, "%s: read error from display process\n",
800 +                                progname);
801 +                exit(1);
802 +        }
803 + }
804 +
805 +
806 + pickchartpos(pfn)               /* display picture and pick chart location */
807 + char    *pfn;
808 + {
809 +        char    combuf[512];
810 +        FILE    *pfp;
811 +
812 +        sprintf(combuf, DISPCOM, pfn);
813 +        if ((pfp = popen(combuf, "r")) == NULL) {
814 +                perror(combuf);
815 +                exit(1);
816 +        }
817 +        fputs("Use middle mouse button to select chart corners:\n", stderr);
818 +        getpos("upper left (dark skin)", bounds[0], pfp);
819 +        getpos("upper right (bluish green)", bounds[1], pfp);
820 +        getpos("lower left (white)", bounds[2], pfp);
821 +        getpos("lower right (black)", bounds[3], pfp);
822 +        fputs("Got it -- quit display program.\n", stderr);
823 +        pclose(pfp);
824   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines