ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.c
Revision: 2.10
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 11 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.9: +5 -8 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_avs.c,v 2.9 2003/02/22 02:07:27 greg Exp $";
3 #endif
4 /*
5 * Convert Radiance file to/from AVS file.
6 */
7
8 #include <stdio.h>
9 #include <math.h>
10 #include <time.h>
11
12 #include "platform.h"
13 #include "color.h"
14 #include "resolu.h"
15
16 double gamcor = 2.2; /* gamma correction */
17
18 int bradj = 0; /* brightness adjustment */
19
20 char *progname;
21
22 int xmax, ymax;
23
24
25 main(argc, argv)
26 int argc;
27 char *argv[];
28 {
29 extern long getint();
30 int reverse = 0;
31 int i;
32
33 progname = argv[0];
34
35 for (i = 1; i < argc; i++)
36 if (argv[i][0] == '-')
37 switch (argv[i][1]) {
38 case 'g': /* gamma correction */
39 gamcor = atof(argv[++i]);
40 break;
41 case 'e': /* exposure adjustment */
42 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
43 goto userr;
44 bradj = atoi(argv[++i]);
45 break;
46 case 'r': /* reverse conversion */
47 reverse = 1;
48 break;
49 default:
50 goto userr;
51 }
52 else
53 break;
54
55 if (i < argc-2)
56 goto userr;
57 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
58 fprintf(stderr, "%s: can't open input \"%s\"\n",
59 progname, argv[i]);
60 exit(1);
61 }
62 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
63 fprintf(stderr, "%s: can't open output \"%s\"\n",
64 progname, argv[i+1]);
65 exit(1);
66 }
67 SET_FILE_BINARY(stdin);
68 SET_FILE_BINARY(stdout);
69 setcolrgam(gamcor); /* set up gamma correction */
70 if (reverse) {
71 /* get their image resolution */
72 xmax = getint(4, stdin);
73 ymax = getint(4, stdin);
74 if (feof(stdin))
75 quiterr("empty input file");
76 /* put our header */
77 newheader("RADIANCE", stdout);
78 printargs(i, argv, stdout);
79 fputformat(COLRFMT, stdout);
80 putchar('\n');
81 fprtresolu(xmax, ymax, stdout);
82 /* convert file */
83 avs2ra();
84 } else {
85 /* get our header */
86 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
87 fgetresolu(&xmax, &ymax, stdin) < 0)
88 quiterr("bad picture format");
89 /* write their header */
90 putint((long)xmax, 4, stdout);
91 putint((long)ymax, 4, stdout);
92 /* convert file */
93 ra2avs();
94 }
95 exit(0);
96 userr:
97 fprintf(stderr,
98 "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
99 progname);
100 exit(1);
101 }
102
103
104 quiterr(err) /* print message and exit */
105 char *err;
106 {
107 if (err != NULL) {
108 fprintf(stderr, "%s: %s\n", progname, err);
109 exit(1);
110 }
111 exit(0);
112 }
113
114
115 avs2ra() /* convert 24-bit scanlines to Radiance picture */
116 {
117 COLR *scanout;
118 register int x;
119 int y;
120 /* allocate scanline */
121 scanout = (COLR *)malloc(xmax*sizeof(COLR));
122 if (scanout == NULL)
123 quiterr("out of memory in avs2ra");
124 /* convert image */
125 for (y = ymax-1; y >= 0; y--) {
126 for (x = 0; x < xmax; x++) {
127 (void)getc(stdin); /* toss alpha */
128 scanout[x][RED] = getc(stdin);
129 scanout[x][GRN] = getc(stdin);
130 scanout[x][BLU] = getc(stdin);
131 }
132 if (feof(stdin) | ferror(stdin))
133 quiterr("error reading AVS image");
134 /* undo gamma */
135 gambs_colrs(scanout, xmax);
136 if (bradj) /* adjust exposure */
137 shiftcolrs(scanout, xmax, bradj);
138 if (fwritecolrs(scanout, xmax, stdout) < 0)
139 quiterr("error writing Radiance picture");
140 }
141 /* free scanline */
142 free((void *)scanout);
143 }
144
145
146 ra2avs() /* convert Radiance scanlines to 24-bit */
147 {
148 COLR *scanin;
149 register int x;
150 int y;
151 /* allocate scanline */
152 scanin = (COLR *)malloc(xmax*sizeof(COLR));
153 if (scanin == NULL)
154 quiterr("out of memory in ra2avs");
155 /* convert image */
156 for (y = ymax-1; y >= 0; y--) {
157 if (freadcolrs(scanin, xmax, stdin) < 0)
158 quiterr("error reading Radiance picture");
159 if (bradj) /* adjust exposure */
160 shiftcolrs(scanin, xmax, bradj);
161 colrs_gambs(scanin, xmax); /* gamma correction */
162 for (x = 0; x < xmax; x++) {
163 putc(0, stdout); /* no alpha */
164 putc(scanin[x][RED], stdout);
165 putc(scanin[x][GRN], stdout);
166 putc(scanin[x][BLU], stdout);
167 }
168 if (ferror(stdout))
169 quiterr("error writing AVS file");
170 }
171 /* free scanline */
172 free((void *)scanin);
173 }