ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.43
Committed: Wed Sep 11 18:56:12 2024 UTC (7 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.42: +94 -35 lines
Log Message:
feat(pcond,pvalue): Added hyperspectral input handling

File Contents

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