ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_avs.c
Revision: 2.11
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 2.10: +19 -9 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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