ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.31
Committed: Fri May 20 02:06:39 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R1, rad4R2P1
Changes since 2.30: +16 -16 lines
Log Message:
Changed every instance of BYTE to uby8 to avoid conflicts

File Contents

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