ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.41
Committed: Fri Feb 4 20:11:49 2022 UTC (3 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +14 -2 lines
Log Message:
feat(pvalue,pextrem): Added -O option to report watts/sr/meter^2 always

File Contents

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