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.24 by greg, Wed Oct 22 02:06:35 2003 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 + double
65 + rgb_bright(clr)
66 + COLOR  clr;
67 + {
68 +        return(bright(clr));
69 + }
70  
71 + double
72 + xyz_bright(clr)
73 + COLOR  clr;
74 + {
75 +        return(clr[CIEY]);
76 + }
77 +
78 + double  (*mybright)() = &rgb_bright;
79 +
80 +
81   main(argc, argv)
82   int  argc;
83   char  **argv;
# Line 152 | Line 164 | char  **argv;
164                                          format = 'b';
165                                          fmtid = "byte";
166                                          break;
167 +                                case 'W':               /* 16-bit swapped */
168 +                                        swapbytes = 1;
169 +                                case 'w':               /* 16-bit */
170 +                                        dataonly = 1;
171 +                                        format = 'w';
172 +                                        fmtid = "16-bit";
173 +                                        break;
174 +                                case 'F':               /* swapped floats */
175 +                                        swapbytes = 1;
176                                  case 'f':               /* float */
177                                          dataonly = 1;
178                                          format = 'f';
179                                          fmtid = "float";
180                                          break;
181 +                                case 'D':               /* swapped doubles */
182 +                                        swapbytes = 1;
183                                  case 'd':               /* double */
184                                          dataonly = 1;
185                                          format = 'd';
# Line 170 | Line 193 | char  **argv;
193                          case 'X':               /* x resolution */
194                                  resolution = 0;
195                                  if (argv[i][0] == '-')
196 <                                        picres.or |= XDECR;
196 >                                        picres.rt |= XDECR;
197                                  picres.xr = atoi(argv[++i]);
198                                  break;
199                          case 'y':               /* y resolution */
200                          case 'Y':               /* y resolution */
201                                  resolution = 0;
202                                  if (argv[i][0] == '-')
203 <                                        picres.or |= YDECR;
203 >                                        picres.rt |= YDECR;
204                                  if (picres.xr == 0)
205 <                                        picres.or |= YMAJOR;
205 >                                        picres.rt |= YMAJOR;
206                                  picres.yr = atoi(argv[++i]);
207                                  break;
208                          default:
# Line 192 | Line 215 | unkopt:
215                  else
216                          break;
217                                          /* recognize special formats */
218 <        if (dataonly && format == 'b')
219 <                if (brightonly)
218 >        if (dataonly && format == 'b') {
219 >                if (putprim == ALL)
220 >                        fmtid = "24-bit_rgb";
221 >                else
222                          fmtid = "8-bit_grey";
223 +        }
224 +        if (dataonly && format == 'w') {
225 +                if (putprim == ALL)
226 +                        fmtid = "48-bit_rgb";
227                  else
228 <                        fmtid = "24-bit_rgb";
228 >                        fmtid = "16-bit_grey";
229 >        }
230                                          /* assign reverse ordering */
231          rord[ord[0]] = 0;
232          rord[ord[1]] = 1;
# Line 210 | Line 240 | unkopt:
240                                                  progname, argv[i]);
241                          quit(1);
242                  }
243 <                if (reverse && !brightonly && i == argc-3) {
243 >                if (reverse && putprim != BRIGHT && i == argc-3) {
244                          if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
245                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
246                                                  progname, argv[i+1]);
# Line 224 | Line 254 | unkopt:
254                          interleave = -1;
255                  } else if (i != argc-1)
256                          fin = NULL;
257 <                if (reverse && !brightonly && !interleave) {
257 >                if (reverse && putprim != BRIGHT && !interleave) {
258                          fin2 = fopen(argv[i], "r");
259                          fin3 = fopen(argv[i], "r");
260                  }
# Line 242 | Line 272 | unkopt:
272          }
273  
274          if (reverse) {
275 < #ifdef MSDOS
276 <                setmode(fileno(stdout), O_BINARY);
275 > #ifdef _WIN32
276 >                SET_FILE_BINARY(stdout);
277                  if (format != 'a' && format != 'i')
278 <                        setmode(fileno(fin), O_BINARY);
278 >                        SET_FILE_BINARY(fin);
279   #endif
280                                          /* get header */
281                  if (header) {
# Line 269 | Line 299 | unkopt:
299                  if (resolution && fin2 != NULL) {
300                          RESOLU  pres2;
301                          if (!fgetsresolu(&pres2, fin2) ||
302 <                                        pres2.or != picres.or ||
302 >                                        pres2.rt != picres.rt ||
303                                          pres2.xr != picres.xr ||
304                                          pres2.yr != picres.yr ||
305                                          !fgetsresolu(&pres2, fin3) ||
306 <                                        pres2.or != picres.or ||
306 >                                        pres2.rt != picres.rt ||
307                                          pres2.xr != picres.xr ||
308                                          pres2.yr != picres.yr) {
309                                  fprintf(stderr, "%s: resolution mismatch\n",
# Line 290 | Line 320 | unkopt:
320                  fputsresolu(&picres, stdout);   /* always put resolution */
321                  valtopix();
322          } else {
323 < #ifdef MSDOS
324 <                setmode(fileno(fin), O_BINARY);
323 > #ifdef _WIN32
324 >                SET_FILE_BINARY(fin);
325                  if (format != 'a' && format != 'i')
326 <                        setmode(fileno(stdout), O_BINARY);
326 >                        SET_FILE_BINARY(stdout);
327   #endif
328                                                  /* get header */
329                  getheader(fin, checkhead, NULL);
330                  if (wrongformat) {
331 <                        fprintf(stderr, "%s: input not a Radiance picture\n",
331 >                        fprintf(stderr,
332 >                                "%s: input not a Radiance RGBE picture\n",
333                                          progname);
334                          quit(1);
335                  }
# Line 330 | Line 361 | char  *line;
361          double  d;
362          COLOR   ctmp;
363  
364 <        if (formatval(fmt, line))
365 <                wrongformat = strcmp(fmt, COLRFMT);
366 <        else if (original && isexpos(line)) {
364 >        if (formatval(fmt, line)) {
365 >                if (!strcmp(fmt, CIEFMT)) {
366 >                        mybright = &xyz_bright;
367 >                        if (original) {
368 >                                scalecolor(exposure, 1./WHTEFFICACY);
369 >                                doexposure++;
370 >                        }
371 >                } else if (!strcmp(fmt, COLRFMT))
372 >                        mybright = &rgb_bright;
373 >                else
374 >                        wrongformat++;
375 >        } else if (original && isexpos(line)) {
376                  d = 1.0/exposval(line);
377                  scalecolor(exposure, d);
378                  doexposure++;
# Line 353 | Line 393 | pixtoval()                             /* convert picture to values */
393          register COLOR  *scanln;
394          int  dogamma;
395          COLOR  lastc;
396 <        FLOAT  hv[2];
396 >        RREAL  hv[2];
397          int  startprim, endprim;
398          long  startpos;
399          int  y;
# Line 385 | Line 425 | pixtoval()                             /* convert picture to values */
425                                  quit(1);
426                          }
427                          for (x = 0; x < scanlen(&picres); x++) {
428 <                                if (uniq)
428 >                                if (uniq) {
429                                          if (    colval(scanln[x],RED) ==
430                                                          colval(lastc,RED) &&
431                                                  colval(scanln[x],GRN) ==
# Line 395 | Line 435 | pixtoval()                             /* convert picture to values */
435                                                  continue;
436                                          else
437                                                  copycolor(lastc, scanln[x]);
438 +                                }
439                                  if (doexposure)
440                                          multcolor(scanln[x], exposure);
441                                  if (dogamma)
# Line 416 | Line 457 | pixtoval()                             /* convert picture to values */
457                          }
458                  }
459          }
460 <        free((char *)scanln);
460 >        free((void *)scanln);
461   }
462  
463  
# Line 460 | Line 501 | valtopix()                     /* convert values to a pixel file */
501                          quit(1);
502                  }
503          }
504 <        free((char *)scanln);
504 >        free((void *)scanln);
505   }
506  
507  
508 + void
509   quit(code)
510   int  code;
511   {
# Line 471 | Line 513 | int  code;
513   }
514  
515  
516 + swap16(wp, n)           /* swap n 16-bit words */
517 + register uint16  *wp;
518 + int  n;
519 + {
520 +        while (n-- > 0) {
521 +                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
522 +                wp++;
523 +        }
524 + }
525 +
526 +
527 + swap32(wp, n)           /* swap n 32-bit words */
528 + register uint32  *wp;
529 + int  n;
530 + {
531 +        while (n-- > 0) {
532 +                *wp = *wp << 24 | ((*wp >> 24) & 0xff) |
533 +                        (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8;
534 +                wp++;
535 +        }
536 + }
537 +
538 +
539 + swap64(wp, n)           /* swap n 64-bit words */
540 + register char  *wp;
541 + int  n;
542 + {
543 +        register int    t;
544 +
545 +        while (n-- > 0) {
546 +                t = wp[0]; wp[0] = wp[7]; wp[7] = t;
547 +                t = wp[1]; wp[1] = wp[6]; wp[6] = t;
548 +                t = wp[2]; wp[2] = wp[5]; wp[5] = t;
549 +                t = wp[3]; wp[3] = wp[4]; wp[4] = t;
550 +                wp += 8;
551 +        }
552 + }
553 +
554 +
555   getcascii(col)          /* get an ascii color value from stream(s) */
556   COLOR  col;
557   {
# Line 504 | Line 585 | COLOR  col;
585                          fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
586                          return(-1);
587          }
588 +        if (swapbytes)
589 +                swap64((char *)vd, 3);
590          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
591          return(0);
592   }
# Line 523 | Line 606 | COLOR  col;
606                          fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
607                          return(-1);
608          }
609 +        if (swapbytes)
610 +                swap32((uint32 *)vf, 3);
611          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
612          return(0);
613   }
# Line 568 | Line 653 | COLOR  col;
653   }
654  
655  
656 + getcword(col)           /* get a 16-bit color value from stream(s) */
657 + COLOR  col;
658 + {
659 +        uint16  vw[3];
660 +
661 +        if (fin2 == NULL) {
662 +                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
663 +                        return(-1);
664 +        } else {
665 +                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
666 +                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
667 +                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
668 +                        return(-1);
669 +        }
670 +        if (swapbytes)
671 +                swap16(vw, 3);
672 +        setcolor(col, (vw[rord[RED]]+.5)/65536.,
673 +                        (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
674 +        return(0);
675 + }
676 +
677 +
678   getbascii(col)          /* get an ascii brightness value from fin */
679   COLOR  col;
680   {
# Line 587 | Line 694 | COLOR  col;
694  
695          if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
696                  return(-1);
697 +        if (swapbytes)
698 +                swap64((char *)&vd, 1);
699          setcolor(col, vd, vd, vd);
700          return(0);
701   }
# Line 599 | Line 708 | COLOR  col;
708  
709          if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
710                  return(-1);
711 +        if (swapbytes)
712 +                swap32((uint32 *)&vf, 1);
713          setcolor(col, vf, vf, vf);
714          return(0);
715   }
# Line 632 | Line 743 | COLOR  col;
743   }
744  
745  
746 + getbword(col)           /* get a 16-bit brightness value from fin */
747 + COLOR  col;
748 + {
749 +        uint16  vw;
750 +        double  d;
751 +
752 +        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
753 +                return(-1);
754 +        if (swapbytes)
755 +                swap16(&vw, 1);
756 +        d = (vw+.5)/65536.;
757 +        setcolor(col, d, d, d);
758 +        return(0);
759 + }
760 +
761 +
762   putcascii(col)                  /* put an ascii color to stdout */
763   COLOR  col;
764   {
# Line 652 | Line 779 | COLOR  col;
779          vf[0] = colval(col,ord[0]);
780          vf[1] = colval(col,ord[1]);
781          vf[2] = colval(col,ord[2]);
782 +        if (swapbytes)
783 +                swap32((uint32 *)vf, 3);
784          fwrite((char *)vf, sizeof(float), 3, stdout);
785  
786          return(ferror(stdout) ? -1 : 0);
# Line 666 | Line 795 | COLOR  col;
795          vd[0] = colval(col,ord[0]);
796          vd[1] = colval(col,ord[1]);
797          vd[2] = colval(col,ord[2]);
798 +        if (swapbytes)
799 +                swap64((char *)vd, 3);
800          fwrite((char *)vd, sizeof(double), 3, stdout);
801  
802          return(ferror(stdout) ? -1 : 0);
# Line 687 | Line 818 | COLOR  col;
818   putcbyte(col)                   /* put a byte color to stdout */
819   COLOR  col;
820   {
821 <        register int  i;
821 >        long  i;
822          BYTE  vb[3];
823  
824          i = colval(col,ord[0])*256.;
# Line 702 | Line 833 | COLOR  col;
833   }
834  
835  
836 + putcword(col)                   /* put a 16-bit color to stdout */
837 + COLOR  col;
838 + {
839 +        long  i;
840 +        uint16  vw[3];
841 +
842 +        i = colval(col,ord[0])*65536.;
843 +        vw[0] = min(i,65535);
844 +        i = colval(col,ord[1])*65536.;
845 +        vw[1] = min(i,65535);
846 +        i = colval(col,ord[2])*65536.;
847 +        vw[2] = min(i,65535);
848 +        if (swapbytes)
849 +                swap16(vw, 3);
850 +        fwrite((char *)vw, sizeof(uint16), 3, stdout);
851 +
852 +        return(ferror(stdout) ? -1 : 0);
853 + }
854 +
855 +
856   putbascii(col)                  /* put an ascii brightness to stdout */
857   COLOR  col;
858   {
859 <        fprintf(stdout, "%15.3e\n", bright(col));
859 >        fprintf(stdout, "%15.3e\n", (*mybright)(col));
860  
861          return(ferror(stdout) ? -1 : 0);
862   }
# Line 716 | Line 867 | COLOR  col;
867   {
868          float  vf;
869  
870 <        vf = bright(col);
870 >        vf = (*mybright)(col);
871 >        if (swapbytes)
872 >                swap32((uint32 *)&vf, 1);
873          fwrite((char *)&vf, sizeof(float), 1, stdout);
874  
875          return(ferror(stdout) ? -1 : 0);
# Line 728 | Line 881 | COLOR  col;
881   {
882          double  vd;
883  
884 <        vd = bright(col);
884 >        vd = (*mybright)(col);
885 >        if (swapbytes)
886 >                swap64((char *)&vd, 1);
887          fwrite((char *)&vd, sizeof(double), 1, stdout);
888  
889          return(ferror(stdout) ? -1 : 0);
# Line 738 | Line 893 | COLOR  col;
893   putbint(col)                    /* put an int brightness to stdout */
894   COLOR  col;
895   {
896 <        fprintf(stdout, "%d\n", (int)(bright(col)*256.));
896 >        fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
897  
898          return(ferror(stdout) ? -1 : 0);
899   }
# Line 750 | Line 905 | COLOR  col;
905          register int  i;
906          BYTE  vb;
907  
908 <        i = bright(col)*256.;
908 >        i = (*mybright)(col)*256.;
909          vb = min(i,255);
910          fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
911  
# Line 758 | Line 913 | COLOR  col;
913   }
914  
915  
916 + putbword(col)                   /* put a 16-bit brightness to stdout */
917 + COLOR  col;
918 + {
919 +        long  i;
920 +        uint16  vw;
921 +
922 +        i = (*mybright)(col)*65536.;
923 +        vw = min(i,65535);
924 +        if (swapbytes)
925 +                swap16(&vw, 1);
926 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
927 +
928 +        return(ferror(stdout) ? -1 : 0);
929 + }
930 +
931 +
932   putpascii(col)                  /* put an ascii primary to stdout */
933   COLOR  col;
934   {
# Line 773 | Line 944 | COLOR  col;
944          float  vf;
945  
946          vf = colval(col,putprim);
947 +        if (swapbytes)
948 +                swap32((uint32 *)&vf, 1);
949          fwrite((char *)&vf, sizeof(float), 1, stdout);
950  
951          return(ferror(stdout) ? -1 : 0);
# Line 785 | Line 958 | COLOR  col;
958          double  vd;
959  
960          vd = colval(col,putprim);
961 +        if (swapbytes)
962 +                swap64((char *)&vd, 1);
963          fwrite((char *)&vd, sizeof(double), 1, stdout);
964  
965          return(ferror(stdout) ? -1 : 0);
# Line 803 | Line 978 | COLOR  col;
978   putpbyte(col)                   /* put a byte primary to stdout */
979   COLOR  col;
980   {
981 <        register int  i;
981 >        long  i;
982          BYTE  vb;
983  
984          i = colval(col,putprim)*256.;
# Line 814 | Line 989 | COLOR  col;
989   }
990  
991  
992 + putpword(col)                   /* put a 16-bit primary to stdout */
993 + COLOR  col;
994 + {
995 +        long  i;
996 +        uint16  vw;
997 +
998 +        i = colval(col,putprim)*65536.;
999 +        vw = min(i,65535);
1000 +        if (swapbytes)
1001 +                swap16(&vw, 1);
1002 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1003 +
1004 +        return(ferror(stdout) ? -1 : 0);
1005 + }
1006 +
1007 +
1008   set_io()                        /* set put and get functions */
1009   {
1010          switch (format) {
# Line 915 | Line 1106 | set_io()                       /* set put and get functions */
1106                                          goto seekerr;
1107                                  if (fseek(fin3,
1108                                  (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1109 +                                        goto seekerr;
1110 +                        }
1111 +                }
1112 +                return;
1113 +        case 'w':                                       /* 16-bit */
1114 +                if (putprim == BRIGHT) {
1115 +                        getval = getbword;
1116 +                        putval = putbword;
1117 +                } else if (putprim != ALL) {
1118 +                        getval = getbword;
1119 +                        putval = putpword;
1120 +                } else {
1121 +                        getval = getcword;
1122 +                        putval = putcword;
1123 +                        if (reverse && !interleave) {
1124 +                                if (fin2 == NULL)
1125 +                                        goto namerr;
1126 +                                if (fseek(fin2,
1127 +                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1128 +                                        goto seekerr;
1129 +                                if (fseek(fin3,
1130 +                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1131                                          goto seekerr;
1132                          }
1133                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines