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.21 by schorsch, Thu Jun 26 00:58:10 2003 UTC vs.
Revision 2.40 by greg, Tue Jun 30 22:30:29 2020 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   *     4/23/86
8   */
9  
10 #include  <time.h>
11
12 #include  "standard.h"
10   #include  "platform.h"
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 21 | Line 20 | static const char RCSid[] = "$Id$";
20   #define  BRIGHT         4
21  
22   RESOLU  picres;                 /* resolution of picture */
24
23   int  uniq = 0;                  /* print only unique values? */
26
24   int  doexposure = 0;            /* exposure change? (>100 to print) */
28
25   int  dataonly = 0;              /* data only format? */
30
26   int  putprim = ALL;             /* what to put out */
32
27   int  reverse = 0;               /* reverse conversion? */
34
28   int  format = 'a';              /* input/output format */
29   char  *fmtid = "ascii";         /* format identifier for header */
37
30   int  header = 1;                /* do header? */
39
31   long  skipbytes = 0;            /* skip bytes in input? */
32 <
42 < int  swapbytes = 0;             /* swap bytes in 16-bit words? */
43 <
32 > int  swapbytes = 0;             /* swap bytes? */
33   int  interleave = 1;            /* file is interleaved? */
45
34   int  resolution = 1;            /* put/get resolution string? */
47
35   int  original = 0;              /* convert to original values? */
49
36   int  wrongformat = 0;           /* wrong input format? */
51
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 61 | Line 49 | char  *progname;
49   FILE  *fin;
50   FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
51  
64 int  (*getval)(), (*putval)();
52  
53 < double
54 < rgb_bright(clr)
55 < COLOR  clr;
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 void set_io(void);
70 > static void pixtoval(void);
71 > static void valtopix(void);
72 >
73 >
74 > static double
75 > rgb_bright(
76 >        COLOR  clr
77 > )
78   {
79          return(bright(clr));
80   }
81  
82 < double
83 < xyz_bright(clr)
84 < COLOR  clr;
82 > static double
83 > xyz_bright(
84 >        COLOR  clr
85 > )
86   {
87          return(clr[CIEY]);
88   }
89  
90 < double  (*mybright)() = &rgb_bright;
91 <
92 <
93 < main(argc, argv)
94 < int  argc;
85 < char  **argv;
90 > int
91 > main(
92 >        int  argc,
93 >        char  **argv
94 > )
95   {
96 <        extern int  checkhead();
97 <        extern long  atol();
98 <        double  d, expval = 1.0;
90 <        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 */
102  
103          for (i = 1; i < argc; i++)
104                  if (argv[i][0] == '-' || argv[i][0] == '+')
# Line 141 | Line 150 | char  **argv;
150                          case 'b':               /* brightness values */
151                                  putprim = argv[i][0] == '-' ? BRIGHT : ALL;
152                                  break;
153 <                        case 'p':               /* put primary */
153 >                        case 'p':               /* primary controls */
154                                  switch (argv[i][2]) {
155 +                                /* these two options affect -r conversion */
156 +                                case '\0':
157 +                                        myprims[RED][CIEX] = atof(argv[++i]);
158 +                                        myprims[RED][CIEY] = atof(argv[++i]);
159 +                                        myprims[GRN][CIEX] = atof(argv[++i]);
160 +                                        myprims[GRN][CIEY] = atof(argv[++i]);
161 +                                        myprims[BLU][CIEX] = atof(argv[++i]);
162 +                                        myprims[BLU][CIEY] = atof(argv[++i]);
163 +                                        myprims[WHT][CIEX] = atof(argv[++i]);
164 +                                        myprims[WHT][CIEY] = atof(argv[++i]);
165 +                                        outprims = myprims;
166 +                                        break;
167 +                                case 'x': case 'X': outprims = NULL; break;
168 +                                /* the following options affect +r only */
169                                  case 'r': case 'R': putprim = RED; break;
170                                  case 'g': case 'G': putprim = GRN; break;
171                                  case 'b': case 'B': putprim = BLU; break;
# Line 150 | Line 173 | char  **argv;
173                                  }
174                                  break;
175                          case 'd':               /* data only (no indices) */
176 <                                dataonly = argv[i][0] == '-';
176 >                                dataonly = (argv[i][0] == '-');
177                                  switch (argv[i][2]) {
178                                  case '\0':
179                                  case 'a':               /* ascii */
# Line 173 | Line 196 | char  **argv;
196                                          format = 'w';
197                                          fmtid = "16-bit";
198                                          break;
199 +                                case 'F':               /* swapped floats */
200 +                                        swapbytes = 1;
201                                  case 'f':               /* float */
202                                          dataonly = 1;
203                                          format = 'f';
204                                          fmtid = "float";
205                                          break;
206 +                                case 'D':               /* swapped doubles */
207 +                                        swapbytes = 1;
208                                  case 'd':               /* double */
209                                          dataonly = 1;
210                                          format = 'd';
# Line 213 | Line 240 | unkopt:
240                  else
241                          break;
242                                          /* recognize special formats */
243 <        if (dataonly && format == 'b')
243 >        if (dataonly && format == 'b') {
244                  if (putprim == ALL)
245                          fmtid = "24-bit_rgb";
246                  else
247                          fmtid = "8-bit_grey";
248 <        if (dataonly && format == 'w')
248 >        }
249 >        if (dataonly && format == 'w') {
250                  if (putprim == ALL)
251                          fmtid = "48-bit_rgb";
252                  else
253                          fmtid = "16-bit_grey";
254 +        }
255 +        if (!reverse || (format != 'a') & (format != 'i'))
256 +                inpmode = "rb";
257                                          /* assign reverse ordering */
258          rord[ord[0]] = 0;
259          rord[ord[1]] = 1;
260          rord[ord[2]] = 2;
261                                          /* get input */
262          if (i == argc) {
263 +                long    n = skipbytes;
264 +                if (inpmode[1] == 'b')
265 +                        SET_FILE_BINARY(stdin);
266 +                while (n-- > 0)
267 +                        if (getchar() == EOF) {
268 +                                fprintf(stderr,
269 +                                "%s: cannot skip %ld bytes on standard input\n",
270 +                                                progname, skipbytes);
271 +                                quit(1);
272 +                        }
273                  fin = stdin;
274          } else if (i < argc) {
275 <                if ((fin = fopen(argv[i], "r")) == NULL) {
275 >                if ((fin = fopen(argv[i], inpmode)) == NULL) {
276                          fprintf(stderr, "%s: can't open file \"%s\"\n",
277                                                  progname, argv[i]);
278                          quit(1);
279                  }
280                  if (reverse && putprim != BRIGHT && i == argc-3) {
281 <                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
281 >                        if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) {
282                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
283                                                  progname, argv[i+1]);
284                                  quit(1);
285                          }
286 <                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
286 >                        if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) {
287                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
288                                                  progname, argv[i+2]);
289                                  quit(1);
# Line 251 | Line 292 | unkopt:
292                  } else if (i != argc-1)
293                          fin = NULL;
294                  if (reverse && putprim != BRIGHT && !interleave) {
295 <                        fin2 = fopen(argv[i], "r");
296 <                        fin3 = fopen(argv[i], "r");
295 >                        fin2 = fopen(argv[i], inpmode);
296 >                        fin3 = fopen(argv[i], inpmode);
297                  }
298 <                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
299 <                                (fseek(fin2, skipbytes, 0) ||
300 <                                fseek(fin3, skipbytes, 0))))) {
298 >                if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL &&
299 >                                fseek(fin2, skipbytes, SEEK_SET) |
300 >                                fseek(fin3, skipbytes, SEEK_SET)))) {
301                          fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
302                                          progname, skipbytes);
303                          quit(1);
# Line 268 | Line 309 | unkopt:
309          }
310  
311          if (reverse) {
271 #ifdef _WIN32
312                  SET_FILE_BINARY(stdout);
273                if (format != 'a' && format != 'i')
274                        SET_FILE_BINARY(fin);
275 #endif
313                                          /* get header */
314                  if (header) {
315 <                        if (checkheader(fin, fmtid, stdout) < 0) {
316 <                                fprintf(stderr, "%s: wrong input format\n",
317 <                                                progname);
315 >                        getheader(fin, checkhead, stdout);
316 >                        if (wrongformat) {
317 >                                fprintf(stderr, "%s: wrong input format (expected %s)\n",
318 >                                                progname, fmtid);
319                                  quit(1);
320                          }
321                          if (fin2 != NULL) {
# Line 311 | Line 349 | unkopt:
349                  printargs(i, argv, stdout);
350                  if (expval < .99 || expval > 1.01)
351                          fputexpos(expval, stdout);
352 <                fputformat(COLRFMT, stdout);
352 >                if (outprims != NULL) {
353 >                        if (outprims != stdprims)
354 >                                fputprims(outprims, stdout);
355 >                        fputformat(COLRFMT, stdout);
356 >                } else                          /* XYZ data */
357 >                        fputformat(CIEFMT, stdout);
358                  putchar('\n');
359                  fputsresolu(&picres, stdout);   /* always put resolution */
360                  valtopix();
361          } else {
362 < #ifdef _WIN32
320 <                SET_FILE_BINARY(fin);
321 <                if (format != 'a' && format != 'i')
362 >                if ((format != 'a') & (format != 'i'))
363                          SET_FILE_BINARY(stdout);
323 #endif
364                                                  /* get header */
365 <                getheader(fin, checkhead, NULL);
365 >                getheader(fin, checkhead, header ? stdout : (FILE *)NULL);
366                  if (wrongformat) {
367                          fprintf(stderr,
368                                  "%s: input not a Radiance RGBE picture\n",
# Line 335 | Line 375 | unkopt:
375                  }
376                  if (header) {
377                          printargs(i, argv, stdout);
378 +                        printf("NCOMP=%d\n", putprim==ALL ? 3 : 1);
379 +                        if (!resolution && dataonly && !uniq)
380 +                                printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres),
381 +                                                numscans(&picres));
382                          if (expval < .99 || expval > 1.01)
383                                  fputexpos(expval, stdout);
384 +                        if (swapbytes) {
385 +                                if (nativebigendian())
386 +                                        puts("BigEndian=0");
387 +                                else
388 +                                        puts("BigEndian=1");
389 +                        } else if ((format != 'a') & (format != 'i') &
390 +                                                (format != 'b'))
391 +                                fputendian(stdout);
392                          fputformat(fmtid, stdout);
393                          putchar('\n');
394                  }
# Line 346 | Line 398 | unkopt:
398          }
399  
400          quit(0);
401 +        return 0; /* pro forma return */
402   }
403  
404  
405 < int
406 < checkhead(line)                         /* deal with line from header */
407 < char  *line;
405 > static int
406 > checkhead(                              /* deal with line from header */
407 >        char    *line,
408 >        void    *p
409 > )
410   {
411 <        char    fmt[32];
411 >        FILE    *fout = (FILE *)p;
412 >        char    fmt[MAXFMTLEN];
413          double  d;
414          COLOR   ctmp;
415 +        int     rv;
416  
417          if (formatval(fmt, line)) {
418 <                if (!strcmp(fmt, CIEFMT)) {
418 >                if (reverse)
419 >                        wrongformat = strcmp(fmt, fmtid);
420 >                else if (!strcmp(fmt, CIEFMT))
421                          mybright = &xyz_bright;
422 <                        if (original) {
364 <                                scalecolor(exposure, 1./WHTEFFICACY);
365 <                                doexposure++;
366 <                        }
367 <                } else if (!strcmp(fmt, COLRFMT))
422 >                else if (!strcmp(fmt, COLRFMT))
423                          mybright = &rgb_bright;
424                  else
425                          wrongformat++;
426 <        } else if (original && isexpos(line)) {
426 >                return(1);
427 >        }
428 >        if (original && isexpos(line)) {
429                  d = 1.0/exposval(line);
430                  scalecolor(exposure, d);
431                  doexposure++;
432 <        } else if (original && iscolcor(line)) {
432 >                return(1);
433 >        }
434 >        if (original && iscolcor(line)) {
435                  colcorval(ctmp, line);
436                  setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
437                                  colval(exposure,GRN)/colval(ctmp,GRN),
438                                  colval(exposure,BLU)/colval(ctmp,BLU));
439                  doexposure++;
440 <        } else if (header)
441 <                fputs(line, stdout);
440 >                return(1);
441 >        }
442 >        if ((rv = isbigendian(line)) >= 0) {
443 >                if (reverse)
444 >                        swapbytes = (nativebigendian() != rv);
445 >                return(1);
446 >        }
447 >        if (fout != NULL)
448 >                fputs(line, fout);
449          return(0);
450   }
451  
452  
453 < pixtoval()                              /* convert picture to values */
453 > static void
454 > pixtoval(void)                          /* convert picture to values */
455   {
456 <        register COLOR  *scanln;
456 >        COLOR   *scanln;
457          int  dogamma;
458          COLOR  lastc;
459          RREAL  hv[2];
460          int  startprim, endprim;
461          long  startpos;
462 <        int  y;
396 <        register int  x;
462 >        int  x, y;
463  
464          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
465          if (scanln == NULL) {
# Line 401 | Line 467 | pixtoval()                             /* convert picture to values */
467                  quit(1);
468          }
469          dogamma = gamcor < .95 || gamcor > 1.05;
470 <        if (putprim == ALL && !interleave) {
470 >        if ((putprim == ALL) & !interleave) {
471                  startprim = RED; endprim = BLU;
472                  startpos = ftell(fin);
473          } else {
474                  startprim = putprim; endprim = putprim;
475          }
476          for (putprim = startprim; putprim <= endprim; putprim++) {
477 <                if (putprim != startprim && fseek(fin, startpos, 0)) {
477 >                if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) {
478                          fprintf(stderr, "%s: seek error on input file\n",
479                                          progname);
480                          quit(1);
# Line 421 | Line 487 | pixtoval()                             /* convert picture to values */
487                                  quit(1);
488                          }
489                          for (x = 0; x < scanlen(&picres); x++) {
490 <                                if (uniq)
490 >                                if (uniq) {
491                                          if (    colval(scanln[x],RED) ==
492                                                          colval(lastc,RED) &&
493                                                  colval(scanln[x],GRN) ==
# Line 431 | Line 497 | pixtoval()                             /* convert picture to values */
497                                                  continue;
498                                          else
499                                                  copycolor(lastc, scanln[x]);
500 +                                }
501                                  if (doexposure)
502                                          multcolor(scanln[x], exposure);
503                                  if (dogamma)
# Line 456 | Line 523 | pixtoval()                             /* convert picture to values */
523   }
524  
525  
526 < valtopix()                      /* convert values to a pixel file */
526 > static void
527 > valtopix(void)                  /* convert values to a pixel file */
528   {
529 <        int  dogamma;
530 <        register COLOR  *scanln;
531 <        int  y;
532 <        register int  x;
529 >        int     dogamma;
530 >        COLOR   *scanln;
531 >        COLR    rgbe;
532 >        int     x, y;
533  
534          scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
535          if (scanln == NULL) {
# Line 490 | Line 558 | valtopix()                     /* convert values to a pixel file */
558                                          pow(colval(scanln[x],BLU), gamcor));
559                          if (doexposure)
560                                  multcolor(scanln[x], exposure);
561 +                        if (uniq) {             /* uncompressed? */
562 +                                setcolr(rgbe,   scanln[x][RED],
563 +                                                scanln[x][GRN],
564 +                                                scanln[x][BLU]);
565 +                                if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1)
566 +                                        goto writerr;
567 +                        }
568                  }
569 <                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
570 <                        fprintf(stderr, "%s: write error\n", progname);
571 <                        quit(1);
497 <                }
569 >                                                /* write scan if compressed */
570 >                if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0)
571 >                        goto writerr;
572          }
573          free((void *)scanln);
574 +        return;
575 + writerr:
576 +        fprintf(stderr, "%s: write error\n", progname);
577 +        quit(1);
578   }
579  
580  
# Line 508 | Line 586 | int  code;
586   }
587  
588  
589 < swap16(wp, n)           /* swap n 16-bit words */
590 < register uint16  *wp;
591 < int  n;
589 > static int
590 > getcascii(              /* get an ascii color value from stream(s) */
591 >        COLOR  col
592 > )
593   {
515        while (n-- > 0) {
516                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
517                wp++;
518        }
519 }
520
521 getcascii(col)          /* get an ascii color value from stream(s) */
522 COLOR  col;
523 {
594          double  vd[3];
595  
596          if (fin2 == NULL) {
# Line 537 | Line 607 | COLOR  col;
607   }
608  
609  
610 < getcdouble(col)         /* get a double color value from stream(s) */
611 < COLOR  col;
610 > static int
611 > getcdouble(             /* get a double color value from stream(s) */
612 >        COLOR  col
613 > )
614   {
615          double  vd[3];
616  
617          if (fin2 == NULL) {
618 <                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
618 >                if (getbinary(vd, sizeof(double), 3, fin) != 3)
619                          return(-1);
620          } else {
621 <                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
622 <                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
623 <                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
621 >                if (getbinary(vd, sizeof(double), 1, fin) != 1 ||
622 >                        getbinary(vd+1, sizeof(double), 1, fin2) != 1 ||
623 >                        getbinary(vd+2, sizeof(double), 1, fin3) != 1)
624                          return(-1);
625          }
626 +        if (swapbytes)
627 +                swap64((char *)vd, 3);
628          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
629          return(0);
630   }
631  
632  
633 < getcfloat(col)          /* get a float color value from stream(s) */
634 < COLOR  col;
633 > static int
634 > getcfloat(              /* get a float color value from stream(s) */
635 >        COLOR  col
636 > )
637   {
638          float  vf[3];
639  
640          if (fin2 == NULL) {
641 <                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
641 >                if (getbinary(vf, sizeof(float), 3, fin) != 3)
642                          return(-1);
643          } else {
644 <                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
645 <                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
646 <                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
644 >                if (getbinary(vf, sizeof(float), 1, fin) != 1 ||
645 >                        getbinary(vf+1, sizeof(float), 1, fin2) != 1 ||
646 >                        getbinary(vf+2, sizeof(float), 1, fin3) != 1)
647                          return(-1);
648          }
649 +        if (swapbytes)
650 +                swap32((char *)vf, 3);
651          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
652          return(0);
653   }
654  
655  
656 < getcint(col)            /* get an int color value from stream(s) */
657 < COLOR  col;
656 > static int
657 > getcint(                /* get an int color value from stream(s) */
658 >        COLOR  col
659 > )
660   {
661          int  vi[3];
662  
# Line 595 | Line 675 | COLOR  col;
675   }
676  
677  
678 < getcbyte(col)           /* get a byte color value from stream(s) */
679 < COLOR  col;
678 > static int
679 > getcbyte(               /* get a byte color value from stream(s) */
680 >        COLOR  col
681 > )
682   {
683 <        BYTE  vb[3];
683 >        uby8  vb[3];
684  
685          if (fin2 == NULL) {
686 <                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
686 >                if (getbinary(vb, sizeof(uby8), 3, fin) != 3)
687                          return(-1);
688          } else {
689 <                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
690 <                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
691 <                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
689 >                if (getbinary(vb, sizeof(uby8), 1, fin) != 1 ||
690 >                        getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 ||
691 >                        getbinary(vb+2, sizeof(uby8), 1, fin3) != 1)
692                          return(-1);
693          }
694          setcolor(col, (vb[rord[RED]]+.5)/256.,
# Line 615 | Line 697 | COLOR  col;
697   }
698  
699  
700 < getcword(col)           /* get a 16-bit color value from stream(s) */
701 < COLOR  col;
700 > static int
701 > getcword(               /* get a 16-bit color value from stream(s) */
702 >        COLOR  col
703 > )
704   {
705          uint16  vw[3];
706  
707          if (fin2 == NULL) {
708 <                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
708 >                if (getbinary(vw, sizeof(uint16), 3, fin) != 3)
709                          return(-1);
710          } else {
711 <                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
712 <                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
713 <                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
711 >                if (getbinary(vw, sizeof(uint16), 1, fin) != 1 ||
712 >                        getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 ||
713 >                        getbinary(vw+2, sizeof(uint16), 1, fin3) != 1)
714                          return(-1);
715          }
716          if (swapbytes)
717 <                swap16(vw, 3);
717 >                swap16((char *)vw, 3);
718          setcolor(col, (vw[rord[RED]]+.5)/65536.,
719                          (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
720          return(0);
721   }
722  
723  
724 < getbascii(col)          /* get an ascii brightness value from fin */
725 < COLOR  col;
724 > static int
725 > getbascii(              /* get an ascii brightness value from fin */
726 >        COLOR  col
727 > )
728   {
729          double  vd;
730  
# Line 649 | Line 735 | COLOR  col;
735   }
736  
737  
738 < getbdouble(col)         /* get a double brightness value from fin */
739 < COLOR  col;
738 > static int
739 > getbdouble(             /* get a double brightness value from fin */
740 >        COLOR  col
741 > )
742   {
743          double  vd;
744  
745 <        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
745 >        if (getbinary(&vd, sizeof(double), 1, fin) != 1)
746                  return(-1);
747 +        if (swapbytes)
748 +                swap64((char *)&vd, 1);
749          setcolor(col, vd, vd, vd);
750          return(0);
751   }
752  
753  
754 < getbfloat(col)          /* get a float brightness value from fin */
755 < COLOR  col;
754 > static int
755 > getbfloat(              /* get a float brightness value from fin */
756 >        COLOR  col
757 > )
758   {
759          float  vf;
760  
761 <        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
761 >        if (getbinary(&vf, sizeof(float), 1, fin) != 1)
762                  return(-1);
763 +        if (swapbytes)
764 +                swap32((char *)&vf, 1);
765          setcolor(col, vf, vf, vf);
766          return(0);
767   }
768  
769  
770 < getbint(col)            /* get an int brightness value from fin */
771 < COLOR  col;
770 > static int
771 > getbint(                /* get an int brightness value from fin */
772 >        COLOR  col
773 > )
774   {
775          int  vi;
776          double  d;
# Line 687 | Line 783 | COLOR  col;
783   }
784  
785  
786 < getbbyte(col)           /* get a byte brightness value from fin */
787 < COLOR  col;
786 > static int
787 > getbbyte(               /* get a byte brightness value from fin */
788 >        COLOR  col
789 > )
790   {
791 <        BYTE  vb;
791 >        uby8  vb;
792          double  d;
793  
794 <        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
794 >        if (getbinary(&vb, sizeof(uby8), 1, fin) != 1)
795                  return(-1);
796          d = (vb+.5)/256.;
797          setcolor(col, d, d, d);
# Line 701 | Line 799 | COLOR  col;
799   }
800  
801  
802 < getbword(col)           /* get a 16-bit brightness value from fin */
803 < COLOR  col;
802 > static int
803 > getbword(               /* get a 16-bit brightness value from fin */
804 >        COLOR  col
805 > )
806   {
807          uint16  vw;
808          double  d;
809  
810 <        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
810 >        if (getbinary(&vw, sizeof(uint16), 1, fin) != 1)
811                  return(-1);
812          if (swapbytes)
813 <                swap16(&vw, 1);
813 >                swap16((char *)&vw, 1);
814          d = (vw+.5)/65536.;
815          setcolor(col, d, d, d);
816          return(0);
817   }
818  
819  
820 < putcascii(col)                  /* put an ascii color to stdout */
821 < COLOR  col;
820 > static int
821 > putcascii(                      /* put an ascii color to stdout */
822 >        COLOR  col
823 > )
824   {
825          fprintf(stdout, "%15.3e %15.3e %15.3e\n",
826                          colval(col,ord[0]),
# Line 729 | Line 831 | COLOR  col;
831   }
832  
833  
834 < putcfloat(col)                  /* put a float color to stdout */
835 < COLOR  col;
834 > static int
835 > putcfloat(                      /* put a float color to stdout */
836 >        COLOR  col
837 > )
838   {
839          float  vf[3];
840  
841          vf[0] = colval(col,ord[0]);
842          vf[1] = colval(col,ord[1]);
843          vf[2] = colval(col,ord[2]);
844 <        fwrite((char *)vf, sizeof(float), 3, stdout);
844 >        if (swapbytes)
845 >                swap32((char *)vf, 3);
846 >        putbinary(vf, sizeof(float), 3, stdout);
847  
848          return(ferror(stdout) ? -1 : 0);
849   }
850  
851  
852 < putcdouble(col)                 /* put a double color to stdout */
853 < COLOR  col;
852 > static int
853 > putcdouble(                     /* put a double color to stdout */
854 >        COLOR  col
855 > )
856   {
857          double  vd[3];
858  
859          vd[0] = colval(col,ord[0]);
860          vd[1] = colval(col,ord[1]);
861          vd[2] = colval(col,ord[2]);
862 <        fwrite((char *)vd, sizeof(double), 3, stdout);
862 >        if (swapbytes)
863 >                swap64((char *)vd, 3);
864 >        putbinary(vd, sizeof(double), 3, stdout);
865  
866          return(ferror(stdout) ? -1 : 0);
867   }
868  
869  
870 < putcint(col)                    /* put an int color to stdout */
871 < COLOR  col;
870 > static int
871 > putcint(                        /* put an int color to stdout */
872 >        COLOR  col
873 > )
874   {
875          fprintf(stdout, "%d %d %d\n",
876                          (int)(colval(col,ord[0])*256.),
# Line 769 | Line 881 | COLOR  col;
881   }
882  
883  
884 < putcbyte(col)                   /* put a byte color to stdout */
885 < COLOR  col;
884 > static int
885 > putcbyte(                       /* put a byte color to stdout */
886 >        COLOR  col
887 > )
888   {
889          long  i;
890 <        BYTE  vb[3];
890 >        uby8  vb[3];
891  
892          i = colval(col,ord[0])*256.;
893          vb[0] = min(i,255);
# Line 781 | Line 895 | COLOR  col;
895          vb[1] = min(i,255);
896          i = colval(col,ord[2])*256.;
897          vb[2] = min(i,255);
898 <        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
898 >        putbinary(vb, sizeof(uby8), 3, stdout);
899  
900          return(ferror(stdout) ? -1 : 0);
901   }
902  
903  
904 < putcword(col)                   /* put a 16-bit color to stdout */
905 < COLOR  col;
904 > static int
905 > putcword(                       /* put a 16-bit color to stdout */
906 >        COLOR  col
907 > )
908   {
909          long  i;
910          uint16  vw[3];
# Line 800 | Line 916 | COLOR  col;
916          i = colval(col,ord[2])*65536.;
917          vw[2] = min(i,65535);
918          if (swapbytes)
919 <                swap16(vw, 3);
920 <        fwrite((char *)vw, sizeof(uint16), 3, stdout);
919 >                swap16((char *)vw, 3);
920 >        putbinary(vw, sizeof(uint16), 3, stdout);
921  
922          return(ferror(stdout) ? -1 : 0);
923   }
924  
925  
926 < putbascii(col)                  /* put an ascii brightness to stdout */
927 < COLOR  col;
926 > static int
927 > putbascii(                      /* put an ascii brightness to stdout */
928 >        COLOR  col
929 > )
930   {
931          fprintf(stdout, "%15.3e\n", (*mybright)(col));
932  
# Line 816 | Line 934 | COLOR  col;
934   }
935  
936  
937 < putbfloat(col)                  /* put a float brightness to stdout */
938 < COLOR  col;
937 > static int
938 > putbfloat(                      /* put a float brightness to stdout */
939 >        COLOR  col
940 > )
941   {
942          float  vf;
943  
944          vf = (*mybright)(col);
945 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
945 >        if (swapbytes)
946 >                swap32((char *)&vf, 1);
947 >        putbinary(&vf, sizeof(float), 1, stdout);
948  
949          return(ferror(stdout) ? -1 : 0);
950   }
951  
952  
953 < putbdouble(col)                 /* put a double brightness to stdout */
954 < COLOR  col;
953 > static int
954 > putbdouble(                     /* put a double brightness to stdout */
955 >        COLOR  col
956 > )
957   {
958          double  vd;
959  
960          vd = (*mybright)(col);
961 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
961 >        if (swapbytes)
962 >                swap64((char *)&vd, 1);
963 >        putbinary(&vd, sizeof(double), 1, stdout);
964  
965          return(ferror(stdout) ? -1 : 0);
966   }
967  
968  
969 < putbint(col)                    /* put an int brightness to stdout */
970 < COLOR  col;
969 > static int
970 > putbint(                        /* put an int brightness to stdout */
971 >        COLOR  col
972 > )
973   {
974          fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
975  
# Line 849 | Line 977 | COLOR  col;
977   }
978  
979  
980 < putbbyte(col)                   /* put a byte brightness to stdout */
981 < COLOR  col;
980 > static int
981 > putbbyte(                       /* put a byte brightness to stdout */
982 >        COLOR  col
983 > )
984   {
985 <        register int  i;
986 <        BYTE  vb;
985 >        int  i;
986 >        uby8  vb;
987  
988          i = (*mybright)(col)*256.;
989          vb = min(i,255);
990 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
990 >        putbinary(&vb, sizeof(uby8), 1, stdout);
991  
992          return(ferror(stdout) ? -1 : 0);
993   }
994  
995  
996 < putbword(col)                   /* put a 16-bit brightness to stdout */
997 < COLOR  col;
996 > static int
997 > putbword(                       /* put a 16-bit brightness to stdout */
998 >        COLOR  col
999 > )
1000   {
1001          long  i;
1002          uint16  vw;
# Line 872 | Line 1004 | COLOR  col;
1004          i = (*mybright)(col)*65536.;
1005          vw = min(i,65535);
1006          if (swapbytes)
1007 <                swap16(&vw, 1);
1008 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1007 >                swap16((char *)&vw, 1);
1008 >        putbinary(&vw, sizeof(uint16), 1, stdout);
1009  
1010          return(ferror(stdout) ? -1 : 0);
1011   }
1012  
1013  
1014 < putpascii(col)                  /* put an ascii primary to stdout */
1015 < COLOR  col;
1014 > static int
1015 > putpascii(                      /* put an ascii primary to stdout */
1016 >        COLOR  col
1017 > )
1018   {
1019          fprintf(stdout, "%15.3e\n", colval(col,putprim));
1020  
# Line 888 | Line 1022 | COLOR  col;
1022   }
1023  
1024  
1025 < putpfloat(col)                  /* put a float primary to stdout */
1026 < COLOR  col;
1025 > static int
1026 > putpfloat(                      /* put a float primary to stdout */
1027 >        COLOR  col
1028 > )
1029   {
1030          float  vf;
1031  
1032          vf = colval(col,putprim);
1033 <        fwrite((char *)&vf, sizeof(float), 1, stdout);
1033 >        if (swapbytes)
1034 >                swap32((char *)&vf, 1);
1035 >        putbinary(&vf, sizeof(float), 1, stdout);
1036  
1037          return(ferror(stdout) ? -1 : 0);
1038   }
1039  
1040  
1041 < putpdouble(col)                 /* put a double primary to stdout */
1042 < COLOR  col;
1041 > static int
1042 > putpdouble(                     /* put a double primary to stdout */
1043 >        COLOR  col
1044 > )
1045   {
1046          double  vd;
1047  
1048          vd = colval(col,putprim);
1049 <        fwrite((char *)&vd, sizeof(double), 1, stdout);
1049 >        if (swapbytes)
1050 >                swap64((char *)&vd, 1);
1051 >        putbinary(&vd, sizeof(double), 1, stdout);
1052  
1053          return(ferror(stdout) ? -1 : 0);
1054   }
1055  
1056  
1057 < putpint(col)                    /* put an int primary to stdout */
1058 < COLOR  col;
1057 > static int
1058 > putpint(                        /* put an int primary to stdout */
1059 >        COLOR  col
1060 > )
1061   {
1062          fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
1063  
# Line 921 | Line 1065 | COLOR  col;
1065   }
1066  
1067  
1068 < putpbyte(col)                   /* put a byte primary to stdout */
1069 < COLOR  col;
1068 > static int
1069 > putpbyte(                       /* put a byte primary to stdout */
1070 >        COLOR  col
1071 > )
1072   {
1073          long  i;
1074 <        BYTE  vb;
1074 >        uby8  vb;
1075  
1076          i = colval(col,putprim)*256.;
1077          vb = min(i,255);
1078 <        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
1078 >        putbinary(&vb, sizeof(uby8), 1, stdout);
1079  
1080          return(ferror(stdout) ? -1 : 0);
1081   }
1082  
1083  
1084 < putpword(col)                   /* put a 16-bit primary to stdout */
1085 < COLOR  col;
1084 > static int
1085 > putpword(                       /* put a 16-bit primary to stdout */
1086 >        COLOR  col
1087 > )
1088   {
1089          long  i;
1090          uint16  vw;
# Line 944 | Line 1092 | COLOR  col;
1092          i = colval(col,putprim)*65536.;
1093          vw = min(i,65535);
1094          if (swapbytes)
1095 <                swap16(&vw, 1);
1096 <        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1095 >                swap16((char *)&vw, 1);
1096 >        putbinary(&vw, sizeof(uint16), 1, stdout);
1097  
1098          return(ferror(stdout) ? -1 : 0);
1099   }
1100  
1101  
1102 < set_io()                        /* set put and get functions */
1102 > static void
1103 > set_io(void)                    /* set put and get functions */
1104   {
1105          switch (format) {
1106          case 'a':                                       /* ascii */
# Line 986 | Line 1135 | set_io()                       /* set put and get functions */
1135                                  if (fin2 == NULL)
1136                                          goto namerr;
1137                                  if (fseek(fin2,
1138 <                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1138 >                                (long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR))
1139                                          goto seekerr;
1140                                  if (fseek(fin3,
1141 <                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1141 >                                (long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR))
1142                                          goto seekerr;
1143                          }
1144                  }
# Line 1008 | Line 1157 | set_io()                       /* set put and get functions */
1157                                  if (fin2 == NULL)
1158                                          goto namerr;
1159                                  if (fseek(fin2,
1160 <                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1160 >                                (long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR))
1161                                          goto seekerr;
1162                                  if (fseek(fin3,
1163 <                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1163 >                                (long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR))
1164                                          goto seekerr;
1165                          }
1166                  }
# Line 1048 | Line 1197 | set_io()                       /* set put and get functions */
1197                                  if (fin2 == NULL)
1198                                          goto namerr;
1199                                  if (fseek(fin2,
1200 <                                (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
1200 >                                (long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR))
1201                                          goto seekerr;
1202                                  if (fseek(fin3,
1203 <                                (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1203 >                                (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR))
1204                                          goto seekerr;
1205                          }
1206                  }
# Line 1070 | Line 1219 | set_io()                       /* set put and get functions */
1219                                  if (fin2 == NULL)
1220                                          goto namerr;
1221                                  if (fseek(fin2,
1222 <                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1222 >                                (long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR))
1223                                          goto seekerr;
1224                                  if (fseek(fin3,
1225 <                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1225 >                                (long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR))
1226                                          goto seekerr;
1227                          }
1228                  }
1229                  return;
1230          }
1231 < badopt:
1231 > /* badopt: */ /* label not used */
1232          fprintf(stderr, "%s: botched file type\n", progname);
1233          quit(1);
1234   namerr:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines