ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.35
Committed: Mon Jul 15 22:39:50 2019 UTC (4 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +22 -13 lines
Log Message:
Made pvalue -u option mean "uncompressed" with -r.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pvalue.c,v 2.34 2018/08/02 18:33:46 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 #if defined(_WIN32) || defined(_WIN64)
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 #if defined(_WIN32) || defined(_WIN64)
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[MAXFMTLEN];
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 COLOR *scanln;
423 int dogamma;
424 COLOR lastc;
425 RREAL hv[2];
426 int startprim, endprim;
427 long startpos;
428 int x, y;
429
430 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
431 if (scanln == NULL) {
432 fprintf(stderr, "%s: out of memory\n", progname);
433 quit(1);
434 }
435 dogamma = gamcor < .95 || gamcor > 1.05;
436 if (putprim == ALL && !interleave) {
437 startprim = RED; endprim = BLU;
438 startpos = ftell(fin);
439 } else {
440 startprim = putprim; endprim = putprim;
441 }
442 for (putprim = startprim; putprim <= endprim; putprim++) {
443 if (putprim != startprim && fseek(fin, startpos, 0)) {
444 fprintf(stderr, "%s: seek error on input file\n",
445 progname);
446 quit(1);
447 }
448 set_io();
449 setcolor(lastc, 0.0, 0.0, 0.0);
450 for (y = 0; y < numscans(&picres); y++) {
451 if (freadscan(scanln, scanlen(&picres), fin) < 0) {
452 fprintf(stderr, "%s: read error\n", progname);
453 quit(1);
454 }
455 for (x = 0; x < scanlen(&picres); x++) {
456 if (uniq) {
457 if ( colval(scanln[x],RED) ==
458 colval(lastc,RED) &&
459 colval(scanln[x],GRN) ==
460 colval(lastc,GRN) &&
461 colval(scanln[x],BLU) ==
462 colval(lastc,BLU) )
463 continue;
464 else
465 copycolor(lastc, scanln[x]);
466 }
467 if (doexposure)
468 multcolor(scanln[x], exposure);
469 if (dogamma)
470 setcolor(scanln[x],
471 pow(colval(scanln[x],RED), 1.0/gamcor),
472 pow(colval(scanln[x],GRN), 1.0/gamcor),
473 pow(colval(scanln[x],BLU), 1.0/gamcor));
474 if (!dataonly) {
475 pix2loc(hv, &picres, x, y);
476 printf("%7d %7d ",
477 (int)(hv[0]*picres.xr),
478 (int)(hv[1]*picres.yr));
479 }
480 if ((*putval)(scanln[x]) < 0) {
481 fprintf(stderr, "%s: write error\n",
482 progname);
483 quit(1);
484 }
485 }
486 }
487 }
488 free((void *)scanln);
489 }
490
491
492 static void
493 valtopix(void) /* convert values to a pixel file */
494 {
495 int dogamma;
496 COLOR *scanln;
497 COLR rgbe;
498 int x, y;
499
500 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
501 if (scanln == NULL) {
502 fprintf(stderr, "%s: out of memory\n", progname);
503 quit(1);
504 }
505 dogamma = gamcor < .95 || gamcor > 1.05;
506 set_io();
507 for (y = 0; y < numscans(&picres); y++) {
508 for (x = 0; x < scanlen(&picres); x++) {
509 if (!dataonly) {
510 fscanf(fin, "%*d %*d");
511 if (fin2 != NULL) {
512 fscanf(fin2, "%*d %*d");
513 fscanf(fin3, "%*d %*d");
514 }
515 }
516 if ((*getval)(scanln[x]) < 0) {
517 fprintf(stderr, "%s: read error\n", progname);
518 quit(1);
519 }
520 if (dogamma)
521 setcolor(scanln[x],
522 pow(colval(scanln[x],RED), gamcor),
523 pow(colval(scanln[x],GRN), gamcor),
524 pow(colval(scanln[x],BLU), gamcor));
525 if (doexposure)
526 multcolor(scanln[x], exposure);
527 if (uniq) { /* uncompressed? */
528 setcolr(rgbe, scanln[x][RED],
529 scanln[x][GRN],
530 scanln[x][BLU]);
531 if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1)
532 goto writerr;
533 }
534 }
535 /* write scan if compressed */
536 if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0)
537 goto writerr;
538 }
539 free((void *)scanln);
540 return;
541 writerr:
542 fprintf(stderr, "%s: write error\n", progname);
543 quit(1);
544 }
545
546
547 void
548 quit(code)
549 int code;
550 {
551 exit(code);
552 }
553
554
555 static int
556 getcascii( /* get an ascii color value from stream(s) */
557 COLOR col
558 )
559 {
560 double vd[3];
561
562 if (fin2 == NULL) {
563 if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
564 return(-1);
565 } else {
566 if (fscanf(fin, "%lf", &vd[0]) != 1 ||
567 fscanf(fin2, "%lf", &vd[1]) != 1 ||
568 fscanf(fin3, "%lf", &vd[2]) != 1)
569 return(-1);
570 }
571 setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
572 return(0);
573 }
574
575
576 static int
577 getcdouble( /* get a double color value from stream(s) */
578 COLOR col
579 )
580 {
581 double vd[3];
582
583 if (fin2 == NULL) {
584 if (getbinary(vd, sizeof(double), 3, fin) != 3)
585 return(-1);
586 } else {
587 if (getbinary(vd, sizeof(double), 1, fin) != 1 ||
588 getbinary(vd+1, sizeof(double), 1, fin2) != 1 ||
589 getbinary(vd+2, sizeof(double), 1, fin3) != 1)
590 return(-1);
591 }
592 if (swapbytes)
593 swap64((char *)vd, 3);
594 setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
595 return(0);
596 }
597
598
599 static int
600 getcfloat( /* get a float color value from stream(s) */
601 COLOR col
602 )
603 {
604 float vf[3];
605
606 if (fin2 == NULL) {
607 if (getbinary(vf, sizeof(float), 3, fin) != 3)
608 return(-1);
609 } else {
610 if (getbinary(vf, sizeof(float), 1, fin) != 1 ||
611 getbinary(vf+1, sizeof(float), 1, fin2) != 1 ||
612 getbinary(vf+2, sizeof(float), 1, fin3) != 1)
613 return(-1);
614 }
615 if (swapbytes)
616 swap32((char *)vf, 3);
617 setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
618 return(0);
619 }
620
621
622 static int
623 getcint( /* get an int color value from stream(s) */
624 COLOR col
625 )
626 {
627 int vi[3];
628
629 if (fin2 == NULL) {
630 if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
631 return(-1);
632 } else {
633 if (fscanf(fin, "%d", &vi[0]) != 1 ||
634 fscanf(fin2, "%d", &vi[1]) != 1 ||
635 fscanf(fin3, "%d", &vi[2]) != 1)
636 return(-1);
637 }
638 setcolor(col, (vi[rord[RED]]+.5)/256.,
639 (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
640 return(0);
641 }
642
643
644 static int
645 getcbyte( /* get a byte color value from stream(s) */
646 COLOR col
647 )
648 {
649 uby8 vb[3];
650
651 if (fin2 == NULL) {
652 if (getbinary(vb, sizeof(uby8), 3, fin) != 3)
653 return(-1);
654 } else {
655 if (getbinary(vb, sizeof(uby8), 1, fin) != 1 ||
656 getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 ||
657 getbinary(vb+2, sizeof(uby8), 1, fin3) != 1)
658 return(-1);
659 }
660 setcolor(col, (vb[rord[RED]]+.5)/256.,
661 (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
662 return(0);
663 }
664
665
666 static int
667 getcword( /* get a 16-bit color value from stream(s) */
668 COLOR col
669 )
670 {
671 uint16 vw[3];
672
673 if (fin2 == NULL) {
674 if (getbinary(vw, sizeof(uint16), 3, fin) != 3)
675 return(-1);
676 } else {
677 if (getbinary(vw, sizeof(uint16), 1, fin) != 1 ||
678 getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 ||
679 getbinary(vw+2, sizeof(uint16), 1, fin3) != 1)
680 return(-1);
681 }
682 if (swapbytes)
683 swap16((char *)vw, 3);
684 setcolor(col, (vw[rord[RED]]+.5)/65536.,
685 (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.);
686 return(0);
687 }
688
689
690 static int
691 getbascii( /* get an ascii brightness value from fin */
692 COLOR col
693 )
694 {
695 double vd;
696
697 if (fscanf(fin, "%lf", &vd) != 1)
698 return(-1);
699 setcolor(col, vd, vd, vd);
700 return(0);
701 }
702
703
704 static int
705 getbdouble( /* get a double brightness value from fin */
706 COLOR col
707 )
708 {
709 double vd;
710
711 if (getbinary(&vd, sizeof(double), 1, fin) != 1)
712 return(-1);
713 if (swapbytes)
714 swap64((char *)&vd, 1);
715 setcolor(col, vd, vd, vd);
716 return(0);
717 }
718
719
720 static int
721 getbfloat( /* get a float brightness value from fin */
722 COLOR col
723 )
724 {
725 float vf;
726
727 if (getbinary(&vf, sizeof(float), 1, fin) != 1)
728 return(-1);
729 if (swapbytes)
730 swap32((char *)&vf, 1);
731 setcolor(col, vf, vf, vf);
732 return(0);
733 }
734
735
736 static int
737 getbint( /* get an int brightness value from fin */
738 COLOR col
739 )
740 {
741 int vi;
742 double d;
743
744 if (fscanf(fin, "%d", &vi) != 1)
745 return(-1);
746 d = (vi+.5)/256.;
747 setcolor(col, d, d, d);
748 return(0);
749 }
750
751
752 static int
753 getbbyte( /* get a byte brightness value from fin */
754 COLOR col
755 )
756 {
757 uby8 vb;
758 double d;
759
760 if (getbinary(&vb, sizeof(uby8), 1, fin) != 1)
761 return(-1);
762 d = (vb+.5)/256.;
763 setcolor(col, d, d, d);
764 return(0);
765 }
766
767
768 static int
769 getbword( /* get a 16-bit brightness value from fin */
770 COLOR col
771 )
772 {
773 uint16 vw;
774 double d;
775
776 if (getbinary(&vw, sizeof(uint16), 1, fin) != 1)
777 return(-1);
778 if (swapbytes)
779 swap16((char *)&vw, 1);
780 d = (vw+.5)/65536.;
781 setcolor(col, d, d, d);
782 return(0);
783 }
784
785
786 static int
787 putcascii( /* put an ascii color to stdout */
788 COLOR col
789 )
790 {
791 fprintf(stdout, "%15.3e %15.3e %15.3e\n",
792 colval(col,ord[0]),
793 colval(col,ord[1]),
794 colval(col,ord[2]));
795
796 return(ferror(stdout) ? -1 : 0);
797 }
798
799
800 static int
801 putcfloat( /* put a float color to stdout */
802 COLOR col
803 )
804 {
805 float vf[3];
806
807 vf[0] = colval(col,ord[0]);
808 vf[1] = colval(col,ord[1]);
809 vf[2] = colval(col,ord[2]);
810 if (swapbytes)
811 swap32((char *)vf, 3);
812 putbinary(vf, sizeof(float), 3, stdout);
813
814 return(ferror(stdout) ? -1 : 0);
815 }
816
817
818 static int
819 putcdouble( /* put a double color to stdout */
820 COLOR col
821 )
822 {
823 double vd[3];
824
825 vd[0] = colval(col,ord[0]);
826 vd[1] = colval(col,ord[1]);
827 vd[2] = colval(col,ord[2]);
828 if (swapbytes)
829 swap64((char *)vd, 3);
830 putbinary(vd, sizeof(double), 3, stdout);
831
832 return(ferror(stdout) ? -1 : 0);
833 }
834
835
836 static int
837 putcint( /* put an int color to stdout */
838 COLOR col
839 )
840 {
841 fprintf(stdout, "%d %d %d\n",
842 (int)(colval(col,ord[0])*256.),
843 (int)(colval(col,ord[1])*256.),
844 (int)(colval(col,ord[2])*256.));
845
846 return(ferror(stdout) ? -1 : 0);
847 }
848
849
850 static int
851 putcbyte( /* put a byte color to stdout */
852 COLOR col
853 )
854 {
855 long i;
856 uby8 vb[3];
857
858 i = colval(col,ord[0])*256.;
859 vb[0] = min(i,255);
860 i = colval(col,ord[1])*256.;
861 vb[1] = min(i,255);
862 i = colval(col,ord[2])*256.;
863 vb[2] = min(i,255);
864 putbinary(vb, sizeof(uby8), 3, stdout);
865
866 return(ferror(stdout) ? -1 : 0);
867 }
868
869
870 static int
871 putcword( /* put a 16-bit color to stdout */
872 COLOR col
873 )
874 {
875 long i;
876 uint16 vw[3];
877
878 i = colval(col,ord[0])*65536.;
879 vw[0] = min(i,65535);
880 i = colval(col,ord[1])*65536.;
881 vw[1] = min(i,65535);
882 i = colval(col,ord[2])*65536.;
883 vw[2] = min(i,65535);
884 if (swapbytes)
885 swap16((char *)vw, 3);
886 putbinary(vw, sizeof(uint16), 3, stdout);
887
888 return(ferror(stdout) ? -1 : 0);
889 }
890
891
892 static int
893 putbascii( /* put an ascii brightness to stdout */
894 COLOR col
895 )
896 {
897 fprintf(stdout, "%15.3e\n", (*mybright)(col));
898
899 return(ferror(stdout) ? -1 : 0);
900 }
901
902
903 static int
904 putbfloat( /* put a float brightness to stdout */
905 COLOR col
906 )
907 {
908 float vf;
909
910 vf = (*mybright)(col);
911 if (swapbytes)
912 swap32((char *)&vf, 1);
913 putbinary(&vf, sizeof(float), 1, stdout);
914
915 return(ferror(stdout) ? -1 : 0);
916 }
917
918
919 static int
920 putbdouble( /* put a double brightness to stdout */
921 COLOR col
922 )
923 {
924 double vd;
925
926 vd = (*mybright)(col);
927 if (swapbytes)
928 swap64((char *)&vd, 1);
929 putbinary(&vd, sizeof(double), 1, stdout);
930
931 return(ferror(stdout) ? -1 : 0);
932 }
933
934
935 static int
936 putbint( /* put an int brightness to stdout */
937 COLOR col
938 )
939 {
940 fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.));
941
942 return(ferror(stdout) ? -1 : 0);
943 }
944
945
946 static int
947 putbbyte( /* put a byte brightness to stdout */
948 COLOR col
949 )
950 {
951 int i;
952 uby8 vb;
953
954 i = (*mybright)(col)*256.;
955 vb = min(i,255);
956 putbinary(&vb, sizeof(uby8), 1, stdout);
957
958 return(ferror(stdout) ? -1 : 0);
959 }
960
961
962 static int
963 putbword( /* put a 16-bit brightness to stdout */
964 COLOR col
965 )
966 {
967 long i;
968 uint16 vw;
969
970 i = (*mybright)(col)*65536.;
971 vw = min(i,65535);
972 if (swapbytes)
973 swap16((char *)&vw, 1);
974 putbinary(&vw, sizeof(uint16), 1, stdout);
975
976 return(ferror(stdout) ? -1 : 0);
977 }
978
979
980 static int
981 putpascii( /* put an ascii primary to stdout */
982 COLOR col
983 )
984 {
985 fprintf(stdout, "%15.3e\n", colval(col,putprim));
986
987 return(ferror(stdout) ? -1 : 0);
988 }
989
990
991 static int
992 putpfloat( /* put a float primary to stdout */
993 COLOR col
994 )
995 {
996 float vf;
997
998 vf = colval(col,putprim);
999 if (swapbytes)
1000 swap32((char *)&vf, 1);
1001 putbinary(&vf, sizeof(float), 1, stdout);
1002
1003 return(ferror(stdout) ? -1 : 0);
1004 }
1005
1006
1007 static int
1008 putpdouble( /* put a double primary to stdout */
1009 COLOR col
1010 )
1011 {
1012 double vd;
1013
1014 vd = colval(col,putprim);
1015 if (swapbytes)
1016 swap64((char *)&vd, 1);
1017 putbinary(&vd, sizeof(double), 1, stdout);
1018
1019 return(ferror(stdout) ? -1 : 0);
1020 }
1021
1022
1023 static int
1024 putpint( /* put an int primary to stdout */
1025 COLOR col
1026 )
1027 {
1028 fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.));
1029
1030 return(ferror(stdout) ? -1 : 0);
1031 }
1032
1033
1034 static int
1035 putpbyte( /* put a byte primary to stdout */
1036 COLOR col
1037 )
1038 {
1039 long i;
1040 uby8 vb;
1041
1042 i = colval(col,putprim)*256.;
1043 vb = min(i,255);
1044 putbinary(&vb, sizeof(uby8), 1, stdout);
1045
1046 return(ferror(stdout) ? -1 : 0);
1047 }
1048
1049
1050 static int
1051 putpword( /* put a 16-bit primary to stdout */
1052 COLOR col
1053 )
1054 {
1055 long i;
1056 uint16 vw;
1057
1058 i = colval(col,putprim)*65536.;
1059 vw = min(i,65535);
1060 if (swapbytes)
1061 swap16((char *)&vw, 1);
1062 putbinary(&vw, sizeof(uint16), 1, stdout);
1063
1064 return(ferror(stdout) ? -1 : 0);
1065 }
1066
1067
1068 static void
1069 set_io(void) /* set put and get functions */
1070 {
1071 switch (format) {
1072 case 'a': /* ascii */
1073 if (putprim == BRIGHT) {
1074 getval = getbascii;
1075 putval = putbascii;
1076 } else if (putprim != ALL) {
1077 getval = getbascii;
1078 putval = putpascii;
1079 } else {
1080 getval = getcascii;
1081 putval = putcascii;
1082 if (reverse && !interleave) {
1083 fprintf(stderr,
1084 "%s: ASCII input files must be interleaved\n",
1085 progname);
1086 quit(1);
1087 }
1088 }
1089 return;
1090 case 'f': /* binary float */
1091 if (putprim == BRIGHT) {
1092 getval = getbfloat;
1093 putval = putbfloat;
1094 } else if (putprim != ALL) {
1095 getval = getbfloat;
1096 putval = putpfloat;
1097 } else {
1098 getval = getcfloat;
1099 putval = putcfloat;
1100 if (reverse && !interleave) {
1101 if (fin2 == NULL)
1102 goto namerr;
1103 if (fseek(fin2,
1104 (long)sizeof(float)*picres.xr*picres.yr, 1))
1105 goto seekerr;
1106 if (fseek(fin3,
1107 (long)sizeof(float)*2*picres.xr*picres.yr, 1))
1108 goto seekerr;
1109 }
1110 }
1111 return;
1112 case 'd': /* binary double */
1113 if (putprim == BRIGHT) {
1114 getval = getbdouble;
1115 putval = putbdouble;
1116 } else if (putprim != ALL) {
1117 getval = getbdouble;
1118 putval = putpdouble;
1119 } else {
1120 getval = getcdouble;
1121 putval = putcdouble;
1122 if (reverse && !interleave) {
1123 if (fin2 == NULL)
1124 goto namerr;
1125 if (fseek(fin2,
1126 (long)sizeof(double)*picres.xr*picres.yr, 1))
1127 goto seekerr;
1128 if (fseek(fin3,
1129 (long)sizeof(double)*2*picres.xr*picres.yr, 1))
1130 goto seekerr;
1131 }
1132 }
1133 return;
1134 case 'i': /* integer */
1135 if (putprim == BRIGHT) {
1136 getval = getbint;
1137 putval = putbint;
1138 } else if (putprim != ALL) {
1139 getval = getbint;
1140 putval = putpint;
1141 } else {
1142 getval = getcint;
1143 putval = putcint;
1144 if (reverse && !interleave) {
1145 fprintf(stderr,
1146 "%s: integer input files must be interleaved\n",
1147 progname);
1148 quit(1);
1149 }
1150 }
1151 return;
1152 case 'b': /* byte */
1153 if (putprim == BRIGHT) {
1154 getval = getbbyte;
1155 putval = putbbyte;
1156 } else if (putprim != ALL) {
1157 getval = getbbyte;
1158 putval = putpbyte;
1159 } else {
1160 getval = getcbyte;
1161 putval = putcbyte;
1162 if (reverse && !interleave) {
1163 if (fin2 == NULL)
1164 goto namerr;
1165 if (fseek(fin2,
1166 (long)sizeof(uby8)*picres.xr*picres.yr, 1))
1167 goto seekerr;
1168 if (fseek(fin3,
1169 (long)sizeof(uby8)*2*picres.xr*picres.yr, 1))
1170 goto seekerr;
1171 }
1172 }
1173 return;
1174 case 'w': /* 16-bit */
1175 if (putprim == BRIGHT) {
1176 getval = getbword;
1177 putval = putbword;
1178 } else if (putprim != ALL) {
1179 getval = getbword;
1180 putval = putpword;
1181 } else {
1182 getval = getcword;
1183 putval = putcword;
1184 if (reverse && !interleave) {
1185 if (fin2 == NULL)
1186 goto namerr;
1187 if (fseek(fin2,
1188 (long)sizeof(uint16)*picres.xr*picres.yr, 1))
1189 goto seekerr;
1190 if (fseek(fin3,
1191 (long)sizeof(uint16)*2*picres.xr*picres.yr, 1))
1192 goto seekerr;
1193 }
1194 }
1195 return;
1196 }
1197 /* badopt: */ /* label not used */
1198 fprintf(stderr, "%s: botched file type\n", progname);
1199 quit(1);
1200 namerr:
1201 fprintf(stderr, "%s: non-interleaved file(s) must be named\n",
1202 progname);
1203 quit(1);
1204 seekerr:
1205 fprintf(stderr, "%s: cannot seek on interleaved input file\n",
1206 progname);
1207 quit(1);
1208 }