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.14 by greg, Sat Feb 22 02:07:27 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines