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 2.4 by greg, Fri May 8 11:55:42 1992 UTC vs.
Revision 2.25 by schorsch, Fri Jan 2 12:47:01 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines