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.29 by greg, Sat Dec 23 17:27:46 2006 UTC vs.
Revision 2.36 by greg, Mon Jul 15 23:42:44 2019 UTC

# Line 36 | Line 36 | int  original = 0;             /* convert to original values? */
36   int  wrongformat = 0;           /* wrong input format? */
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 90 | Line 93 | main(
93          char  **argv
94   )
95   {
96 <        double  d, expval = 1.0;
97 <        int  i;
96 >        const char      *inpmode = "r";
97 >        double          d, expval = 1.0;
98 >        int             i;
99  
100          progname = argv[0];
101          mybright = &rgb_bright; /* default */
# Line 146 | Line 150 | main(
150                          case 'b':               /* brightness values */
151                                  putprim = argv[i][0] == '-' ? BRIGHT : ALL;
152                                  break;
153 <                        case 'p':               /* put primary */
153 >                        case 'p':               /* primary controls */
154                                  switch (argv[i][2]) {
155 +                                /* these two options affect -r conversion */
156 +                                case '\0':
157 +                                        myprims[RED][CIEX] = atof(argv[++i]);
158 +                                        myprims[RED][CIEY] = atof(argv[++i]);
159 +                                        myprims[GRN][CIEX] = atof(argv[++i]);
160 +                                        myprims[GRN][CIEY] = atof(argv[++i]);
161 +                                        myprims[BLU][CIEX] = atof(argv[++i]);
162 +                                        myprims[BLU][CIEY] = atof(argv[++i]);
163 +                                        myprims[WHT][CIEX] = atof(argv[++i]);
164 +                                        myprims[WHT][CIEY] = atof(argv[++i]);
165 +                                        outprims = myprims;
166 +                                        break;
167 +                                case 'x': case 'X': outprims = NULL; break;
168 +                                /* the following options affect +r only */
169                                  case 'r': case 'R': putprim = RED; break;
170                                  case 'g': case 'G': putprim = GRN; break;
171                                  case 'b': case 'B': putprim = BLU; break;
# Line 234 | Line 252 | unkopt:
252                  else
253                          fmtid = "16-bit_grey";
254          }
255 +        if (!reverse || (format != 'a') & (format != 'i'))
256 +                inpmode = "rb";
257                                          /* assign reverse ordering */
258          rord[ord[0]] = 0;
259          rord[ord[1]] = 1;
260          rord[ord[2]] = 2;
261                                          /* get input */
262          if (i == argc) {
263 +                long    n = skipbytes;
264 +                if (inpmode[1] == 'b')
265 +                        SET_FILE_BINARY(stdin);
266 +                while (n-- > 0)
267 +                        if (getchar() == EOF) {
268 +                                fprintf(stderr,
269 +                                "%s: cannot skip %ld bytes on standard input\n",
270 +                                                progname, skipbytes);
271 +                                quit(1);
272 +                        }
273                  fin = stdin;
274          } else if (i < argc) {
275 <                if ((fin = fopen(argv[i], "r")) == NULL) {
275 >                if ((fin = fopen(argv[i], inpmode)) == NULL) {
276                          fprintf(stderr, "%s: can't open file \"%s\"\n",
277                                                  progname, argv[i]);
278                          quit(1);
279                  }
280                  if (reverse && putprim != BRIGHT && i == argc-3) {
281 <                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
281 >                        if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) {
282                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
283                                                  progname, argv[i+1]);
284                                  quit(1);
285                          }
286 <                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
286 >                        if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) {
287                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
288                                                  progname, argv[i+2]);
289                                  quit(1);
# Line 262 | Line 292 | unkopt:
292                  } else if (i != argc-1)
293                          fin = NULL;
294                  if (reverse && putprim != BRIGHT && !interleave) {
295 <                        fin2 = fopen(argv[i], "r");
296 <                        fin3 = fopen(argv[i], "r");
295 >                        fin2 = fopen(argv[i], inpmode);
296 >                        fin3 = fopen(argv[i], inpmode);
297                  }
298 <                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
299 <                                (fseek(fin2, skipbytes, 0) ||
300 <                                fseek(fin3, skipbytes, 0))))) {
298 >                if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL &&
299 >                                fseek(fin2, skipbytes, SEEK_SET) |
300 >                                fseek(fin3, skipbytes, SEEK_SET)))) {
301                          fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
302                                          progname, skipbytes);
303                          quit(1);
# Line 279 | Line 309 | unkopt:
309          }
310  
311          if (reverse) {
282 #ifdef _WIN32
312                  SET_FILE_BINARY(stdout);
284                if (format != 'a' && format != 'i')
285                        SET_FILE_BINARY(fin);
286 #endif
313                                          /* get header */
314                  if (header) {
315                          if (checkheader(fin, fmtid, stdout) < 0) {
# Line 322 | Line 348 | unkopt:
348                  printargs(i, argv, stdout);
349                  if (expval < .99 || expval > 1.01)
350                          fputexpos(expval, stdout);
351 <                fputformat(COLRFMT, stdout);
351 >                if (outprims != NULL) {
352 >                        if (outprims != stdprims)
353 >                                fputprims(outprims, stdout);
354 >                        fputformat(COLRFMT, stdout);
355 >                } else                          /* XYZ data */
356 >                        fputformat(CIEFMT, stdout);
357                  putchar('\n');
358                  fputsresolu(&picres, stdout);   /* always put resolution */
359                  valtopix();
360          } else {
361 < #ifdef _WIN32
331 <                SET_FILE_BINARY(fin);
332 <                if (format != 'a' && format != 'i')
361 >                if ((format != 'a') & (format != 'i'))
362                          SET_FILE_BINARY(stdout);
334 #endif
363                                                  /* get header */
364                  getheader(fin, checkhead, NULL);
365                  if (wrongformat) {
# Line 367 | Line 395 | checkhead(                             /* deal with line from header */
395          void    *p
396   )
397   {
398 <        char    fmt[32];
398 >        char    fmt[MAXFMTLEN];
399          double  d;
400          COLOR   ctmp;
401  
# Line 397 | Line 425 | checkhead(                             /* deal with line from header */
425   static void
426   pixtoval(void)                          /* convert picture to values */
427   {
428 <        register COLOR  *scanln;
428 >        COLOR   *scanln;
429          int  dogamma;
430          COLOR  lastc;
431          RREAL  hv[2];
432          int  startprim, endprim;
433          long  startpos;
434 <        int  y;
407 <        register int  x;
434 >        int  x, y;
435  
436          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
437          if (scanln == NULL) {
# Line 412 | Line 439 | pixtoval(void)                         /* convert picture to values */
439                  quit(1);
440          }
441          dogamma = gamcor < .95 || gamcor > 1.05;
442 <        if (putprim == ALL && !interleave) {
442 >        if ((putprim == ALL) & !interleave) {
443                  startprim = RED; endprim = BLU;
444                  startpos = ftell(fin);
445          } else {
446                  startprim = putprim; endprim = putprim;
447          }
448          for (putprim = startprim; putprim <= endprim; putprim++) {
449 <                if (putprim != startprim && fseek(fin, startpos, 0)) {
449 >                if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) {
450                          fprintf(stderr, "%s: seek error on input file\n",
451                                          progname);
452                          quit(1);
# Line 471 | Line 498 | pixtoval(void)                         /* convert picture to values */
498   static void
499   valtopix(void)                  /* convert values to a pixel file */
500   {
501 <        int  dogamma;
502 <        register COLOR  *scanln;
503 <        int  y;
504 <        register int  x;
501 >        int     dogamma;
502 >        COLOR   *scanln;
503 >        COLR    rgbe;
504 >        int     x, y;
505  
506          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
507          if (scanln == NULL) {
# Line 503 | Line 530 | valtopix(void)                 /* convert values to a pixel file */
530                                          pow(colval(scanln[x],BLU), gamcor));
531                          if (doexposure)
532                                  multcolor(scanln[x], exposure);
533 +                        if (uniq) {             /* uncompressed? */
534 +                                setcolr(rgbe,   scanln[x][RED],
535 +                                                scanln[x][GRN],
536 +                                                scanln[x][BLU]);
537 +                                if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1)
538 +                                        goto writerr;
539 +                        }
540                  }
541 <                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
542 <                        fprintf(stderr, "%s: write error\n", progname);
543 <                        quit(1);
510 <                }
541 >                                                /* write scan if compressed */
542 >                if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0)
543 >                        goto writerr;
544          }
545          free((void *)scanln);
546 +        return;
547 + writerr:
548 +        fprintf(stderr, "%s: write error\n", progname);
549 +        quit(1);
550   }
551  
552  
# Line 550 | Line 587 | getcdouble(            /* get a double color value from stream(s
587          double  vd[3];
588  
589          if (fin2 == NULL) {
590 <                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
590 >                if (getbinary(vd, sizeof(double), 3, fin) != 3)
591                          return(-1);
592          } else {
593 <                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
594 <                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
595 <                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
593 >                if (getbinary(vd, sizeof(double), 1, fin) != 1 ||
594 >                        getbinary(vd+1, sizeof(double), 1, fin2) != 1 ||
595 >                        getbinary(vd+2, sizeof(double), 1, fin3) != 1)
596                          return(-1);
597          }
598          if (swapbytes)
# Line 573 | Line 610 | getcfloat(             /* get a float color value from stream(s)
610          float  vf[3];
611  
612          if (fin2 == NULL) {
613 <                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
613 >                if (getbinary(vf, sizeof(float), 3, fin) != 3)
614                          return(-1);
615          } else {
616 <                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
617 <                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
618 <                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
616 >                if (getbinary(vf, sizeof(float), 1, fin) != 1 ||
617 >                        getbinary(vf+1, sizeof(float), 1, fin2) != 1 ||
618 >                        getbinary(vf+2, sizeof(float), 1, fin3) != 1)
619                          return(-1);
620          }
621          if (swapbytes)
# Line 615 | Line 652 | getcbyte(              /* get a byte color value from stream(s) */
652          COLOR  col
653   )
654   {
655 <        BYTE  vb[3];
655 >        uby8  vb[3];
656  
657          if (fin2 == NULL) {
658 <                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
658 >                if (getbinary(vb, sizeof(uby8), 3, fin) != 3)
659                          return(-1);
660          } else {
661 <                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
662 <                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
663 <                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
661 >                if (getbinary(vb, sizeof(uby8), 1, fin) != 1 ||
662 >                        getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 ||
663 >                        getbinary(vb+2, sizeof(uby8), 1, fin3) != 1)
664                          return(-1);
665          }
666          setcolor(col, (vb[rord[RED]]+.5)/256.,
# Line 640 | Line 677 | getcword(              /* get a 16-bit color value from stream(s)
677          uint16  vw[3];
678  
679          if (fin2 == NULL) {
680 <                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
680 >                if (getbinary(vw, sizeof(uint16), 3, fin) != 3)
681                          return(-1);
682          } else {
683 <                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
684 <                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
685 <                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
683 >                if (getbinary(vw, sizeof(uint16), 1, fin) != 1 ||
684 >                        getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 ||
685 >                        getbinary(vw+2, sizeof(uint16), 1, fin3) != 1)
686                          return(-1);
687          }
688          if (swapbytes)
# Line 677 | Line 714 | getbdouble(            /* get a double brightness value from fin
714   {
715          double  vd;
716  
717 <        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
717 >        if (getbinary(&vd, sizeof(double), 1, fin) != 1)
718                  return(-1);
719          if (swapbytes)
720                  swap64((char *)&vd, 1);
# Line 693 | Line 730 | getbfloat(             /* get a float brightness value from fin *
730   {
731          float  vf;
732  
733 <        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
733 >        if (getbinary(&vf, sizeof(float), 1, fin) != 1)
734                  return(-1);
735          if (swapbytes)
736                  swap32((char *)&vf, 1);
# Line 723 | Line 760 | getbbyte(              /* get a byte brightness value from fin */
760          COLOR  col
761   )
762   {
763 <        BYTE  vb;
763 >        uby8  vb;
764          double  d;
765  
766 <        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
766 >        if (getbinary(&vb, sizeof(uby8), 1, fin) != 1)
767                  return(-1);
768          d = (vb+.5)/256.;
769          setcolor(col, d, d, d);
# Line 742 | Line 779 | getbword(              /* get a 16-bit brightness value from fin *
779          uint16  vw;
780          double  d;
781  
782 <        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
782 >        if (getbinary(&vw, sizeof(uint16), 1, fin) != 1)
783                  return(-1);
784          if (swapbytes)
785                  swap16((char *)&vw, 1);
# Line 778 | Line 815 | putcfloat(                     /* put a float color to stdout */
815          vf[2] = colval(col,ord[2]);
816          if (swapbytes)
817                  swap32((char *)vf, 3);
818 <        fwrite((char *)vf, sizeof(float), 3, stdout);
818 >        putbinary(vf, sizeof(float), 3, stdout);
819  
820          return(ferror(stdout) ? -1 : 0);
821   }
# Line 796 | Line 833 | putcdouble(                    /* put a double color to stdout */
833          vd[2] = colval(col,ord[2]);
834          if (swapbytes)
835                  swap64((char *)vd, 3);
836 <        fwrite((char *)vd, sizeof(double), 3, stdout);
836 >        putbinary(vd, sizeof(double), 3, stdout);
837  
838          return(ferror(stdout) ? -1 : 0);
839   }
# Line 822 | Line 859 | putcbyte(                      /* put a byte color to stdout */
859   )
860   {
861          long  i;
862 <        BYTE  vb[3];
862 >        uby8  vb[3];
863  
864          i = colval(col,ord[0])*256.;
865          vb[0] = min(i,255);
# Line 830 | Line 867 | putcbyte(                      /* put a byte color to stdout */
867          vb[1] = min(i,255);
868          i = colval(col,ord[2])*256.;
869          vb[2] = min(i,255);
870 <        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
870 >        putbinary(vb, sizeof(uby8), 3, stdout);
871  
872          return(ferror(stdout) ? -1 : 0);
873   }
# Line 852 | Line 889 | putcword(                      /* put a 16-bit color to stdout */
889          vw[2] = min(i,65535);
890          if (swapbytes)
891                  swap16((char *)vw, 3);
892 <        fwrite((char *)vw, sizeof(uint16), 3, stdout);
892 >        putbinary(vw, sizeof(uint16), 3, stdout);
893  
894          return(ferror(stdout) ? -1 : 0);
895   }
# Line 879 | Line 916 | putbfloat(                     /* put a float brightness to stdout */
916          vf = (*mybright)(col);
917          if (swapbytes)
918                  swap32((char *)&vf, 1);
919 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
919 >        putbinary(&vf, sizeof(float), 1, stdout);
920  
921          return(ferror(stdout) ? -1 : 0);
922   }
# Line 895 | Line 932 | putbdouble(                    /* put a double brightness to stdout */
932          vd = (*mybright)(col);
933          if (swapbytes)
934                  swap64((char *)&vd, 1);
935 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
935 >        putbinary(&vd, sizeof(double), 1, stdout);
936  
937          return(ferror(stdout) ? -1 : 0);
938   }
# Line 917 | Line 954 | putbbyte(                      /* put a byte brightness to stdout */
954          COLOR  col
955   )
956   {
957 <        register int  i;
958 <        BYTE  vb;
957 >        int  i;
958 >        uby8  vb;
959  
960          i = (*mybright)(col)*256.;
961          vb = min(i,255);
962 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
962 >        putbinary(&vb, sizeof(uby8), 1, stdout);
963  
964          return(ferror(stdout) ? -1 : 0);
965   }
# Line 940 | Line 977 | putbword(                      /* put a 16-bit brightness to stdout */
977          vw = min(i,65535);
978          if (swapbytes)
979                  swap16((char *)&vw, 1);
980 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
980 >        putbinary(&vw, sizeof(uint16), 1, stdout);
981  
982          return(ferror(stdout) ? -1 : 0);
983   }
# Line 967 | Line 1004 | putpfloat(                     /* put a float primary to stdout */
1004          vf = colval(col,putprim);
1005          if (swapbytes)
1006                  swap32((char *)&vf, 1);
1007 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
1007 >        putbinary(&vf, sizeof(float), 1, stdout);
1008  
1009          return(ferror(stdout) ? -1 : 0);
1010   }
# Line 983 | Line 1020 | putpdouble(                    /* put a double primary to stdout */
1020          vd = colval(col,putprim);
1021          if (swapbytes)
1022                  swap64((char *)&vd, 1);
1023 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
1023 >        putbinary(&vd, sizeof(double), 1, stdout);
1024  
1025          return(ferror(stdout) ? -1 : 0);
1026   }
# Line 1006 | Line 1043 | putpbyte(                      /* put a byte primary to stdout */
1043   )
1044   {
1045          long  i;
1046 <        BYTE  vb;
1046 >        uby8  vb;
1047  
1048          i = colval(col,putprim)*256.;
1049          vb = min(i,255);
1050 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
1050 >        putbinary(&vb, sizeof(uby8), 1, stdout);
1051  
1052          return(ferror(stdout) ? -1 : 0);
1053   }
# Line 1028 | Line 1065 | putpword(                      /* put a 16-bit primary to stdout */
1065          vw = min(i,65535);
1066          if (swapbytes)
1067                  swap16((char *)&vw, 1);
1068 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1068 >        putbinary(&vw, sizeof(uint16), 1, stdout);
1069  
1070          return(ferror(stdout) ? -1 : 0);
1071   }
# Line 1070 | Line 1107 | set_io(void)                   /* set put and get functions */
1107                                  if (fin2 == NULL)
1108                                          goto namerr;
1109                                  if (fseek(fin2,
1110 <                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1110 >                                (long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR))
1111                                          goto seekerr;
1112                                  if (fseek(fin3,
1113 <                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1113 >                                (long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR))
1114                                          goto seekerr;
1115                          }
1116                  }
# Line 1092 | Line 1129 | set_io(void)                   /* set put and get functions */
1129                                  if (fin2 == NULL)
1130                                          goto namerr;
1131                                  if (fseek(fin2,
1132 <                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1132 >                                (long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR))
1133                                          goto seekerr;
1134                                  if (fseek(fin3,
1135 <                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1135 >                                (long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR))
1136                                          goto seekerr;
1137                          }
1138                  }
# Line 1132 | Line 1169 | set_io(void)                   /* set put and get functions */
1169                                  if (fin2 == NULL)
1170                                          goto namerr;
1171                                  if (fseek(fin2,
1172 <                                (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
1172 >                                (long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR))
1173                                          goto seekerr;
1174                                  if (fseek(fin3,
1175 <                                (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1175 >                                (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR))
1176                                          goto seekerr;
1177                          }
1178                  }
# Line 1154 | Line 1191 | set_io(void)                   /* set put and get functions */
1191                                  if (fin2 == NULL)
1192                                          goto namerr;
1193                                  if (fseek(fin2,
1194 <                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1194 >                                (long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR))
1195                                          goto seekerr;
1196                                  if (fseek(fin3,
1197 <                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1197 >                                (long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR))
1198                                          goto seekerr;
1199                          }
1200                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines