ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.27
Committed: Fri Oct 1 07:45:30 2004 UTC (19 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.26: +4 -7 lines
Log Message:
Fixed bug where pvalue -o would report watts/sr/m^2 from XYZE input

File Contents

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