ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.5
Committed: Mon Sep 21 12:14:37 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +24 -14 lines
Log Message:
Changes for PC port

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * pvalue.c - program to print pixel values.
9 *
10 * 4/23/86
11 */
12
13 #include "standard.h"
14
15 #include "color.h"
16
17 #include "resolu.h"
18
19 #define min(a,b) ((a)<(b)?(a):(b))
20
21 RESOLU picres; /* resolution of picture */
22
23 int uniq = 0; /* print only unique values? */
24
25 int original = 0; /* convert back to original values? */
26
27 int dataonly = 0; /* data only format? */
28
29 int brightonly = 0; /* only brightness values? */
30
31 int reverse = 0; /* reverse conversion? */
32
33 int format = 'a'; /* input/output format */
34 char *fmtid = "ascii"; /* format identifier for header */
35
36 int header = 1; /* do header? */
37
38 int resolution = 1; /* put/get resolution string? */
39
40 int wrongformat = 0; /* wrong input format? */
41
42 double gamcor = 1.0; /* gamma correction */
43
44 int ord[3] = {RED, GRN, BLU}; /* RGB ordering */
45 int rord[4]; /* reverse ordering */
46
47 COLOR exposure = WHTCOLOR;
48
49 char *progname;
50
51 FILE *fin;
52
53 int (*getval)(), (*putval)();
54
55
56 main(argc, argv)
57 int argc;
58 char **argv;
59 {
60 extern int checkhead();
61 int i;
62
63 progname = argv[0];
64
65 for (i = 1; i < argc; i++)
66 if (argv[i][0] == '-' || argv[i][0] == '+')
67 switch (argv[i][1]) {
68 case 'h': /* header */
69 header = argv[i][0] == '+';
70 break;
71 case 'H': /* resolution string */
72 resolution = argv[i][0] == '+';
73 break;
74 case 'u': /* unique values */
75 uniq = argv[i][0] == '-';
76 break;
77 case 'o': /* original values */
78 original = argv[i][0] == '-';
79 break;
80 case 'g': /* gamma correction */
81 gamcor = atof(argv[++i]);
82 if (argv[i][0] == '+')
83 gamcor = 1.0/gamcor;
84 break;
85 case 'R': /* reverse byte sequence */
86 if (argv[i][0] == '-') {
87 ord[0]=BLU; ord[1]=GRN; ord[2]=RED;
88 } else {
89 ord[0]=RED; ord[1]=GRN; ord[2]=BLU;
90 }
91 break;
92 case 'r': /* reverse conversion */
93 reverse = argv[i][0] == '-';
94 break;
95 case 'b': /* brightness values */
96 brightonly = argv[i][0] == '-';
97 break;
98 case 'd': /* data only (no indices) */
99 dataonly = argv[i][0] == '-';
100 switch (argv[i][2]) {
101 case '\0':
102 case 'a': /* ascii */
103 format = 'a';
104 fmtid = "ascii";
105 break;
106 case 'i': /* integer */
107 format = 'i';
108 fmtid = "ascii";
109 break;
110 case 'b': /* byte */
111 dataonly = 1;
112 format = 'b';
113 fmtid = "byte";
114 break;
115 case 'f': /* float */
116 dataonly = 1;
117 format = 'f';
118 fmtid = "float";
119 break;
120 case 'd': /* double */
121 dataonly = 1;
122 format = 'd';
123 fmtid = "double";
124 break;
125 default:
126 goto unkopt;
127 }
128 break;
129 case 'x': /* x resolution */
130 resolution = 0;
131 if (argv[i][0] == '-')
132 picres.or |= XDECR;
133 picres.xr = atoi(argv[++i]);
134 break;
135 case 'y': /* y resolution */
136 resolution = 0;
137 if (argv[i][0] == '-')
138 picres.or |= YDECR;
139 if (picres.xr == 0)
140 picres.or |= YMAJOR;
141 picres.yr = atoi(argv[++i]);
142 break;
143 default:
144 unkopt:
145 fprintf(stderr, "%s: unknown option: %s\n",
146 progname, argv[i]);
147 quit(1);
148 break;
149 }
150 else
151 break;
152 /* recognize special formats */
153 if (dataonly && format == 'b')
154 if (brightonly)
155 fmtid = "8-bit_grey";
156 else
157 fmtid = "24-bit_rgb";
158 /* assign reverse ordering */
159 rord[ord[0]] = 0;
160 rord[ord[1]] = 1;
161 rord[ord[2]] = 2;
162 /* get input */
163 if (i == argc) {
164 fin = stdin;
165 } else if (i == argc-1) {
166 if ((fin = fopen(argv[i], "r")) == NULL) {
167 fprintf(stderr, "%s: can't open file \"%s\"\n",
168 progname, argv[i]);
169 quit(1);
170 }
171 } else {
172 fprintf(stderr, "%s: bad # file arguments\n", progname);
173 quit(1);
174 }
175
176 set_io();
177
178 if (reverse) {
179 #ifdef MSDOS
180 setmode(fileno(stdout), O_BINARY);
181 if (format != 'a' && format != 'i')
182 setmode(fileno(fin), O_BINARY);
183 #endif
184 /* get header */
185 if (header && checkheader(fin, fmtid, stdout) < 0) {
186 fprintf(stderr, "%s: wrong input format\n", progname);
187 quit(1);
188 }
189 /* get resolution */
190 if ((resolution && !fgetsresolu(&picres, fin)) ||
191 picres.xr <= 0 || picres.yr <= 0) {
192 fprintf(stderr, "%s: missing resolution\n", progname);
193 quit(1);
194 }
195 /* add to header */
196 printargs(i, argv, stdout);
197 fputformat(COLRFMT, stdout);
198 putchar('\n');
199 fputsresolu(&picres, stdout); /* always put resolution */
200 valtopix();
201 } else {
202 #ifdef MSDOS
203 setmode(fileno(fin), O_BINARY);
204 if (format != 'a' && format != 'i')
205 setmode(fileno(stdout), O_BINARY);
206 #endif
207 /* get header */
208 getheader(fin, checkhead, NULL);
209 if (wrongformat) {
210 fprintf(stderr, "%s: input not a Radiance picture\n",
211 progname);
212 quit(1);
213 }
214 if (!fgetsresolu(&picres, fin)) {
215 fprintf(stderr, "%s: missing resolution\n", progname);
216 quit(1);
217 }
218 if (header) {
219 printargs(i, argv, stdout);
220 fputformat(fmtid, stdout);
221 putchar('\n');
222 }
223 if (resolution) /* put resolution */
224 fputsresolu(&picres, stdout);
225 pixtoval();
226 }
227
228 quit(0);
229 }
230
231
232 checkhead(line) /* deal with line from header */
233 char *line;
234 {
235 char fmt[32];
236 double d;
237 COLOR ctmp;
238
239 if (isformat(line)) {
240 formatval(fmt, line);
241 wrongformat = strcmp(fmt, COLRFMT);
242 } else if (original && isexpos(line)) {
243 d = 1.0/exposval(line);
244 scalecolor(exposure, d);
245 } else if (original && iscolcor(line)) {
246 colcorval(ctmp, line);
247 setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
248 colval(exposure,GRN)/colval(ctmp,GRN),
249 colval(exposure,BLU)/colval(ctmp,BLU));
250 } else if (header)
251 fputs(line, stdout);
252 }
253
254
255 pixtoval() /* convert picture to values */
256 {
257 register COLOR *scanln;
258 int dogamma;
259 COLOR lastc;
260 FLOAT hv[2];
261 int y;
262 register int x;
263
264 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
265 if (scanln == NULL) {
266 fprintf(stderr, "%s: out of memory\n", progname);
267 quit(1);
268 }
269 dogamma = gamcor < .95 || gamcor > 1.05;
270 setcolor(lastc, 0.0, 0.0, 0.0);
271 for (y = 0; y < numscans(&picres); y++) {
272 if (freadscan(scanln, scanlen(&picres), fin) < 0) {
273 fprintf(stderr, "%s: read error\n", progname);
274 quit(1);
275 }
276 for (x = 0; x < scanlen(&picres); x++) {
277 if (uniq)
278 if ( colval(scanln[x],RED) ==
279 colval(lastc,RED) &&
280 colval(scanln[x],GRN) ==
281 colval(lastc,GRN) &&
282 colval(scanln[x],BLU) ==
283 colval(lastc,BLU) )
284 continue;
285 else
286 copycolor(lastc, scanln[x]);
287 if (original)
288 multcolor(scanln[x], exposure);
289 if (dogamma)
290 setcolor(scanln[x],
291 pow(colval(scanln[x],RED), 1.0/gamcor),
292 pow(colval(scanln[x],GRN), 1.0/gamcor),
293 pow(colval(scanln[x],BLU), 1.0/gamcor));
294 if (!dataonly) {
295 pix2loc(hv, &picres, x, y);
296 printf("%7d %7d ", (int)(hv[0]*picres.xr),
297 (int)(hv[1]*picres.yr));
298 }
299 if ((*putval)(scanln[x], stdout) < 0) {
300 fprintf(stderr, "%s: write error\n", progname);
301 quit(1);
302 }
303 }
304 }
305 free((char *)scanln);
306 }
307
308
309 valtopix() /* convert values to a pixel file */
310 {
311 int dogamma;
312 register COLOR *scanln;
313 int y;
314 register int x;
315
316 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
317 if (scanln == NULL) {
318 fprintf(stderr, "%s: out of memory\n", progname);
319 quit(1);
320 }
321 dogamma = gamcor < .95 || gamcor > 1.05;
322 for (y = 0; y < numscans(&picres); y++) {
323 for (x = 0; x < scanlen(&picres); x++) {
324 if (!dataonly)
325 fscanf(fin, "%*d %*d");
326 if ((*getval)(scanln[x], fin) < 0) {
327 fprintf(stderr, "%s: read error\n", progname);
328 quit(1);
329 }
330 if (dogamma)
331 setcolor(scanln[x],
332 pow(colval(scanln[x],RED), gamcor),
333 pow(colval(scanln[x],GRN), gamcor),
334 pow(colval(scanln[x],BLU), gamcor));
335 }
336 if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
337 fprintf(stderr, "%s: write error\n", progname);
338 quit(1);
339 }
340 }
341 free((char *)scanln);
342 }
343
344
345 quit(code)
346 int code;
347 {
348 exit(code);
349 }
350
351
352 getcascii(col, fp) /* get an ascii color value from fp */
353 COLOR col;
354 FILE *fp;
355 {
356 double vd[3];
357
358 if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
359 return(-1);
360 setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
361 return(0);
362 }
363
364
365 getcdouble(col, fp) /* get a double color value from fp */
366 COLOR col;
367 FILE *fp;
368 {
369 double vd[3];
370
371 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
372 return(-1);
373 setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
374 return(0);
375 }
376
377
378 getcfloat(col, fp) /* get a float color value from fp */
379 COLOR col;
380 FILE *fp;
381 {
382 float vf[3];
383
384 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
385 return(-1);
386 setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
387 return(0);
388 }
389
390
391 getcint(col, fp) /* get an int color value from fp */
392 COLOR col;
393 FILE *fp;
394 {
395 int vi[3];
396
397 if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
398 return(-1);
399 setcolor(col, (vi[rord[RED]]+.5)/256.,
400 (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
401 return(0);
402 }
403
404
405 getcbyte(col, fp) /* get a byte color value from fp */
406 COLOR col;
407 FILE *fp;
408 {
409 BYTE vb[3];
410
411 if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
412 return(-1);
413 setcolor(col, (vb[rord[RED]]+.5)/256.,
414 (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
415 return(0);
416 }
417
418
419 getbascii(col, fp) /* get an ascii brightness value from fp */
420 COLOR col;
421 FILE *fp;
422 {
423 double vd;
424
425 if (fscanf(fp, "%lf", &vd) != 1)
426 return(-1);
427 setcolor(col, vd, vd, vd);
428 return(0);
429 }
430
431
432 getbdouble(col, fp) /* get a double brightness value from fp */
433 COLOR col;
434 FILE *fp;
435 {
436 double vd;
437
438 if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
439 return(-1);
440 setcolor(col, vd, vd, vd);
441 return(0);
442 }
443
444
445 getbfloat(col, fp) /* get a float brightness value from fp */
446 COLOR col;
447 FILE *fp;
448 {
449 float vf;
450
451 if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
452 return(-1);
453 setcolor(col, vf, vf, vf);
454 return(0);
455 }
456
457
458 getbint(col, fp) /* get an int brightness value from fp */
459 COLOR col;
460 FILE *fp;
461 {
462 int vi;
463 double d;
464
465 if (fscanf(fp, "%d", &vi) != 1)
466 return(-1);
467 d = (vi+.5)/256.;
468 setcolor(col, d, d, d);
469 return(0);
470 }
471
472
473 getbbyte(col, fp) /* get a byte brightness value from fp */
474 COLOR col;
475 FILE *fp;
476 {
477 BYTE vb;
478 double d;
479
480 if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
481 return(-1);
482 d = (vb+.5)/256.;
483 setcolor(col, d, d, d);
484 return(0);
485 }
486
487
488 putcascii(col, fp) /* put an ascii color to fp */
489 COLOR col;
490 FILE *fp;
491 {
492 fprintf(fp, "%15.3e %15.3e %15.3e\n",
493 colval(col,ord[0]),
494 colval(col,ord[1]),
495 colval(col,ord[2]));
496
497 return(ferror(fp) ? -1 : 0);
498 }
499
500
501 putcfloat(col, fp) /* put a float color to fp */
502 COLOR col;
503 FILE *fp;
504 {
505 float vf[3];
506
507 vf[0] = colval(col,ord[0]);
508 vf[1] = colval(col,ord[1]);
509 vf[2] = colval(col,ord[2]);
510 fwrite((char *)vf, sizeof(float), 3, fp);
511
512 return(ferror(fp) ? -1 : 0);
513 }
514
515
516 putcdouble(col, fp) /* put a double color to fp */
517 COLOR col;
518 FILE *fp;
519 {
520 double vd[3];
521
522 vd[0] = colval(col,ord[0]);
523 vd[1] = colval(col,ord[1]);
524 vd[2] = colval(col,ord[2]);
525 fwrite((char *)vd, sizeof(double), 3, fp);
526
527 return(ferror(fp) ? -1 : 0);
528 }
529
530
531 putcint(col, fp) /* put an int color to fp */
532 COLOR col;
533 FILE *fp;
534 {
535 fprintf(fp, "%d %d %d\n",
536 (int)(colval(col,ord[0])*256.),
537 (int)(colval(col,ord[1])*256.),
538 (int)(colval(col,ord[2])*256.));
539
540 return(ferror(fp) ? -1 : 0);
541 }
542
543
544 putcbyte(col, fp) /* put a byte color to fp */
545 COLOR col;
546 FILE *fp;
547 {
548 register int i;
549 BYTE vb[3];
550
551 i = colval(col,ord[0])*256.;
552 vb[0] = min(i,255);
553 i = colval(col,ord[1])*256.;
554 vb[1] = min(i,255);
555 i = colval(col,ord[2])*256.;
556 vb[2] = min(i,255);
557 fwrite((char *)vb, sizeof(BYTE), 3, fp);
558
559 return(ferror(fp) ? -1 : 0);
560 }
561
562
563 putbascii(col, fp) /* put an ascii brightness to fp */
564 COLOR col;
565 FILE *fp;
566 {
567 fprintf(fp, "%15.3e\n", bright(col));
568
569 return(ferror(fp) ? -1 : 0);
570 }
571
572
573 putbfloat(col, fp) /* put a float brightness to fp */
574 COLOR col;
575 FILE *fp;
576 {
577 float vf;
578
579 vf = bright(col);
580 fwrite((char *)&vf, sizeof(float), 1, fp);
581
582 return(ferror(fp) ? -1 : 0);
583 }
584
585
586 putbdouble(col, fp) /* put a double brightness to fp */
587 COLOR col;
588 FILE *fp;
589 {
590 double vd;
591
592 vd = bright(col);
593 fwrite((char *)&vd, sizeof(double), 1, fp);
594
595 return(ferror(fp) ? -1 : 0);
596 }
597
598
599 putbint(col, fp) /* put an int brightness to fp */
600 COLOR col;
601 FILE *fp;
602 {
603 fprintf(fp, "%d\n", (int)(bright(col)*256.));
604
605 return(ferror(fp) ? -1 : 0);
606 }
607
608
609 putbbyte(col, fp) /* put a byte brightness to fp */
610 COLOR col;
611 FILE *fp;
612 {
613 register int i;
614 BYTE vb;
615
616 i = bright(col)*256.;
617 vb = min(i,255);
618 fwrite((char *)&vb, sizeof(BYTE), 1, fp);
619
620 return(ferror(fp) ? -1 : 0);
621 }
622
623
624 set_io() /* set put and get functions */
625 {
626 switch (format) {
627 case 'a': /* ascii */
628 if (brightonly) {
629 getval = getbascii;
630 putval = putbascii;
631 } else {
632 getval = getcascii;
633 putval = putcascii;
634 }
635 return;
636 case 'f': /* binary float */
637 if (brightonly) {
638 getval = getbfloat;
639 putval = putbfloat;
640 } else {
641 getval = getcfloat;
642 putval = putcfloat;
643 }
644 return;
645 case 'd': /* binary double */
646 if (brightonly) {
647 getval = getbdouble;
648 putval = putbdouble;
649 } else {
650 getval = getcdouble;
651 putval = putcdouble;
652 }
653 return;
654 case 'i': /* integer */
655 if (brightonly) {
656 getval = getbint;
657 putval = putbint;
658 } else {
659 getval = getcint;
660 putval = putcint;
661 }
662 return;
663 case 'b': /* byte */
664 if (brightonly) {
665 getval = getbbyte;
666 putval = putbbyte;
667 } else {
668 getval = getcbyte;
669 putval = putcbyte;
670 }
671 return;
672 }
673 }