ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
(Generate patch)

Comparing ray/src/px/pvalue.c (file contents):
Revision 2.22 by schorsch, Mon Jul 21 22:30:19 2003 UTC vs.
Revision 2.30 by greg, Thu Dec 16 21:34:41 2010 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   *     4/23/86
8   */
9  
10 #include  <time.h>
11
12 #include  "standard.h"
10   #include  "platform.h"
11 + #include  "standard.h"
12   #include  "color.h"
13   #include  "resolu.h"
14 + #include  "view.h"
15  
16   #define  min(a,b)               ((a)<(b)?(a):(b))
17  
# Line 21 | Line 20 | static const char RCSid[] = "$Id$";
20   #define  BRIGHT         4
21  
22   RESOLU  picres;                 /* resolution of picture */
24
23   int  uniq = 0;                  /* print only unique values? */
26
24   int  doexposure = 0;            /* exposure change? (>100 to print) */
28
25   int  dataonly = 0;              /* data only format? */
30
26   int  putprim = ALL;             /* what to put out */
32
27   int  reverse = 0;               /* reverse conversion? */
34
28   int  format = 'a';              /* input/output format */
29   char  *fmtid = "ascii";         /* format identifier for header */
37
30   int  header = 1;                /* do header? */
39
31   long  skipbytes = 0;            /* skip bytes in input? */
32 <
42 < int  swapbytes = 0;             /* swap bytes in 16-bit words? */
43 <
32 > int  swapbytes = 0;             /* swap bytes? */
33   int  interleave = 1;            /* file is interleaved? */
45
34   int  resolution = 1;            /* put/get resolution string? */
47
35   int  original = 0;              /* convert to original values? */
49
36   int  wrongformat = 0;           /* wrong input format? */
51
37   double  gamcor = 1.0;           /* gamma correction */
38  
39 + RGBPRIMP  outprims = stdprims;  /* output primaries for reverse conversion */
40 + RGBPRIMS  myprims;
41 +
42   int  ord[3] = {RED, GRN, BLU};  /* RGB ordering */
43   int  rord[4];                   /* reverse ordering */
44  
# Line 61 | Line 49 | char  *progname;
49   FILE  *fin;
50   FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
51  
64 int  (*getval)(), (*putval)();
52  
53 < double
54 < rgb_bright(clr)
55 < COLOR  clr;
53 > typedef int getfunc_t(COLOR  col);
54 > typedef int putfunc_t(COLOR  col);
55 > typedef double brightfunc_t(COLOR  col);
56 >
57 > getfunc_t *getval;
58 > putfunc_t *putval;
59 > brightfunc_t *mybright;
60 >
61 > static gethfunc checkhead;
62 > static brightfunc_t rgb_bright, xyz_bright;
63 > static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword;
64 > static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword;
65 > static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword;
66 > static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword;
67 > static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword;
68 >
69 > static void set_io(void);
70 > static void pixtoval(void);
71 > static void valtopix(void);
72 >
73 >
74 > static double
75 > rgb_bright(
76 >        COLOR  clr
77 > )
78   {
79          return(bright(clr));
80   }
81  
82 < double
83 < xyz_bright(clr)
84 < COLOR  clr;
82 > static double
83 > xyz_bright(
84 >        COLOR  clr
85 > )
86   {
87          return(clr[CIEY]);
88   }
89  
90 < double  (*mybright)() = &rgb_bright;
91 <
92 <
93 < main(argc, argv)
94 < int  argc;
85 < char  **argv;
90 > int
91 > main(
92 >        int  argc,
93 >        char  **argv
94 > )
95   {
87        extern int  checkhead();
88        extern long  atol();
96          double  d, expval = 1.0;
97          int  i;
98  
99          progname = argv[0];
100 +        mybright = &rgb_bright; /* default */
101  
102          for (i = 1; i < argc; i++)
103                  if (argv[i][0] == '-' || argv[i][0] == '+')
# Line 141 | Line 149 | char  **argv;
149                          case 'b':               /* brightness values */
150                                  putprim = argv[i][0] == '-' ? BRIGHT : ALL;
151                                  break;
152 <                        case 'p':               /* put primary */
152 >                        case 'p':               /* primary controls */
153                                  switch (argv[i][2]) {
154 +                                /* these two options affect -r conversion */
155 +                                case '\0':
156 +                                        myprims[RED][CIEX] = atof(argv[++i]);
157 +                                        myprims[RED][CIEY] = atof(argv[++i]);
158 +                                        myprims[GRN][CIEX] = atof(argv[++i]);
159 +                                        myprims[GRN][CIEY] = atof(argv[++i]);
160 +                                        myprims[BLU][CIEX] = atof(argv[++i]);
161 +                                        myprims[BLU][CIEY] = atof(argv[++i]);
162 +                                        myprims[WHT][CIEX] = atof(argv[++i]);
163 +                                        myprims[WHT][CIEY] = atof(argv[++i]);
164 +                                        outprims = myprims;
165 +                                        break;
166 +                                case 'x': case 'X': outprims = NULL; break;
167 +                                /* the following options affect +r only */
168                                  case 'r': case 'R': putprim = RED; break;
169                                  case 'g': case 'G': putprim = GRN; break;
170                                  case 'b': case 'B': putprim = BLU; break;
# Line 173 | Line 195 | char  **argv;
195                                          format = 'w';
196                                          fmtid = "16-bit";
197                                          break;
198 +                                case 'F':               /* swapped floats */
199 +                                        swapbytes = 1;
200                                  case 'f':               /* float */
201                                          dataonly = 1;
202                                          format = 'f';
203                                          fmtid = "float";
204                                          break;
205 +                                case 'D':               /* swapped doubles */
206 +                                        swapbytes = 1;
207                                  case 'd':               /* double */
208                                          dataonly = 1;
209                                          format = 'd';
# Line 313 | Line 339 | unkopt:
339                  printargs(i, argv, stdout);
340                  if (expval < .99 || expval > 1.01)
341                          fputexpos(expval, stdout);
342 <                fputformat(COLRFMT, stdout);
342 >                if (outprims != NULL) {
343 >                        if (outprims != stdprims)
344 >                                fputprims(outprims, stdout);
345 >                        fputformat(COLRFMT, stdout);
346 >                } else                          /* XYZ data */
347 >                        fputformat(CIEFMT, stdout);
348                  putchar('\n');
349                  fputsresolu(&picres, stdout);   /* always put resolution */
350                  valtopix();
# Line 348 | Line 379 | unkopt:
379          }
380  
381          quit(0);
382 +        return 0; /* pro forma return */
383   }
384  
385  
386 < int
387 < checkhead(line)                         /* deal with line from header */
388 < char  *line;
386 > static int
387 > checkhead(                              /* deal with line from header */
388 >        char    *line,
389 >        void    *p
390 > )
391   {
392          char    fmt[32];
393          double  d;
394          COLOR   ctmp;
395  
396          if (formatval(fmt, line)) {
397 <                if (!strcmp(fmt, CIEFMT)) {
397 >                if (!strcmp(fmt, CIEFMT))
398                          mybright = &xyz_bright;
399 <                        if (original) {
366 <                                scalecolor(exposure, 1./WHTEFFICACY);
367 <                                doexposure++;
368 <                        }
369 <                } else if (!strcmp(fmt, COLRFMT))
399 >                else if (!strcmp(fmt, COLRFMT))
400                          mybright = &rgb_bright;
401                  else
402                          wrongformat++;
# Line 386 | Line 416 | char  *line;
416   }
417  
418  
419 < pixtoval()                              /* convert picture to values */
419 > static void
420 > pixtoval(void)                          /* convert picture to values */
421   {
422          register COLOR  *scanln;
423          int  dogamma;
# Line 459 | Line 490 | pixtoval()                             /* convert picture to values */
490   }
491  
492  
493 < valtopix()                      /* convert values to a pixel file */
493 > static void
494 > valtopix(void)                  /* convert values to a pixel file */
495   {
496          int  dogamma;
497          register COLOR  *scanln;
# Line 511 | Line 543 | int  code;
543   }
544  
545  
546 < swap16(wp, n)           /* swap n 16-bit words */
547 < register uint16  *wp;
548 < int  n;
546 > static int
547 > getcascii(              /* get an ascii color value from stream(s) */
548 >        COLOR  col
549 > )
550   {
518        while (n-- > 0) {
519                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
520                wp++;
521        }
522 }
523
524 getcascii(col)          /* get an ascii color value from stream(s) */
525 COLOR  col;
526 {
551          double  vd[3];
552  
553          if (fin2 == NULL) {
# Line 540 | Line 564 | COLOR  col;
564   }
565  
566  
567 < getcdouble(col)         /* get a double color value from stream(s) */
568 < COLOR  col;
567 > static int
568 > getcdouble(             /* get a double color value from stream(s) */
569 >        COLOR  col
570 > )
571   {
572          double  vd[3];
573  
# Line 554 | Line 580 | COLOR  col;
580                          fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
581                          return(-1);
582          }
583 +        if (swapbytes)
584 +                swap64((char *)vd, 3);
585          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
586          return(0);
587   }
588  
589  
590 < getcfloat(col)          /* get a float color value from stream(s) */
591 < COLOR  col;
590 > static int
591 > getcfloat(              /* get a float color value from stream(s) */
592 >        COLOR  col
593 > )
594   {
595          float  vf[3];
596  
# Line 573 | Line 603 | COLOR  col;
603                          fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
604                          return(-1);
605          }
606 +        if (swapbytes)
607 +                swap32((char *)vf, 3);
608          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
609          return(0);
610   }
611  
612  
613 < getcint(col)            /* get an int color value from stream(s) */
614 < COLOR  col;
613 > static int
614 > getcint(                /* get an int color value from stream(s) */
615 >        COLOR  col
616 > )
617   {
618          int  vi[3];
619  
# Line 598 | Line 632 | COLOR  col;
632   }
633  
634  
635 < getcbyte(col)           /* get a byte color value from stream(s) */
636 < COLOR  col;
635 > static int
636 > getcbyte(               /* get a byte color value from stream(s) */
637 >        COLOR  col
638 > )
639   {
640          BYTE  vb[3];
641  
# Line 618 | Line 654 | COLOR  col;
654   }
655  
656  
657 < getcword(col)           /* get a 16-bit color value from stream(s) */
658 < COLOR  col;
657 > static int
658 > getcword(               /* get a 16-bit color value from stream(s) */
659 >        COLOR  col
660 > )
661   {
662          uint16  vw[3];
663  
# Line 633 | Line 671 | COLOR  col;
671                          return(-1);
672          }
673          if (swapbytes)
674 <                swap16(vw, 3);
674 >                swap16((char *)vw, 3);
675          setcolor(col, (vw[rord[RED]]+.5)/65536.,
676                          (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
677          return(0);
678   }
679  
680  
681 < getbascii(col)          /* get an ascii brightness value from fin */
682 < COLOR  col;
681 > static int
682 > getbascii(              /* get an ascii brightness value from fin */
683 >        COLOR  col
684 > )
685   {
686          double  vd;
687  
# Line 652 | Line 692 | COLOR  col;
692   }
693  
694  
695 < getbdouble(col)         /* get a double brightness value from fin */
696 < COLOR  col;
695 > static int
696 > getbdouble(             /* get a double brightness value from fin */
697 >        COLOR  col
698 > )
699   {
700          double  vd;
701  
702          if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
703                  return(-1);
704 +        if (swapbytes)
705 +                swap64((char *)&vd, 1);
706          setcolor(col, vd, vd, vd);
707          return(0);
708   }
709  
710  
711 < getbfloat(col)          /* get a float brightness value from fin */
712 < COLOR  col;
711 > static int
712 > getbfloat(              /* get a float brightness value from fin */
713 >        COLOR  col
714 > )
715   {
716          float  vf;
717  
718          if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
719                  return(-1);
720 +        if (swapbytes)
721 +                swap32((char *)&vf, 1);
722          setcolor(col, vf, vf, vf);
723          return(0);
724   }
725  
726  
727 < getbint(col)            /* get an int brightness value from fin */
728 < COLOR  col;
727 > static int
728 > getbint(                /* get an int brightness value from fin */
729 >        COLOR  col
730 > )
731   {
732          int  vi;
733          double  d;
# Line 690 | Line 740 | COLOR  col;
740   }
741  
742  
743 < getbbyte(col)           /* get a byte brightness value from fin */
744 < COLOR  col;
743 > static int
744 > getbbyte(               /* get a byte brightness value from fin */
745 >        COLOR  col
746 > )
747   {
748          BYTE  vb;
749          double  d;
# Line 704 | Line 756 | COLOR  col;
756   }
757  
758  
759 < getbword(col)           /* get a 16-bit brightness value from fin */
760 < COLOR  col;
759 > static int
760 > getbword(               /* get a 16-bit brightness value from fin */
761 >        COLOR  col
762 > )
763   {
764          uint16  vw;
765          double  d;
# Line 713 | Line 767 | COLOR  col;
767          if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
768                  return(-1);
769          if (swapbytes)
770 <                swap16(&vw, 1);
770 >                swap16((char *)&vw, 1);
771          d = (vw+.5)/65536.;
772          setcolor(col, d, d, d);
773          return(0);
774   }
775  
776  
777 < putcascii(col)                  /* put an ascii color to stdout */
778 < COLOR  col;
777 > static int
778 > putcascii(                      /* put an ascii color to stdout */
779 >        COLOR  col
780 > )
781   {
782          fprintf(stdout, "%15.3e %15.3e %15.3e\n",
783                          colval(col,ord[0]),
# Line 732 | Line 788 | COLOR  col;
788   }
789  
790  
791 < putcfloat(col)                  /* put a float color to stdout */
792 < COLOR  col;
791 > static int
792 > putcfloat(                      /* put a float color to stdout */
793 >        COLOR  col
794 > )
795   {
796          float  vf[3];
797  
798          vf[0] = colval(col,ord[0]);
799          vf[1] = colval(col,ord[1]);
800          vf[2] = colval(col,ord[2]);
801 +        if (swapbytes)
802 +                swap32((char *)vf, 3);
803          fwrite((char *)vf, sizeof(float), 3, stdout);
804  
805          return(ferror(stdout) ? -1 : 0);
806   }
807  
808  
809 < putcdouble(col)                 /* put a double color to stdout */
810 < COLOR  col;
809 > static int
810 > putcdouble(                     /* put a double color to stdout */
811 >        COLOR  col
812 > )
813   {
814          double  vd[3];
815  
816          vd[0] = colval(col,ord[0]);
817          vd[1] = colval(col,ord[1]);
818          vd[2] = colval(col,ord[2]);
819 +        if (swapbytes)
820 +                swap64((char *)vd, 3);
821          fwrite((char *)vd, sizeof(double), 3, stdout);
822  
823          return(ferror(stdout) ? -1 : 0);
824   }
825  
826  
827 < putcint(col)                    /* put an int color to stdout */
828 < COLOR  col;
827 > static int
828 > putcint(                        /* put an int color to stdout */
829 >        COLOR  col
830 > )
831   {
832          fprintf(stdout, "%d %d %d\n",
833                          (int)(colval(col,ord[0])*256.),
# Line 772 | Line 838 | COLOR  col;
838   }
839  
840  
841 < putcbyte(col)                   /* put a byte color to stdout */
842 < COLOR  col;
841 > static int
842 > putcbyte(                       /* put a byte color to stdout */
843 >        COLOR  col
844 > )
845   {
846          long  i;
847          BYTE  vb[3];
# Line 790 | Line 858 | COLOR  col;
858   }
859  
860  
861 < putcword(col)                   /* put a 16-bit color to stdout */
862 < COLOR  col;
861 > static int
862 > putcword(                       /* put a 16-bit color to stdout */
863 >        COLOR  col
864 > )
865   {
866          long  i;
867          uint16  vw[3];
# Line 803 | Line 873 | COLOR  col;
873          i = colval(col,ord[2])*65536.;
874          vw[2] = min(i,65535);
875          if (swapbytes)
876 <                swap16(vw, 3);
876 >                swap16((char *)vw, 3);
877          fwrite((char *)vw, sizeof(uint16), 3, stdout);
878  
879          return(ferror(stdout) ? -1 : 0);
880   }
881  
882  
883 < putbascii(col)                  /* put an ascii brightness to stdout */
884 < COLOR  col;
883 > static int
884 > putbascii(                      /* put an ascii brightness to stdout */
885 >        COLOR  col
886 > )
887   {
888          fprintf(stdout, "%15.3e\n", (*mybright)(col));
889  
# Line 819 | Line 891 | COLOR  col;
891   }
892  
893  
894 < putbfloat(col)                  /* put a float brightness to stdout */
895 < COLOR  col;
894 > static int
895 > putbfloat(                      /* put a float brightness to stdout */
896 >        COLOR  col
897 > )
898   {
899          float  vf;
900  
901          vf = (*mybright)(col);
902 +        if (swapbytes)
903 +                swap32((char *)&vf, 1);
904          fwrite((char *)&vf, sizeof(float), 1, stdout);
905  
906          return(ferror(stdout) ? -1 : 0);
907   }
908  
909  
910 < putbdouble(col)                 /* put a double brightness to stdout */
911 < COLOR  col;
910 > static int
911 > putbdouble(                     /* put a double brightness to stdout */
912 >        COLOR  col
913 > )
914   {
915          double  vd;
916  
917          vd = (*mybright)(col);
918 +        if (swapbytes)
919 +                swap64((char *)&vd, 1);
920          fwrite((char *)&vd, sizeof(double), 1, stdout);
921  
922          return(ferror(stdout) ? -1 : 0);
923   }
924  
925  
926 < putbint(col)                    /* put an int brightness to stdout */
927 < COLOR  col;
926 > static int
927 > putbint(                        /* put an int brightness to stdout */
928 >        COLOR  col
929 > )
930   {
931          fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
932  
# Line 852 | Line 934 | COLOR  col;
934   }
935  
936  
937 < putbbyte(col)                   /* put a byte brightness to stdout */
938 < COLOR  col;
937 > static int
938 > putbbyte(                       /* put a byte brightness to stdout */
939 >        COLOR  col
940 > )
941   {
942          register int  i;
943          BYTE  vb;
# Line 866 | Line 950 | COLOR  col;
950   }
951  
952  
953 < putbword(col)                   /* put a 16-bit brightness to stdout */
954 < COLOR  col;
953 > static int
954 > putbword(                       /* put a 16-bit brightness to stdout */
955 >        COLOR  col
956 > )
957   {
958          long  i;
959          uint16  vw;
# Line 875 | Line 961 | COLOR  col;
961          i = (*mybright)(col)*65536.;
962          vw = min(i,65535);
963          if (swapbytes)
964 <                swap16(&vw, 1);
964 >                swap16((char *)&vw, 1);
965          fwrite((char *)&vw, sizeof(uint16), 1, stdout);
966  
967          return(ferror(stdout) ? -1 : 0);
968   }
969  
970  
971 < putpascii(col)                  /* put an ascii primary to stdout */
972 < COLOR  col;
971 > static int
972 > putpascii(                      /* put an ascii primary to stdout */
973 >        COLOR  col
974 > )
975   {
976          fprintf(stdout, "%15.3e\n", colval(col,putprim));
977  
# Line 891 | Line 979 | COLOR  col;
979   }
980  
981  
982 < putpfloat(col)                  /* put a float primary to stdout */
983 < COLOR  col;
982 > static int
983 > putpfloat(                      /* put a float primary to stdout */
984 >        COLOR  col
985 > )
986   {
987          float  vf;
988  
989          vf = colval(col,putprim);
990 +        if (swapbytes)
991 +                swap32((char *)&vf, 1);
992          fwrite((char *)&vf, sizeof(float), 1, stdout);
993  
994          return(ferror(stdout) ? -1 : 0);
995   }
996  
997  
998 < putpdouble(col)                 /* put a double primary to stdout */
999 < COLOR  col;
998 > static int
999 > putpdouble(                     /* put a double primary to stdout */
1000 >        COLOR  col
1001 > )
1002   {
1003          double  vd;
1004  
1005          vd = colval(col,putprim);
1006 +        if (swapbytes)
1007 +                swap64((char *)&vd, 1);
1008          fwrite((char *)&vd, sizeof(double), 1, stdout);
1009  
1010          return(ferror(stdout) ? -1 : 0);
1011   }
1012  
1013  
1014 < putpint(col)                    /* put an int primary to stdout */
1015 < COLOR  col;
1014 > static int
1015 > putpint(                        /* put an int primary to stdout */
1016 >        COLOR  col
1017 > )
1018   {
1019          fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
1020  
# Line 924 | Line 1022 | COLOR  col;
1022   }
1023  
1024  
1025 < putpbyte(col)                   /* put a byte primary to stdout */
1026 < COLOR  col;
1025 > static int
1026 > putpbyte(                       /* put a byte primary to stdout */
1027 >        COLOR  col
1028 > )
1029   {
1030          long  i;
1031          BYTE  vb;
# Line 938 | Line 1038 | COLOR  col;
1038   }
1039  
1040  
1041 < putpword(col)                   /* put a 16-bit primary to stdout */
1042 < COLOR  col;
1041 > static int
1042 > putpword(                       /* put a 16-bit primary to stdout */
1043 >        COLOR  col
1044 > )
1045   {
1046          long  i;
1047          uint16  vw;
# Line 947 | Line 1049 | COLOR  col;
1049          i = colval(col,putprim)*65536.;
1050          vw = min(i,65535);
1051          if (swapbytes)
1052 <                swap16(&vw, 1);
1052 >                swap16((char *)&vw, 1);
1053          fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1054  
1055          return(ferror(stdout) ? -1 : 0);
1056   }
1057  
1058  
1059 < set_io()                        /* set put and get functions */
1059 > static void
1060 > set_io(void)                    /* set put and get functions */
1061   {
1062          switch (format) {
1063          case 'a':                                       /* ascii */
# Line 1082 | Line 1185 | set_io()                       /* set put and get functions */
1185                  }
1186                  return;
1187          }
1188 < badopt:
1188 > /* badopt: */ /* label not used */
1189          fprintf(stderr, "%s: botched file type\n", progname);
1190          quit(1);
1191   namerr:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines