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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines