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.10 by greg, Wed Jan 8 07:43:49 1997 UTC vs.
Revision 2.26 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  pvalue.c - program to print pixel values.
6   *
7   *     4/23/86
8   */
9  
10 + #include  "platform.h"
11   #include  "standard.h"
14
12   #include  "color.h"
16
13   #include  "resolu.h"
14 + #include  "view.h"
15  
16   #define  min(a,b)               ((a)<(b)?(a):(b))
17  
# Line 22 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19   #define  ALL            3
20   #define  BRIGHT         4
21  
25 #define  brightonly     (putprim==BRIGHT)
26
22   RESOLU  picres;                 /* resolution of picture */
28
23   int  uniq = 0;                  /* print only unique values? */
30
24   int  doexposure = 0;            /* exposure change? (>100 to print) */
32
25   int  dataonly = 0;              /* data only format? */
34
26   int  putprim = ALL;             /* what to put out */
36
27   int  reverse = 0;               /* reverse conversion? */
38
28   int  format = 'a';              /* input/output format */
29   char  *fmtid = "ascii";         /* format identifier for header */
41
30   int  header = 1;                /* do header? */
43
31   long  skipbytes = 0;            /* skip bytes in input? */
32 <
32 > int  swapbytes = 0;             /* swap bytes? */
33 > int  interleave = 1;            /* file is interleaved? */
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   int  ord[3] = {RED, GRN, BLU};  /* RGB ordering */
# Line 61 | Line 46 | char  *progname;
46   FILE  *fin;
47   FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
48  
64 int  (*getval)(), (*putval)();
49  
50 + typedef int getfunc_t(COLOR  col);
51 + typedef int putfunc_t(COLOR  col);
52 + typedef double brightfunc_t(COLOR  col);
53  
54 < main(argc, argv)
55 < int  argc;
56 < char  **argv;
54 > getfunc_t *getval;
55 > putfunc_t *putval;
56 > brightfunc_t *mybright;
57 >
58 > static gethfunc checkhead;
59 > static brightfunc_t rgb_bright, xyz_bright;
60 > static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword;
61 > static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword;
62 > static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword;
63 > static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword;
64 > static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword;
65 >
66 > static void set_io(void);
67 > static void pixtoval(void);
68 > static void valtopix(void);
69 > static void swap16(uint16  *wp, int  n);
70 > static void swap32(uint32  *wp, int  n);
71 > static void swap64(char  *wp, int  n);
72 >
73 >
74 > static double
75 > rgb_bright(
76 >        COLOR  clr
77 > )
78   {
79 <        extern int  checkhead();
80 <        extern long  atol();
79 >        return(bright(clr));
80 > }
81 >
82 > static double
83 > xyz_bright(
84 >        COLOR  clr
85 > )
86 > {
87 >        return(clr[CIEY]);
88 > }
89 >
90 > int
91 > main(
92 >        int  argc,
93 >        char  **argv
94 > )
95 > {
96          double  d, expval = 1.0;
97          int  i;
98  
99          progname = argv[0];
100 +        mybright = &rgb_bright; /* default */
101  
102          for (i = 1; i < argc; i++)
103                  if (argv[i][0] == '-' || argv[i][0] == '+')
# Line 119 | Line 143 | char  **argv;
143                          case 'r':               /* reverse conversion */
144                                  reverse = argv[i][0] == '-';
145                                  break;
146 +                        case 'n':               /* non-interleaved RGB */
147 +                                interleave = argv[i][0] == '+';
148 +                                break;
149                          case 'b':               /* brightness values */
150                                  putprim = argv[i][0] == '-' ? BRIGHT : ALL;
151                                  break;
# Line 147 | Line 174 | char  **argv;
174                                          format = 'b';
175                                          fmtid = "byte";
176                                          break;
177 +                                case 'W':               /* 16-bit swapped */
178 +                                        swapbytes = 1;
179 +                                case 'w':               /* 16-bit */
180 +                                        dataonly = 1;
181 +                                        format = 'w';
182 +                                        fmtid = "16-bit";
183 +                                        break;
184 +                                case 'F':               /* swapped floats */
185 +                                        swapbytes = 1;
186                                  case 'f':               /* float */
187                                          dataonly = 1;
188                                          format = 'f';
189                                          fmtid = "float";
190                                          break;
191 +                                case 'D':               /* swapped doubles */
192 +                                        swapbytes = 1;
193                                  case 'd':               /* double */
194                                          dataonly = 1;
195                                          format = 'd';
# Line 165 | Line 203 | char  **argv;
203                          case 'X':               /* x resolution */
204                                  resolution = 0;
205                                  if (argv[i][0] == '-')
206 <                                        picres.or |= XDECR;
206 >                                        picres.rt |= XDECR;
207                                  picres.xr = atoi(argv[++i]);
208                                  break;
209                          case 'y':               /* y resolution */
210                          case 'Y':               /* y resolution */
211                                  resolution = 0;
212                                  if (argv[i][0] == '-')
213 <                                        picres.or |= YDECR;
213 >                                        picres.rt |= YDECR;
214                                  if (picres.xr == 0)
215 <                                        picres.or |= YMAJOR;
215 >                                        picres.rt |= YMAJOR;
216                                  picres.yr = atoi(argv[++i]);
217                                  break;
218                          default:
# Line 187 | Line 225 | unkopt:
225                  else
226                          break;
227                                          /* recognize special formats */
228 <        if (dataonly && format == 'b')
229 <                if (brightonly)
228 >        if (dataonly && format == 'b') {
229 >                if (putprim == ALL)
230 >                        fmtid = "24-bit_rgb";
231 >                else
232                          fmtid = "8-bit_grey";
233 +        }
234 +        if (dataonly && format == 'w') {
235 +                if (putprim == ALL)
236 +                        fmtid = "48-bit_rgb";
237                  else
238 <                        fmtid = "24-bit_rgb";
238 >                        fmtid = "16-bit_grey";
239 >        }
240                                          /* assign reverse ordering */
241          rord[ord[0]] = 0;
242          rord[ord[1]] = 1;
# Line 205 | Line 250 | unkopt:
250                                                  progname, argv[i]);
251                          quit(1);
252                  }
253 <                if (skipbytes && fseek(fin, skipbytes, 0))
209 <                        goto seekerr;
210 <                if (reverse && !brightonly && i == argc-3) {
253 >                if (reverse && putprim != BRIGHT && i == argc-3) {
254                          if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
255                                  fprintf(stderr, "%s: can't open file \"%s\"\n",
256                                                  progname, argv[i+1]);
# Line 218 | Line 261 | unkopt:
261                                                  progname, argv[i+2]);
262                                  quit(1);
263                          }
264 <                        if (skipbytes && (fseek(fin2, skipbytes, 0) ||
222 <                                        fseek(fin3, skipbytes, 0)))
223 <                                goto seekerr;
264 >                        interleave = -1;
265                  } else if (i != argc-1)
266                          fin = NULL;
267 +                if (reverse && putprim != BRIGHT && !interleave) {
268 +                        fin2 = fopen(argv[i], "r");
269 +                        fin3 = fopen(argv[i], "r");
270 +                }
271 +                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
272 +                                (fseek(fin2, skipbytes, 0) ||
273 +                                fseek(fin3, skipbytes, 0))))) {
274 +                        fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
275 +                                        progname, skipbytes);
276 +                        quit(1);
277 +                }
278          }
279          if (fin == NULL) {
280                  fprintf(stderr, "%s: bad # file arguments\n", progname);
281                  quit(1);
282          }
283  
232        set_io();
233
284          if (reverse) {
285 < #ifdef MSDOS
286 <                setmode(fileno(stdout), O_BINARY);
285 > #ifdef _WIN32
286 >                SET_FILE_BINARY(stdout);
287                  if (format != 'a' && format != 'i')
288 <                        setmode(fileno(fin), O_BINARY);
288 >                        SET_FILE_BINARY(fin);
289   #endif
290                                          /* get header */
291                  if (header) {
# Line 259 | Line 309 | unkopt:
309                  if (resolution && fin2 != NULL) {
310                          RESOLU  pres2;
311                          if (!fgetsresolu(&pres2, fin2) ||
312 <                                        pres2.or != picres.or ||
312 >                                        pres2.rt != picres.rt ||
313                                          pres2.xr != picres.xr ||
314                                          pres2.yr != picres.yr ||
315                                          !fgetsresolu(&pres2, fin3) ||
316 <                                        pres2.or != picres.or ||
316 >                                        pres2.rt != picres.rt ||
317                                          pres2.xr != picres.xr ||
318                                          pres2.yr != picres.yr) {
319                                  fprintf(stderr, "%s: resolution mismatch\n",
# Line 280 | Line 330 | unkopt:
330                  fputsresolu(&picres, stdout);   /* always put resolution */
331                  valtopix();
332          } else {
333 < #ifdef MSDOS
334 <                setmode(fileno(fin), O_BINARY);
333 > #ifdef _WIN32
334 >                SET_FILE_BINARY(fin);
335                  if (format != 'a' && format != 'i')
336 <                        setmode(fileno(stdout), O_BINARY);
336 >                        SET_FILE_BINARY(stdout);
337   #endif
338                                                  /* get header */
339                  getheader(fin, checkhead, NULL);
340                  if (wrongformat) {
341 <                        fprintf(stderr, "%s: input not a Radiance picture\n",
341 >                        fprintf(stderr,
342 >                                "%s: input not a Radiance RGBE picture\n",
343                                          progname);
344                          quit(1);
345                  }
# Line 309 | Line 360 | unkopt:
360          }
361  
362          quit(0);
363 < seekerr:
313 <        fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
314 <                        progname, skipbytes);
315 <        quit(1);
363 >        return 0; /* pro forma return */
364   }
365  
366  
367 < checkhead(line)                         /* deal with line from header */
368 < char  *line;
367 > static int
368 > checkhead(                              /* deal with line from header */
369 >        char    *line,
370 >        void    *p
371 > )
372   {
373          char    fmt[32];
374          double  d;
375          COLOR   ctmp;
376  
377 <        if (formatval(fmt, line))
378 <                wrongformat = strcmp(fmt, COLRFMT);
379 <        else if (original && isexpos(line)) {
377 >        if (formatval(fmt, line)) {
378 >                if (!strcmp(fmt, CIEFMT)) {
379 >                        mybright = &xyz_bright;
380 >                        if (original) {
381 >                                scalecolor(exposure, 1./WHTEFFICACY);
382 >                                doexposure++;
383 >                        }
384 >                } else if (!strcmp(fmt, COLRFMT))
385 >                        mybright = &rgb_bright;
386 >                else
387 >                        wrongformat++;
388 >        } else if (original && isexpos(line)) {
389                  d = 1.0/exposval(line);
390                  scalecolor(exposure, d);
391                  doexposure++;
# Line 337 | Line 397 | char  *line;
397                  doexposure++;
398          } else if (header)
399                  fputs(line, stdout);
400 +        return(0);
401   }
402  
403  
404 < pixtoval()                              /* convert picture to values */
404 > static void
405 > pixtoval(void)                          /* convert picture to values */
406   {
407          register COLOR  *scanln;
408          int  dogamma;
409          COLOR  lastc;
410 <        FLOAT  hv[2];
410 >        RREAL  hv[2];
411 >        int  startprim, endprim;
412 >        long  startpos;
413          int  y;
414          register int  x;
415  
# Line 355 | Line 419 | pixtoval()                             /* convert picture to values */
419                  quit(1);
420          }
421          dogamma = gamcor < .95 || gamcor > 1.05;
422 <        setcolor(lastc, 0.0, 0.0, 0.0);
423 <        for (y = 0; y < numscans(&picres); y++) {
424 <                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
425 <                        fprintf(stderr, "%s: read error\n", progname);
422 >        if (putprim == ALL && !interleave) {
423 >                startprim = RED; endprim = BLU;
424 >                startpos = ftell(fin);
425 >        } else {
426 >                startprim = putprim; endprim = putprim;
427 >        }
428 >        for (putprim = startprim; putprim <= endprim; putprim++) {
429 >                if (putprim != startprim && fseek(fin, startpos, 0)) {
430 >                        fprintf(stderr, "%s: seek error on input file\n",
431 >                                        progname);
432                          quit(1);
433                  }
434 <                for (x = 0; x < scanlen(&picres); x++) {
435 <                        if (uniq)
436 <                                if (    colval(scanln[x],RED) ==
437 <                                                colval(lastc,RED) &&
438 <                                        colval(scanln[x],GRN) ==
369 <                                                colval(lastc,GRN) &&
370 <                                        colval(scanln[x],BLU) ==
371 <                                                colval(lastc,BLU)       )
372 <                                        continue;
373 <                                else
374 <                                        copycolor(lastc, scanln[x]);
375 <                        if (doexposure)
376 <                                multcolor(scanln[x], exposure);
377 <                        if (dogamma)
378 <                                setcolor(scanln[x],
379 <                                pow(colval(scanln[x],RED), 1.0/gamcor),
380 <                                pow(colval(scanln[x],GRN), 1.0/gamcor),
381 <                                pow(colval(scanln[x],BLU), 1.0/gamcor));
382 <                        if (!dataonly) {
383 <                                pix2loc(hv, &picres, x, y);
384 <                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
385 <                                                (int)(hv[1]*picres.yr));
386 <                        }
387 <                        if ((*putval)(scanln[x], stdout) < 0) {
388 <                                fprintf(stderr, "%s: write error\n", progname);
434 >                set_io();
435 >                setcolor(lastc, 0.0, 0.0, 0.0);
436 >                for (y = 0; y < numscans(&picres); y++) {
437 >                        if (freadscan(scanln, scanlen(&picres), fin) < 0) {
438 >                                fprintf(stderr, "%s: read error\n", progname);
439                                  quit(1);
440                          }
441 +                        for (x = 0; x < scanlen(&picres); x++) {
442 +                                if (uniq) {
443 +                                        if (    colval(scanln[x],RED) ==
444 +                                                        colval(lastc,RED) &&
445 +                                                colval(scanln[x],GRN) ==
446 +                                                        colval(lastc,GRN) &&
447 +                                                colval(scanln[x],BLU) ==
448 +                                                        colval(lastc,BLU)       )
449 +                                                continue;
450 +                                        else
451 +                                                copycolor(lastc, scanln[x]);
452 +                                }
453 +                                if (doexposure)
454 +                                        multcolor(scanln[x], exposure);
455 +                                if (dogamma)
456 +                                        setcolor(scanln[x],
457 +                                        pow(colval(scanln[x],RED), 1.0/gamcor),
458 +                                        pow(colval(scanln[x],GRN), 1.0/gamcor),
459 +                                        pow(colval(scanln[x],BLU), 1.0/gamcor));
460 +                                if (!dataonly) {
461 +                                        pix2loc(hv, &picres, x, y);
462 +                                        printf("%7d %7d ",
463 +                                                        (int)(hv[0]*picres.xr),
464 +                                                        (int)(hv[1]*picres.yr));
465 +                                }
466 +                                if ((*putval)(scanln[x]) < 0) {
467 +                                        fprintf(stderr, "%s: write error\n",
468 +                                                        progname);
469 +                                        quit(1);
470 +                                }
471 +                        }
472                  }
473          }
474 <        free((char *)scanln);
474 >        free((void *)scanln);
475   }
476  
477  
478 < valtopix()                      /* convert values to a pixel file */
478 > static void
479 > valtopix(void)                  /* convert values to a pixel file */
480   {
481          int  dogamma;
482          register COLOR  *scanln;
# Line 407 | Line 489 | valtopix()                     /* convert values to a pixel file */
489                  quit(1);
490          }
491          dogamma = gamcor < .95 || gamcor > 1.05;
492 +        set_io();
493          for (y = 0; y < numscans(&picres); y++) {
494                  for (x = 0; x < scanlen(&picres); x++) {
495                          if (!dataonly) {
# Line 416 | Line 499 | valtopix()                     /* convert values to a pixel file */
499                                          fscanf(fin3, "%*d %*d");
500                                  }
501                          }
502 <                        if ((*getval)(scanln[x], fin, fin2, fin3) < 0) {
502 >                        if ((*getval)(scanln[x]) < 0) {
503                                  fprintf(stderr, "%s: read error\n", progname);
504                                  quit(1);
505                          }
# Line 433 | Line 516 | valtopix()                     /* convert values to a pixel file */
516                          quit(1);
517                  }
518          }
519 <        free((char *)scanln);
519 >        free((void *)scanln);
520   }
521  
522  
523 + void
524   quit(code)
525   int  code;
526   {
# Line 444 | Line 528 | int  code;
528   }
529  
530  
531 < getcascii(col, f1, f2, f3)      /* get an ascii color value from stream(s) */
532 < COLOR  col;
533 < FILE  *f1, *f2, *f3;
531 > static void
532 > swap16(         /* swap n 16-bit words */
533 >        register uint16  *wp,
534 >        int  n
535 > )
536   {
537 +        while (n-- > 0) {
538 +                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
539 +                wp++;
540 +        }
541 + }
542 +
543 +
544 + static void
545 + swap32(         /* swap n 32-bit words */
546 +        register uint32  *wp,
547 +        int  n
548 + )
549 + {
550 +        while (n-- > 0) {
551 +                *wp = *wp << 24 | ((*wp >> 24) & 0xff) |
552 +                        (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8;
553 +                wp++;
554 +        }
555 + }
556 +
557 +
558 + static void
559 + swap64(         /* swap n 64-bit words */
560 +        register char  *wp,
561 +        int  n
562 + )
563 + {
564 +        register int    t;
565 +
566 +        while (n-- > 0) {
567 +                t = wp[0]; wp[0] = wp[7]; wp[7] = t;
568 +                t = wp[1]; wp[1] = wp[6]; wp[6] = t;
569 +                t = wp[2]; wp[2] = wp[5]; wp[5] = t;
570 +                t = wp[3]; wp[3] = wp[4]; wp[4] = t;
571 +                wp += 8;
572 +        }
573 + }
574 +
575 +
576 + static int
577 + getcascii(              /* get an ascii color value from stream(s) */
578 +        COLOR  col
579 + )
580 + {
581          double  vd[3];
582  
583 <        if (f2 == NULL) {
584 <                if (fscanf(f1, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
583 >        if (fin2 == NULL) {
584 >                if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
585                          return(-1);
586          } else {
587 <                if (fscanf(f1, "%lf", &vd[0]) != 1 ||
588 <                                fscanf(f2, "%lf", &vd[1]) != 1 ||
589 <                                fscanf(f3, "%lf", &vd[2]) != 1)
587 >                if (fscanf(fin, "%lf", &vd[0]) != 1 ||
588 >                                fscanf(fin2, "%lf", &vd[1]) != 1 ||
589 >                                fscanf(fin3, "%lf", &vd[2]) != 1)
590                          return(-1);
591          }
592          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
# Line 464 | Line 594 | FILE  *f1, *f2, *f3;
594   }
595  
596  
597 < getcdouble(col, f1, f2, f3)     /* get a double color value from stream(s) */
598 < COLOR  col;
599 < FILE  *f1, *f2, *f3;
597 > static int
598 > getcdouble(             /* get a double color value from stream(s) */
599 >        COLOR  col
600 > )
601   {
602          double  vd[3];
603  
604 <        if (f2 == NULL) {
605 <                if (fread((char *)vd, sizeof(double), 3, f1) != 3)
604 >        if (fin2 == NULL) {
605 >                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
606                          return(-1);
607          } else {
608 <                if (fread((char *)vd, sizeof(double), 1, f1) != 1 ||
609 <                        fread((char *)(vd+1), sizeof(double), 1, f2) != 1 ||
610 <                        fread((char *)(vd+2), sizeof(double), 1, f3) != 1)
608 >                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
609 >                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
610 >                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
611                          return(-1);
612          }
613 +        if (swapbytes)
614 +                swap64((char *)vd, 3);
615          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
616          return(0);
617   }
618  
619  
620 < getcfloat(col, f1, f2, f3)      /* get a float color value from stream(s) */
621 < COLOR  col;
622 < FILE  *f1, *f2, *f3;
620 > static int
621 > getcfloat(              /* get a float color value from stream(s) */
622 >        COLOR  col
623 > )
624   {
625          float  vf[3];
626  
627 <        if (f2 == NULL) {
628 <                if (fread((char *)vf, sizeof(float), 3, f1) != 3)
627 >        if (fin2 == NULL) {
628 >                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
629                          return(-1);
630          } else {
631 <                if (fread((char *)vf, sizeof(float), 1, f1) != 1 ||
632 <                        fread((char *)(vf+1), sizeof(float), 1, f2) != 1 ||
633 <                        fread((char *)(vf+2), sizeof(float), 1, f3) != 1)
631 >                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
632 >                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
633 >                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
634                          return(-1);
635          }
636 +        if (swapbytes)
637 +                swap32((uint32 *)vf, 3);
638          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
639          return(0);
640   }
641  
642  
643 < getcint(col, f1, f2, f3)        /* get an int color value from stream(s) */
644 < COLOR  col;
645 < FILE  *f1, *f2, *f3;
643 > static int
644 > getcint(                /* get an int color value from stream(s) */
645 >        COLOR  col
646 > )
647   {
648          int  vi[3];
649  
650 <        if (f2 == NULL) {
651 <                if (fscanf(f1, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
650 >        if (fin2 == NULL) {
651 >                if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
652                          return(-1);
653          } else {
654 <                if (fscanf(f1, "%d", &vi[0]) != 1 ||
655 <                                fscanf(f2, "%d", &vi[1]) != 1 ||
656 <                                fscanf(f3, "%d", &vi[2]) != 1)
654 >                if (fscanf(fin, "%d", &vi[0]) != 1 ||
655 >                                fscanf(fin2, "%d", &vi[1]) != 1 ||
656 >                                fscanf(fin3, "%d", &vi[2]) != 1)
657                          return(-1);
658          }
659          setcolor(col, (vi[rord[RED]]+.5)/256.,
# Line 525 | Line 662 | FILE  *f1, *f2, *f3;
662   }
663  
664  
665 < getcbyte(col, f1, f2, f3)       /* get a byte color value from stream(s) */
666 < COLOR  col;
667 < FILE  *f1, *f2, *f3;
665 > static int
666 > getcbyte(               /* get a byte color value from stream(s) */
667 >        COLOR  col
668 > )
669   {
670          BYTE  vb[3];
671  
672 <        if (f2 == NULL) {
673 <                if (fread((char *)vb, sizeof(BYTE), 3, f1) != 3)
672 >        if (fin2 == NULL) {
673 >                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
674                          return(-1);
675          } else {
676 <                if (fread((char *)vb, sizeof(BYTE), 1, f1) != 1 ||
677 <                        fread((char *)(vb+1), sizeof(BYTE), 1, f2) != 1 ||
678 <                        fread((char *)(vb+2), sizeof(BYTE), 1, f3) != 1)
676 >                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
677 >                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
678 >                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
679                          return(-1);
680          }
681          setcolor(col, (vb[rord[RED]]+.5)/256.,
# Line 546 | Line 684 | FILE  *f1, *f2, *f3;
684   }
685  
686  
687 < getbascii(col, fp)              /* get an ascii brightness value from fp */
688 < COLOR  col;
689 < FILE  *fp;
687 > static int
688 > getcword(               /* get a 16-bit color value from stream(s) */
689 >        COLOR  col
690 > )
691   {
692 +        uint16  vw[3];
693 +
694 +        if (fin2 == NULL) {
695 +                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
696 +                        return(-1);
697 +        } else {
698 +                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
699 +                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
700 +                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
701 +                        return(-1);
702 +        }
703 +        if (swapbytes)
704 +                swap16(vw, 3);
705 +        setcolor(col, (vw[rord[RED]]+.5)/65536.,
706 +                        (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
707 +        return(0);
708 + }
709 +
710 +
711 + static int
712 + getbascii(              /* get an ascii brightness value from fin */
713 +        COLOR  col
714 + )
715 + {
716          double  vd;
717  
718 <        if (fscanf(fp, "%lf", &vd) != 1)
718 >        if (fscanf(fin, "%lf", &vd) != 1)
719                  return(-1);
720          setcolor(col, vd, vd, vd);
721          return(0);
722   }
723  
724  
725 < getbdouble(col, fp)             /* get a double brightness value from fp */
726 < COLOR  col;
727 < FILE  *fp;
725 > static int
726 > getbdouble(             /* get a double brightness value from fin */
727 >        COLOR  col
728 > )
729   {
730          double  vd;
731  
732 <        if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
732 >        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
733                  return(-1);
734 +        if (swapbytes)
735 +                swap64((char *)&vd, 1);
736          setcolor(col, vd, vd, vd);
737          return(0);
738   }
739  
740  
741 < getbfloat(col, fp)              /* get a float brightness value from fp */
742 < COLOR  col;
743 < FILE  *fp;
741 > static int
742 > getbfloat(              /* get a float brightness value from fin */
743 >        COLOR  col
744 > )
745   {
746          float  vf;
747  
748 <        if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
748 >        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
749                  return(-1);
750 +        if (swapbytes)
751 +                swap32((uint32 *)&vf, 1);
752          setcolor(col, vf, vf, vf);
753          return(0);
754   }
755  
756  
757 < getbint(col, fp)                /* get an int brightness value from fp */
758 < COLOR  col;
759 < FILE  *fp;
757 > static int
758 > getbint(                /* get an int brightness value from fin */
759 >        COLOR  col
760 > )
761   {
762          int  vi;
763          double  d;
764  
765 <        if (fscanf(fp, "%d", &vi) != 1)
765 >        if (fscanf(fin, "%d", &vi) != 1)
766                  return(-1);
767          d = (vi+.5)/256.;
768          setcolor(col, d, d, d);
# Line 600 | Line 770 | FILE  *fp;
770   }
771  
772  
773 < getbbyte(col, fp)               /* get a byte brightness value from fp */
774 < COLOR  col;
775 < FILE  *fp;
773 > static int
774 > getbbyte(               /* get a byte brightness value from fin */
775 >        COLOR  col
776 > )
777   {
778          BYTE  vb;
779          double  d;
780  
781 <        if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
781 >        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
782                  return(-1);
783          d = (vb+.5)/256.;
784          setcolor(col, d, d, d);
# Line 615 | Line 786 | FILE  *fp;
786   }
787  
788  
789 < putcascii(col, fp)                      /* put an ascii color to fp */
790 < COLOR  col;
791 < FILE  *fp;
789 > static int
790 > getbword(               /* get a 16-bit brightness value from fin */
791 >        COLOR  col
792 > )
793   {
794 <        fprintf(fp, "%15.3e %15.3e %15.3e\n",
794 >        uint16  vw;
795 >        double  d;
796 >
797 >        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
798 >                return(-1);
799 >        if (swapbytes)
800 >                swap16(&vw, 1);
801 >        d = (vw+.5)/65536.;
802 >        setcolor(col, d, d, d);
803 >        return(0);
804 > }
805 >
806 >
807 > static int
808 > putcascii(                      /* put an ascii color to stdout */
809 >        COLOR  col
810 > )
811 > {
812 >        fprintf(stdout, "%15.3e %15.3e %15.3e\n",
813                          colval(col,ord[0]),
814                          colval(col,ord[1]),
815                          colval(col,ord[2]));
816  
817 <        return(ferror(fp) ? -1 : 0);
817 >        return(ferror(stdout) ? -1 : 0);
818   }
819  
820  
821 < putcfloat(col, fp)                      /* put a float color to fp */
822 < COLOR  col;
823 < FILE  *fp;
821 > static int
822 > putcfloat(                      /* put a float color to stdout */
823 >        COLOR  col
824 > )
825   {
826          float  vf[3];
827  
828          vf[0] = colval(col,ord[0]);
829          vf[1] = colval(col,ord[1]);
830          vf[2] = colval(col,ord[2]);
831 <        fwrite((char *)vf, sizeof(float), 3, fp);
831 >        if (swapbytes)
832 >                swap32((uint32 *)vf, 3);
833 >        fwrite((char *)vf, sizeof(float), 3, stdout);
834  
835 <        return(ferror(fp) ? -1 : 0);
835 >        return(ferror(stdout) ? -1 : 0);
836   }
837  
838  
839 < putcdouble(col, fp)                     /* put a double color to fp */
840 < COLOR  col;
841 < FILE  *fp;
839 > static int
840 > putcdouble(                     /* put a double color to stdout */
841 >        COLOR  col
842 > )
843   {
844          double  vd[3];
845  
846          vd[0] = colval(col,ord[0]);
847          vd[1] = colval(col,ord[1]);
848          vd[2] = colval(col,ord[2]);
849 <        fwrite((char *)vd, sizeof(double), 3, fp);
849 >        if (swapbytes)
850 >                swap64((char *)vd, 3);
851 >        fwrite((char *)vd, sizeof(double), 3, stdout);
852  
853 <        return(ferror(fp) ? -1 : 0);
853 >        return(ferror(stdout) ? -1 : 0);
854   }
855  
856  
857 < putcint(col, fp)                        /* put an int color to fp */
858 < COLOR  col;
859 < FILE  *fp;
857 > static int
858 > putcint(                        /* put an int color to stdout */
859 >        COLOR  col
860 > )
861   {
862 <        fprintf(fp, "%d %d %d\n",
862 >        fprintf(stdout, "%d %d %d\n",
863                          (int)(colval(col,ord[0])*256.),
864                          (int)(colval(col,ord[1])*256.),
865                          (int)(colval(col,ord[2])*256.));
866  
867 <        return(ferror(fp) ? -1 : 0);
867 >        return(ferror(stdout) ? -1 : 0);
868   }
869  
870  
871 < putcbyte(col, fp)                       /* put a byte color to fp */
872 < COLOR  col;
873 < FILE  *fp;
871 > static int
872 > putcbyte(                       /* put a byte color to stdout */
873 >        COLOR  col
874 > )
875   {
876 <        register int  i;
876 >        long  i;
877          BYTE  vb[3];
878  
879          i = colval(col,ord[0])*256.;
# Line 684 | Line 882 | FILE  *fp;
882          vb[1] = min(i,255);
883          i = colval(col,ord[2])*256.;
884          vb[2] = min(i,255);
885 <        fwrite((char *)vb, sizeof(BYTE), 3, fp);
885 >        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
886  
887 <        return(ferror(fp) ? -1 : 0);
887 >        return(ferror(stdout) ? -1 : 0);
888   }
889  
890  
891 < putbascii(col, fp)                      /* put an ascii brightness to fp */
892 < COLOR  col;
893 < FILE  *fp;
891 > static int
892 > putcword(                       /* put a 16-bit color to stdout */
893 >        COLOR  col
894 > )
895   {
896 <        fprintf(fp, "%15.3e\n", bright(col));
896 >        long  i;
897 >        uint16  vw[3];
898  
899 <        return(ferror(fp) ? -1 : 0);
899 >        i = colval(col,ord[0])*65536.;
900 >        vw[0] = min(i,65535);
901 >        i = colval(col,ord[1])*65536.;
902 >        vw[1] = min(i,65535);
903 >        i = colval(col,ord[2])*65536.;
904 >        vw[2] = min(i,65535);
905 >        if (swapbytes)
906 >                swap16(vw, 3);
907 >        fwrite((char *)vw, sizeof(uint16), 3, stdout);
908 >
909 >        return(ferror(stdout) ? -1 : 0);
910   }
911  
912  
913 < putbfloat(col, fp)                      /* put a float brightness to fp */
914 < COLOR  col;
915 < FILE  *fp;
913 > static int
914 > putbascii(                      /* put an ascii brightness to stdout */
915 >        COLOR  col
916 > )
917   {
918 +        fprintf(stdout, "%15.3e\n", (*mybright)(col));
919 +
920 +        return(ferror(stdout) ? -1 : 0);
921 + }
922 +
923 +
924 + static int
925 + putbfloat(                      /* put a float brightness to stdout */
926 +        COLOR  col
927 + )
928 + {
929          float  vf;
930  
931 <        vf = bright(col);
932 <        fwrite((char *)&vf, sizeof(float), 1, fp);
931 >        vf = (*mybright)(col);
932 >        if (swapbytes)
933 >                swap32((uint32 *)&vf, 1);
934 >        fwrite((char *)&vf, sizeof(float), 1, stdout);
935  
936 <        return(ferror(fp) ? -1 : 0);
936 >        return(ferror(stdout) ? -1 : 0);
937   }
938  
939  
940 < putbdouble(col, fp)                     /* put a double brightness to fp */
941 < COLOR  col;
942 < FILE  *fp;
940 > static int
941 > putbdouble(                     /* put a double brightness to stdout */
942 >        COLOR  col
943 > )
944   {
945          double  vd;
946  
947 <        vd = bright(col);
948 <        fwrite((char *)&vd, sizeof(double), 1, fp);
947 >        vd = (*mybright)(col);
948 >        if (swapbytes)
949 >                swap64((char *)&vd, 1);
950 >        fwrite((char *)&vd, sizeof(double), 1, stdout);
951  
952 <        return(ferror(fp) ? -1 : 0);
952 >        return(ferror(stdout) ? -1 : 0);
953   }
954  
955  
956 < putbint(col, fp)                        /* put an int brightness to fp */
957 < COLOR  col;
958 < FILE  *fp;
956 > static int
957 > putbint(                        /* put an int brightness to stdout */
958 >        COLOR  col
959 > )
960   {
961 <        fprintf(fp, "%d\n", (int)(bright(col)*256.));
961 >        fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
962  
963 <        return(ferror(fp) ? -1 : 0);
963 >        return(ferror(stdout) ? -1 : 0);
964   }
965  
966  
967 < putbbyte(col, fp)                       /* put a byte brightness to fp */
968 < COLOR  col;
969 < FILE  *fp;
967 > static int
968 > putbbyte(                       /* put a byte brightness to stdout */
969 >        COLOR  col
970 > )
971   {
972          register int  i;
973          BYTE  vb;
974  
975 <        i = bright(col)*256.;
975 >        i = (*mybright)(col)*256.;
976          vb = min(i,255);
977 <        fwrite((char *)&vb, sizeof(BYTE), 1, fp);
977 >        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
978  
979 <        return(ferror(fp) ? -1 : 0);
979 >        return(ferror(stdout) ? -1 : 0);
980   }
981  
982  
983 < putpascii(col, fp)                      /* put an ascii primary to fp */
984 < COLOR  col;
985 < FILE  *fp;
983 > static int
984 > putbword(                       /* put a 16-bit brightness to stdout */
985 >        COLOR  col
986 > )
987   {
988 <        fprintf(fp, "%15.3e\n", colval(col,putprim));
988 >        long  i;
989 >        uint16  vw;
990  
991 <        return(ferror(fp) ? -1 : 0);
991 >        i = (*mybright)(col)*65536.;
992 >        vw = min(i,65535);
993 >        if (swapbytes)
994 >                swap16(&vw, 1);
995 >        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
996 >
997 >        return(ferror(stdout) ? -1 : 0);
998   }
999  
1000  
1001 < putpfloat(col, fp)                      /* put a float primary to fp */
1002 < COLOR  col;
1003 < FILE  *fp;
1001 > static int
1002 > putpascii(                      /* put an ascii primary to stdout */
1003 >        COLOR  col
1004 > )
1005   {
1006 +        fprintf(stdout, "%15.3e\n", colval(col,putprim));
1007 +
1008 +        return(ferror(stdout) ? -1 : 0);
1009 + }
1010 +
1011 +
1012 + static int
1013 + putpfloat(                      /* put a float primary to stdout */
1014 +        COLOR  col
1015 + )
1016 + {
1017          float  vf;
1018  
1019          vf = colval(col,putprim);
1020 <        fwrite((char *)&vf, sizeof(float), 1, fp);
1020 >        if (swapbytes)
1021 >                swap32((uint32 *)&vf, 1);
1022 >        fwrite((char *)&vf, sizeof(float), 1, stdout);
1023  
1024 <        return(ferror(fp) ? -1 : 0);
1024 >        return(ferror(stdout) ? -1 : 0);
1025   }
1026  
1027  
1028 < putpdouble(col, fp)                     /* put a double primary to fp */
1029 < COLOR  col;
1030 < FILE  *fp;
1028 > static int
1029 > putpdouble(                     /* put a double primary to stdout */
1030 >        COLOR  col
1031 > )
1032   {
1033          double  vd;
1034  
1035          vd = colval(col,putprim);
1036 <        fwrite((char *)&vd, sizeof(double), 1, fp);
1036 >        if (swapbytes)
1037 >                swap64((char *)&vd, 1);
1038 >        fwrite((char *)&vd, sizeof(double), 1, stdout);
1039  
1040 <        return(ferror(fp) ? -1 : 0);
1040 >        return(ferror(stdout) ? -1 : 0);
1041   }
1042  
1043  
1044 < putpint(col, fp)                        /* put an int primary to fp */
1045 < COLOR  col;
1046 < FILE  *fp;
1044 > static int
1045 > putpint(                        /* put an int primary to stdout */
1046 >        COLOR  col
1047 > )
1048   {
1049 <        fprintf(fp, "%d\n", (int)(colval(col,putprim)*256.));
1049 >        fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
1050  
1051 <        return(ferror(fp) ? -1 : 0);
1051 >        return(ferror(stdout) ? -1 : 0);
1052   }
1053  
1054  
1055 < putpbyte(col, fp)                       /* put a byte primary to fp */
1056 < COLOR  col;
1057 < FILE  *fp;
1055 > static int
1056 > putpbyte(                       /* put a byte primary to stdout */
1057 >        COLOR  col
1058 > )
1059   {
1060 <        register int  i;
1060 >        long  i;
1061          BYTE  vb;
1062  
1063          i = colval(col,putprim)*256.;
1064          vb = min(i,255);
1065 <        fwrite((char *)&vb, sizeof(BYTE), 1, fp);
1065 >        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
1066  
1067 <        return(ferror(fp) ? -1 : 0);
1067 >        return(ferror(stdout) ? -1 : 0);
1068   }
1069  
1070  
1071 < set_io()                        /* set put and get functions */
1071 > static int
1072 > putpword(                       /* put a 16-bit primary to stdout */
1073 >        COLOR  col
1074 > )
1075   {
1076 +        long  i;
1077 +        uint16  vw;
1078 +
1079 +        i = colval(col,putprim)*65536.;
1080 +        vw = min(i,65535);
1081 +        if (swapbytes)
1082 +                swap16(&vw, 1);
1083 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1084 +
1085 +        return(ferror(stdout) ? -1 : 0);
1086 + }
1087 +
1088 +
1089 + static void
1090 + set_io(void)                    /* set put and get functions */
1091 + {
1092          switch (format) {
1093          case 'a':                                       /* ascii */
1094                  if (putprim == BRIGHT) {
# Line 825 | Line 1100 | set_io()                       /* set put and get functions */
1100                  } else {
1101                          getval = getcascii;
1102                          putval = putcascii;
1103 +                        if (reverse && !interleave) {
1104 +                                fprintf(stderr,
1105 +                                "%s: ASCII input files must be interleaved\n",
1106 +                                                progname);
1107 +                                quit(1);
1108 +                        }
1109                  }
1110                  return;
1111          case 'f':                                       /* binary float */
# Line 837 | Line 1118 | set_io()                       /* set put and get functions */
1118                  } else {
1119                          getval = getcfloat;
1120                          putval = putcfloat;
1121 +                        if (reverse && !interleave) {
1122 +                                if (fin2 == NULL)
1123 +                                        goto namerr;
1124 +                                if (fseek(fin2,
1125 +                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1126 +                                        goto seekerr;
1127 +                                if (fseek(fin3,
1128 +                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1129 +                                        goto seekerr;
1130 +                        }
1131                  }
1132                  return;
1133          case 'd':                                       /* binary double */
# Line 849 | Line 1140 | set_io()                       /* set put and get functions */
1140                  } else {
1141                          getval = getcdouble;
1142                          putval = putcdouble;
1143 +                        if (reverse && !interleave) {
1144 +                                if (fin2 == NULL)
1145 +                                        goto namerr;
1146 +                                if (fseek(fin2,
1147 +                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1148 +                                        goto seekerr;
1149 +                                if (fseek(fin3,
1150 +                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1151 +                                        goto seekerr;
1152 +                        }
1153                  }
1154                  return;
1155          case 'i':                                       /* integer */
# Line 861 | Line 1162 | set_io()                       /* set put and get functions */
1162                  } else {
1163                          getval = getcint;
1164                          putval = putcint;
1165 +                        if (reverse && !interleave) {
1166 +                                fprintf(stderr,
1167 +                                "%s: integer input files must be interleaved\n",
1168 +                                                progname);
1169 +                                quit(1);
1170 +                        }
1171                  }
1172                  return;
1173          case 'b':                                       /* byte */
# Line 873 | Line 1180 | set_io()                       /* set put and get functions */
1180                  } else {
1181                          getval = getcbyte;
1182                          putval = putcbyte;
1183 +                        if (reverse && !interleave) {
1184 +                                if (fin2 == NULL)
1185 +                                        goto namerr;
1186 +                                if (fseek(fin2,
1187 +                                (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
1188 +                                        goto seekerr;
1189 +                                if (fseek(fin3,
1190 +                                (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1191 +                                        goto seekerr;
1192 +                        }
1193                  }
1194                  return;
1195 +        case 'w':                                       /* 16-bit */
1196 +                if (putprim == BRIGHT) {
1197 +                        getval = getbword;
1198 +                        putval = putbword;
1199 +                } else if (putprim != ALL) {
1200 +                        getval = getbword;
1201 +                        putval = putpword;
1202 +                } else {
1203 +                        getval = getcword;
1204 +                        putval = putcword;
1205 +                        if (reverse && !interleave) {
1206 +                                if (fin2 == NULL)
1207 +                                        goto namerr;
1208 +                                if (fseek(fin2,
1209 +                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1210 +                                        goto seekerr;
1211 +                                if (fseek(fin3,
1212 +                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1213 +                                        goto seekerr;
1214 +                        }
1215 +                }
1216 +                return;
1217          }
1218 + /* badopt: */ /* label not used */
1219 +        fprintf(stderr, "%s: botched file type\n", progname);
1220 +        quit(1);
1221 + namerr:
1222 +        fprintf(stderr, "%s: non-interleaved file(s) must be named\n",
1223 +                        progname);
1224 +        quit(1);
1225 + seekerr:
1226 +        fprintf(stderr, "%s: cannot seek on interleaved input file\n",
1227 +                        progname);
1228 +        quit(1);
1229   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines