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.25 by schorsch, Fri Jan 2 12:47:01 2004 UTC vs.
Revision 2.45 by greg, Mon Sep 16 23:02:53 2024 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include  "standard.h"
12   #include  "color.h"
13   #include  "resolu.h"
14 + #include  "view.h"
15  
16   #define  min(a,b)               ((a)<(b)?(a):(b))
17  
# Line 19 | Line 20 | static const char RCSid[] = "$Id$";
20   #define  BRIGHT         4
21  
22   RESOLU  picres;                 /* resolution of picture */
22
23   int  uniq = 0;                  /* print only unique values? */
24
24   int  doexposure = 0;            /* exposure change? (>100 to print) */
26
25   int  dataonly = 0;              /* data only format? */
28
26   int  putprim = ALL;             /* what to put out */
30
27   int  reverse = 0;               /* reverse conversion? */
32
28   int  format = 'a';              /* input/output format */
29 < char  *fmtid = "ascii";         /* format identifier for header */
35 <
29 > const char  *fmtid = "ascii";   /* format identifier for header */
30   int  header = 1;                /* do header? */
37
31   long  skipbytes = 0;            /* skip bytes in input? */
39
32   int  swapbytes = 0;             /* swap bytes? */
41
33   int  interleave = 1;            /* file is interleaved? */
43
34   int  resolution = 1;            /* put/get resolution string? */
45
35   int  original = 0;              /* convert to original values? */
47
36   int  wrongformat = 0;           /* wrong input format? */
49
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 59 | Line 49 | char  *progname;
49   FILE  *fin;
50   FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
51  
62 int  (*getval)(), (*putval)();
52  
53 + typedef int getfunc_t(COLOR  col);
54 + typedef int putfunc_t(COLOR  col);
55 + typedef double brightfunc_t(COLOR  col);
56 +
57 + getfunc_t *getval;
58 + putfunc_t *putval;
59 + brightfunc_t *mybright;
60 +
61   static gethfunc checkhead;
62 + static brightfunc_t rgb_bright, xyz_bright;
63 + static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword;
64 + static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword;
65 + static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword;
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);
73  
74 < double
75 < rgb_bright(clr)
76 < COLOR  clr;
74 >
75 > static double
76 > rgb_bright(
77 >        COLOR  clr
78 > )
79   {
80          return(bright(clr));
81   }
82  
83 < double
84 < xyz_bright(clr)
85 < COLOR  clr;
83 > static double
84 > xyz_bright(
85 >        COLOR  clr
86 > )
87   {
88          return(clr[CIEY]);
89   }
90  
91 < double  (*mybright)() = &rgb_bright;
92 <
93 <
94 < main(argc, argv)
95 < int  argc;
86 < char  **argv;
91 > int
92 > main(
93 >        int  argc,
94 >        char  **argv
95 > )
96   {
97 <        extern long  atol();
98 <        double  d, expval = 1.0;
99 <        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 109 | Line 120 | char  **argv;
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 141 | Line 155 | char  **argv;
155                          case 'b':               /* brightness values */
156                                  putprim = argv[i][0] == '-' ? BRIGHT : ALL;
157                                  break;
158 <                        case 'p':               /* put primary */
158 >                        case 'p':               /* primary controls */
159                                  switch (argv[i][2]) {
160 +                                /* these two options affect -r conversion */
161 +                                case '\0':
162 +                                        myprims[RED][CIEX] = atof(argv[++i]);
163 +                                        myprims[RED][CIEY] = atof(argv[++i]);
164 +                                        myprims[GRN][CIEX] = atof(argv[++i]);
165 +                                        myprims[GRN][CIEY] = atof(argv[++i]);
166 +                                        myprims[BLU][CIEX] = atof(argv[++i]);
167 +                                        myprims[BLU][CIEY] = atof(argv[++i]);
168 +                                        myprims[WHT][CIEX] = atof(argv[++i]);
169 +                                        myprims[WHT][CIEY] = atof(argv[++i]);
170 +                                        outprims = myprims;
171 +                                        break;
172 +                                case 'x': case 'X': outprims = NULL; break;
173 +                                /* the following options affect +r only */
174                                  case 'r': case 'R': putprim = RED; break;
175                                  case 'g': case 'G': putprim = GRN; break;
176                                  case 'b': case 'B': putprim = BLU; break;
# Line 150 | Line 178 | char  **argv;
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 192 | Line 220 | char  **argv;
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 217 | 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 256 | 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 272 | Line 318 | unkopt:
318                  fprintf(stderr, "%s: bad # file arguments\n", progname);
319                  quit(1);
320          }
275
321          if (reverse) {
277 #ifdef _WIN32
322                  SET_FILE_BINARY(stdout);
279                if (format != 'a' && format != 'i')
280                        SET_FILE_BINARY(fin);
281 #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 315 | 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 <                fputformat(COLRFMT, 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 */
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 < #ifdef _WIN32
326 <                SET_FILE_BINARY(fin);
327 <                if (format != 'a' && format != 'i')
395 >                if ((format != 'a') & (format != 'i'))
396                          SET_FILE_BINARY(stdout);
329 #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 352 | Line 439 | unkopt:
439          }
440  
441          quit(0);
442 +        return 0; /* pro forma return */
443   }
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 <                        if (original) {
372 <                                scalecolor(exposure, 1./WHTEFFICACY);
373 <                                doexposure++;
374 <                        }
375 <                } 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  
530  
531 < pixtoval()                              /* convert picture to values */
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;
404 <        register int  x;
540 >        int  x, y;
541  
542          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
543          if (scanln == NULL) {
# Line 409 | Line 545 | pixtoval()                             /* 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 424 | Line 560 | pixtoval()                             /* 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 465 | Line 602 | pixtoval()                             /* convert picture to values */
602   }
603  
604  
605 < valtopix()                      /* convert values to a pixel file */
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 499 | Line 637 | valtopix()                     /* 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);
506 <                }
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 517 | Line 665 | int  code;
665   }
666  
667  
668 < swap16(wp, n)           /* swap n 16-bit words */
669 < register uint16  *wp;
670 < int  n;
668 > static int
669 > getcascii(              /* get an ascii color value from stream(s) */
670 >        COLOR  col
671 > )
672   {
524        while (n-- > 0) {
525                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
526                wp++;
527        }
528 }
529
530
531 swap32(wp, n)           /* swap n 32-bit words */
532 register uint32  *wp;
533 int  n;
534 {
535        while (n-- > 0) {
536                *wp = *wp << 24 | ((*wp >> 24) & 0xff) |
537                        (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8;
538                wp++;
539        }
540 }
541
542
543 swap64(wp, n)           /* swap n 64-bit words */
544 register char  *wp;
545 int  n;
546 {
547        register int    t;
548
549        while (n-- > 0) {
550                t = wp[0]; wp[0] = wp[7]; wp[7] = t;
551                t = wp[1]; wp[1] = wp[6]; wp[6] = t;
552                t = wp[2]; wp[2] = wp[5]; wp[5] = t;
553                t = wp[3]; wp[3] = wp[4]; wp[4] = t;
554                wp += 8;
555        }
556 }
557
558
559 getcascii(col)          /* get an ascii color value from stream(s) */
560 COLOR  col;
561 {
673          double  vd[3];
674  
675          if (fin2 == NULL) {
# Line 575 | Line 686 | COLOR  col;
686   }
687  
688  
689 < getcdouble(col)         /* get a double color value from stream(s) */
690 < COLOR  col;
689 > static int
690 > getcdouble(             /* get a double color value from stream(s) */
691 >        COLOR  col
692 > )
693   {
694          double  vd[3];
695  
696          if (fin2 == NULL) {
697 <                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
697 >                if (getbinary(vd, sizeof(double), 3, fin) != 3)
698                          return(-1);
699          } else {
700 <                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
701 <                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
702 <                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
700 >                if (getbinary(vd, sizeof(double), 1, fin) != 1 ||
701 >                        getbinary(vd+1, sizeof(double), 1, fin2) != 1 ||
702 >                        getbinary(vd+2, sizeof(double), 1, fin3) != 1)
703                          return(-1);
704          }
705          if (swapbytes)
# Line 596 | Line 709 | COLOR  col;
709   }
710  
711  
712 < getcfloat(col)          /* get a float color value from stream(s) */
713 < COLOR  col;
712 > static int
713 > getcfloat(              /* get a float color value from stream(s) */
714 >        COLOR  col
715 > )
716   {
717          float  vf[3];
718  
719          if (fin2 == NULL) {
720 <                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
720 >                if (getbinary(vf, sizeof(float), 3, fin) != 3)
721                          return(-1);
722          } else {
723 <                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
724 <                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
725 <                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
723 >                if (getbinary(vf, sizeof(float), 1, fin) != 1 ||
724 >                        getbinary(vf+1, sizeof(float), 1, fin2) != 1 ||
725 >                        getbinary(vf+2, sizeof(float), 1, fin3) != 1)
726                          return(-1);
727          }
728          if (swapbytes)
729 <                swap32((uint32 *)vf, 3);
729 >                swap32((char *)vf, 3);
730          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
731          return(0);
732   }
733  
734  
735 < getcint(col)            /* get an int color value from stream(s) */
736 < COLOR  col;
735 > static int
736 > getcint(                /* get an int color value from stream(s) */
737 >        COLOR  col
738 > )
739   {
740          int  vi[3];
741  
# Line 637 | Line 754 | COLOR  col;
754   }
755  
756  
757 < getcbyte(col)           /* get a byte color value from stream(s) */
758 < COLOR  col;
757 > static int
758 > getcbyte(               /* get a byte color value from stream(s) */
759 >        COLOR  col
760 > )
761   {
762 <        BYTE  vb[3];
762 >        uby8  vb[3];
763  
764          if (fin2 == NULL) {
765 <                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
765 >                if (getbinary(vb, sizeof(uby8), 3, fin) != 3)
766                          return(-1);
767          } else {
768 <                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
769 <                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
770 <                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
768 >                if (getbinary(vb, sizeof(uby8), 1, fin) != 1 ||
769 >                        getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 ||
770 >                        getbinary(vb+2, sizeof(uby8), 1, fin3) != 1)
771                          return(-1);
772          }
773          setcolor(col, (vb[rord[RED]]+.5)/256.,
# Line 657 | Line 776 | COLOR  col;
776   }
777  
778  
779 < getcword(col)           /* get a 16-bit color value from stream(s) */
780 < COLOR  col;
779 > static int
780 > getcword(               /* get a 16-bit color value from stream(s) */
781 >        COLOR  col
782 > )
783   {
784          uint16  vw[3];
785  
786          if (fin2 == NULL) {
787 <                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
787 >                if (getbinary(vw, sizeof(uint16), 3, fin) != 3)
788                          return(-1);
789          } else {
790 <                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
791 <                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
792 <                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
790 >                if (getbinary(vw, sizeof(uint16), 1, fin) != 1 ||
791 >                        getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 ||
792 >                        getbinary(vw+2, sizeof(uint16), 1, fin3) != 1)
793                          return(-1);
794          }
795          if (swapbytes)
796 <                swap16(vw, 3);
796 >                swap16((char *)vw, 3);
797          setcolor(col, (vw[rord[RED]]+.5)/65536.,
798                          (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
799          return(0);
800   }
801  
802  
803 < getbascii(col)          /* get an ascii brightness value from fin */
804 < COLOR  col;
803 > static int
804 > getbascii(              /* get an ascii brightness value from fin */
805 >        COLOR  col
806 > )
807   {
808          double  vd;
809  
# Line 691 | Line 814 | COLOR  col;
814   }
815  
816  
817 < getbdouble(col)         /* get a double brightness value from fin */
818 < COLOR  col;
817 > static int
818 > getbdouble(             /* get a double brightness value from fin */
819 >        COLOR  col
820 > )
821   {
822          double  vd;
823  
824 <        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
824 >        if (getbinary(&vd, sizeof(double), 1, fin) != 1)
825                  return(-1);
826          if (swapbytes)
827                  swap64((char *)&vd, 1);
# Line 705 | Line 830 | COLOR  col;
830   }
831  
832  
833 < getbfloat(col)          /* get a float brightness value from fin */
834 < COLOR  col;
833 > static int
834 > getbfloat(              /* get a float brightness value from fin */
835 >        COLOR  col
836 > )
837   {
838          float  vf;
839  
840 <        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
840 >        if (getbinary(&vf, sizeof(float), 1, fin) != 1)
841                  return(-1);
842          if (swapbytes)
843 <                swap32((uint32 *)&vf, 1);
843 >                swap32((char *)&vf, 1);
844          setcolor(col, vf, vf, vf);
845          return(0);
846   }
847  
848  
849 < getbint(col)            /* get an int brightness value from fin */
850 < COLOR  col;
849 > static int
850 > getbint(                /* get an int brightness value from fin */
851 >        COLOR  col
852 > )
853   {
854          int  vi;
855          double  d;
# Line 733 | Line 862 | COLOR  col;
862   }
863  
864  
865 < getbbyte(col)           /* get a byte brightness value from fin */
866 < COLOR  col;
865 > static int
866 > getbbyte(               /* get a byte brightness value from fin */
867 >        COLOR  col
868 > )
869   {
870 <        BYTE  vb;
870 >        uby8  vb;
871          double  d;
872  
873 <        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
873 >        if (getbinary(&vb, sizeof(uby8), 1, fin) != 1)
874                  return(-1);
875          d = (vb+.5)/256.;
876          setcolor(col, d, d, d);
# Line 747 | Line 878 | COLOR  col;
878   }
879  
880  
881 < getbword(col)           /* get a 16-bit brightness value from fin */
882 < COLOR  col;
881 > static int
882 > getbword(               /* get a 16-bit brightness value from fin */
883 >        COLOR  col
884 > )
885   {
886          uint16  vw;
887          double  d;
888  
889 <        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
889 >        if (getbinary(&vw, sizeof(uint16), 1, fin) != 1)
890                  return(-1);
891          if (swapbytes)
892 <                swap16(&vw, 1);
892 >                swap16((char *)&vw, 1);
893          d = (vw+.5)/65536.;
894          setcolor(col, d, d, d);
895          return(0);
896   }
897  
898  
899 < putcascii(col)                  /* put an ascii color to stdout */
900 < COLOR  col;
899 > static int
900 > putcascii(                      /* put an ascii color to stdout */
901 >        COLOR  col
902 > )
903   {
904          fprintf(stdout, "%15.3e %15.3e %15.3e\n",
905                          colval(col,ord[0]),
# Line 775 | Line 910 | COLOR  col;
910   }
911  
912  
913 < putcfloat(col)                  /* put a float color to stdout */
914 < COLOR  col;
913 > static int
914 > putcfloat(                      /* put a float color to stdout */
915 >        COLOR  col
916 > )
917   {
918          float  vf[3];
919  
# Line 784 | Line 921 | COLOR  col;
921          vf[1] = colval(col,ord[1]);
922          vf[2] = colval(col,ord[2]);
923          if (swapbytes)
924 <                swap32((uint32 *)vf, 3);
925 <        fwrite((char *)vf, sizeof(float), 3, stdout);
924 >                swap32((char *)vf, 3);
925 >        putbinary(vf, sizeof(float), 3, stdout);
926  
927          return(ferror(stdout) ? -1 : 0);
928   }
929  
930  
931 < putcdouble(col)                 /* put a double color to stdout */
932 < COLOR  col;
931 > static int
932 > putcdouble(                     /* put a double color to stdout */
933 >        COLOR  col
934 > )
935   {
936          double  vd[3];
937  
# Line 801 | Line 940 | COLOR  col;
940          vd[2] = colval(col,ord[2]);
941          if (swapbytes)
942                  swap64((char *)vd, 3);
943 <        fwrite((char *)vd, sizeof(double), 3, stdout);
943 >        putbinary(vd, sizeof(double), 3, stdout);
944  
945          return(ferror(stdout) ? -1 : 0);
946   }
947  
948  
949 < putcint(col)                    /* put an int color to stdout */
950 < COLOR  col;
949 > static int
950 > putcint(                        /* put an int color to stdout */
951 >        COLOR  col
952 > )
953   {
954          fprintf(stdout, "%d %d %d\n",
955                          (int)(colval(col,ord[0])*256.),
# Line 819 | Line 960 | COLOR  col;
960   }
961  
962  
963 < putcbyte(col)                   /* put a byte color to stdout */
964 < COLOR  col;
963 > static int
964 > putcbyte(                       /* put a byte color to stdout */
965 >        COLOR  col
966 > )
967   {
968          long  i;
969 <        BYTE  vb[3];
969 >        uby8  vb[3];
970  
971          i = colval(col,ord[0])*256.;
972          vb[0] = min(i,255);
# Line 831 | Line 974 | COLOR  col;
974          vb[1] = min(i,255);
975          i = colval(col,ord[2])*256.;
976          vb[2] = min(i,255);
977 <        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
977 >        putbinary(vb, sizeof(uby8), 3, stdout);
978  
979          return(ferror(stdout) ? -1 : 0);
980   }
981  
982  
983 < putcword(col)                   /* put a 16-bit color to stdout */
984 < COLOR  col;
983 > static int
984 > putcword(                       /* put a 16-bit color to stdout */
985 >        COLOR  col
986 > )
987   {
988          long  i;
989          uint16  vw[3];
# Line 850 | Line 995 | COLOR  col;
995          i = colval(col,ord[2])*65536.;
996          vw[2] = min(i,65535);
997          if (swapbytes)
998 <                swap16(vw, 3);
999 <        fwrite((char *)vw, sizeof(uint16), 3, stdout);
998 >                swap16((char *)vw, 3);
999 >        putbinary(vw, sizeof(uint16), 3, stdout);
1000  
1001          return(ferror(stdout) ? -1 : 0);
1002   }
1003  
1004  
1005 < putbascii(col)                  /* put an ascii brightness to stdout */
1006 < COLOR  col;
1005 > static int
1006 > putbascii(                      /* put an ascii brightness to stdout */
1007 >        COLOR  col
1008 > )
1009   {
1010          fprintf(stdout, "%15.3e\n", (*mybright)(col));
1011  
# Line 866 | Line 1013 | COLOR  col;
1013   }
1014  
1015  
1016 < putbfloat(col)                  /* put a float brightness to stdout */
1017 < COLOR  col;
1016 > static int
1017 > putbfloat(                      /* put a float brightness to stdout */
1018 >        COLOR  col
1019 > )
1020   {
1021          float  vf;
1022  
1023          vf = (*mybright)(col);
1024          if (swapbytes)
1025 <                swap32((uint32 *)&vf, 1);
1026 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
1025 >                swap32((char *)&vf, 1);
1026 >        putbinary(&vf, sizeof(float), 1, stdout);
1027  
1028          return(ferror(stdout) ? -1 : 0);
1029   }
1030  
1031  
1032 < putbdouble(col)                 /* put a double brightness to stdout */
1033 < COLOR  col;
1032 > static int
1033 > putbdouble(                     /* put a double brightness to stdout */
1034 >        COLOR  col
1035 > )
1036   {
1037          double  vd;
1038  
1039          vd = (*mybright)(col);
1040          if (swapbytes)
1041                  swap64((char *)&vd, 1);
1042 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
1042 >        putbinary(&vd, sizeof(double), 1, stdout);
1043  
1044          return(ferror(stdout) ? -1 : 0);
1045   }
1046  
1047  
1048 < putbint(col)                    /* put an int brightness to stdout */
1049 < COLOR  col;
1048 > static int
1049 > putbint(                        /* put an int brightness to stdout */
1050 >        COLOR  col
1051 > )
1052   {
1053          fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
1054  
# Line 903 | Line 1056 | COLOR  col;
1056   }
1057  
1058  
1059 < putbbyte(col)                   /* put a byte brightness to stdout */
1060 < COLOR  col;
1059 > static int
1060 > putbbyte(                       /* put a byte brightness to stdout */
1061 >        COLOR  col
1062 > )
1063   {
1064 <        register int  i;
1065 <        BYTE  vb;
1064 >        int  i;
1065 >        uby8  vb;
1066  
1067          i = (*mybright)(col)*256.;
1068          vb = min(i,255);
1069 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
1069 >        putbinary(&vb, sizeof(uby8), 1, stdout);
1070  
1071          return(ferror(stdout) ? -1 : 0);
1072   }
1073  
1074  
1075 < putbword(col)                   /* put a 16-bit brightness to stdout */
1076 < COLOR  col;
1075 > static int
1076 > putbword(                       /* put a 16-bit brightness to stdout */
1077 >        COLOR  col
1078 > )
1079   {
1080          long  i;
1081          uint16  vw;
# Line 926 | Line 1083 | COLOR  col;
1083          i = (*mybright)(col)*65536.;
1084          vw = min(i,65535);
1085          if (swapbytes)
1086 <                swap16(&vw, 1);
1087 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1086 >                swap16((char *)&vw, 1);
1087 >        putbinary(&vw, sizeof(uint16), 1, stdout);
1088  
1089          return(ferror(stdout) ? -1 : 0);
1090   }
1091  
1092  
1093 < putpascii(col)                  /* put an ascii primary to stdout */
1094 < COLOR  col;
1093 > static int
1094 > putpascii(                      /* put an ascii primary to stdout */
1095 >        COLOR  col
1096 > )
1097   {
1098          fprintf(stdout, "%15.3e\n", colval(col,putprim));
1099  
# Line 942 | Line 1101 | COLOR  col;
1101   }
1102  
1103  
1104 < putpfloat(col)                  /* put a float primary to stdout */
1105 < COLOR  col;
1104 > static int
1105 > putpfloat(                      /* put a float primary to stdout */
1106 >        COLOR  col
1107 > )
1108   {
1109          float  vf;
1110  
1111          vf = colval(col,putprim);
1112          if (swapbytes)
1113 <                swap32((uint32 *)&vf, 1);
1114 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
1113 >                swap32((char *)&vf, 1);
1114 >        putbinary(&vf, sizeof(float), 1, stdout);
1115  
1116          return(ferror(stdout) ? -1 : 0);
1117   }
1118  
1119  
1120 < putpdouble(col)                 /* put a double primary to stdout */
1121 < COLOR  col;
1120 > static int
1121 > putpdouble(                     /* put a double primary to stdout */
1122 >        COLOR  col
1123 > )
1124   {
1125          double  vd;
1126  
1127          vd = colval(col,putprim);
1128          if (swapbytes)
1129                  swap64((char *)&vd, 1);
1130 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
1130 >        putbinary(&vd, sizeof(double), 1, stdout);
1131  
1132          return(ferror(stdout) ? -1 : 0);
1133   }
1134  
1135  
1136 < putpint(col)                    /* put an int primary to stdout */
1137 < COLOR  col;
1136 > static int
1137 > putpint(                        /* put an int primary to stdout */
1138 >        COLOR  col
1139 > )
1140   {
1141          fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
1142  
# Line 979 | Line 1144 | COLOR  col;
1144   }
1145  
1146  
1147 < putpbyte(col)                   /* put a byte primary to stdout */
1148 < COLOR  col;
1147 > static int
1148 > putpbyte(                       /* put a byte primary to stdout */
1149 >        COLOR  col
1150 > )
1151   {
1152          long  i;
1153 <        BYTE  vb;
1153 >        uby8  vb;
1154  
1155          i = colval(col,putprim)*256.;
1156          vb = min(i,255);
1157 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
1157 >        putbinary(&vb, sizeof(uby8), 1, stdout);
1158  
1159          return(ferror(stdout) ? -1 : 0);
1160   }
1161  
1162  
1163 < putpword(col)                   /* put a 16-bit primary to stdout */
1164 < COLOR  col;
1163 > static int
1164 > putpword(                       /* put a 16-bit primary to stdout */
1165 >        COLOR  col
1166 > )
1167   {
1168          long  i;
1169          uint16  vw;
# Line 1002 | Line 1171 | COLOR  col;
1171          i = colval(col,putprim)*65536.;
1172          vw = min(i,65535);
1173          if (swapbytes)
1174 <                swap16(&vw, 1);
1175 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1174 >                swap16((char *)&vw, 1);
1175 >        putbinary(&vw, sizeof(uint16), 1, stdout);
1176  
1177          return(ferror(stdout) ? -1 : 0);
1178   }
1179  
1180  
1181 < set_io()                        /* set put and get functions */
1181 > static void
1182 > set_io(void)                    /* set put and get functions */
1183   {
1184          switch (format) {
1185          case 'a':                                       /* ascii */
# Line 1044 | Line 1214 | set_io()                       /* 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 1066 | Line 1236 | set_io()                       /* 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 1106 | Line 1276 | set_io()                       /* set put and get functions */
1276                                  if (fin2 == NULL)
1277                                          goto namerr;
1278                                  if (fseek(fin2,
1279 <                                (long)sizeof(BYTE)*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(BYTE)*2*picres.xr*picres.yr, 1))
1282 >                                (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR))
1283                                          goto seekerr;
1284                          }
1285                  }
# Line 1128 | Line 1298 | set_io()                       /* 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                  }
1308                  return;
1309          }
1310 < badopt:
1310 > /* badopt: */ /* label not used */
1311          fprintf(stderr, "%s: botched file type\n", progname);
1312          quit(1);
1313   namerr:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines