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.33 by greg, Thu Aug 18 00:52:48 2016 UTC vs.
Revision 2.41 by greg, Fri Feb 4 20:11:49 2022 UTC

# Line 93 | 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 117 | Line 118 | main(
118                          case 'o':               /* original values */
119                                  original = argv[i][0] == '-';
120                                  break;
121 +                        case 'O':               /* original watts/sr/m^2 */
122 +                                original = -(argv[i][0] == '-');
123 +                                break;
124                          case 'g':               /* gamma correction */
125                                  gamcor = atof(argv[i+1]);
126                                  if (argv[i][0] == '+')
# Line 172 | Line 176 | main(
176                                  }
177                                  break;
178                          case 'd':               /* data only (no indices) */
179 <                                dataonly = argv[i][0] == '-';
179 >                                dataonly = (argv[i][0] == '-');
180                                  switch (argv[i][2]) {
181                                  case '\0':
182                                  case 'a':               /* ascii */
# Line 251 | Line 255 | unkopt:
255                  else
256                          fmtid = "16-bit_grey";
257          }
258 +        if (!reverse || (format != 'a') & (format != 'i'))
259 +                inpmode = "rb";
260                                          /* assign reverse ordering */
261          rord[ord[0]] = 0;
262          rord[ord[1]] = 1;
263          rord[ord[2]] = 2;
264                                          /* get input */
265          if (i == argc) {
266 +                long    n = skipbytes;
267 +                if (inpmode[1] == 'b')
268 +                        SET_FILE_BINARY(stdin);
269 +                while (n-- > 0)
270 +                        if (getchar() == EOF) {
271 +                                fprintf(stderr,
272 +                                "%s: cannot skip %ld bytes on standard input\n",
273 +                                                progname, skipbytes);
274 +                                quit(1);
275 +                        }
276                  fin = stdin;
277          } else if (i < argc) {
278 <                if ((fin = fopen(argv[i], "r")) == NULL) {
278 >                if ((fin = fopen(argv[i], inpmode)) == NULL) {
279                          fprintf(stderr, "%s: can't open file \"%s\"\n",
280                                                  progname, argv[i]);
281                          quit(1);
282                  }
283                  if (reverse && putprim != BRIGHT && i == argc-3) {
284 <                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
284 >                        if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) {
285                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
286                                                  progname, argv[i+1]);
287                                  quit(1);
288                          }
289 <                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
289 >                        if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) {
290                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
291                                                  progname, argv[i+2]);
292                                  quit(1);
# Line 279 | Line 295 | unkopt:
295                  } else if (i != argc-1)
296                          fin = NULL;
297                  if (reverse && putprim != BRIGHT && !interleave) {
298 <                        fin2 = fopen(argv[i], "r");
299 <                        fin3 = fopen(argv[i], "r");
298 >                        fin2 = fopen(argv[i], inpmode);
299 >                        fin3 = fopen(argv[i], inpmode);
300                  }
301 <                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
302 <                                (fseek(fin2, skipbytes, 0) ||
303 <                                fseek(fin3, skipbytes, 0))))) {
301 >                if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL &&
302 >                                fseek(fin2, skipbytes, SEEK_SET) |
303 >                                fseek(fin3, skipbytes, SEEK_SET)))) {
304                          fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
305                                          progname, skipbytes);
306                          quit(1);
# Line 296 | Line 312 | unkopt:
312          }
313  
314          if (reverse) {
299 #if defined(_WIN32) || defined(_WIN64)
315                  SET_FILE_BINARY(stdout);
301                if (format != 'a' && format != 'i')
302                        SET_FILE_BINARY(fin);
303 #endif
316                                          /* get header */
317                  if (header) {
318 <                        if (checkheader(fin, fmtid, stdout) < 0) {
319 <                                fprintf(stderr, "%s: wrong input format\n",
320 <                                                progname);
318 >                        getheader(fin, checkhead, stdout);
319 >                        if (wrongformat) {
320 >                                fprintf(stderr, "%s: wrong input format (expected %s)\n",
321 >                                                progname, fmtid);
322                                  quit(1);
323                          }
324                          if (fin2 != NULL) {
# Line 343 | Line 356 | unkopt:
356                          if (outprims != stdprims)
357                                  fputprims(outprims, stdout);
358                          fputformat(COLRFMT, stdout);
359 <                } else                          /* XYZ data */
359 >                } else {                        /* XYZ data */
360 >                        if (original < 0) {
361 >                                scalecolor(exposure, WHTEFFICACY);
362 >                                doexposure++;
363 >                        }
364                          fputformat(CIEFMT, stdout);
365 +                }
366                  putchar('\n');
367                  fputsresolu(&picres, stdout);   /* always put resolution */
368                  valtopix();
369          } else {
370 < #if defined(_WIN32) || defined(_WIN64)
353 <                SET_FILE_BINARY(fin);
354 <                if (format != 'a' && format != 'i')
370 >                if ((format != 'a') & (format != 'i'))
371                          SET_FILE_BINARY(stdout);
356 #endif
372                                                  /* get header */
373 <                getheader(fin, checkhead, NULL);
373 >                getheader(fin, checkhead, header ? stdout : (FILE *)NULL);
374                  if (wrongformat) {
375                          fprintf(stderr,
376                                  "%s: input not a Radiance RGBE picture\n",
# Line 366 | Line 381 | unkopt:
381                          fprintf(stderr, "%s: missing resolution\n", progname);
382                          quit(1);
383                  }
384 +                if (original < 0 && mybright == &xyz_bright) {
385 +                        scalecolor(exposure, 1./WHTEFFICACY);
386 +                        doexposure++;
387 +                }
388                  if (header) {
389                          printargs(i, argv, stdout);
390 +                        printf("NCOMP=%d\n", putprim==ALL ? 3 : 1);
391 +                        if (!resolution && dataonly && !uniq)
392 +                                printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres),
393 +                                                numscans(&picres));
394                          if (expval < .99 || expval > 1.01)
395                                  fputexpos(expval, stdout);
396 +                        if (swapbytes) {
397 +                                if (nativebigendian())
398 +                                        puts("BigEndian=0");
399 +                                else
400 +                                        puts("BigEndian=1");
401 +                        } else if ((format != 'a') & (format != 'i') &
402 +                                                (format != 'b'))
403 +                                fputendian(stdout);
404                          fputformat(fmtid, stdout);
405                          putchar('\n');
406                  }
# Line 389 | Line 420 | checkhead(                             /* deal with line from header */
420          void    *p
421   )
422   {
423 <        char    fmt[32];
423 >        FILE    *fout = (FILE *)p;
424 >        char    fmt[MAXFMTLEN];
425          double  d;
426          COLOR   ctmp;
427 +        int     rv;
428  
429          if (formatval(fmt, line)) {
430 <                if (!strcmp(fmt, CIEFMT))
430 >                if (reverse)
431 >                        wrongformat = strcmp(fmt, fmtid);
432 >                else if (!strcmp(fmt, CIEFMT))
433                          mybright = &xyz_bright;
434                  else if (!strcmp(fmt, COLRFMT))
435                          mybright = &rgb_bright;
436                  else
437                          wrongformat++;
438 <        } else if (original && isexpos(line)) {
438 >                return(1);
439 >        }
440 >        if (original && isexpos(line)) {
441                  d = 1.0/exposval(line);
442                  scalecolor(exposure, d);
443                  doexposure++;
444 <        } else if (original && iscolcor(line)) {
444 >                return(1);
445 >        }
446 >        if (original && iscolcor(line)) {
447                  colcorval(ctmp, line);
448                  setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
449                                  colval(exposure,GRN)/colval(ctmp,GRN),
450                                  colval(exposure,BLU)/colval(ctmp,BLU));
451                  doexposure++;
452 <        } else if (header)
453 <                fputs(line, stdout);
452 >                return(1);
453 >        }
454 >        if ((rv = isbigendian(line)) >= 0) {
455 >                if (reverse)
456 >                        swapbytes = (nativebigendian() != rv);
457 >                return(1);
458 >        }
459 >        if (fout != NULL)
460 >                fputs(line, fout);
461          return(0);
462   }
463  
# Line 419 | Line 465 | checkhead(                             /* deal with line from header */
465   static void
466   pixtoval(void)                          /* convert picture to values */
467   {
468 <        register COLOR  *scanln;
468 >        COLOR   *scanln;
469          int  dogamma;
470          COLOR  lastc;
471          RREAL  hv[2];
472          int  startprim, endprim;
473          long  startpos;
474 <        int  y;
429 <        register int  x;
474 >        int  x, y;
475  
476          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
477          if (scanln == NULL) {
# Line 434 | Line 479 | pixtoval(void)                         /* convert picture to values */
479                  quit(1);
480          }
481          dogamma = gamcor < .95 || gamcor > 1.05;
482 <        if (putprim == ALL && !interleave) {
482 >        if ((putprim == ALL) & !interleave) {
483                  startprim = RED; endprim = BLU;
484                  startpos = ftell(fin);
485          } else {
486                  startprim = putprim; endprim = putprim;
487          }
488          for (putprim = startprim; putprim <= endprim; putprim++) {
489 <                if (putprim != startprim && fseek(fin, startpos, 0)) {
489 >                if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) {
490                          fprintf(stderr, "%s: seek error on input file\n",
491                                          progname);
492                          quit(1);
# Line 493 | Line 538 | pixtoval(void)                         /* convert picture to values */
538   static void
539   valtopix(void)                  /* convert values to a pixel file */
540   {
541 <        int  dogamma;
542 <        register COLOR  *scanln;
543 <        int  y;
544 <        register int  x;
541 >        int     dogamma;
542 >        COLOR   *scanln;
543 >        COLR    rgbe;
544 >        int     x, y;
545  
546          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
547          if (scanln == NULL) {
# Line 525 | Line 570 | valtopix(void)                 /* convert values to a pixel file */
570                                          pow(colval(scanln[x],BLU), gamcor));
571                          if (doexposure)
572                                  multcolor(scanln[x], exposure);
573 +                        if (uniq) {             /* uncompressed? */
574 +                                setcolr(rgbe,   scanln[x][RED],
575 +                                                scanln[x][GRN],
576 +                                                scanln[x][BLU]);
577 +                                if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1)
578 +                                        goto writerr;
579 +                        }
580                  }
581 <                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
582 <                        fprintf(stderr, "%s: write error\n", progname);
583 <                        quit(1);
532 <                }
581 >                                                /* write scan if compressed */
582 >                if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0)
583 >                        goto writerr;
584          }
585          free((void *)scanln);
586 +        return;
587 + writerr:
588 +        fprintf(stderr, "%s: write error\n", progname);
589 +        quit(1);
590   }
591  
592  
# Line 939 | Line 994 | putbbyte(                      /* put a byte brightness to stdout */
994          COLOR  col
995   )
996   {
997 <        register int  i;
997 >        int  i;
998          uby8  vb;
999  
1000          i = (*mybright)(col)*256.;
# Line 1092 | Line 1147 | set_io(void)                   /* set put and get functions */
1147                                  if (fin2 == NULL)
1148                                          goto namerr;
1149                                  if (fseek(fin2,
1150 <                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1150 >                                (long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR))
1151                                          goto seekerr;
1152                                  if (fseek(fin3,
1153 <                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1153 >                                (long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR))
1154                                          goto seekerr;
1155                          }
1156                  }
# Line 1114 | Line 1169 | set_io(void)                   /* set put and get functions */
1169                                  if (fin2 == NULL)
1170                                          goto namerr;
1171                                  if (fseek(fin2,
1172 <                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1172 >                                (long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR))
1173                                          goto seekerr;
1174                                  if (fseek(fin3,
1175 <                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1175 >                                (long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR))
1176                                          goto seekerr;
1177                          }
1178                  }
# Line 1154 | Line 1209 | set_io(void)                   /* set put and get functions */
1209                                  if (fin2 == NULL)
1210                                          goto namerr;
1211                                  if (fseek(fin2,
1212 <                                (long)sizeof(uby8)*picres.xr*picres.yr, 1))
1212 >                                (long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR))
1213                                          goto seekerr;
1214                                  if (fseek(fin3,
1215 <                                (long)sizeof(uby8)*2*picres.xr*picres.yr, 1))
1215 >                                (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR))
1216                                          goto seekerr;
1217                          }
1218                  }
# Line 1176 | Line 1231 | set_io(void)                   /* set put and get functions */
1231                                  if (fin2 == NULL)
1232                                          goto namerr;
1233                                  if (fseek(fin2,
1234 <                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1234 >                                (long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR))
1235                                          goto seekerr;
1236                                  if (fseek(fin3,
1237 <                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1237 >                                (long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR))
1238                                          goto seekerr;
1239                          }
1240                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines