ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.6
Committed: Thu Jul 29 13:36:19 1993 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +2 -0 lines
Log Message:
made pvalue accept upper case X and Y for resolution spec. also

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