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.45 by greg, Mon Sep 16 23:02:53 2024 UTC

# Line 26 | Line 26 | int  dataonly = 0;             /* data only format? */
26   int  putprim = ALL;             /* what to put out */
27   int  reverse = 0;               /* reverse conversion? */
28   int  format = 'a';              /* input/output format */
29 < char  *fmtid = "ascii";         /* format identifier for header */
29 > const char  *fmtid = "ascii";   /* format identifier for header */
30   int  header = 1;                /* do header? */
31   long  skipbytes = 0;            /* skip bytes in input? */
32   int  swapbytes = 0;             /* swap bytes? */
# Line 66 | Line 66 | static putfunc_t putbascii, putbint, putbdouble, putbf
66   static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword;
67   static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword;
68  
69 + static int IDformat(const char *s);
70   static void set_io(void);
71   static void pixtoval(void);
72   static void valtopix(void);
# Line 93 | Line 94 | main(
94          char  **argv
95   )
96   {
97 <        double  d, expval = 1.0;
98 <        int  i;
97 >        const char      *inpmode = "r";
98 >        double          d, expval = 1.0;
99 >        int             i;
100  
101          progname = argv[0];
102          mybright = &rgb_bright; /* default */
103 +        picres.rt = PIXSTANDARD;
104  
105          for (i = 1; i < argc; i++)
106                  if (argv[i][0] == '-' || argv[i][0] == '+')
# Line 117 | Line 120 | main(
120                          case 'o':               /* original values */
121                                  original = argv[i][0] == '-';
122                                  break;
123 +                        case 'O':               /* original watts/sr/m^2 */
124 +                                original = -(argv[i][0] == '-');
125 +                                break;
126                          case 'g':               /* gamma correction */
127                                  gamcor = atof(argv[i+1]);
128                                  if (argv[i][0] == '+')
# Line 172 | Line 178 | main(
178                                  }
179                                  break;
180                          case 'd':               /* data only (no indices) */
181 <                                dataonly = argv[i][0] == '-';
181 >                                dataonly = (argv[i][0] == '-');
182                                  switch (argv[i][2]) {
183                                  case '\0':
184                                  case 'a':               /* ascii */
# Line 214 | Line 220 | main(
220                                  }
221                                  break;
222                          case 'x':               /* x resolution */
223 <                        case 'X':               /* x resolution */
223 >                        case 'X':
224                                  resolution = 0;
225                                  if (argv[i][0] == '-')
226                                          picres.rt |= XDECR;
227 +                                else
228 +                                        picres.rt &= ~XDECR;
229 +                                if (picres.yr == 0)
230 +                                        picres.rt &= ~YMAJOR;
231                                  picres.xr = atoi(argv[++i]);
232                                  break;
233                          case 'y':               /* y resolution */
234 <                        case 'Y':               /* y resolution */
234 >                        case 'Y':
235                                  resolution = 0;
236                                  if (argv[i][0] == '-')
237                                          picres.rt |= YDECR;
238 +                                else
239 +                                        picres.rt &= ~YDECR;
240                                  if (picres.xr == 0)
241                                          picres.rt |= YMAJOR;
242                                  picres.yr = atoi(argv[++i]);
# Line 239 | Line 251 | unkopt:
251                  else
252                          break;
253                                          /* recognize special formats */
254 <        if (dataonly && format == 'b') {
254 >        if (dataonly & (format == 'b')) {
255                  if (putprim == ALL)
256                          fmtid = "24-bit_rgb";
257                  else
258                          fmtid = "8-bit_grey";
259          }
260 <        if (dataonly && format == 'w') {
260 >        if (dataonly & (format == 'w')) {
261                  if (putprim == ALL)
262                          fmtid = "48-bit_rgb";
263                  else
264                          fmtid = "16-bit_grey";
265          }
266 +        if (!reverse | ((format != 'a') & (format != 'i')))
267 +                inpmode = "rb";
268                                          /* assign reverse ordering */
269          rord[ord[0]] = 0;
270          rord[ord[1]] = 1;
271          rord[ord[2]] = 2;
272                                          /* get input */
273          if (i == argc) {
274 +                long    n = skipbytes;
275 +                if (inpmode[1] == 'b')
276 +                        SET_FILE_BINARY(stdin);
277 +                while (n-- > 0)
278 +                        if (getchar() == EOF) {
279 +                                fprintf(stderr,
280 +                                "%s: cannot skip %ld bytes on standard input\n",
281 +                                                progname, skipbytes);
282 +                                quit(1);
283 +                        }
284                  fin = stdin;
285          } else if (i < argc) {
286 <                if ((fin = fopen(argv[i], "r")) == NULL) {
286 >                if ((fin = fopen(argv[i], inpmode)) == NULL) {
287                          fprintf(stderr, "%s: can't open file \"%s\"\n",
288                                                  progname, argv[i]);
289                          quit(1);
290                  }
291 <                if (reverse && putprim != BRIGHT && i == argc-3) {
292 <                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
291 >                if (reverse & (putprim != BRIGHT ) & (i == argc-3)) {
292 >                        if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) {
293                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
294                                                  progname, argv[i+1]);
295                                  quit(1);
296                          }
297 <                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
297 >                        if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) {
298                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
299                                                  progname, argv[i+2]);
300                                  quit(1);
# Line 278 | Line 302 | unkopt:
302                          interleave = -1;
303                  } else if (i != argc-1)
304                          fin = NULL;
305 <                if (reverse && putprim != BRIGHT && !interleave) {
306 <                        fin2 = fopen(argv[i], "r");
307 <                        fin3 = fopen(argv[i], "r");
305 >                if (reverse & (putprim != BRIGHT) & !interleave) {
306 >                        fin2 = fopen(argv[i], inpmode);
307 >                        fin3 = fopen(argv[i], inpmode);
308                  }
309 <                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
310 <                                (fseek(fin2, skipbytes, 0) ||
311 <                                fseek(fin3, skipbytes, 0))))) {
309 >                if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL &&
310 >                                fseek(fin2, skipbytes, SEEK_SET) |
311 >                                fseek(fin3, skipbytes, SEEK_SET)))) {
312                          fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
313                                          progname, skipbytes);
314                          quit(1);
# Line 294 | Line 318 | unkopt:
318                  fprintf(stderr, "%s: bad # file arguments\n", progname);
319                  quit(1);
320          }
297
321          if (reverse) {
299 #if defined(_WIN32) || defined(_WIN64)
322                  SET_FILE_BINARY(stdout);
301                if (format != 'a' && format != 'i')
302                        SET_FILE_BINARY(fin);
303 #endif
323                                          /* get header */
324                  if (header) {
325 <                        if (checkheader(fin, fmtid, stdout) < 0) {
326 <                                fprintf(stderr, "%s: wrong input format\n",
327 <                                                progname);
325 >                        getheader(fin, checkhead, stdout);
326 >                        if (wrongformat) {
327 >                                fprintf(stderr, "%s: wrong input format (expected %s)\n",
328 >                                                progname, fmtid);
329                                  quit(1);
330                          }
331                          if (fin2 != NULL) {
332                                  getheader(fin2, NULL, NULL);
333                                  getheader(fin3, NULL, NULL);
334                          }
335 <                } else
335 >                        resolution &= (picres.xr <= 0) | (picres.yr <= 0);
336 >                } else {
337                          newheader("RADIANCE", stdout);
338 +                }
339                                          /* get resolution */
340                  if ((resolution && !fgetsresolu(&picres, fin)) ||
341 <                                picres.xr <= 0 || picres.yr <= 0) {
341 >                                (picres.xr <= 0) | (picres.yr <= 0)) {
342                          fprintf(stderr, "%s: missing resolution\n", progname);
343                          quit(1);
344                  }
345                  if (resolution && fin2 != NULL) {
346                          RESOLU  pres2;
347                          if (!fgetsresolu(&pres2, fin2) ||
348 <                                        pres2.rt != picres.rt ||
349 <                                        pres2.xr != picres.xr ||
350 <                                        pres2.yr != picres.yr ||
348 >                                        (pres2.rt != picres.rt) |
349 >                                        (pres2.xr != picres.xr) |
350 >                                        (pres2.yr != picres.yr) ||
351                                          !fgetsresolu(&pres2, fin3) ||
352 <                                        pres2.rt != picres.rt ||
353 <                                        pres2.xr != picres.xr ||
354 <                                        pres2.yr != picres.yr) {
352 >                                        (pres2.rt != picres.rt) |
353 >                                        (pres2.xr != picres.xr) |
354 >                                        (pres2.yr != picres.yr)) {
355                                  fprintf(stderr, "%s: resolution mismatch\n",
356                                                  progname);
357                                  quit(1);
# Line 337 | Line 359 | unkopt:
359                  }
360                                                  /* add to header */
361                  printargs(i, argv, stdout);
362 <                if (expval < .99 || expval > 1.01)
362 >                if ((expval < .99) | (expval > 1.01))
363                          fputexpos(expval, stdout);
364 +                if (!header & (putprim != ALL)) {
365 +                        NCSAMP = 1;             /* no header, so trust user */
366 +                } else if (NCSAMP == 1) {
367 +                        if (putprim == ALL)
368 +                                putprim = BRIGHT;
369 +                } else {
370 +                        if (NCSAMP != 3) {
371 +                                fprintf(stderr, "%s: input NCOMP must be 1 or 3\n",
372 +                                                progname);
373 +                                quit(1);
374 +                        }
375 +                        if (putprim != ALL) {
376 +                                fprintf(stderr, "%s: NCOMP mismatch\n", progname);
377 +                                quit(1);
378 +                        }
379 +                }
380                  if (outprims != NULL) {
381                          if (outprims != stdprims)
382                                  fputprims(outprims, stdout);
383                          fputformat(COLRFMT, stdout);
384 <                } else                          /* XYZ data */
384 >                } else {                        /* XYZ data */
385 >                        if (original < 0) {
386 >                                scalecolor(exposure, WHTEFFICACY);
387 >                                doexposure++;
388 >                        }
389                          fputformat(CIEFMT, stdout);
390 +                }
391                  putchar('\n');
392                  fputsresolu(&picres, stdout);   /* always put resolution */
393                  valtopix();
394          } else {
395 < #if defined(_WIN32) || defined(_WIN64)
353 <                SET_FILE_BINARY(fin);
354 <                if (format != 'a' && format != 'i')
395 >                if ((format != 'a') & (format != 'i'))
396                          SET_FILE_BINARY(stdout);
356 #endif
397                                                  /* get header */
398 <                getheader(fin, checkhead, NULL);
398 >                picres.rt = PIXSTANDARD;
399 >                picres.xr = picres.yr = 0;
400 >                getheader(fin, checkhead, header ? stdout : (FILE *)NULL);
401                  if (wrongformat) {
402                          fprintf(stderr,
403                                  "%s: input not a Radiance RGBE picture\n",
404                                          progname);
405                          quit(1);
406                  }
407 <                if (!fgetsresolu(&picres, fin)) {
407 >                if ((picres.xr <= 0) | (picres.yr <= 0) &&
408 >                                !fgetsresolu(&picres, fin)) {
409                          fprintf(stderr, "%s: missing resolution\n", progname);
410                          quit(1);
411                  }
412 +                if ((original < 0) & (mybright == &xyz_bright)) {
413 +                        scalecolor(exposure, 1./WHTEFFICACY);
414 +                        doexposure++;
415 +                }
416                  if (header) {
417                          printargs(i, argv, stdout);
418 <                        if (expval < .99 || expval > 1.01)
418 >                        if (dataonly)
419 >                                printf("%s%c\n", NCOMPSTR, "13"[putprim==ALL]);
420 >                        if (dataonly && !resolution & !uniq)
421 >                                printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres),
422 >                                                numscans(&picres));
423 >                        if ((expval < .99) | (expval > 1.01))
424                                  fputexpos(expval, stdout);
425 +                        if (swapbytes) {
426 +                                if (nativebigendian())
427 +                                        puts("BigEndian=0");
428 +                                else
429 +                                        puts("BigEndian=1");
430 +                        } else if ((format != 'a') & (format != 'i') &
431 +                                                (format != 'b'))
432 +                                fputendian(stdout);
433                          fputformat(fmtid, stdout);
434                          putchar('\n');
435                  }
# Line 384 | Line 444 | unkopt:
444  
445  
446   static int
447 + IDformat(const char *s)
448 + {
449 +        if (!s || !*s) return(0);
450 +        if (!strcmp(s, "ascii")) return('a');
451 +        if (!strcmp(s, "float")) return('f');
452 +        if (!strcmp(s, "double")) return('d');
453 +        if (!strcmp(s, "byte")) return('b');
454 +        if (!strcmp(s, "16-bit")) return('w');
455 +        return(0);
456 + }
457 +
458 +
459 + static int
460   checkhead(                              /* deal with line from header */
461          char    *line,
462          void    *p
463   )
464   {
465 <        char    fmt[32];
465 >        static char     fmt[MAXFMTLEN];
466 >        FILE    *fout = (FILE *)p;
467          double  d;
468          COLOR   ctmp;
469 +        int     rv;
470  
471          if (formatval(fmt, line)) {
472 <                if (!strcmp(fmt, CIEFMT))
472 >                if (reverse) {
473 >                        if (format == 'a') {
474 >                                format = IDformat(fmt);
475 >                                if (format)
476 >                                        fmtid = fmt;
477 >                                else
478 >                                        wrongformat = 1;
479 >                        } else
480 >                                wrongformat = strcmp(fmt, fmtid);
481 >                } else if (!strcmp(fmt, CIEFMT))
482                          mybright = &xyz_bright;
483 <                else if (!strcmp(fmt, COLRFMT))
483 >                else if (!strcmp(fmt, COLRFMT) || !strcmp(fmt, SPECFMT))
484                          mybright = &rgb_bright;
485                  else
486 <                        wrongformat++;
487 <        } else if (original && isexpos(line)) {
486 >                        wrongformat = 1;
487 >                return(1);
488 >        }
489 >        if (!strncmp(line,"NROWS=",6)) {
490 >                picres.yr = atoi(line+6);
491 >                return(1);
492 >        }
493 >        if (!strncmp(line,"NCOLS=",6)) {
494 >                picres.xr = atoi(line+6);
495 >                return(1);
496 >        }
497 >        if (isncomp(line)) {
498 >                NCSAMP = ncompval(line);
499 >                dataonly |= reverse;    /* pretty safe assumption */
500 >                return(1);
501 >        }
502 >        if (iswlsplit(line)) {
503 >                wlsplitval(WLPART, line);
504 >                return(1);
505 >        }
506 >        if (original && isexpos(line)) {
507                  d = 1.0/exposval(line);
508                  scalecolor(exposure, d);
509                  doexposure++;
510 <        } else if (original && iscolcor(line)) {
510 >                return(1);
511 >        }
512 >        if (original && iscolcor(line)) {
513                  colcorval(ctmp, line);
514                  setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
515                                  colval(exposure,GRN)/colval(ctmp,GRN),
516                                  colval(exposure,BLU)/colval(ctmp,BLU));
517                  doexposure++;
518 <        } else if (header)
519 <                fputs(line, stdout);
518 >                return(1);
519 >        }
520 >        if ((rv = isbigendian(line)) >= 0) {
521 >                if (reverse)
522 >                        swapbytes = (nativebigendian() != rv);
523 >                return(1);
524 >        }
525 >        if (fout != NULL)
526 >                fputs(line, fout);
527          return(0);
528   }
529  
# Line 419 | Line 531 | checkhead(                             /* deal with line from header */
531   static void
532   pixtoval(void)                          /* convert picture to values */
533   {
534 <        register COLOR  *scanln;
534 >        COLOR   *scanln;
535          int  dogamma;
536          COLOR  lastc;
537          RREAL  hv[2];
538          int  startprim, endprim;
539          long  startpos;
540 <        int  y;
429 <        register int  x;
540 >        int  x, y;
541  
542          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
543          if (scanln == NULL) {
# Line 434 | Line 545 | pixtoval(void)                         /* convert picture to values */
545                  quit(1);
546          }
547          dogamma = gamcor < .95 || gamcor > 1.05;
548 <        if (putprim == ALL && !interleave) {
548 >        if ((putprim == ALL) & !interleave) {
549                  startprim = RED; endprim = BLU;
550                  startpos = ftell(fin);
551          } else {
552                  startprim = putprim; endprim = putprim;
553          }
554          for (putprim = startprim; putprim <= endprim; putprim++) {
555 <                if (putprim != startprim && fseek(fin, startpos, 0)) {
555 >                if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) {
556                          fprintf(stderr, "%s: seek error on input file\n",
557                                          progname);
558                          quit(1);
# Line 449 | Line 560 | pixtoval(void)                         /* convert picture to values */
560                  set_io();
561                  setcolor(lastc, 0.0, 0.0, 0.0);
562                  for (y = 0; y < numscans(&picres); y++) {
563 <                        if (freadscan(scanln, scanlen(&picres), fin) < 0) {
563 >                        if (fread2scan(scanln, scanlen(&picres), fin,
564 >                                                NCSAMP, WLPART) < 0) {
565                                  fprintf(stderr, "%s: read error\n", progname);
566                                  quit(1);
567                          }
# Line 493 | Line 605 | pixtoval(void)                         /* convert picture to values */
605   static void
606   valtopix(void)                  /* convert values to a pixel file */
607   {
608 <        int  dogamma;
609 <        register COLOR  *scanln;
610 <        int  y;
611 <        register int  x;
608 >        int     dogamma;
609 >        COLOR   *scanln;
610 >        COLR    rgbe;
611 >        int     x, y;
612  
613          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
614          if (scanln == NULL) {
# Line 525 | Line 637 | valtopix(void)                 /* convert values to a pixel file */
637                                          pow(colval(scanln[x],BLU), gamcor));
638                          if (doexposure)
639                                  multcolor(scanln[x], exposure);
640 +                        if (uniq) {             /* uncompressed? */
641 +                                setcolr(rgbe,   scanln[x][RED],
642 +                                                scanln[x][GRN],
643 +                                                scanln[x][BLU]);
644 +                                if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1)
645 +                                        goto writerr;
646 +                        }
647                  }
648 <                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
649 <                        fprintf(stderr, "%s: write error\n", progname);
650 <                        quit(1);
532 <                }
648 >                                                /* write scan if compressed */
649 >                if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0)
650 >                        goto writerr;
651          }
652          free((void *)scanln);
653 +        return;
654 + writerr:
655 +        fprintf(stderr, "%s: write error\n", progname);
656 +        quit(1);
657   }
658  
659  
# Line 939 | Line 1061 | putbbyte(                      /* put a byte brightness to stdout */
1061          COLOR  col
1062   )
1063   {
1064 <        register int  i;
1064 >        int  i;
1065          uby8  vb;
1066  
1067          i = (*mybright)(col)*256.;
# Line 1092 | Line 1214 | set_io(void)                   /* set put and get functions */
1214                                  if (fin2 == NULL)
1215                                          goto namerr;
1216                                  if (fseek(fin2,
1217 <                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1217 >                                (long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR))
1218                                          goto seekerr;
1219                                  if (fseek(fin3,
1220 <                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1220 >                                (long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR))
1221                                          goto seekerr;
1222                          }
1223                  }
# Line 1114 | Line 1236 | set_io(void)                   /* set put and get functions */
1236                                  if (fin2 == NULL)
1237                                          goto namerr;
1238                                  if (fseek(fin2,
1239 <                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1239 >                                (long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR))
1240                                          goto seekerr;
1241                                  if (fseek(fin3,
1242 <                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1242 >                                (long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR))
1243                                          goto seekerr;
1244                          }
1245                  }
# Line 1154 | Line 1276 | set_io(void)                   /* set put and get functions */
1276                                  if (fin2 == NULL)
1277                                          goto namerr;
1278                                  if (fseek(fin2,
1279 <                                (long)sizeof(uby8)*picres.xr*picres.yr, 1))
1279 >                                (long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR))
1280                                          goto seekerr;
1281                                  if (fseek(fin3,
1282 <                                (long)sizeof(uby8)*2*picres.xr*picres.yr, 1))
1282 >                                (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR))
1283                                          goto seekerr;
1284                          }
1285                  }
# Line 1176 | Line 1298 | set_io(void)                   /* set put and get functions */
1298                                  if (fin2 == NULL)
1299                                          goto namerr;
1300                                  if (fseek(fin2,
1301 <                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1301 >                                (long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR))
1302                                          goto seekerr;
1303                                  if (fseek(fin3,
1304 <                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1304 >                                (long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR))
1305                                          goto seekerr;
1306                          }
1307                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines