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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines