ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.29
Committed: Sat Dec 23 17:27:46 2006 UTC (17 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R0, rad3R9
Changes since 2.28: +11 -59 lines
Log Message:
Added byte-swapping i/o options to rcalc

File Contents

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