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.19 by schorsch, Sun Jun 8 12:03:10 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines