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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines