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.12 by greg, Wed Jan 8 08:51:43 1997 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 156 | Line 325 | unkopt:
325   checkhead(line)                         /* deal with line from header */
326   char  *line;
327   {
328 <        if (header)
328 >        char    fmt[32];
329 >        double  d;
330 >        COLOR   ctmp;
331 >
332 >        if (formatval(fmt, line))
333 >                wrongformat = strcmp(fmt, COLRFMT);
334 >        else if (original && isexpos(line)) {
335 >                d = 1.0/exposval(line);
336 >                scalecolor(exposure, d);
337 >                doexposure++;
338 >        } else if (original && iscolcor(line)) {
339 >                colcorval(ctmp, line);
340 >                setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
341 >                                colval(exposure,GRN)/colval(ctmp,GRN),
342 >                                colval(exposure,BLU)/colval(ctmp,BLU));
343 >                doexposure++;
344 >        } else if (header)
345                  fputs(line, stdout);
161        if (!strncmp(line, "EXPOSURE=", 9))
162                exposure *= atof(line+9);
346   }
347  
348  
349   pixtoval()                              /* convert picture to values */
350   {
351 <        COLOR  *scanln;
351 >        register COLOR  *scanln;
352 >        int  dogamma;
353          COLOR  lastc;
354 +        FLOAT  hv[2];
355 +        int  startprim, endprim;
356 +        long  startpos;
357          int  y;
358          register int  x;
359  
360 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
360 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
361          if (scanln == NULL) {
362                  fprintf(stderr, "%s: out of memory\n", progname);
363                  quit(1);
364          }
365 <        setcolor(lastc, 0.0, 0.0, 0.0);
366 <        for (y = yres-1; y >= 0; y--) {
367 <                if (freadscan(scanln, xres, fin) < 0) {
368 <                        fprintf(stderr, "%s: read error\n", progname);
365 >        dogamma = gamcor < .95 || gamcor > 1.05;
366 >        if (putprim == ALL && !interleave) {
367 >                startprim = RED; endprim = BLU;
368 >                startpos = ftell(fin);
369 >        } else {
370 >                startprim = putprim; endprim = putprim;
371 >        }
372 >        for (putprim = startprim; putprim <= endprim; putprim++) {
373 >                if (putprim != startprim && fseek(fin, startpos, 0)) {
374 >                        fprintf(stderr, "%s: seek error on input file\n",
375 >                                        progname);
376                          quit(1);
377                  }
378 <                for (x = 0; x < xres; x++) {
379 <                        if (uniq)
380 <                                if (    scanln[x][RED] == lastc[RED] &&
381 <                                        scanln[x][GRN] == lastc[GRN] &&
382 <                                        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);
378 >                set_io();
379 >                setcolor(lastc, 0.0, 0.0, 0.0);
380 >                for (y = 0; y < numscans(&picres); y++) {
381 >                        if (freadscan(scanln, scanlen(&picres), fin) < 0) {
382 >                                fprintf(stderr, "%s: read error\n", progname);
383                                  quit(1);
384                          }
385 +                        for (x = 0; x < scanlen(&picres); x++) {
386 +                                if (uniq)
387 +                                        if (    colval(scanln[x],RED) ==
388 +                                                        colval(lastc,RED) &&
389 +                                                colval(scanln[x],GRN) ==
390 +                                                        colval(lastc,GRN) &&
391 +                                                colval(scanln[x],BLU) ==
392 +                                                        colval(lastc,BLU)       )
393 +                                                continue;
394 +                                        else
395 +                                                copycolor(lastc, scanln[x]);
396 +                                if (doexposure)
397 +                                        multcolor(scanln[x], exposure);
398 +                                if (dogamma)
399 +                                        setcolor(scanln[x],
400 +                                        pow(colval(scanln[x],RED), 1.0/gamcor),
401 +                                        pow(colval(scanln[x],GRN), 1.0/gamcor),
402 +                                        pow(colval(scanln[x],BLU), 1.0/gamcor));
403 +                                if (!dataonly) {
404 +                                        pix2loc(hv, &picres, x, y);
405 +                                        printf("%7d %7d ",
406 +                                                        (int)(hv[0]*picres.xr),
407 +                                                        (int)(hv[1]*picres.yr));
408 +                                }
409 +                                if ((*putval)(scanln[x]) < 0) {
410 +                                        fprintf(stderr, "%s: write error\n",
411 +                                                        progname);
412 +                                        quit(1);
413 +                                }
414 +                        }
415                  }
416          }
417          free((char *)scanln);
# Line 205 | Line 420 | pixtoval()                             /* convert picture to values */
420  
421   valtopix()                      /* convert values to a pixel file */
422   {
423 <        COLOR  *scanln;
423 >        int  dogamma;
424 >        register COLOR  *scanln;
425          int  y;
426          register int  x;
427  
428 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
428 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
429          if (scanln == NULL) {
430                  fprintf(stderr, "%s: out of memory\n", progname);
431                  quit(1);
432          }
433 <        for (y = yres-1; y >= 0; y--) {
434 <                for (x = 0; x < xres; x++) {
435 <                        if (!dataonly)
433 >        dogamma = gamcor < .95 || gamcor > 1.05;
434 >        set_io();
435 >        for (y = 0; y < numscans(&picres); y++) {
436 >                for (x = 0; x < scanlen(&picres); x++) {
437 >                        if (!dataonly) {
438                                  fscanf(fin, "%*d %*d");
439 <                        if ((*getval)(scanln[x], fin) < 0) {
439 >                                if (fin2 != NULL) {
440 >                                        fscanf(fin2, "%*d %*d");
441 >                                        fscanf(fin3, "%*d %*d");
442 >                                }
443 >                        }
444 >                        if ((*getval)(scanln[x]) < 0) {
445                                  fprintf(stderr, "%s: read error\n", progname);
446                                  quit(1);
447                          }
448 +                        if (dogamma)
449 +                                setcolor(scanln[x],
450 +                                        pow(colval(scanln[x],RED), gamcor),
451 +                                        pow(colval(scanln[x],GRN), gamcor),
452 +                                        pow(colval(scanln[x],BLU), gamcor));
453 +                        if (doexposure)
454 +                                multcolor(scanln[x], exposure);
455                  }
456 <                if (fwritescan(scanln, xres, stdout) < 0
227 <                                || fflush(stdout) < 0) {
456 >                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
457                          fprintf(stderr, "%s: write error\n", progname);
458                          quit(1);
459                  }
# Line 240 | Line 469 | int  code;
469   }
470  
471  
472 < getcascii(col, fp)              /* get an ascii color value from fp */
472 > getcascii(col)          /* get an ascii color value from stream(s) */
473   COLOR  col;
245 FILE  *fp;
474   {
475 <        double  vd[3];
475 >        double  vd[3];
476  
477 <        if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
478 <                return(-1);
479 <        setcolor(col, vd[0], vd[1], vd[2]);
477 >        if (fin2 == NULL) {
478 >                if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
479 >                        return(-1);
480 >        } else {
481 >                if (fscanf(fin, "%lf", &vd[0]) != 1 ||
482 >                                fscanf(fin2, "%lf", &vd[1]) != 1 ||
483 >                                fscanf(fin3, "%lf", &vd[2]) != 1)
484 >                        return(-1);
485 >        }
486 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
487          return(0);
488   }
489  
490  
491 < getcdouble(col, fp)             /* get a double color value from fp */
491 > getcdouble(col)         /* get a double color value from stream(s) */
492   COLOR  col;
258 FILE  *fp;
493   {
494 <        double  vd[3];
494 >        double  vd[3];
495  
496 <        if (fread(vd, sizeof(double), 3, fp) != 3)
497 <                return(-1);
498 <        setcolor(col, vd[0], vd[1], vd[2]);
496 >        if (fin2 == NULL) {
497 >                if (fread((char *)vd, sizeof(double), 3, fin) != 3)
498 >                        return(-1);
499 >        } else {
500 >                if (fread((char *)vd, sizeof(double), 1, fin) != 1 ||
501 >                        fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 ||
502 >                        fread((char *)(vd+2), sizeof(double), 1, fin3) != 1)
503 >                        return(-1);
504 >        }
505 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
506          return(0);
507   }
508  
509  
510 < getcfloat(col, fp)              /* get a float color value from fp */
510 > getcfloat(col)          /* get a float color value from stream(s) */
511   COLOR  col;
271 FILE  *fp;
512   {
513          float  vf[3];
514  
515 <        if (fread(vf, sizeof(float), 3, fp) != 3)
516 <                return(-1);
517 <        setcolor(col, vf[0], vf[1], vf[2]);
515 >        if (fin2 == NULL) {
516 >                if (fread((char *)vf, sizeof(float), 3, fin) != 3)
517 >                        return(-1);
518 >        } else {
519 >                if (fread((char *)vf, sizeof(float), 1, fin) != 1 ||
520 >                        fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 ||
521 >                        fread((char *)(vf+2), sizeof(float), 1, fin3) != 1)
522 >                        return(-1);
523 >        }
524 >        setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
525          return(0);
526   }
527  
528  
529 < getcint(col, fp)                /* get an int color value from fp */
529 > getcint(col)            /* get an int color value from stream(s) */
530   COLOR  col;
284 FILE  *fp;
531   {
532          int  vi[3];
533  
534 <        if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
535 <                return(-1);
536 <        setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
534 >        if (fin2 == NULL) {
535 >                if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
536 >                        return(-1);
537 >        } else {
538 >                if (fscanf(fin, "%d", &vi[0]) != 1 ||
539 >                                fscanf(fin2, "%d", &vi[1]) != 1 ||
540 >                                fscanf(fin3, "%d", &vi[2]) != 1)
541 >                        return(-1);
542 >        }
543 >        setcolor(col, (vi[rord[RED]]+.5)/256.,
544 >                        (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
545          return(0);
546   }
547  
548  
549 < getcbyte(col, fp)               /* get a byte color value from fp */
549 > getcbyte(col)           /* get a byte color value from stream(s) */
550   COLOR  col;
297 FILE  *fp;
551   {
552          BYTE  vb[3];
553  
554 <        if (fread(vb, sizeof(BYTE), 3, fp) != 3)
555 <                return(-1);
556 <        setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
554 >        if (fin2 == NULL) {
555 >                if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3)
556 >                        return(-1);
557 >        } else {
558 >                if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 ||
559 >                        fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 ||
560 >                        fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1)
561 >                        return(-1);
562 >        }
563 >        setcolor(col, (vb[rord[RED]]+.5)/256.,
564 >                        (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
565          return(0);
566   }
567  
568  
569 < getbascii(col, fp)              /* get an ascii brightness value from fp */
569 > getbascii(col)          /* get an ascii brightness value from fin */
570   COLOR  col;
310 FILE  *fp;
571   {
572 <        double  vd;
572 >        double  vd;
573  
574 <        if (fscanf(fp, "%lf", &vd) != 1)
574 >        if (fscanf(fin, "%lf", &vd) != 1)
575                  return(-1);
576          setcolor(col, vd, vd, vd);
577          return(0);
578   }
579  
580  
581 < getbdouble(col, fp)             /* get a double brightness value from fp */
581 > getbdouble(col)         /* get a double brightness value from fin */
582   COLOR  col;
323 FILE  *fp;
583   {
584 <        double  vd;
584 >        double  vd;
585  
586 <        if (fread(&vd, sizeof(double), 1, fp) != 1)
586 >        if (fread((char *)&vd, sizeof(double), 1, fin) != 1)
587                  return(-1);
588          setcolor(col, vd, vd, vd);
589          return(0);
590   }
591  
592  
593 < getbfloat(col, fp)              /* get a float brightness value from fp */
593 > getbfloat(col)          /* get a float brightness value from fin */
594   COLOR  col;
336 FILE  *fp;
595   {
596          float  vf;
597  
598 <        if (fread(&vf, sizeof(float), 1, fp) != 1)
598 >        if (fread((char *)&vf, sizeof(float), 1, fin) != 1)
599                  return(-1);
600          setcolor(col, vf, vf, vf);
601          return(0);
602   }
603  
604  
605 < getbint(col, fp)                /* get an int brightness value from fp */
605 > getbint(col)            /* get an int brightness value from fin */
606   COLOR  col;
349 FILE  *fp;
607   {
608          int  vi;
609 <        double  d;
609 >        double  d;
610  
611 <        if (fscanf(fp, "%d", &vi) != 1)
611 >        if (fscanf(fin, "%d", &vi) != 1)
612                  return(-1);
613          d = (vi+.5)/256.;
614          setcolor(col, d, d, d);
# Line 359 | Line 616 | FILE  *fp;
616   }
617  
618  
619 < getbbyte(col, fp)               /* get a byte brightness value from fp */
619 > getbbyte(col)           /* get a byte brightness value from fin */
620   COLOR  col;
364 FILE  *fp;
621   {
622          BYTE  vb;
623 <        double  d;
623 >        double  d;
624  
625 <        if (fread(&vb, sizeof(BYTE), 1, fp) != 1)
625 >        if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1)
626                  return(-1);
627          d = (vb+.5)/256.;
628          setcolor(col, d, d, d);
# Line 374 | Line 630 | FILE  *fp;
630   }
631  
632  
633 < putcascii(col, fp)                      /* put an ascii color to fp */
633 > putcascii(col)                  /* put an ascii color to stdout */
634   COLOR  col;
379 FILE  *fp;
635   {
636 <        fprintf(fp, "%15.3e %15.3e %15.3e\n",
637 <                        colval(col,RED),
638 <                        colval(col,GRN),
639 <                        colval(col,BLU));
636 >        fprintf(stdout, "%15.3e %15.3e %15.3e\n",
637 >                        colval(col,ord[0]),
638 >                        colval(col,ord[1]),
639 >                        colval(col,ord[2]));
640  
641 <        return(ferror(fp) ? -1 : 0);
641 >        return(ferror(stdout) ? -1 : 0);
642   }
643  
644  
645 < putcfloat(col, fp)                      /* put a float color to fp */
645 > putcfloat(col)                  /* put a float color to stdout */
646   COLOR  col;
392 FILE  *fp;
647   {
648          float  vf[3];
649  
650 <        vf[0] = colval(col,RED);
651 <        vf[1] = colval(col,GRN);
652 <        vf[2] = colval(col,BLU);
653 <        fwrite(vf, sizeof(float), 3, fp);
650 >        vf[0] = colval(col,ord[0]);
651 >        vf[1] = colval(col,ord[1]);
652 >        vf[2] = colval(col,ord[2]);
653 >        fwrite((char *)vf, sizeof(float), 3, stdout);
654  
655 <        return(ferror(fp) ? -1 : 0);
655 >        return(ferror(stdout) ? -1 : 0);
656   }
657  
658  
659 < putcdouble(col, fp)                     /* put a double color to fp */
659 > putcdouble(col)                 /* put a double color to stdout */
660   COLOR  col;
407 FILE  *fp;
661   {
662 <        double  vd[3];
662 >        double  vd[3];
663  
664 <        vd[0] = colval(col,RED);
665 <        vd[1] = colval(col,GRN);
666 <        vd[2] = colval(col,BLU);
667 <        fwrite(vd, sizeof(double), 3, fp);
664 >        vd[0] = colval(col,ord[0]);
665 >        vd[1] = colval(col,ord[1]);
666 >        vd[2] = colval(col,ord[2]);
667 >        fwrite((char *)vd, sizeof(double), 3, stdout);
668  
669 <        return(ferror(fp) ? -1 : 0);
669 >        return(ferror(stdout) ? -1 : 0);
670   }
671  
672  
673 < putcint(col, fp)                        /* put an int color to fp */
673 > putcint(col)                    /* put an int color to stdout */
674   COLOR  col;
422 FILE  *fp;
675   {
676 <        fprintf(fp, "%d %d %d\n",
677 <                        (int)(colval(col,RED)*256.),
678 <                        (int)(colval(col,GRN)*256.),
679 <                        (int)(colval(col,BLU)*256.));
676 >        fprintf(stdout, "%d %d %d\n",
677 >                        (int)(colval(col,ord[0])*256.),
678 >                        (int)(colval(col,ord[1])*256.),
679 >                        (int)(colval(col,ord[2])*256.));
680  
681 <        return(ferror(fp) ? -1 : 0);
681 >        return(ferror(stdout) ? -1 : 0);
682   }
683  
684  
685 < putcbyte(col, fp)                       /* put a byte color to fp */
685 > putcbyte(col)                   /* put a byte color to stdout */
686   COLOR  col;
435 FILE  *fp;
687   {
688          register int  i;
689          BYTE  vb[3];
690  
691 <        i = colval(col,RED)*256.;
691 >        i = colval(col,ord[0])*256.;
692          vb[0] = min(i,255);
693 <        i = colval(col,GRN)*256.;
693 >        i = colval(col,ord[1])*256.;
694          vb[1] = min(i,255);
695 <        i = colval(col,BLU)*256.;
695 >        i = colval(col,ord[2])*256.;
696          vb[2] = min(i,255);
697 <        fwrite(vb, sizeof(BYTE), 3, fp);
697 >        fwrite((char *)vb, sizeof(BYTE), 3, stdout);
698  
699 <        return(ferror(fp) ? -1 : 0);
699 >        return(ferror(stdout) ? -1 : 0);
700   }
701  
702  
703 < putbascii(col, fp)                      /* put an ascii brightness to fp */
703 > putbascii(col)                  /* put an ascii brightness to stdout */
704   COLOR  col;
454 FILE  *fp;
705   {
706 <        fprintf(fp, "%15.3e\n", bright(col));
706 >        fprintf(stdout, "%15.3e\n", bright(col));
707  
708 <        return(ferror(fp) ? -1 : 0);
708 >        return(ferror(stdout) ? -1 : 0);
709   }
710  
711  
712 < putbfloat(col, fp)                      /* put a float brightness to fp */
712 > putbfloat(col)                  /* put a float brightness to stdout */
713   COLOR  col;
464 FILE  *fp;
714   {
715          float  vf;
716  
717          vf = bright(col);
718 <        fwrite(&vf, sizeof(float), 1, fp);
718 >        fwrite((char *)&vf, sizeof(float), 1, stdout);
719  
720 <        return(ferror(fp) ? -1 : 0);
720 >        return(ferror(stdout) ? -1 : 0);
721   }
722  
723  
724 < putbdouble(col, fp)                     /* put a double brightness to fp */
724 > putbdouble(col)                 /* put a double brightness to stdout */
725   COLOR  col;
477 FILE  *fp;
726   {
727 <        double  vd;
727 >        double  vd;
728  
729          vd = bright(col);
730 <        fwrite(&vd, sizeof(double), 1, fp);
730 >        fwrite((char *)&vd, sizeof(double), 1, stdout);
731  
732 <        return(ferror(fp) ? -1 : 0);
732 >        return(ferror(stdout) ? -1 : 0);
733   }
734  
735  
736 < putbint(col, fp)                        /* put an int brightness to fp */
736 > putbint(col)                    /* put an int brightness to stdout */
737   COLOR  col;
490 FILE  *fp;
738   {
739 <        fprintf(fp, "%d\n", (int)(bright(col)*256.));
739 >        fprintf(stdout, "%d\n", (int)(bright(col)*256.));
740  
741 <        return(ferror(fp) ? -1 : 0);
741 >        return(ferror(stdout) ? -1 : 0);
742   }
743  
744  
745 < putbbyte(col, fp)                       /* put a byte brightness to fp */
745 > putbbyte(col)                   /* put a byte brightness to stdout */
746   COLOR  col;
500 FILE  *fp;
747   {
748          register int  i;
749          BYTE  vb;
750  
751          i = bright(col)*256.;
752          vb = min(i,255);
753 <        fwrite(&vb, sizeof(BYTE), 1, fp);
753 >        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
754  
755 <        return(ferror(fp) ? -1 : 0);
755 >        return(ferror(stdout) ? -1 : 0);
756   }
757  
758  
759 + putpascii(col)                  /* put an ascii primary to stdout */
760 + COLOR  col;
761 + {
762 +        fprintf(stdout, "%15.3e\n", colval(col,putprim));
763 +
764 +        return(ferror(stdout) ? -1 : 0);
765 + }
766 +
767 +
768 + putpfloat(col)                  /* put a float primary to stdout */
769 + COLOR  col;
770 + {
771 +        float  vf;
772 +
773 +        vf = colval(col,putprim);
774 +        fwrite((char *)&vf, sizeof(float), 1, stdout);
775 +
776 +        return(ferror(stdout) ? -1 : 0);
777 + }
778 +
779 +
780 + putpdouble(col)                 /* put a double primary to stdout */
781 + COLOR  col;
782 + {
783 +        double  vd;
784 +
785 +        vd = colval(col,putprim);
786 +        fwrite((char *)&vd, sizeof(double), 1, stdout);
787 +
788 +        return(ferror(stdout) ? -1 : 0);
789 + }
790 +
791 +
792 + putpint(col)                    /* put an int primary to stdout */
793 + COLOR  col;
794 + {
795 +        fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
796 +
797 +        return(ferror(stdout) ? -1 : 0);
798 + }
799 +
800 +
801 + putpbyte(col)                   /* put a byte primary to stdout */
802 + COLOR  col;
803 + {
804 +        register int  i;
805 +        BYTE  vb;
806 +
807 +        i = colval(col,putprim)*256.;
808 +        vb = min(i,255);
809 +        fwrite((char *)&vb, sizeof(BYTE), 1, stdout);
810 +
811 +        return(ferror(stdout) ? -1 : 0);
812 + }
813 +
814 +
815   set_io()                        /* set put and get functions */
816   {
817          switch (format) {
818          case 'a':                                       /* ascii */
819 <                if (brightonly) {
819 >                if (putprim == BRIGHT) {
820                          getval = getbascii;
821                          putval = putbascii;
822 +                } else if (putprim != ALL) {
823 +                        getval = getbascii;
824 +                        putval = putpascii;
825                  } else {
826                          getval = getcascii;
827                          putval = putcascii;
828 +                        if (reverse && !interleave) {
829 +                                fprintf(stderr,
830 +                                "%s: ASCII input files must be interleaved\n",
831 +                                                progname);
832 +                                quit(1);
833 +                        }
834                  }
835                  return;
836          case 'f':                                       /* binary float */
837 <                if (brightonly) {
837 >                if (putprim == BRIGHT) {
838                          getval = getbfloat;
839                          putval = putbfloat;
840 +                } else if (putprim != ALL) {
841 +                        getval = getbfloat;
842 +                        putval = putpfloat;
843                  } else {
844                          getval = getcfloat;
845                          putval = putcfloat;
846 +                        if (reverse && !interleave) {
847 +                                if (fin2 == NULL)
848 +                                        goto namerr;
849 +                                if (fseek(fin2,
850 +                                (long)sizeof(float)*picres.xr*picres.yr, 1))
851 +                                        goto seekerr;
852 +                                if (fseek(fin3,
853 +                                (long)sizeof(float)*2*picres.xr*picres.yr, 1))
854 +                                        goto seekerr;
855 +                        }
856                  }
857                  return;
858          case 'd':                                       /* binary double */
859 <                if (brightonly) {
859 >                if (putprim == BRIGHT) {
860                          getval = getbdouble;
861                          putval = putbdouble;
862 +                } else if (putprim != ALL) {
863 +                        getval = getbdouble;
864 +                        putval = putpdouble;
865                  } else {
866                          getval = getcdouble;
867                          putval = putcdouble;
868 +                        if (reverse && !interleave) {
869 +                                if (fin2 == NULL)
870 +                                        goto namerr;
871 +                                if (fseek(fin2,
872 +                                (long)sizeof(double)*picres.xr*picres.yr, 1))
873 +                                        goto seekerr;
874 +                                if (fseek(fin3,
875 +                                (long)sizeof(double)*2*picres.xr*picres.yr, 1))
876 +                                        goto seekerr;
877 +                        }
878                  }
879                  return;
880          case 'i':                                       /* integer */
881 <                if (brightonly) {
881 >                if (putprim == BRIGHT) {
882                          getval = getbint;
883                          putval = putbint;
884 +                } else if (putprim != ALL) {
885 +                        getval = getbint;
886 +                        putval = putpint;
887                  } else {
888                          getval = getcint;
889                          putval = putcint;
890 +                        if (reverse && !interleave) {
891 +                                fprintf(stderr,
892 +                                "%s: integer input files must be interleaved\n",
893 +                                                progname);
894 +                                quit(1);
895 +                        }
896                  }
897                  return;
898          case 'b':                                       /* byte */
899 <                if (brightonly) {
899 >                if (putprim == BRIGHT) {
900                          getval = getbbyte;
901                          putval = putbbyte;
902 +                } else if (putprim != ALL) {
903 +                        getval = getbbyte;
904 +                        putval = putpbyte;
905                  } else {
906                          getval = getcbyte;
907                          putval = putcbyte;
908 +                        if (reverse && !interleave) {
909 +                                if (fin2 == NULL)
910 +                                        goto namerr;
911 +                                if (fseek(fin2,
912 +                                (long)sizeof(BYTE)*picres.xr*picres.yr, 1))
913 +                                        goto seekerr;
914 +                                if (fseek(fin3,
915 +                                (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1))
916 +                                        goto seekerr;
917 +                        }
918                  }
919                  return;
920          }
921 + badopt:
922 +        fprintf(stderr, "%s: botched file type\n", progname);
923 +        quit(1);
924 + namerr:
925 +        fprintf(stderr, "%s: non-interleaved file(s) must be named\n",
926 +                        progname);
927 +        quit(1);
928 + seekerr:
929 +        fprintf(stderr, "%s: cannot seek on interleaved input file\n",
930 +                        progname);
931 +        quit(1);
932   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines