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.9 by greg, Mon Oct 23 12:20:03 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 95 | Line 98 | short  mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutra
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 */
# Line 104 | Line 115 | long   inpflags = 0;           /* flags of which colors were inpu
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  
# 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 379 | Line 433 | compute()                      /* compute color mapping */
433          COLOR   clrin[24], clrout[24];
434          long    cflags;
435          COLOR   ctmp;
436 <        register int    i, j, n;
436 >        register int    i, n;
437                                          /* did we get what we need? */
438          if ((inpflags & REQFLGS) != REQFLGS) {
439                  fprintf(stderr, "%s: missing required input colors\n",
# Line 391 | 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 space gamut */
449 +        if (scanning) {
450 +                copycolor(colmin, cblack);
451 +                copycolor(colmax, cwhite);
452 +                scalecolor(colmax, irrad);
453 +        } else
454 +                for (i = 0; i < 3; i++) {
455 +                        colval(colmin,i) = colval(bramp[0][0],i) -
456 +                                colval(bramp[0][1],i) *
457 +                                (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
458 +                                (colval(bramp[1][1],i)-colval(bramp[1][0],i));
459 +                        colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
460 +                                (1.-colval(bramp[NMBNEU-2][1],i)) *
461 +                                (colval(bramp[NMBNEU-1][0],i) -
462 +                                        colval(bramp[NMBNEU-2][0],i)) /
463 +                                (colval(bramp[NMBNEU-1][1],i) -
464 +                                        colval(bramp[NMBNEU-2][1],i));
465 +                }
466                                          /* compute color mapping */
467          do {
468                  cflags = inpflags & ~gmtflags;
# Line 402 | Line 474 | compute()                      /* compute color mapping */
474                                  n++;
475                          }
476                  compsoln(clrin, clrout, n);
477 <                                                /* check out-of-gamut colors */
478 <                for (i = 0; i < 24; i++)
479 <                        if (cflags & 1L<<i) {
480 <                                cresp(ctmp, mbRGB[i]);
409 <                                for (j = 0; j < 3; j++)
410 <                                        if (colval(ctmp,j) <= 0. ||
411 <                                                colval(ctmp,j) >= 1.) {
412 <                                                gmtflags |= 1L<<i;
413 <                                                break;
414 <                                        }
415 <                        }
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,
# Line 421 | Line 486 | compute()                      /* compute color mapping */
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 435 | 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");
438                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 450 | 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 519 | 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          COLOR   ctmp;
618 +        int     clipped;
619  
620 <        bresp(ctmp, cin);
621 <        cresp(cout, ctmp);
622 <        if (colval(cout,RED) < 0.)
623 <                colval(cout,RED) = 0.;
624 <        if (colval(cout,GRN) < 0.)
625 <                colval(cout,GRN) = 0.;
626 <        if (colval(cout,BLU) < 0.)
627 <                colval(cout,BLU) = 0.;
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 <        double  r, g, b;
639 <
543 <        r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1]
544 <                        + colval(cin,2)*solmat[0][2];
545 <        g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1]
546 <                        + colval(cin,2)*solmat[1][2];
547 <        b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1]
548 <                        + colval(cin,2)*solmat[2][2];
549 <        setcolor(cout, r, g, b);
638 >        colortrans(cout, solmat, cin);
639 >        return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
640   }
641  
642  
# Line 561 | 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  
# Line 598 | Line 689 | picdebug()                     /* put out debugging picture */
689                  for (x = 0; x < xmax; x++) {
690                          rg = chartndx(x, y, &i);
691                          if (rg == RG_CENT) {
692 <                                if (!(1L<<i & gmtflags) || (x+y)&07)
692 >                                if (!(1L<<i & gmtflags) || (x+y)&07) {
693                                          copycolor(scan[x], mbRGB[i]);
694 <                                else
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]);
# Line 615 | 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  
# Line 624 | Line 717 | clrdebug()                     /* put out debug picture from color input
717          static COLR     blkclr = BLKCOLR;
718          COLR    mbclr[24], cvclr[24], orclr[24];
719          COLR    *scan;
720 <        COLOR   ctmp;
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 <                        setcolr(orclr[i], colval(inpRGB[i],RED),
731 <                                        colval(inpRGB[i],GRN),
732 <                                        colval(inpRGB[i],BLU));
638 <                        cvtcolor(ctmp, inpRGB[i]);
639 <                        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 674 | 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