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.13 by gwlarson, Tue Oct 27 09:08:27 1998 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines