ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.40
Committed: Tue Jun 30 22:30:29 2020 UTC (3 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.39: +3 -2 lines
Log Message:
fix(pvalue): removed output of BigEndian flag when not needed for -db mode

File Contents

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