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.5 by greg, Fri Dec 14 16:33:19 1990 UTC vs.
Revision 2.10 by greg, Wed Jan 8 07:43:49 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 + long  skipbytes = 0;            /* skip bytes in input? */
45 +
46 + int  resolution = 1;            /* put/get resolution string? */
47 +
48 + int  original = 0;              /* convert to original values? */
49 +
50 + int  wrongformat = 0;           /* wrong input format? */
51 +
52 + double  gamcor = 1.0;           /* gamma correction */
53 +
54 + int  ord[3] = {RED, GRN, BLU};  /* RGB ordering */
55 + int  rord[4];                   /* reverse ordering */
56 +
57   COLOR  exposure = WHTCOLOR;
58  
59   char  *progname;
60  
61   FILE  *fin;
62 + FILE  *fin2 = NULL, *fin3 = NULL;       /* for other color channels */
63  
64   int  (*getval)(), (*putval)();
65  
# Line 46 | Line 68 | main(argc, argv)
68   int  argc;
69   char  **argv;
70   {
49        extern double  atof();
71          extern int  checkhead();
72 +        extern long  atol();
73 +        double  d, expval = 1.0;
74          int  i;
75  
76          progname = argv[0];
77  
78          for (i = 1; i < argc; i++)
79 <                if (argv[i][0] == '-')
79 >                if (argv[i][0] == '-' || argv[i][0] == '+')
80                          switch (argv[i][1]) {
81 <                        case 'h':               /* no header */
82 <                                header = 0;
81 >                        case 'h':               /* header */
82 >                                header = argv[i][0] == '+';
83                                  break;
84 +                        case 'H':               /* resolution string */
85 +                                resolution = argv[i][0] == '+';
86 +                                break;
87 +                        case 's':               /* skip bytes in header */
88 +                                skipbytes = atol(argv[++i]);
89 +                                break;
90                          case 'u':               /* unique values */
91 <                                uniq = 1;
91 >                                uniq = argv[i][0] == '-';
92                                  break;
93                          case 'o':               /* original values */
94 <                                original = 1;
94 >                                original = argv[i][0] == '-';
95                                  break;
96 +                        case 'g':               /* gamma correction */
97 +                                gamcor = atof(argv[i+1]);
98 +                                if (argv[i][0] == '+')
99 +                                        gamcor = 1.0/gamcor;
100 +                                i++;
101 +                                break;
102 +                        case 'e':               /* exposure correction */
103 +                                d = atof(argv[i+1]);
104 +                                if (argv[i+1][0] == '-' || argv[i+1][0] == '+')
105 +                                        d = pow(2.0, d);
106 +                                if (argv[i][0] == '-')
107 +                                        expval *= d;
108 +                                scalecolor(exposure, d);
109 +                                doexposure++;
110 +                                i++;
111 +                                break;
112 +                        case 'R':               /* reverse byte sequence */
113 +                                if (argv[i][0] == '-') {
114 +                                        ord[0]=BLU; ord[1]=GRN; ord[2]=RED;
115 +                                } else {
116 +                                        ord[0]=RED; ord[1]=GRN; ord[2]=BLU;
117 +                                }
118 +                                break;
119                          case 'r':               /* reverse conversion */
120 <                                reverse = 1;
120 >                                reverse = argv[i][0] == '-';
121                                  break;
122                          case 'b':               /* brightness values */
123 <                                brightonly = 1;
123 >                                putprim = argv[i][0] == '-' ? BRIGHT : ALL;
124                                  break;
125 +                        case 'p':               /* put primary */
126 +                                switch (argv[i][2]) {
127 +                                case 'r': case 'R': putprim = RED; break;
128 +                                case 'g': case 'G': putprim = GRN; break;
129 +                                case 'b': case 'B': putprim = BLU; break;
130 +                                default: goto unkopt;
131 +                                }
132 +                                break;
133                          case 'd':               /* data only (no indices) */
134 <                                dataonly = 1;
134 >                                dataonly = argv[i][0] == '-';
135                                  switch (argv[i][2]) {
136                                  case '\0':
137                                  case 'a':               /* ascii */
138                                          format = 'a';
139 +                                        fmtid = "ascii";
140                                          break;
141                                  case 'i':               /* integer */
142 +                                        format = 'i';
143 +                                        fmtid = "ascii";
144 +                                        break;
145                                  case 'b':               /* byte */
146 +                                        dataonly = 1;
147 +                                        format = 'b';
148 +                                        fmtid = "byte";
149 +                                        break;
150                                  case 'f':               /* float */
151 +                                        dataonly = 1;
152 +                                        format = 'f';
153 +                                        fmtid = "float";
154 +                                        break;
155                                  case 'd':               /* double */
156 <                                        format = argv[i][2];
156 >                                        dataonly = 1;
157 >                                        format = 'd';
158 >                                        fmtid = "double";
159                                          break;
160                                  default:
161                                          goto unkopt;
162                                  }
163                                  break;
164                          case 'x':               /* x resolution */
165 <                                xres = atoi(argv[++i]);
165 >                        case 'X':               /* x resolution */
166 >                                resolution = 0;
167 >                                if (argv[i][0] == '-')
168 >                                        picres.or |= XDECR;
169 >                                picres.xr = atoi(argv[++i]);
170                                  break;
171                          case 'y':               /* y resolution */
172 <                                yres = atoi(argv[++i]);
172 >                        case 'Y':               /* y resolution */
173 >                                resolution = 0;
174 >                                if (argv[i][0] == '-')
175 >                                        picres.or |= YDECR;
176 >                                if (picres.xr == 0)
177 >                                        picres.or |= YMAJOR;
178 >                                picres.yr = atoi(argv[++i]);
179                                  break;
180                          default:
181   unkopt:
# Line 102 | Line 186 | unkopt:
186                          }
187                  else
188                          break;
189 <                        
189 >                                        /* recognize special formats */
190 >        if (dataonly && format == 'b')
191 >                if (brightonly)
192 >                        fmtid = "8-bit_grey";
193 >                else
194 >                        fmtid = "24-bit_rgb";
195 >                                        /* assign reverse ordering */
196 >        rord[ord[0]] = 0;
197 >        rord[ord[1]] = 1;
198 >        rord[ord[2]] = 2;
199 >                                        /* get input */
200          if (i == argc) {
201                  fin = stdin;
202 <        } else if (i == argc-1) {
202 >        } else if (i < argc) {
203                  if ((fin = fopen(argv[i], "r")) == NULL) {
204                          fprintf(stderr, "%s: can't open file \"%s\"\n",
205                                                  progname, argv[i]);
206                          quit(1);
207                  }
208 <        } else {
208 >                if (skipbytes && fseek(fin, skipbytes, 0))
209 >                        goto seekerr;
210 >                if (reverse && !brightonly && i == argc-3) {
211 >                        if ((fin2 = fopen(argv[i+1], "r")) == NULL) {
212 >                                fprintf(stderr, "%s: can't open file \"%s\"\n",
213 >                                                progname, argv[i+1]);
214 >                                quit(1);
215 >                        }
216 >                        if ((fin3 = fopen(argv[i+2], "r")) == NULL) {
217 >                                fprintf(stderr, "%s: can't open file \"%s\"\n",
218 >                                                progname, argv[i+2]);
219 >                                quit(1);
220 >                        }
221 >                        if (skipbytes && (fseek(fin2, skipbytes, 0) ||
222 >                                        fseek(fin3, skipbytes, 0)))
223 >                                goto seekerr;
224 >                } else if (i != argc-1)
225 >                        fin = NULL;
226 >        }
227 >        if (fin == NULL) {
228                  fprintf(stderr, "%s: bad # file arguments\n", progname);
229                  quit(1);
230          }
# Line 119 | Line 232 | unkopt:
232          set_io();
233  
234          if (reverse) {
235 <                if (header)                     /* get header */
236 <                        copyheader(fin, stdout);
237 <                                                /* add to header */
238 <                printargs(i, argv, stdout);
239 <                printf("\n");
240 <                if (yres <= 0 || xres <= 0) {
241 <                        fprintf(stderr, "%s: missing x and y resolution\n",
242 <                                        progname);
235 > #ifdef MSDOS
236 >                setmode(fileno(stdout), O_BINARY);
237 >                if (format != 'a' && format != 'i')
238 >                        setmode(fileno(fin), O_BINARY);
239 > #endif
240 >                                        /* get header */
241 >                if (header) {
242 >                        if (checkheader(fin, fmtid, stdout) < 0) {
243 >                                fprintf(stderr, "%s: wrong input format\n",
244 >                                                progname);
245 >                                quit(1);
246 >                        }
247 >                        if (fin2 != NULL) {
248 >                                getheader(fin2, NULL, NULL);
249 >                                getheader(fin3, NULL, NULL);
250 >                        }
251 >                } else
252 >                        newheader("RADIANCE", stdout);
253 >                                        /* get resolution */
254 >                if ((resolution && !fgetsresolu(&picres, fin)) ||
255 >                                picres.xr <= 0 || picres.yr <= 0) {
256 >                        fprintf(stderr, "%s: missing resolution\n", progname);
257                          quit(1);
258                  }
259 <                fputresolu(YMAJOR|YDECR, xres, yres, stdout);
260 <                valtopix();
261 <        } else {
262 <                                                /* get header */
263 <                getheader(fin, checkhead);
264 <
265 <                if (xres <= 0 || yres <= 0)             /* get picture size */
266 <                        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
267 <                                fprintf(stderr,
268 <                                "%s: missing x and y resolution\n",
259 >                if (resolution && fin2 != NULL) {
260 >                        RESOLU  pres2;
261 >                        if (!fgetsresolu(&pres2, fin2) ||
262 >                                        pres2.or != picres.or ||
263 >                                        pres2.xr != picres.xr ||
264 >                                        pres2.yr != picres.yr ||
265 >                                        !fgetsresolu(&pres2, fin3) ||
266 >                                        pres2.or != picres.or ||
267 >                                        pres2.xr != picres.xr ||
268 >                                        pres2.yr != picres.yr) {
269 >                                fprintf(stderr, "%s: resolution mismatch\n",
270                                                  progname);
271                                  quit(1);
272                          }
273 +                }
274 +                                                /* add to header */
275 +                printargs(i, argv, stdout);
276 +                if (expval < .99 || expval > 1.01)
277 +                        fputexpos(expval, stdout);
278 +                fputformat(COLRFMT, stdout);
279 +                putchar('\n');
280 +                fputsresolu(&picres, stdout);   /* always put resolution */
281 +                valtopix();
282 +        } else {
283 + #ifdef MSDOS
284 +                setmode(fileno(fin), O_BINARY);
285 +                if (format != 'a' && format != 'i')
286 +                        setmode(fileno(stdout), O_BINARY);
287 + #endif
288 +                                                /* get header */
289 +                getheader(fin, checkhead, NULL);
290 +                if (wrongformat) {
291 +                        fprintf(stderr, "%s: input not a Radiance picture\n",
292 +                                        progname);
293 +                        quit(1);
294 +                }
295 +                if (!fgetsresolu(&picres, fin)) {
296 +                        fprintf(stderr, "%s: missing resolution\n", progname);
297 +                        quit(1);
298 +                }
299                  if (header) {
300                          printargs(i, argv, stdout);
301 <                        printf("\n");
301 >                        if (expval < .99 || expval > 1.01)
302 >                                fputexpos(expval, stdout);
303 >                        fputformat(fmtid, stdout);
304 >                        putchar('\n');
305                  }
306 +                if (resolution)                 /* put resolution */
307 +                        fputsresolu(&picres, stdout);
308                  pixtoval();
309          }
310  
311          quit(0);
312 + seekerr:
313 +        fprintf(stderr, "%s: cannot skip %ld bytes on input\n",
314 +                        progname, skipbytes);
315 +        quit(1);
316   }
317  
318  
319   checkhead(line)                         /* deal with line from header */
320   char  *line;
321   {
322 +        char    fmt[32];
323          double  d;
324          COLOR   ctmp;
325  
326 <        if (header)
327 <                fputs(line, stdout);
328 <        if (isexpos(line)) {
326 >        if (formatval(fmt, line))
327 >                wrongformat = strcmp(fmt, COLRFMT);
328 >        else if (original && isexpos(line)) {
329                  d = 1.0/exposval(line);
330                  scalecolor(exposure, d);
331 <        } else if (iscolcor(line)) {
331 >                doexposure++;
332 >        } else if (original && iscolcor(line)) {
333                  colcorval(ctmp, line);
334 <                colval(exposure,RED) /= colval(ctmp,RED);
335 <                colval(exposure,GRN) /= colval(ctmp,GRN);
336 <                colval(exposure,BLU) /= colval(ctmp,BLU);
337 <        }
334 >                setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
335 >                                colval(exposure,GRN)/colval(ctmp,GRN),
336 >                                colval(exposure,BLU)/colval(ctmp,BLU));
337 >                doexposure++;
338 >        } else if (header)
339 >                fputs(line, stdout);
340   }
341  
342  
343   pixtoval()                              /* convert picture to values */
344   {
345 <        COLOR  *scanln;
345 >        register COLOR  *scanln;
346 >        int  dogamma;
347          COLOR  lastc;
348 +        FLOAT  hv[2];
349          int  y;
350          register int  x;
351  
352 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
352 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
353          if (scanln == NULL) {
354                  fprintf(stderr, "%s: out of memory\n", progname);
355                  quit(1);
356          }
357 +        dogamma = gamcor < .95 || gamcor > 1.05;
358          setcolor(lastc, 0.0, 0.0, 0.0);
359 <        for (y = yres-1; y >= 0; y--) {
360 <                if (freadscan(scanln, xres, fin) < 0) {
359 >        for (y = 0; y < numscans(&picres); y++) {
360 >                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
361                          fprintf(stderr, "%s: read error\n", progname);
362                          quit(1);
363                  }
364 <                for (x = 0; x < xres; x++) {
364 >                for (x = 0; x < scanlen(&picres); x++) {
365                          if (uniq)
366 <                                if (    scanln[x][RED] == lastc[RED] &&
367 <                                        scanln[x][GRN] == lastc[GRN] &&
368 <                                        scanln[x][BLU] == lastc[BLU]    )
366 >                                if (    colval(scanln[x],RED) ==
367 >                                                colval(lastc,RED) &&
368 >                                        colval(scanln[x],GRN) ==
369 >                                                colval(lastc,GRN) &&
370 >                                        colval(scanln[x],BLU) ==
371 >                                                colval(lastc,BLU)       )
372                                          continue;
373                                  else
374                                          copycolor(lastc, scanln[x]);
375 <                        if (original)
375 >                        if (doexposure)
376                                  multcolor(scanln[x], exposure);
377 <                        if (!dataonly)
378 <                                printf("%7d %7d ", x, y);
377 >                        if (dogamma)
378 >                                setcolor(scanln[x],
379 >                                pow(colval(scanln[x],RED), 1.0/gamcor),
380 >                                pow(colval(scanln[x],GRN), 1.0/gamcor),
381 >                                pow(colval(scanln[x],BLU), 1.0/gamcor));
382 >                        if (!dataonly) {
383 >                                pix2loc(hv, &picres, x, y);
384 >                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
385 >                                                (int)(hv[1]*picres.yr));
386 >                        }
387                          if ((*putval)(scanln[x], stdout) < 0) {
388                                  fprintf(stderr, "%s: write error\n", progname);
389                                  quit(1);
# Line 215 | Line 396 | pixtoval()                             /* convert picture to values */
396  
397   valtopix()                      /* convert values to a pixel file */
398   {
399 <        COLOR  *scanln;
399 >        int  dogamma;
400 >        register COLOR  *scanln;
401          int  y;
402          register int  x;
403  
404 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
404 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
405          if (scanln == NULL) {
406                  fprintf(stderr, "%s: out of memory\n", progname);
407                  quit(1);
408          }
409 <        for (y = yres-1; y >= 0; y--) {
410 <                for (x = 0; x < xres; x++) {
411 <                        if (!dataonly)
409 >        dogamma = gamcor < .95 || gamcor > 1.05;
410 >        for (y = 0; y < numscans(&picres); y++) {
411 >                for (x = 0; x < scanlen(&picres); x++) {
412 >                        if (!dataonly) {
413                                  fscanf(fin, "%*d %*d");
414 <                        if ((*getval)(scanln[x], fin) < 0) {
414 >                                if (fin2 != NULL) {
415 >                                        fscanf(fin2, "%*d %*d");
416 >                                        fscanf(fin3, "%*d %*d");
417 >                                }
418 >                        }
419 >                        if ((*getval)(scanln[x], fin, fin2, fin3) < 0) {
420                                  fprintf(stderr, "%s: read error\n", progname);
421                                  quit(1);
422                          }
423 +                        if (dogamma)
424 +                                setcolor(scanln[x],
425 +                                        pow(colval(scanln[x],RED), gamcor),
426 +                                        pow(colval(scanln[x],GRN), gamcor),
427 +                                        pow(colval(scanln[x],BLU), gamcor));
428 +                        if (doexposure)
429 +                                multcolor(scanln[x], exposure);
430                  }
431 <                if (fwritescan(scanln, xres, stdout) < 0
237 <                                || fflush(stdout) < 0) {
431 >                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
432                          fprintf(stderr, "%s: write error\n", progname);
433                          quit(1);
434                  }
# Line 250 | Line 444 | int  code;
444   }
445  
446  
447 < getcascii(col, fp)              /* get an ascii color value from fp */
447 > getcascii(col, f1, f2, f3)      /* get an ascii color value from stream(s) */
448   COLOR  col;
449 < FILE  *fp;
449 > FILE  *f1, *f2, *f3;
450   {
451 <        double  vd[3];
451 >        double  vd[3];
452  
453 <        if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
454 <                return(-1);
455 <        setcolor(col, vd[0], vd[1], vd[2]);
453 >        if (f2 == NULL) {
454 >                if (fscanf(f1, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
455 >                        return(-1);
456 >        } else {
457 >                if (fscanf(f1, "%lf", &vd[0]) != 1 ||
458 >                                fscanf(f2, "%lf", &vd[1]) != 1 ||
459 >                                fscanf(f3, "%lf", &vd[2]) != 1)
460 >                        return(-1);
461 >        }
462 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
463          return(0);
464   }
465  
466  
467 < getcdouble(col, fp)             /* get a double color value from fp */
467 > getcdouble(col, f1, f2, f3)     /* get a double color value from stream(s) */
468   COLOR  col;
469 < FILE  *fp;
469 > FILE  *f1, *f2, *f3;
470   {
471 <        double  vd[3];
471 >        double  vd[3];
472  
473 <        if (fread((char *)vd, sizeof(double), 3, fp) != 3)
474 <                return(-1);
475 <        setcolor(col, vd[0], vd[1], vd[2]);
473 >        if (f2 == NULL) {
474 >                if (fread((char *)vd, sizeof(double), 3, f1) != 3)
475 >                        return(-1);
476 >        } else {
477 >                if (fread((char *)vd, sizeof(double), 1, f1) != 1 ||
478 >                        fread((char *)(vd+1), sizeof(double), 1, f2) != 1 ||
479 >                        fread((char *)(vd+2), sizeof(double), 1, f3) != 1)
480 >                        return(-1);
481 >        }
482 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
483          return(0);
484   }
485  
486  
487 < getcfloat(col, fp)              /* get a float color value from fp */
487 > getcfloat(col, f1, f2, f3)      /* get a float color value from stream(s) */
488   COLOR  col;
489 < FILE  *fp;
489 > FILE  *f1, *f2, *f3;
490   {
491          float  vf[3];
492  
493 <        if (fread((char *)vf, sizeof(float), 3, fp) != 3)
494 <                return(-1);
495 <        setcolor(col, vf[0], vf[1], vf[2]);
493 >        if (f2 == NULL) {
494 >                if (fread((char *)vf, sizeof(float), 3, f1) != 3)
495 >                        return(-1);
496 >        } else {
497 >                if (fread((char *)vf, sizeof(float), 1, f1) != 1 ||
498 >                        fread((char *)(vf+1), sizeof(float), 1, f2) != 1 ||
499 >                        fread((char *)(vf+2), sizeof(float), 1, f3) != 1)
500 >                        return(-1);
501 >        }
502 >        setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
503          return(0);
504   }
505  
506  
507 < getcint(col, fp)                /* get an int color value from fp */
507 > getcint(col, f1, f2, f3)        /* get an int color value from stream(s) */
508   COLOR  col;
509 < FILE  *fp;
509 > FILE  *f1, *f2, *f3;
510   {
511          int  vi[3];
512  
513 <        if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
514 <                return(-1);
515 <        setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
513 >        if (f2 == NULL) {
514 >                if (fscanf(f1, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
515 >                        return(-1);
516 >        } else {
517 >                if (fscanf(f1, "%d", &vi[0]) != 1 ||
518 >                                fscanf(f2, "%d", &vi[1]) != 1 ||
519 >                                fscanf(f3, "%d", &vi[2]) != 1)
520 >                        return(-1);
521 >        }
522 >        setcolor(col, (vi[rord[RED]]+.5)/256.,
523 >                        (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
524          return(0);
525   }
526  
527  
528 < getcbyte(col, fp)               /* get a byte color value from fp */
528 > getcbyte(col, f1, f2, f3)       /* get a byte color value from stream(s) */
529   COLOR  col;
530 < FILE  *fp;
530 > FILE  *f1, *f2, *f3;
531   {
532          BYTE  vb[3];
533  
534 <        if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
535 <                return(-1);
536 <        setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
534 >        if (f2 == NULL) {
535 >                if (fread((char *)vb, sizeof(BYTE), 3, f1) != 3)
536 >                        return(-1);
537 >        } else {
538 >                if (fread((char *)vb, sizeof(BYTE), 1, f1) != 1 ||
539 >                        fread((char *)(vb+1), sizeof(BYTE), 1, f2) != 1 ||
540 >                        fread((char *)(vb+2), sizeof(BYTE), 1, f3) != 1)
541 >                        return(-1);
542 >        }
543 >        setcolor(col, (vb[rord[RED]]+.5)/256.,
544 >                        (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
545          return(0);
546   }
547  
# Line 319 | Line 550 | getbascii(col, fp)             /* get an ascii brightness value f
550   COLOR  col;
551   FILE  *fp;
552   {
553 <        double  vd;
553 >        double  vd;
554  
555          if (fscanf(fp, "%lf", &vd) != 1)
556                  return(-1);
# Line 332 | Line 563 | getbdouble(col, fp)            /* get a double brightness value
563   COLOR  col;
564   FILE  *fp;
565   {
566 <        double  vd;
566 >        double  vd;
567  
568          if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
569                  return(-1);
# Line 359 | Line 590 | COLOR  col;
590   FILE  *fp;
591   {
592          int  vi;
593 <        double  d;
593 >        double  d;
594  
595          if (fscanf(fp, "%d", &vi) != 1)
596                  return(-1);
# Line 374 | Line 605 | COLOR  col;
605   FILE  *fp;
606   {
607          BYTE  vb;
608 <        double  d;
608 >        double  d;
609  
610          if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
611                  return(-1);
# Line 389 | Line 620 | COLOR  col;
620   FILE  *fp;
621   {
622          fprintf(fp, "%15.3e %15.3e %15.3e\n",
623 <                        colval(col,RED),
624 <                        colval(col,GRN),
625 <                        colval(col,BLU));
623 >                        colval(col,ord[0]),
624 >                        colval(col,ord[1]),
625 >                        colval(col,ord[2]));
626  
627          return(ferror(fp) ? -1 : 0);
628   }
# Line 403 | Line 634 | FILE  *fp;
634   {
635          float  vf[3];
636  
637 <        vf[0] = colval(col,RED);
638 <        vf[1] = colval(col,GRN);
639 <        vf[2] = colval(col,BLU);
637 >        vf[0] = colval(col,ord[0]);
638 >        vf[1] = colval(col,ord[1]);
639 >        vf[2] = colval(col,ord[2]);
640          fwrite((char *)vf, sizeof(float), 3, fp);
641  
642          return(ferror(fp) ? -1 : 0);
# Line 416 | Line 647 | putcdouble(col, fp)                    /* put a double color to fp */
647   COLOR  col;
648   FILE  *fp;
649   {
650 <        double  vd[3];
650 >        double  vd[3];
651  
652 <        vd[0] = colval(col,RED);
653 <        vd[1] = colval(col,GRN);
654 <        vd[2] = colval(col,BLU);
652 >        vd[0] = colval(col,ord[0]);
653 >        vd[1] = colval(col,ord[1]);
654 >        vd[2] = colval(col,ord[2]);
655          fwrite((char *)vd, sizeof(double), 3, fp);
656  
657          return(ferror(fp) ? -1 : 0);
# Line 432 | Line 663 | COLOR  col;
663   FILE  *fp;
664   {
665          fprintf(fp, "%d %d %d\n",
666 <                        (int)(colval(col,RED)*256.),
667 <                        (int)(colval(col,GRN)*256.),
668 <                        (int)(colval(col,BLU)*256.));
666 >                        (int)(colval(col,ord[0])*256.),
667 >                        (int)(colval(col,ord[1])*256.),
668 >                        (int)(colval(col,ord[2])*256.));
669  
670          return(ferror(fp) ? -1 : 0);
671   }
# Line 447 | Line 678 | FILE  *fp;
678          register int  i;
679          BYTE  vb[3];
680  
681 <        i = colval(col,RED)*256.;
681 >        i = colval(col,ord[0])*256.;
682          vb[0] = min(i,255);
683 <        i = colval(col,GRN)*256.;
683 >        i = colval(col,ord[1])*256.;
684          vb[1] = min(i,255);
685 <        i = colval(col,BLU)*256.;
685 >        i = colval(col,ord[2])*256.;
686          vb[2] = min(i,255);
687          fwrite((char *)vb, sizeof(BYTE), 3, fp);
688  
# Line 486 | Line 717 | putbdouble(col, fp)                    /* put a double brightness to fp
717   COLOR  col;
718   FILE  *fp;
719   {
720 <        double  vd;
720 >        double  vd;
721  
722          vd = bright(col);
723          fwrite((char *)&vd, sizeof(double), 1, fp);
# Line 520 | Line 751 | FILE  *fp;
751   }
752  
753  
754 + putpascii(col, fp)                      /* put an ascii primary to fp */
755 + COLOR  col;
756 + FILE  *fp;
757 + {
758 +        fprintf(fp, "%15.3e\n", colval(col,putprim));
759 +
760 +        return(ferror(fp) ? -1 : 0);
761 + }
762 +
763 +
764 + putpfloat(col, fp)                      /* put a float primary to fp */
765 + COLOR  col;
766 + FILE  *fp;
767 + {
768 +        float  vf;
769 +
770 +        vf = colval(col,putprim);
771 +        fwrite((char *)&vf, sizeof(float), 1, fp);
772 +
773 +        return(ferror(fp) ? -1 : 0);
774 + }
775 +
776 +
777 + putpdouble(col, fp)                     /* put a double primary to fp */
778 + COLOR  col;
779 + FILE  *fp;
780 + {
781 +        double  vd;
782 +
783 +        vd = colval(col,putprim);
784 +        fwrite((char *)&vd, sizeof(double), 1, fp);
785 +
786 +        return(ferror(fp) ? -1 : 0);
787 + }
788 +
789 +
790 + putpint(col, fp)                        /* put an int primary to fp */
791 + COLOR  col;
792 + FILE  *fp;
793 + {
794 +        fprintf(fp, "%d\n", (int)(colval(col,putprim)*256.));
795 +
796 +        return(ferror(fp) ? -1 : 0);
797 + }
798 +
799 +
800 + putpbyte(col, fp)                       /* put a byte primary to fp */
801 + COLOR  col;
802 + FILE  *fp;
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, fp);
810 +
811 +        return(ferror(fp) ? -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                  }
829                  return;
830          case 'f':                                       /* binary float */
831 <                if (brightonly) {
831 >                if (putprim == BRIGHT) {
832                          getval = getbfloat;
833                          putval = putbfloat;
834 +                } else if (putprim != ALL) {
835 +                        getval = getbfloat;
836 +                        putval = putpfloat;
837                  } else {
838                          getval = getcfloat;
839                          putval = putcfloat;
840                  }
841                  return;
842          case 'd':                                       /* binary double */
843 <                if (brightonly) {
843 >                if (putprim == BRIGHT) {
844                          getval = getbdouble;
845                          putval = putbdouble;
846 +                } else if (putprim != ALL) {
847 +                        getval = getbdouble;
848 +                        putval = putpdouble;
849                  } else {
850                          getval = getcdouble;
851                          putval = putcdouble;
852                  }
853                  return;
854          case 'i':                                       /* integer */
855 <                if (brightonly) {
855 >                if (putprim == BRIGHT) {
856                          getval = getbint;
857                          putval = putbint;
858 +                } else if (putprim != ALL) {
859 +                        getval = getbint;
860 +                        putval = putpint;
861                  } else {
862                          getval = getcint;
863                          putval = putcint;
864                  }
865                  return;
866          case 'b':                                       /* byte */
867 <                if (brightonly) {
867 >                if (putprim == BRIGHT) {
868                          getval = getbbyte;
869                          putval = putbbyte;
870 +                } else if (putprim != ALL) {
871 +                        getval = getbbyte;
872 +                        putval = putpbyte;
873                  } else {
874                          getval = getcbyte;
875                          putval = putcbyte;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines