ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.37
Committed: Wed Aug 14 18:20:02 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.36: +11 -1 lines
Log Message:
Added explicit byte-swap checks in headers using BigEndian= line

File Contents

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