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.8 by greg, Sun Feb 27 10:16:48 1994 UTC vs.
Revision 2.15 by greg, Tue Feb 25 16:22:05 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   *
# Line 14 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #include  "color.h"
13  
14 + #include  <time.h>
15 +
16   #include  "resolu.h"
17  
18 + typedef unsigned short uint16;  /* sizeof (uint16) must == 2 */
19 +
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? */
# Line 26 | Line 33 | int  doexposure = 0;           /* exposure change? (>100 to pri
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  
# Line 35 | Line 42 | char  *fmtid = "ascii";                /* format identifier for head
42  
43   int  header = 1;                /* do header? */
44  
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? */
# Line 51 | Line 62 | COLOR  exposure = WHTCOLOR;
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   {
90          extern int  checkhead();
91 +        extern long  atol();
92          double  d, expval = 1.0;
93          int  i;
94  
# Line 74 | Line 103 | char  **argv;
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 = argv[i][0] == '-';
111                                  break;
# Line 91 | Line 123 | char  **argv;
123                                  if (argv[i+1][0] == '-' || argv[i+1][0] == '+')
124                                          d = pow(2.0, d);
125                                  if (argv[i][0] == '-')
126 <                                        doexposure = 100;
126 >                                        expval *= d;
127                                  scalecolor(exposure, d);
96                                expval *= d;
128                                  doexposure++;
129                                  i++;
130                                  break;
# Line 107 | Line 138 | char  **argv;
138                          case 'r':               /* reverse conversion */
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 = argv[i][0] == '-';
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 = argv[i][0] == '-';
157                                  switch (argv[i][2]) {
# Line 127 | Line 169 | char  **argv;
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';
# Line 145 | Line 192 | char  **argv;
192                          case 'X':               /* x resolution */
193                                  resolution = 0;
194                                  if (argv[i][0] == '-')
195 <                                        picres.or |= XDECR;
195 >                                        picres.rt |= XDECR;
196                                  picres.xr = atoi(argv[++i]);
197                                  break;
198                          case 'y':               /* y resolution */
199                          case 'Y':               /* y resolution */
200                                  resolution = 0;
201                                  if (argv[i][0] == '-')
202 <                                        picres.or |= YDECR;
202 >                                        picres.rt |= YDECR;
203                                  if (picres.xr == 0)
204 <                                        picres.or |= YMAJOR;
204 >                                        picres.rt |= YMAJOR;
205                                  picres.yr = atoi(argv[++i]);
206                                  break;
207                          default:
# Line 172 | Line 219 | unkopt:
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;
# Line 179 | Line 231 | unkopt:
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  
193        set_io();
194
271          if (reverse) {
272   #ifdef MSDOS
273                  setmode(fileno(stdout), O_BINARY);
# Line 205 | Line 281 | unkopt:
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 */
# Line 213 | Line 293 | unkopt:
293                          fprintf(stderr, "%s: missing resolution\n", progname);
294                          quit(1);
295                  }
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 (doexposure > 100)
313 >                if (expval < .99 || expval > 1.01)
314                          fputexpos(expval, stdout);
315                  fputformat(COLRFMT, stdout);
316                  putchar('\n');
# Line 230 | Line 325 | unkopt:
325                                                  /* get header */
326                  getheader(fin, checkhead, NULL);
327                  if (wrongformat) {
328 <                        fprintf(stderr, "%s: input not a Radiance picture\n",
328 >                        fprintf(stderr,
329 >                                "%s: input not a Radiance RGBE picture\n",
330                                          progname);
331                          quit(1);
332                  }
# Line 240 | Line 336 | unkopt:
336                  }
337                  if (header) {
338                          printargs(i, argv, stdout);
339 <                        if (doexposure > 100)
339 >                        if (expval < .99 || expval > 1.01)
340                                  fputexpos(expval, stdout);
341                          fputformat(fmtid, stdout);
342                          putchar('\n');
# Line 254 | Line 350 | unkopt:
350   }
351  
352  
353 + int
354   checkhead(line)                         /* deal with line from header */
355   char  *line;
356   {
# Line 261 | Line 358 | char  *line;
358          double  d;
359          COLOR   ctmp;
360  
361 <        if (formatval(fmt, line))
362 <                wrongformat = strcmp(fmt, COLRFMT);
363 <        else if (original && isexpos(line)) {
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++;
# Line 275 | Line 381 | char  *line;
381                  doexposure++;
382          } else if (header)
383                  fputs(line, stdout);
384 +        return(0);
385   }
386  
387  
# Line 284 | Line 391 | pixtoval()                             /* convert picture to values */
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  
# Line 293 | Line 402 | pixtoval()                             /* convert picture to values */
402                  quit(1);
403          }
404          dogamma = gamcor < .95 || gamcor > 1.05;
405 <        setcolor(lastc, 0.0, 0.0, 0.0);
406 <        for (y = 0; y < numscans(&picres); y++) {
407 <                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
408 <                        fprintf(stderr, "%s: read error\n", progname);
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 < scanlen(&picres); x++) {
418 <                        if (uniq)
419 <                                if (    colval(scanln[x],RED) ==
420 <                                                colval(lastc,RED) &&
421 <                                        colval(scanln[x],GRN) ==
307 <                                                colval(lastc,GRN) &&
308 <                                        colval(scanln[x],BLU) ==
309 <                                                colval(lastc,BLU)       )
310 <                                        continue;
311 <                                else
312 <                                        copycolor(lastc, scanln[x]);
313 <                        if (doexposure)
314 <                                multcolor(scanln[x], exposure);
315 <                        if (dogamma)
316 <                                setcolor(scanln[x],
317 <                                pow(colval(scanln[x],RED), 1.0/gamcor),
318 <                                pow(colval(scanln[x],GRN), 1.0/gamcor),
319 <                                pow(colval(scanln[x],BLU), 1.0/gamcor));
320 <                        if (!dataonly) {
321 <                                pix2loc(hv, &picres, x, y);
322 <                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
323 <                                                (int)(hv[1]*picres.yr));
324 <                        }
325 <                        if ((*putval)(scanln[x], stdout) < 0) {
326 <                                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  
# Line 345 | Line 470 | valtopix()                     /* convert values to a pixel file */
470                  quit(1);
471          }
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)
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                          }
# Line 366 | Line 497 | valtopix()                     /* convert values to a pixel file */
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 377 | 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;
382 FILE  *fp;
514   {
515          double  vd[3];
516  
517 <        if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
518 <                return(-1);
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;
395 FILE  *fp;
533   {
534          double  vd[3];
535  
536 <        if (fread((char *)vd, sizeof(double), 3, fp) != 3)
537 <                return(-1);
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;
408 FILE  *fp;
552   {
553          float  vf[3];
554  
555 <        if (fread((char *)vf, sizeof(float), 3, fp) != 3)
556 <                return(-1);
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;
421 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);
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;
435 FILE  *fp;
591   {
592          BYTE  vb[3];
593  
594 <        if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
595 <                return(-1);
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;
449 FILE  *fp;
611   {
612 +        uint16  vw[3];
613 +
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(fp, "%lf", &vd) != 1)
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;
462 FILE  *fp;
643   {
644          double  vd;
645  
646 <        if (fread((char *)&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;
475 FILE  *fp;
655   {
656          float  vf;
657  
658 <        if (fread((char *)&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;
488 FILE  *fp;
667   {
668          int  vi;
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 498 | 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;
503 FILE  *fp;
681   {
682          BYTE  vb;
683          double  d;
684  
685 <        if (fread((char *)&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 513 | 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;
518 FILE  *fp;
695   {
696 <        fprintf(fp, "%15.3e %15.3e %15.3e\n",
696 >        uint16  vw;
697 >        double  d;
698 >
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 > putcascii(col)                  /* put an ascii color to stdout */
708 > COLOR  col;
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(fp) ? -1 : 0);
715 >        return(ferror(stdout) ? -1 : 0);
716   }
717  
718  
719 < putcfloat(col, fp)                      /* put a float color to fp */
719 > putcfloat(col)                  /* put a float color to stdout */
720   COLOR  col;
531 FILE  *fp;
721   {
722          float  vf[3];
723  
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, fp);
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;
546 FILE  *fp;
735   {
736          double  vd[3];
737  
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, fp);
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;
561 FILE  *fp;
749   {
750 <        fprintf(fp, "%d %d %d\n",
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;
574 FILE  *fp;
761   {
762 <        register int  i;
762 >        long  i;
763          BYTE  vb[3];
764  
765          i = colval(col,ord[0])*256.;
# Line 582 | Line 768 | FILE  *fp;
768          vb[1] = min(i,255);
769          i = colval(col,ord[2])*256.;
770          vb[2] = min(i,255);
771 <        fwrite((char *)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;
593 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;
603 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((char *)&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;
616 FILE  *fp;
818   {
819          double  vd;
820  
821 <        vd = bright(col);
822 <        fwrite((char *)&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;
629 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;
639 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((char *)&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