ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.36
Committed: Mon Jul 15 23:42:44 2019 UTC (4 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.35: +35 -29 lines
Log Message:
Allow skipping header bytes on standard input and more logical binary mode

File Contents

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