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 1.2 by greg, Tue Sep 12 13:04:30 1989 UTC vs.
Revision 2.22 by schorsch, Mon Jul 21 22:30:19 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines