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.13 by gwlarson, Tue Oct 27 09:08:27 1998 UTC vs.
Revision 2.25 by schorsch, Fri Jan 2 12:47:01 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   *  pvalue.c - program to print pixel values.
6   *
7   *     4/23/86
8   */
9  
10 + #include  "platform.h"
11   #include  "standard.h"
14
12   #include  "color.h"
16
13   #include  "resolu.h"
14  
15   #define  min(a,b)               ((a)<(b)?(a):(b))
# Line 22 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18   #define  ALL            3
19   #define  BRIGHT         4
20  
25 #define  brightonly     (putprim==BRIGHT)
26
21   RESOLU  picres;                 /* resolution of picture */
22  
23   int  uniq = 0;                  /* print only unique values? */
# Line 43 | Line 37 | int  header = 1;               /* do header? */
37  
38   long  skipbytes = 0;            /* skip bytes in input? */
39  
40 + int  swapbytes = 0;             /* swap bytes? */
41 +
42   int  interleave = 1;            /* file is interleaved? */
43  
44   int  resolution = 1;            /* put/get resolution string? */
# Line 65 | Line 61 | FILE  *fin2 = NULL, *fin3 = NULL;      /* for other color c
61  
62   int  (*getval)(), (*putval)();
63  
64 + static gethfunc checkhead;
65  
66 +
67 + double
68 + rgb_bright(clr)
69 + COLOR  clr;
70 + {
71 +        return(bright(clr));
72 + }
73 +
74 + double
75 + xyz_bright(clr)
76 + COLOR  clr;
77 + {
78 +        return(clr[CIEY]);
79 + }
80 +
81 + double  (*mybright)() = &rgb_bright;
82 +
83 +
84   main(argc, argv)
85   int  argc;
86   char  **argv;
87   {
73        extern int  checkhead();
88          extern long  atol();
89          double  d, expval = 1.0;
90          int  i;
# Line 152 | Line 166 | char  **argv;
166                                          format = 'b';
167                                          fmtid = "byte";
168                                          break;
169 +                                case 'W':               /* 16-bit swapped */
170 +                                        swapbytes = 1;
171 +                                case 'w':               /* 16-bit */
172 +                                        dataonly = 1;
173 +                                        format = 'w';
174 +                                        fmtid = "16-bit";
175 +                                        break;
176 +                                case 'F':               /* swapped floats */
177 +                                        swapbytes = 1;
178                                  case 'f':               /* float */
179                                          dataonly = 1;
180                                          format = 'f';
181                                          fmtid = "float";
182                                          break;
183 +                                case 'D':               /* swapped doubles */
184 +                                        swapbytes = 1;
185                                  case 'd':               /* double */
186                                          dataonly = 1;
187                                          format = 'd';
# Line 170 | Line 195 | char  **argv;
195                          case 'X':               /* x resolution */
196                                  resolution = 0;
197                                  if (argv[i][0] == '-')
198 <                                        picres.or |= XDECR;
198 >                                        picres.rt |= XDECR;
199                                  picres.xr = atoi(argv[++i]);
200                                  break;
201                          case 'y':               /* y resolution */
202                          case 'Y':               /* y resolution */
203                                  resolution = 0;
204                                  if (argv[i][0] == '-')
205 <                                        picres.or |= YDECR;
205 >                                        picres.rt |= YDECR;
206                                  if (picres.xr == 0)
207 <                                        picres.or |= YMAJOR;
207 >                                        picres.rt |= YMAJOR;
208                                  picres.yr = atoi(argv[++i]);
209                                  break;
210                          default:
# Line 192 | Line 217 | unkopt:
217                  else
218                          break;
219                                          /* recognize special formats */
220 <        if (dataonly && format == 'b')
221 <                if (brightonly)
220 >        if (dataonly && format == 'b') {
221 >                if (putprim == ALL)
222 >                        fmtid = "24-bit_rgb";
223 >                else
224                          fmtid = "8-bit_grey";
225 +        }
226 +        if (dataonly && format == 'w') {
227 +                if (putprim == ALL)
228 +                        fmtid = "48-bit_rgb";
229                  else
230 <                        fmtid = "24-bit_rgb";
230 >                        fmtid = "16-bit_grey";
231 >        }
232                                          /* assign reverse ordering */
233          rord[ord[0]] = 0;
234          rord[ord[1]] = 1;
# Line 210 | Line 242 | unkopt:
242                                                  progname, argv[i]);
243                          quit(1);
244                  }
245 <                if (reverse && !brightonly && i == argc-3) {
245 >                if (reverse && putprim != BRIGHT && i == argc-3) {
246                          if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
247                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
248                                                  progname, argv[i+1]);
# Line 224 | Line 256 | unkopt:
256                          interleave = -1;
257                  } else if (i != argc-1)
258                          fin = NULL;
259 <                if (reverse && !brightonly && !interleave) {
259 >                if (reverse && putprim != BRIGHT && !interleave) {
260                          fin2 = fopen(argv[i], "r");
261                          fin3 = fopen(argv[i], "r");
262                  }
# Line 242 | Line 274 | unkopt:
274          }
275  
276          if (reverse) {
277 < #ifdef MSDOS
278 <                setmode(fileno(stdout), O_BINARY);
277 > #ifdef _WIN32
278 >                SET_FILE_BINARY(stdout);
279                  if (format != 'a' && format != 'i')
280 <                        setmode(fileno(fin), O_BINARY);
280 >                        SET_FILE_BINARY(fin);
281   #endif
282                                          /* get header */
283                  if (header) {
# Line 269 | Line 301 | unkopt:
301                  if (resolution && fin2 != NULL) {
302                          RESOLU  pres2;
303                          if (!fgetsresolu(&pres2, fin2) ||
304 <                                        pres2.or != picres.or ||
304 >                                        pres2.rt != picres.rt ||
305                                          pres2.xr != picres.xr ||
306                                          pres2.yr != picres.yr ||
307                                          !fgetsresolu(&pres2, fin3) ||
308 <                                        pres2.or != picres.or ||
308 >                                        pres2.rt != picres.rt ||
309                                          pres2.xr != picres.xr ||
310                                          pres2.yr != picres.yr) {
311                                  fprintf(stderr, "%s: resolution mismatch\n",
# Line 290 | Line 322 | unkopt:
322                  fputsresolu(&picres, stdout);   /* always put resolution */
323                  valtopix();
324          } else {
325 < #ifdef MSDOS
326 <                setmode(fileno(fin), O_BINARY);
325 > #ifdef _WIN32
326 >                SET_FILE_BINARY(fin);
327                  if (format != 'a' && format != 'i')
328 <                        setmode(fileno(stdout), O_BINARY);
328 >                        SET_FILE_BINARY(stdout);
329   #endif
330                                                  /* get header */
331                  getheader(fin, checkhead, NULL);
332                  if (wrongformat) {
333 <                        fprintf(stderr, "%s: input not a Radiance picture\n",
333 >                        fprintf(stderr,
334 >                                "%s: input not a Radiance RGBE picture\n",
335                                          progname);
336                          quit(1);
337                  }
# Line 322 | Line 355 | unkopt:
355   }
356  
357  
358 < int
359 < checkhead(line)                         /* deal with line from header */
360 < char  *line;
358 > static int
359 > checkhead(                              /* deal with line from header */
360 >        char    *line,
361 >        void    *p
362 > )
363   {
364          char    fmt[32];
365          double  d;
366          COLOR   ctmp;
367  
368 <        if (formatval(fmt, line))
369 <                wrongformat = strcmp(fmt, COLRFMT);
370 <        else if (original && isexpos(line)) {
368 >        if (formatval(fmt, line)) {
369 >                if (!strcmp(fmt, CIEFMT)) {
370 >                        mybright = &xyz_bright;
371 >                        if (original) {
372 >                                scalecolor(exposure, 1./WHTEFFICACY);
373 >                                doexposure++;
374 >                        }
375 >                } else if (!strcmp(fmt, COLRFMT))
376 >                        mybright = &rgb_bright;
377 >                else
378 >                        wrongformat++;
379 >        } else if (original && isexpos(line)) {
380                  d = 1.0/exposval(line);
381                  scalecolor(exposure, d);
382                  doexposure++;
# Line 353 | Line 397 | pixtoval()                             /* convert picture to values */
397          register COLOR  *scanln;
398          int  dogamma;
399          COLOR  lastc;
400 <        FLOAT  hv[2];
400 >        RREAL  hv[2];
401          int  startprim, endprim;
402          long  startpos;
403          int  y;
# Line 385 | Line 429 | pixtoval()                             /* convert picture to values */
429                                  quit(1);
430                          }
431                          for (x = 0; x < scanlen(&picres); x++) {
432 <                                if (uniq)
432 >                                if (uniq) {
433                                          if (    colval(scanln[x],RED) ==
434                                                          colval(lastc,RED) &&
435                                                  colval(scanln[x],GRN) ==
# Line 395 | Line 439 | pixtoval()                             /* convert picture to values */
439                                                  continue;
440                                          else
441                                                  copycolor(lastc, scanln[x]);
442 +                                }
443                                  if (doexposure)
444                                          multcolor(scanln[x], exposure);
445                                  if (dogamma)
# Line 416 | Line 461 | pixtoval()                             /* convert picture to values */
461                          }
462                  }
463          }
464 <        free((char *)scanln);
464 >        free((void *)scanln);
465   }
466  
467  
# Line 460 | Line 505 | valtopix()                     /* convert values to a pixel file */
505                          quit(1);
506                  }
507          }
508 <        free((char *)scanln);
508 >        free((void *)scanln);
509   }
510  
511  
512 + void
513   quit(code)
514   int  code;
515   {
# Line 471 | Line 517 | int  code;
517   }
518  
519  
520 + swap16(wp, n)           /* swap n 16-bit words */
521 + register uint16  *wp;
522 + int  n;
523 + {
524 +        while (n-- > 0) {
525 +                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
526 +                wp++;
527 +        }
528 + }
529 +
530 +
531 + swap32(wp, n)           /* swap n 32-bit words */
532 + register uint32  *wp;
533 + int  n;
534 + {
535 +        while (n-- > 0) {
536 +                *wp = *wp << 24 | ((*wp >> 24) & 0xff) |
537 +                        (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8;
538 +                wp++;
539 +        }
540 + }
541 +
542 +
543 + swap64(wp, n)           /* swap n 64-bit words */
544 + register char  *wp;
545 + int  n;
546 + {
547 +        register int    t;
548 +
549 +        while (n-- > 0) {
550 +                t = wp[0]; wp[0] = wp[7]; wp[7] = t;
551 +                t = wp[1]; wp[1] = wp[6]; wp[6] = t;
552 +                t = wp[2]; wp[2] = wp[5]; wp[5] = t;
553 +                t = wp[3]; wp[3] = wp[4]; wp[4] = t;
554 +                wp += 8;
555 +        }
556 + }
557 +
558 +
559   getcascii(col)          /* get an ascii color value from stream(s) */
560   COLOR  col;
561   {
# Line 504 | Line 589 | COLOR  col;
589                          fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
590                          return(-1);
591          }
592 +        if (swapbytes)
593 +                swap64((char *)vd, 3);
594          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
595          return(0);
596   }
# Line 523 | Line 610 | COLOR  col;
610                          fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
611                          return(-1);
612          }
613 +        if (swapbytes)
614 +                swap32((uint32 *)vf, 3);
615          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
616          return(0);
617   }
# Line 568 | Line 657 | COLOR  col;
657   }
658  
659  
660 + getcword(col)           /* get a 16-bit color value from stream(s) */
661 + COLOR  col;
662 + {
663 +        uint16  vw[3];
664 +
665 +        if (fin2 == NULL) {
666 +                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
667 +                        return(-1);
668 +        } else {
669 +                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
670 +                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
671 +                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
672 +                        return(-1);
673 +        }
674 +        if (swapbytes)
675 +                swap16(vw, 3);
676 +        setcolor(col, (vw[rord[RED]]+.5)/65536.,
677 +                        (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
678 +        return(0);
679 + }
680 +
681 +
682   getbascii(col)          /* get an ascii brightness value from fin */
683   COLOR  col;
684   {
# Line 587 | Line 698 | COLOR  col;
698  
699          if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
700                  return(-1);
701 +        if (swapbytes)
702 +                swap64((char *)&vd, 1);
703          setcolor(col, vd, vd, vd);
704          return(0);
705   }
# Line 599 | Line 712 | COLOR  col;
712  
713          if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
714                  return(-1);
715 +        if (swapbytes)
716 +                swap32((uint32 *)&vf, 1);
717          setcolor(col, vf, vf, vf);
718          return(0);
719   }
# Line 632 | Line 747 | COLOR  col;
747   }
748  
749  
750 + getbword(col)           /* get a 16-bit brightness value from fin */
751 + COLOR  col;
752 + {
753 +        uint16  vw;
754 +        double  d;
755 +
756 +        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
757 +                return(-1);
758 +        if (swapbytes)
759 +                swap16(&vw, 1);
760 +        d = (vw+.5)/65536.;
761 +        setcolor(col, d, d, d);
762 +        return(0);
763 + }
764 +
765 +
766   putcascii(col)                  /* put an ascii color to stdout */
767   COLOR  col;
768   {
# Line 652 | Line 783 | COLOR  col;
783          vf[0] = colval(col,ord[0]);
784          vf[1] = colval(col,ord[1]);
785          vf[2] = colval(col,ord[2]);
786 +        if (swapbytes)
787 +                swap32((uint32 *)vf, 3);
788          fwrite((char *)vf, sizeof(float), 3, stdout);
789  
790          return(ferror(stdout) ? -1 : 0);
# Line 666 | Line 799 | COLOR  col;
799          vd[0] = colval(col,ord[0]);
800          vd[1] = colval(col,ord[1]);
801          vd[2] = colval(col,ord[2]);
802 +        if (swapbytes)
803 +                swap64((char *)vd, 3);
804          fwrite((char *)vd, sizeof(double), 3, stdout);
805  
806          return(ferror(stdout) ? -1 : 0);
# Line 687 | Line 822 | COLOR  col;
822   putcbyte(col)                   /* put a byte color to stdout */
823   COLOR  col;
824   {
825 <        register int  i;
825 >        long  i;
826          BYTE  vb[3];
827  
828          i = colval(col,ord[0])*256.;
# Line 702 | Line 837 | COLOR  col;
837   }
838  
839  
840 + putcword(col)                   /* put a 16-bit color to stdout */
841 + COLOR  col;
842 + {
843 +        long  i;
844 +        uint16  vw[3];
845 +
846 +        i = colval(col,ord[0])*65536.;
847 +        vw[0] = min(i,65535);
848 +        i = colval(col,ord[1])*65536.;
849 +        vw[1] = min(i,65535);
850 +        i = colval(col,ord[2])*65536.;
851 +        vw[2] = min(i,65535);
852 +        if (swapbytes)
853 +                swap16(vw, 3);
854 +        fwrite((char *)vw, sizeof(uint16), 3, stdout);
855 +
856 +        return(ferror(stdout) ? -1 : 0);
857 + }
858 +
859 +
860   putbascii(col)                  /* put an ascii brightness to stdout */
861   COLOR  col;
862   {
863 <        fprintf(stdout, "%15.3e\n", bright(col));
863 >        fprintf(stdout, "%15.3e\n", (*mybright)(col));
864  
865          return(ferror(stdout) ? -1 : 0);
866   }
# Line 716 | Line 871 | COLOR  col;
871   {
872          float  vf;
873  
874 <        vf = bright(col);
874 >        vf = (*mybright)(col);
875 >        if (swapbytes)
876 >                swap32((uint32 *)&vf, 1);
877          fwrite((char *)&vf, sizeof(float), 1, stdout);
878  
879          return(ferror(stdout) ? -1 : 0);
# Line 728 | Line 885 | COLOR  col;
885   {
886          double  vd;
887  
888 <        vd = bright(col);
888 >        vd = (*mybright)(col);
889 >        if (swapbytes)
890 >                swap64((char *)&vd, 1);
891          fwrite((char *)&vd, sizeof(double), 1, stdout);
892  
893          return(ferror(stdout) ? -1 : 0);
# Line 738 | Line 897 | COLOR  col;
897   putbint(col)                    /* put an int brightness to stdout */
898   COLOR  col;
899   {
900 <        fprintf(stdout, "%d\n", (int)(bright(col)*256.));
900 >        fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
901  
902          return(ferror(stdout) ? -1 : 0);
903   }
# Line 750 | Line 909 | COLOR  col;
909          register int  i;
910          BYTE  vb;
911  
912 <        i = bright(col)*256.;
912 >        i = (*mybright)(col)*256.;
913          vb = min(i,255);
914          fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
915  
# Line 758 | Line 917 | COLOR  col;
917   }
918  
919  
920 + putbword(col)                   /* put a 16-bit brightness to stdout */
921 + COLOR  col;
922 + {
923 +        long  i;
924 +        uint16  vw;
925 +
926 +        i = (*mybright)(col)*65536.;
927 +        vw = min(i,65535);
928 +        if (swapbytes)
929 +                swap16(&vw, 1);
930 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
931 +
932 +        return(ferror(stdout) ? -1 : 0);
933 + }
934 +
935 +
936   putpascii(col)                  /* put an ascii primary to stdout */
937   COLOR  col;
938   {
# Line 773 | Line 948 | COLOR  col;
948          float  vf;
949  
950          vf = colval(col,putprim);
951 +        if (swapbytes)
952 +                swap32((uint32 *)&vf, 1);
953          fwrite((char *)&vf, sizeof(float), 1, stdout);
954  
955          return(ferror(stdout) ? -1 : 0);
# Line 785 | Line 962 | COLOR  col;
962          double  vd;
963  
964          vd = colval(col,putprim);
965 +        if (swapbytes)
966 +                swap64((char *)&vd, 1);
967          fwrite((char *)&vd, sizeof(double), 1, stdout);
968  
969          return(ferror(stdout) ? -1 : 0);
# Line 803 | Line 982 | COLOR  col;
982   putpbyte(col)                   /* put a byte primary to stdout */
983   COLOR  col;
984   {
985 <        register int  i;
985 >        long  i;
986          BYTE  vb;
987  
988          i = colval(col,putprim)*256.;
# Line 814 | Line 993 | COLOR  col;
993   }
994  
995  
996 + putpword(col)                   /* put a 16-bit primary to stdout */
997 + COLOR  col;
998 + {
999 +        long  i;
1000 +        uint16  vw;
1001 +
1002 +        i = colval(col,putprim)*65536.;
1003 +        vw = min(i,65535);
1004 +        if (swapbytes)
1005 +                swap16(&vw, 1);
1006 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1007 +
1008 +        return(ferror(stdout) ? -1 : 0);
1009 + }
1010 +
1011 +
1012   set_io()                        /* set put and get functions */
1013   {
1014          switch (format) {
# Line 915 | Line 1110 | set_io()                       /* set put and get functions */
1110                                          goto seekerr;
1111                                  if (fseek(fin3,
1112                                  (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1113 +                                        goto seekerr;
1114 +                        }
1115 +                }
1116 +                return;
1117 +        case 'w':                                       /* 16-bit */
1118 +                if (putprim == BRIGHT) {
1119 +                        getval = getbword;
1120 +                        putval = putbword;
1121 +                } else if (putprim != ALL) {
1122 +                        getval = getbword;
1123 +                        putval = putpword;
1124 +                } else {
1125 +                        getval = getcword;
1126 +                        putval = putcword;
1127 +                        if (reverse && !interleave) {
1128 +                                if (fin2 == NULL)
1129 +                                        goto namerr;
1130 +                                if (fseek(fin2,
1131 +                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1132 +                                        goto seekerr;
1133 +                                if (fseek(fin3,
1134 +                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1135                                          goto seekerr;
1136                          }
1137                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines