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.8 by greg, Sun Feb 27 10:16:48 1994 UTC vs.
Revision 2.24 by greg, Wed Oct 22 02:06:35 2003 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  
15   #define  min(a,b)               ((a)<(b)?(a):(b))
16  
17 +                                /* what to put out (also RED, GRN, BLU) */
18 + #define  ALL            3
19 + #define  BRIGHT         4
20 +
21   RESOLU  picres;                 /* resolution of picture */
22  
23   int  uniq = 0;                  /* print only unique values? */
# Line 26 | Line 26 | int  doexposure = 0;           /* exposure change? (>100 to pri
26  
27   int  dataonly = 0;              /* data only format? */
28  
29 < int  brightonly = 0;            /* only brightness values? */
29 > int  putprim = ALL;             /* what to put out */
30  
31   int  reverse = 0;               /* reverse conversion? */
32  
# Line 35 | Line 35 | char  *fmtid = "ascii";                /* format identifier for head
35  
36   int  header = 1;                /* do header? */
37  
38 + long  skipbytes = 0;            /* skip bytes in input? */
39 +
40 + int  swapbytes = 0;             /* swap bytes? */
41 +
42 + int  interleave = 1;            /* file is interleaved? */
43 +
44   int  resolution = 1;            /* put/get resolution string? */
45  
46   int  original = 0;              /* convert to original values? */
# Line 51 | Line 57 | COLOR  exposure = WHTCOLOR;
57   char  *progname;
58  
59   FILE  *fin;
60 + FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
61  
62   int  (*getval)(), (*putval)();
63  
64 + double
65 + rgb_bright(clr)
66 + COLOR  clr;
67 + {
68 +        return(bright(clr));
69 + }
70  
71 + double
72 + xyz_bright(clr)
73 + COLOR  clr;
74 + {
75 +        return(clr[CIEY]);
76 + }
77 +
78 + double  (*mybright)() = &rgb_bright;
79 +
80 +
81   main(argc, argv)
82   int  argc;
83   char  **argv;
84   {
85          extern int  checkhead();
86 +        extern long  atol();
87          double  d, expval = 1.0;
88          int  i;
89  
# Line 74 | Line 98 | char  **argv;
98                          case 'H':               /* resolution string */
99                                  resolution = argv[i][0] == '+';
100                                  break;
101 +                        case 's':               /* skip bytes in header */
102 +                                skipbytes = atol(argv[++i]);
103 +                                break;
104                          case 'u':               /* unique values */
105                                  uniq = argv[i][0] == '-';
106                                  break;
# Line 91 | Line 118 | char  **argv;
118                                  if (argv[i+1][0] == '-' || argv[i+1][0] == '+')
119                                          d = pow(2.0, d);
120                                  if (argv[i][0] == '-')
121 <                                        doexposure = 100;
121 >                                        expval *= d;
122                                  scalecolor(exposure, d);
96                                expval *= d;
123                                  doexposure++;
124                                  i++;
125                                  break;
# Line 107 | Line 133 | char  **argv;
133                          case 'r':               /* reverse conversion */
134                                  reverse = argv[i][0] == '-';
135                                  break;
136 +                        case 'n':               /* non-interleaved RGB */
137 +                                interleave = argv[i][0] == '+';
138 +                                break;
139                          case 'b':               /* brightness values */
140 <                                brightonly = argv[i][0] == '-';
140 >                                putprim = argv[i][0] == '-' ? BRIGHT : ALL;
141                                  break;
142 +                        case 'p':               /* put primary */
143 +                                switch (argv[i][2]) {
144 +                                case 'r': case 'R': putprim = RED; break;
145 +                                case 'g': case 'G': putprim = GRN; break;
146 +                                case 'b': case 'B': putprim = BLU; break;
147 +                                default: goto unkopt;
148 +                                }
149 +                                break;
150                          case 'd':               /* data only (no indices) */
151                                  dataonly = argv[i][0] == '-';
152                                  switch (argv[i][2]) {
# Line 127 | Line 164 | char  **argv;
164                                          format = 'b';
165                                          fmtid = "byte";
166                                          break;
167 +                                case 'W':               /* 16-bit swapped */
168 +                                        swapbytes = 1;
169 +                                case 'w':               /* 16-bit */
170 +                                        dataonly = 1;
171 +                                        format = 'w';
172 +                                        fmtid = "16-bit";
173 +                                        break;
174 +                                case 'F':               /* swapped floats */
175 +                                        swapbytes = 1;
176                                  case 'f':               /* float */
177                                          dataonly = 1;
178                                          format = 'f';
179                                          fmtid = "float";
180                                          break;
181 +                                case 'D':               /* swapped doubles */
182 +                                        swapbytes = 1;
183                                  case 'd':               /* double */
184                                          dataonly = 1;
185                                          format = 'd';
# Line 145 | Line 193 | char  **argv;
193                          case 'X':               /* x resolution */
194                                  resolution = 0;
195                                  if (argv[i][0] == '-')
196 <                                        picres.or |= XDECR;
196 >                                        picres.rt |= XDECR;
197                                  picres.xr = atoi(argv[++i]);
198                                  break;
199                          case 'y':               /* y resolution */
200                          case 'Y':               /* y resolution */
201                                  resolution = 0;
202                                  if (argv[i][0] == '-')
203 <                                        picres.or |= YDECR;
203 >                                        picres.rt |= YDECR;
204                                  if (picres.xr == 0)
205 <                                        picres.or |= YMAJOR;
205 >                                        picres.rt |= YMAJOR;
206                                  picres.yr = atoi(argv[++i]);
207                                  break;
208                          default:
# Line 167 | Line 215 | unkopt:
215                  else
216                          break;
217                                          /* recognize special formats */
218 <        if (dataonly && format == 'b')
219 <                if (brightonly)
218 >        if (dataonly && format == 'b') {
219 >                if (putprim == ALL)
220 >                        fmtid = "24-bit_rgb";
221 >                else
222                          fmtid = "8-bit_grey";
223 +        }
224 +        if (dataonly && format == 'w') {
225 +                if (putprim == ALL)
226 +                        fmtid = "48-bit_rgb";
227                  else
228 <                        fmtid = "24-bit_rgb";
228 >                        fmtid = "16-bit_grey";
229 >        }
230                                          /* assign reverse ordering */
231          rord[ord[0]] = 0;
232          rord[ord[1]] = 1;
# Line 179 | Line 234 | unkopt:
234                                          /* get input */
235          if (i == argc) {
236                  fin = stdin;
237 <        } else if (i == argc-1) {
237 >        } else if (i < argc) {
238                  if ((fin = fopen(argv[i], "r")) == NULL) {
239                          fprintf(stderr, "%s: can't open file \"%s\"\n",
240                                                  progname, argv[i]);
241                          quit(1);
242                  }
243 <        } else {
243 >                if (reverse && putprim != BRIGHT && i == argc-3) {
244 >                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
245 >                                fprintf(stderr, "%s: can't open file \"%s\"\n",
246 >                                                progname, argv[i+1]);
247 >                                quit(1);
248 >                        }
249 >                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
250 >                                fprintf(stderr, "%s: can't open file \"%s\"\n",
251 >                                                progname, argv[i+2]);
252 >                                quit(1);
253 >                        }
254 >                        interleave = -1;
255 >                } else if (i != argc-1)
256 >                        fin = NULL;
257 >                if (reverse && putprim != BRIGHT && !interleave) {
258 >                        fin2 = fopen(argv[i], "r");
259 >                        fin3 = fopen(argv[i], "r");
260 >                }
261 >                if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL &&
262 >                                (fseek(fin2, skipbytes, 0) ||
263 >                                fseek(fin3, skipbytes, 0))))) {
264 >                        fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
265 >                                        progname, skipbytes);
266 >                        quit(1);
267 >                }
268 >        }
269 >        if (fin == NULL) {
270                  fprintf(stderr, "%s: bad # file arguments\n", progname);
271                  quit(1);
272          }
273  
193        set_io();
194
274          if (reverse) {
275 < #ifdef MSDOS
276 <                setmode(fileno(stdout), O_BINARY);
275 > #ifdef _WIN32
276 >                SET_FILE_BINARY(stdout);
277                  if (format != 'a' && format != 'i')
278 <                        setmode(fileno(fin), O_BINARY);
278 >                        SET_FILE_BINARY(fin);
279   #endif
280                                          /* get header */
281                  if (header) {
# Line 205 | Line 284 | unkopt:
284                                                  progname);
285                                  quit(1);
286                          }
287 +                        if (fin2 != NULL) {
288 +                                getheader(fin2, NULL, NULL);
289 +                                getheader(fin3, NULL, NULL);
290 +                        }
291                  } else
292                          newheader("RADIANCE", stdout);
293                                          /* get resolution */
# Line 213 | Line 296 | unkopt:
296                          fprintf(stderr, "%s: missing resolution\n", progname);
297                          quit(1);
298                  }
299 +                if (resolution && fin2 != NULL) {
300 +                        RESOLU  pres2;
301 +                        if (!fgetsresolu(&pres2, fin2) ||
302 +                                        pres2.rt != picres.rt ||
303 +                                        pres2.xr != picres.xr ||
304 +                                        pres2.yr != picres.yr ||
305 +                                        !fgetsresolu(&pres2, fin3) ||
306 +                                        pres2.rt != picres.rt ||
307 +                                        pres2.xr != picres.xr ||
308 +                                        pres2.yr != picres.yr) {
309 +                                fprintf(stderr, "%s: resolution mismatch\n",
310 +                                                progname);
311 +                                quit(1);
312 +                        }
313 +                }
314                                                  /* add to header */
315                  printargs(i, argv, stdout);
316 <                if (doexposure > 100)
316 >                if (expval < .99 || expval > 1.01)
317                          fputexpos(expval, stdout);
318                  fputformat(COLRFMT, stdout);
319                  putchar('\n');
320                  fputsresolu(&picres, stdout);   /* always put resolution */
321                  valtopix();
322          } else {
323 < #ifdef MSDOS
324 <                setmode(fileno(fin), O_BINARY);
323 > #ifdef _WIN32
324 >                SET_FILE_BINARY(fin);
325                  if (format != 'a' && format != 'i')
326 <                        setmode(fileno(stdout), O_BINARY);
326 >                        SET_FILE_BINARY(stdout);
327   #endif
328                                                  /* get header */
329                  getheader(fin, checkhead, NULL);
330                  if (wrongformat) {
331 <                        fprintf(stderr, "%s: input not a Radiance picture\n",
331 >                        fprintf(stderr,
332 >                                "%s: input not a Radiance RGBE picture\n",
333                                          progname);
334                          quit(1);
335                  }
# Line 240 | Line 339 | unkopt:
339                  }
340                  if (header) {
341                          printargs(i, argv, stdout);
342 <                        if (doexposure > 100)
342 >                        if (expval < .99 || expval > 1.01)
343                                  fputexpos(expval, stdout);
344                          fputformat(fmtid, stdout);
345                          putchar('\n');
# Line 254 | Line 353 | unkopt:
353   }
354  
355  
356 + int
357   checkhead(line)                         /* deal with line from header */
358   char  *line;
359   {
# Line 261 | Line 361 | char  *line;
361          double  d;
362          COLOR   ctmp;
363  
364 <        if (formatval(fmt, line))
365 <                wrongformat = strcmp(fmt, COLRFMT);
366 <        else if (original && isexpos(line)) {
364 >        if (formatval(fmt, line)) {
365 >                if (!strcmp(fmt, CIEFMT)) {
366 >                        mybright = &xyz_bright;
367 >                        if (original) {
368 >                                scalecolor(exposure, 1./WHTEFFICACY);
369 >                                doexposure++;
370 >                        }
371 >                } else if (!strcmp(fmt, COLRFMT))
372 >                        mybright = &rgb_bright;
373 >                else
374 >                        wrongformat++;
375 >        } else if (original && isexpos(line)) {
376                  d = 1.0/exposval(line);
377                  scalecolor(exposure, d);
378                  doexposure++;
# Line 275 | Line 384 | char  *line;
384                  doexposure++;
385          } else if (header)
386                  fputs(line, stdout);
387 +        return(0);
388   }
389  
390  
# Line 283 | Line 393 | pixtoval()                             /* convert picture to values */
393          register COLOR  *scanln;
394          int  dogamma;
395          COLOR  lastc;
396 <        FLOAT  hv[2];
396 >        RREAL  hv[2];
397 >        int  startprim, endprim;
398 >        long  startpos;
399          int  y;
400          register int  x;
401  
# Line 293 | Line 405 | pixtoval()                             /* convert picture to values */
405                  quit(1);
406          }
407          dogamma = gamcor < .95 || gamcor > 1.05;
408 <        setcolor(lastc, 0.0, 0.0, 0.0);
409 <        for (y = 0; y < numscans(&picres); y++) {
410 <                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
411 <                        fprintf(stderr, "%s: read error\n", progname);
408 >        if (putprim == ALL && !interleave) {
409 >                startprim = RED; endprim = BLU;
410 >                startpos = ftell(fin);
411 >        } else {
412 >                startprim = putprim; endprim = putprim;
413 >        }
414 >        for (putprim = startprim; putprim <= endprim; putprim++) {
415 >                if (putprim != startprim && fseek(fin, startpos, 0)) {
416 >                        fprintf(stderr, "%s: seek error on input file\n",
417 >                                        progname);
418                          quit(1);
419                  }
420 <                for (x = 0; x < scanlen(&picres); x++) {
421 <                        if (uniq)
422 <                                if (    colval(scanln[x],RED) ==
423 <                                                colval(lastc,RED) &&
424 <                                        colval(scanln[x],GRN) ==
307 <                                                colval(lastc,GRN) &&
308 <                                        colval(scanln[x],BLU) ==
309 <                                                colval(lastc,BLU)       )
310 <                                        continue;
311 <                                else
312 <                                        copycolor(lastc, scanln[x]);
313 <                        if (doexposure)
314 <                                multcolor(scanln[x], exposure);
315 <                        if (dogamma)
316 <                                setcolor(scanln[x],
317 <                                pow(colval(scanln[x],RED), 1.0/gamcor),
318 <                                pow(colval(scanln[x],GRN), 1.0/gamcor),
319 <                                pow(colval(scanln[x],BLU), 1.0/gamcor));
320 <                        if (!dataonly) {
321 <                                pix2loc(hv, &picres, x, y);
322 <                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
323 <                                                (int)(hv[1]*picres.yr));
324 <                        }
325 <                        if ((*putval)(scanln[x], stdout) < 0) {
326 <                                fprintf(stderr, "%s: write error\n", progname);
420 >                set_io();
421 >                setcolor(lastc, 0.0, 0.0, 0.0);
422 >                for (y = 0; y < numscans(&picres); y++) {
423 >                        if (freadscan(scanln, scanlen(&picres), fin) < 0) {
424 >                                fprintf(stderr, "%s: read error\n", progname);
425                                  quit(1);
426                          }
427 +                        for (x = 0; x < scanlen(&picres); x++) {
428 +                                if (uniq) {
429 +                                        if (    colval(scanln[x],RED) ==
430 +                                                        colval(lastc,RED) &&
431 +                                                colval(scanln[x],GRN) ==
432 +                                                        colval(lastc,GRN) &&
433 +                                                colval(scanln[x],BLU) ==
434 +                                                        colval(lastc,BLU)       )
435 +                                                continue;
436 +                                        else
437 +                                                copycolor(lastc, scanln[x]);
438 +                                }
439 +                                if (doexposure)
440 +                                        multcolor(scanln[x], exposure);
441 +                                if (dogamma)
442 +                                        setcolor(scanln[x],
443 +                                        pow(colval(scanln[x],RED), 1.0/gamcor),
444 +                                        pow(colval(scanln[x],GRN), 1.0/gamcor),
445 +                                        pow(colval(scanln[x],BLU), 1.0/gamcor));
446 +                                if (!dataonly) {
447 +                                        pix2loc(hv, &picres, x, y);
448 +                                        printf("%7d %7d ",
449 +                                                        (int)(hv[0]*picres.xr),
450 +                                                        (int)(hv[1]*picres.yr));
451 +                                }
452 +                                if ((*putval)(scanln[x]) < 0) {
453 +                                        fprintf(stderr, "%s: write error\n",
454 +                                                        progname);
455 +                                        quit(1);
456 +                                }
457 +                        }
458                  }
459          }
460 <        free((char *)scanln);
460 >        free((void *)scanln);
461   }
462  
463  
# Line 345 | Line 474 | valtopix()                     /* convert values to a pixel file */
474                  quit(1);
475          }
476          dogamma = gamcor < .95 || gamcor > 1.05;
477 +        set_io();
478          for (y = 0; y < numscans(&picres); y++) {
479                  for (x = 0; x < scanlen(&picres); x++) {
480 <                        if (!dataonly)
480 >                        if (!dataonly) {
481                                  fscanf(fin, "%*d %*d");
482 <                        if ((*getval)(scanln[x], fin) < 0) {
482 >                                if (fin2 != NULL) {
483 >                                        fscanf(fin2, "%*d %*d");
484 >                                        fscanf(fin3, "%*d %*d");
485 >                                }
486 >                        }
487 >                        if ((*getval)(scanln[x]) < 0) {
488                                  fprintf(stderr, "%s: read error\n", progname);
489                                  quit(1);
490                          }
# Line 366 | Line 501 | valtopix()                     /* convert values to a pixel file */
501                          quit(1);
502                  }
503          }
504 <        free((char *)scanln);
504 >        free((void *)scanln);
505   }
506  
507  
508 + void
509   quit(code)
510   int  code;
511   {
# Line 377 | Line 513 | int  code;
513   }
514  
515  
516 < getcascii(col, fp)              /* get an ascii color value from fp */
516 > swap16(wp, n)           /* swap n 16-bit words */
517 > register uint16  *wp;
518 > int  n;
519 > {
520 >        while (n-- > 0) {
521 >                *wp = *wp << 8 | ((*wp >> 8) & 0xff);
522 >                wp++;
523 >        }
524 > }
525 >
526 >
527 > swap32(wp, n)           /* swap n 32-bit words */
528 > register uint32  *wp;
529 > int  n;
530 > {
531 >        while (n-- > 0) {
532 >                *wp = *wp << 24 | ((*wp >> 24) & 0xff) |
533 >                        (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8;
534 >                wp++;
535 >        }
536 > }
537 >
538 >
539 > swap64(wp, n)           /* swap n 64-bit words */
540 > register char  *wp;
541 > int  n;
542 > {
543 >        register int    t;
544 >
545 >        while (n-- > 0) {
546 >                t = wp[0]; wp[0] = wp[7]; wp[7] = t;
547 >                t = wp[1]; wp[1] = wp[6]; wp[6] = t;
548 >                t = wp[2]; wp[2] = wp[5]; wp[5] = t;
549 >                t = wp[3]; wp[3] = wp[4]; wp[4] = t;
550 >                wp += 8;
551 >        }
552 > }
553 >
554 >
555 > getcascii(col)          /* get an ascii color value from stream(s) */
556   COLOR  col;
382 FILE  *fp;
557   {
558          double  vd[3];
559  
560 <        if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
561 <                return(-1);
560 >        if (fin2 == NULL) {
561 >                if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
562 >                        return(-1);
563 >        } else {
564 >                if (fscanf(fin, "%lf", &vd[0]) != 1 ||
565 >                                fscanf(fin2, "%lf", &vd[1]) != 1 ||
566 >                                fscanf(fin3, "%lf", &vd[2]) != 1)
567 >                        return(-1);
568 >        }
569          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
570          return(0);
571   }
572  
573  
574 < getcdouble(col, fp)             /* get a double color value from fp */
574 > getcdouble(col)         /* get a double color value from stream(s) */
575   COLOR  col;
395 FILE  *fp;
576   {
577          double  vd[3];
578  
579 <        if (fread((char *)vd, sizeof(double), 3, fp) != 3)
580 <                return(-1);
579 >        if (fin2 == NULL) {
580 >                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
581 >                        return(-1);
582 >        } else {
583 >                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
584 >                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
585 >                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
586 >                        return(-1);
587 >        }
588 >        if (swapbytes)
589 >                swap64((char *)vd, 3);
590          setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
591          return(0);
592   }
593  
594  
595 < getcfloat(col, fp)              /* get a float color value from fp */
595 > getcfloat(col)          /* get a float color value from stream(s) */
596   COLOR  col;
408 FILE  *fp;
597   {
598          float  vf[3];
599  
600 <        if (fread((char *)vf, sizeof(float), 3, fp) != 3)
601 <                return(-1);
600 >        if (fin2 == NULL) {
601 >                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
602 >                        return(-1);
603 >        } else {
604 >                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
605 >                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
606 >                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
607 >                        return(-1);
608 >        }
609 >        if (swapbytes)
610 >                swap32((uint32 *)vf, 3);
611          setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
612          return(0);
613   }
614  
615  
616 < getcint(col, fp)                /* get an int color value from fp */
616 > getcint(col)            /* get an int color value from stream(s) */
617   COLOR  col;
421 FILE  *fp;
618   {
619          int  vi[3];
620  
621 <        if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
622 <                return(-1);
621 >        if (fin2 == NULL) {
622 >                if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
623 >                        return(-1);
624 >        } else {
625 >                if (fscanf(fin, "%d", &vi[0]) != 1 ||
626 >                                fscanf(fin2, "%d", &vi[1]) != 1 ||
627 >                                fscanf(fin3, "%d", &vi[2]) != 1)
628 >                        return(-1);
629 >        }
630          setcolor(col, (vi[rord[RED]]+.5)/256.,
631                          (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
632          return(0);
633   }
634  
635  
636 < getcbyte(col, fp)               /* get a byte color value from fp */
636 > getcbyte(col)           /* get a byte color value from stream(s) */
637   COLOR  col;
435 FILE  *fp;
638   {
639          BYTE  vb[3];
640  
641 <        if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
642 <                return(-1);
641 >        if (fin2 == NULL) {
642 >                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
643 >                        return(-1);
644 >        } else {
645 >                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
646 >                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
647 >                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
648 >                        return(-1);
649 >        }
650          setcolor(col, (vb[rord[RED]]+.5)/256.,
651                          (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
652          return(0);
653   }
654  
655  
656 < getbascii(col, fp)              /* get an ascii brightness value from fp */
656 > getcword(col)           /* get a 16-bit color value from stream(s) */
657   COLOR  col;
449 FILE  *fp;
658   {
659 +        uint16  vw[3];
660 +
661 +        if (fin2 == NULL) {
662 +                if (fread((char *)vw, sizeof(uint16), 3, fin) != 3)
663 +                        return(-1);
664 +        } else {
665 +                if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 ||
666 +                        fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 ||
667 +                        fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1)
668 +                        return(-1);
669 +        }
670 +        if (swapbytes)
671 +                swap16(vw, 3);
672 +        setcolor(col, (vw[rord[RED]]+.5)/65536.,
673 +                        (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
674 +        return(0);
675 + }
676 +
677 +
678 + getbascii(col)          /* get an ascii brightness value from fin */
679 + COLOR  col;
680 + {
681          double  vd;
682  
683 <        if (fscanf(fp, "%lf", &vd) != 1)
683 >        if (fscanf(fin, "%lf", &vd) != 1)
684                  return(-1);
685          setcolor(col, vd, vd, vd);
686          return(0);
687   }
688  
689  
690 < getbdouble(col, fp)             /* get a double brightness value from fp */
690 > getbdouble(col)         /* get a double brightness value from fin */
691   COLOR  col;
462 FILE  *fp;
692   {
693          double  vd;
694  
695 <        if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
695 >        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
696                  return(-1);
697 +        if (swapbytes)
698 +                swap64((char *)&vd, 1);
699          setcolor(col, vd, vd, vd);
700          return(0);
701   }
702  
703  
704 < getbfloat(col, fp)              /* get a float brightness value from fp */
704 > getbfloat(col)          /* get a float brightness value from fin */
705   COLOR  col;
475 FILE  *fp;
706   {
707          float  vf;
708  
709 <        if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
709 >        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
710                  return(-1);
711 +        if (swapbytes)
712 +                swap32((uint32 *)&vf, 1);
713          setcolor(col, vf, vf, vf);
714          return(0);
715   }
716  
717  
718 < getbint(col, fp)                /* get an int brightness value from fp */
718 > getbint(col)            /* get an int brightness value from fin */
719   COLOR  col;
488 FILE  *fp;
720   {
721          int  vi;
722          double  d;
723  
724 <        if (fscanf(fp, "%d", &vi) != 1)
724 >        if (fscanf(fin, "%d", &vi) != 1)
725                  return(-1);
726          d = (vi+.5)/256.;
727          setcolor(col, d, d, d);
# Line 498 | Line 729 | FILE  *fp;
729   }
730  
731  
732 < getbbyte(col, fp)               /* get a byte brightness value from fp */
732 > getbbyte(col)           /* get a byte brightness value from fin */
733   COLOR  col;
503 FILE  *fp;
734   {
735          BYTE  vb;
736          double  d;
737  
738 <        if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
738 >        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
739                  return(-1);
740          d = (vb+.5)/256.;
741          setcolor(col, d, d, d);
# Line 513 | Line 743 | FILE  *fp;
743   }
744  
745  
746 < putcascii(col, fp)                      /* put an ascii color to fp */
746 > getbword(col)           /* get a 16-bit brightness value from fin */
747   COLOR  col;
518 FILE  *fp;
748   {
749 <        fprintf(fp, "%15.3e %15.3e %15.3e\n",
749 >        uint16  vw;
750 >        double  d;
751 >
752 >        if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1)
753 >                return(-1);
754 >        if (swapbytes)
755 >                swap16(&vw, 1);
756 >        d = (vw+.5)/65536.;
757 >        setcolor(col, d, d, d);
758 >        return(0);
759 > }
760 >
761 >
762 > putcascii(col)                  /* put an ascii color to stdout */
763 > COLOR  col;
764 > {
765 >        fprintf(stdout, "%15.3e %15.3e %15.3e\n",
766                          colval(col,ord[0]),
767                          colval(col,ord[1]),
768                          colval(col,ord[2]));
769  
770 <        return(ferror(fp) ? -1 : 0);
770 >        return(ferror(stdout) ? -1 : 0);
771   }
772  
773  
774 < putcfloat(col, fp)                      /* put a float color to fp */
774 > putcfloat(col)                  /* put a float color to stdout */
775   COLOR  col;
531 FILE  *fp;
776   {
777          float  vf[3];
778  
779          vf[0] = colval(col,ord[0]);
780          vf[1] = colval(col,ord[1]);
781          vf[2] = colval(col,ord[2]);
782 <        fwrite((char *)vf, sizeof(float), 3, fp);
782 >        if (swapbytes)
783 >                swap32((uint32 *)vf, 3);
784 >        fwrite((char *)vf, sizeof(float), 3, stdout);
785  
786 <        return(ferror(fp) ? -1 : 0);
786 >        return(ferror(stdout) ? -1 : 0);
787   }
788  
789  
790 < putcdouble(col, fp)                     /* put a double color to fp */
790 > putcdouble(col)                 /* put a double color to stdout */
791   COLOR  col;
546 FILE  *fp;
792   {
793          double  vd[3];
794  
795          vd[0] = colval(col,ord[0]);
796          vd[1] = colval(col,ord[1]);
797          vd[2] = colval(col,ord[2]);
798 <        fwrite((char *)vd, sizeof(double), 3, fp);
798 >        if (swapbytes)
799 >                swap64((char *)vd, 3);
800 >        fwrite((char *)vd, sizeof(double), 3, stdout);
801  
802 <        return(ferror(fp) ? -1 : 0);
802 >        return(ferror(stdout) ? -1 : 0);
803   }
804  
805  
806 < putcint(col, fp)                        /* put an int color to fp */
806 > putcint(col)                    /* put an int color to stdout */
807   COLOR  col;
561 FILE  *fp;
808   {
809 <        fprintf(fp, "%d %d %d\n",
809 >        fprintf(stdout, "%d %d %d\n",
810                          (int)(colval(col,ord[0])*256.),
811                          (int)(colval(col,ord[1])*256.),
812                          (int)(colval(col,ord[2])*256.));
813  
814 <        return(ferror(fp) ? -1 : 0);
814 >        return(ferror(stdout) ? -1 : 0);
815   }
816  
817  
818 < putcbyte(col, fp)                       /* put a byte color to fp */
818 > putcbyte(col)                   /* put a byte color to stdout */
819   COLOR  col;
574 FILE  *fp;
820   {
821 <        register int  i;
821 >        long  i;
822          BYTE  vb[3];
823  
824          i = colval(col,ord[0])*256.;
# Line 582 | Line 827 | FILE  *fp;
827          vb[1] = min(i,255);
828          i = colval(col,ord[2])*256.;
829          vb[2] = min(i,255);
830 <        fwrite((char *)vb, sizeof(BYTE), 3, fp);
830 >        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
831  
832 <        return(ferror(fp) ? -1 : 0);
832 >        return(ferror(stdout) ? -1 : 0);
833   }
834  
835  
836 < putbascii(col, fp)                      /* put an ascii brightness to fp */
836 > putcword(col)                   /* put a 16-bit color to stdout */
837   COLOR  col;
593 FILE  *fp;
838   {
839 <        fprintf(fp, "%15.3e\n", bright(col));
839 >        long  i;
840 >        uint16  vw[3];
841  
842 <        return(ferror(fp) ? -1 : 0);
842 >        i = colval(col,ord[0])*65536.;
843 >        vw[0] = min(i,65535);
844 >        i = colval(col,ord[1])*65536.;
845 >        vw[1] = min(i,65535);
846 >        i = colval(col,ord[2])*65536.;
847 >        vw[2] = min(i,65535);
848 >        if (swapbytes)
849 >                swap16(vw, 3);
850 >        fwrite((char *)vw, sizeof(uint16), 3, stdout);
851 >
852 >        return(ferror(stdout) ? -1 : 0);
853   }
854  
855  
856 < putbfloat(col, fp)                      /* put a float brightness to fp */
856 > putbascii(col)                  /* put an ascii brightness to stdout */
857   COLOR  col;
603 FILE  *fp;
858   {
859 +        fprintf(stdout, "%15.3e\n", (*mybright)(col));
860 +
861 +        return(ferror(stdout) ? -1 : 0);
862 + }
863 +
864 +
865 + putbfloat(col)                  /* put a float brightness to stdout */
866 + COLOR  col;
867 + {
868          float  vf;
869  
870 <        vf = bright(col);
871 <        fwrite((char *)&vf, sizeof(float), 1, fp);
870 >        vf = (*mybright)(col);
871 >        if (swapbytes)
872 >                swap32((uint32 *)&vf, 1);
873 >        fwrite((char *)&vf, sizeof(float), 1, stdout);
874  
875 <        return(ferror(fp) ? -1 : 0);
875 >        return(ferror(stdout) ? -1 : 0);
876   }
877  
878  
879 < putbdouble(col, fp)                     /* put a double brightness to fp */
879 > putbdouble(col)                 /* put a double brightness to stdout */
880   COLOR  col;
616 FILE  *fp;
881   {
882          double  vd;
883  
884 <        vd = bright(col);
885 <        fwrite((char *)&vd, sizeof(double), 1, fp);
884 >        vd = (*mybright)(col);
885 >        if (swapbytes)
886 >                swap64((char *)&vd, 1);
887 >        fwrite((char *)&vd, sizeof(double), 1, stdout);
888  
889 <        return(ferror(fp) ? -1 : 0);
889 >        return(ferror(stdout) ? -1 : 0);
890   }
891  
892  
893 < putbint(col, fp)                        /* put an int brightness to fp */
893 > putbint(col)                    /* put an int brightness to stdout */
894   COLOR  col;
629 FILE  *fp;
895   {
896 <        fprintf(fp, "%d\n", (int)(bright(col)*256.));
896 >        fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
897  
898 <        return(ferror(fp) ? -1 : 0);
898 >        return(ferror(stdout) ? -1 : 0);
899   }
900  
901  
902 < putbbyte(col, fp)                       /* put a byte brightness to fp */
902 > putbbyte(col)                   /* put a byte brightness to stdout */
903   COLOR  col;
639 FILE  *fp;
904   {
905          register int  i;
906          BYTE  vb;
907  
908 <        i = bright(col)*256.;
908 >        i = (*mybright)(col)*256.;
909          vb = min(i,255);
910 <        fwrite((char *)&vb, sizeof(BYTE), 1, fp);
910 >        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
911  
912 <        return(ferror(fp) ? -1 : 0);
912 >        return(ferror(stdout) ? -1 : 0);
913   }
914  
915  
916 + putbword(col)                   /* put a 16-bit brightness to stdout */
917 + COLOR  col;
918 + {
919 +        long  i;
920 +        uint16  vw;
921 +
922 +        i = (*mybright)(col)*65536.;
923 +        vw = min(i,65535);
924 +        if (swapbytes)
925 +                swap16(&vw, 1);
926 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
927 +
928 +        return(ferror(stdout) ? -1 : 0);
929 + }
930 +
931 +
932 + putpascii(col)                  /* put an ascii primary to stdout */
933 + COLOR  col;
934 + {
935 +        fprintf(stdout, "%15.3e\n", colval(col,putprim));
936 +
937 +        return(ferror(stdout) ? -1 : 0);
938 + }
939 +
940 +
941 + putpfloat(col)                  /* put a float primary to stdout */
942 + COLOR  col;
943 + {
944 +        float  vf;
945 +
946 +        vf = colval(col,putprim);
947 +        if (swapbytes)
948 +                swap32((uint32 *)&vf, 1);
949 +        fwrite((char *)&vf, sizeof(float), 1, stdout);
950 +
951 +        return(ferror(stdout) ? -1 : 0);
952 + }
953 +
954 +
955 + putpdouble(col)                 /* put a double primary to stdout */
956 + COLOR  col;
957 + {
958 +        double  vd;
959 +
960 +        vd = colval(col,putprim);
961 +        if (swapbytes)
962 +                swap64((char *)&vd, 1);
963 +        fwrite((char *)&vd, sizeof(double), 1, stdout);
964 +
965 +        return(ferror(stdout) ? -1 : 0);
966 + }
967 +
968 +
969 + putpint(col)                    /* put an int primary to stdout */
970 + COLOR  col;
971 + {
972 +        fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
973 +
974 +        return(ferror(stdout) ? -1 : 0);
975 + }
976 +
977 +
978 + putpbyte(col)                   /* put a byte primary to stdout */
979 + COLOR  col;
980 + {
981 +        long  i;
982 +        BYTE  vb;
983 +
984 +        i = colval(col,putprim)*256.;
985 +        vb = min(i,255);
986 +        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
987 +
988 +        return(ferror(stdout) ? -1 : 0);
989 + }
990 +
991 +
992 + putpword(col)                   /* put a 16-bit primary to stdout */
993 + COLOR  col;
994 + {
995 +        long  i;
996 +        uint16  vw;
997 +
998 +        i = colval(col,putprim)*65536.;
999 +        vw = min(i,65535);
1000 +        if (swapbytes)
1001 +                swap16(&vw, 1);
1002 +        fwrite((char *)&vw, sizeof(uint16), 1, stdout);
1003 +
1004 +        return(ferror(stdout) ? -1 : 0);
1005 + }
1006 +
1007 +
1008   set_io()                        /* set put and get functions */
1009   {
1010          switch (format) {
1011          case 'a':                                       /* ascii */
1012 <                if (brightonly) {
1012 >                if (putprim == BRIGHT) {
1013                          getval = getbascii;
1014                          putval = putbascii;
1015 +                } else if (putprim != ALL) {
1016 +                        getval = getbascii;
1017 +                        putval = putpascii;
1018                  } else {
1019                          getval = getcascii;
1020                          putval = putcascii;
1021 +                        if (reverse && !interleave) {
1022 +                                fprintf(stderr,
1023 +                                "%s: ASCII input files must be interleaved\n",
1024 +                                                progname);
1025 +                                quit(1);
1026 +                        }
1027                  }
1028                  return;
1029          case 'f':                                       /* binary float */
1030 <                if (brightonly) {
1030 >                if (putprim == BRIGHT) {
1031                          getval = getbfloat;
1032                          putval = putbfloat;
1033 +                } else if (putprim != ALL) {
1034 +                        getval = getbfloat;
1035 +                        putval = putpfloat;
1036                  } else {
1037                          getval = getcfloat;
1038                          putval = putcfloat;
1039 +                        if (reverse && !interleave) {
1040 +                                if (fin2 == NULL)
1041 +                                        goto namerr;
1042 +                                if (fseek(fin2,
1043 +                                (long)sizeof(float)*picres.xr*picres.yr, 1))
1044 +                                        goto seekerr;
1045 +                                if (fseek(fin3,
1046 +                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1047 +                                        goto seekerr;
1048 +                        }
1049                  }
1050                  return;
1051          case 'd':                                       /* binary double */
1052 <                if (brightonly) {
1052 >                if (putprim == BRIGHT) {
1053                          getval = getbdouble;
1054                          putval = putbdouble;
1055 +                } else if (putprim != ALL) {
1056 +                        getval = getbdouble;
1057 +                        putval = putpdouble;
1058                  } else {
1059                          getval = getcdouble;
1060                          putval = putcdouble;
1061 +                        if (reverse && !interleave) {
1062 +                                if (fin2 == NULL)
1063 +                                        goto namerr;
1064 +                                if (fseek(fin2,
1065 +                                (long)sizeof(double)*picres.xr*picres.yr, 1))
1066 +                                        goto seekerr;
1067 +                                if (fseek(fin3,
1068 +                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1069 +                                        goto seekerr;
1070 +                        }
1071                  }
1072                  return;
1073          case 'i':                                       /* integer */
1074 <                if (brightonly) {
1074 >                if (putprim == BRIGHT) {
1075                          getval = getbint;
1076                          putval = putbint;
1077 +                } else if (putprim != ALL) {
1078 +                        getval = getbint;
1079 +                        putval = putpint;
1080                  } else {
1081                          getval = getcint;
1082                          putval = putcint;
1083 +                        if (reverse && !interleave) {
1084 +                                fprintf(stderr,
1085 +                                "%s: integer input files must be interleaved\n",
1086 +                                                progname);
1087 +                                quit(1);
1088 +                        }
1089                  }
1090                  return;
1091          case 'b':                                       /* byte */
1092 <                if (brightonly) {
1092 >                if (putprim == BRIGHT) {
1093                          getval = getbbyte;
1094                          putval = putbbyte;
1095 +                } else if (putprim != ALL) {
1096 +                        getval = getbbyte;
1097 +                        putval = putpbyte;
1098                  } else {
1099                          getval = getcbyte;
1100                          putval = putcbyte;
1101 +                        if (reverse && !interleave) {
1102 +                                if (fin2 == NULL)
1103 +                                        goto namerr;
1104 +                                if (fseek(fin2,
1105 +                                (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
1106 +                                        goto seekerr;
1107 +                                if (fseek(fin3,
1108 +                                (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
1109 +                                        goto seekerr;
1110 +                        }
1111                  }
1112                  return;
1113 +        case 'w':                                       /* 16-bit */
1114 +                if (putprim == BRIGHT) {
1115 +                        getval = getbword;
1116 +                        putval = putbword;
1117 +                } else if (putprim != ALL) {
1118 +                        getval = getbword;
1119 +                        putval = putpword;
1120 +                } else {
1121 +                        getval = getcword;
1122 +                        putval = putcword;
1123 +                        if (reverse && !interleave) {
1124 +                                if (fin2 == NULL)
1125 +                                        goto namerr;
1126 +                                if (fseek(fin2,
1127 +                                (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1128 +                                        goto seekerr;
1129 +                                if (fseek(fin3,
1130 +                                (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1131 +                                        goto seekerr;
1132 +                        }
1133 +                }
1134 +                return;
1135          }
1136 + badopt:
1137 +        fprintf(stderr, "%s: botched file type\n", progname);
1138 +        quit(1);
1139 + namerr:
1140 +        fprintf(stderr, "%s: non-interleaved file(s) must be named\n",
1141 +                        progname);
1142 +        quit(1);
1143 + seekerr:
1144 +        fprintf(stderr, "%s: cannot seek on interleaved input file\n",
1145 +                        progname);
1146 +        quit(1);
1147   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines