1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_bn.c,v 2.8 2003/06/05 19:29:34 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* ra_bn.c - program to convert between RADIANCE and barneyscan picture format. |
6 |
* |
7 |
* 10/12/88 |
8 |
*/ |
9 |
|
10 |
#include <stdio.h> |
11 |
#include <string.h> |
12 |
#include <time.h> |
13 |
#include <math.h> |
14 |
|
15 |
#include "platform.h" |
16 |
#include "color.h" |
17 |
#include "resolu.h" |
18 |
|
19 |
double gamcor = 2.0; /* gamma correction */ |
20 |
int bradj = 0; /* brightness adjustment */ |
21 |
char *progname; |
22 |
char errmsg[128]; |
23 |
FILE *rafp, *bnfp[3]; |
24 |
int xmax, ymax; |
25 |
|
26 |
static void quiterr(char *err); |
27 |
static int openbarney(char *fname, char *mode); |
28 |
static int getint(FILE *fp); |
29 |
static void putint(int val, FILE *fp); |
30 |
static void ra2bn(void); |
31 |
static void bn2ra(void); |
32 |
|
33 |
|
34 |
int |
35 |
main( |
36 |
int argc, |
37 |
char *argv[] |
38 |
) |
39 |
{ |
40 |
int reverse = 0; |
41 |
int i; |
42 |
SET_DEFAULT_BINARY(); |
43 |
SET_FILE_BINARY(stdin); |
44 |
SET_FILE_BINARY(stdout); |
45 |
progname = argv[0]; |
46 |
|
47 |
for (i = 1; i < argc; i++) |
48 |
if (argv[i][0] == '-') |
49 |
switch (argv[i][1]) { |
50 |
case 'g': |
51 |
gamcor = atof(argv[++i]); |
52 |
break; |
53 |
case 'r': |
54 |
reverse = !reverse; |
55 |
break; |
56 |
case 'e': |
57 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
58 |
goto userr; |
59 |
bradj = atoi(argv[++i]); |
60 |
break; |
61 |
default: |
62 |
goto userr; |
63 |
} |
64 |
else |
65 |
break; |
66 |
/* set gamma correction */ |
67 |
setcolrgam(gamcor); |
68 |
|
69 |
if (reverse) { |
70 |
if (i > argc-1 || i < argc-2) |
71 |
goto userr; |
72 |
if (openbarney(argv[i], "r") < 0) { |
73 |
sprintf(errmsg, "cannot open input \"%s\"", argv[i]); |
74 |
quiterr(errmsg); |
75 |
} |
76 |
if (i == argc-1 || !strcmp(argv[i+1], "-")) |
77 |
rafp = stdout; |
78 |
else if ((rafp = fopen(argv[i+1], "w")) == NULL) { |
79 |
sprintf(errmsg, "cannot open output \"%s\"", |
80 |
argv[i+1]); |
81 |
quiterr(errmsg); |
82 |
} |
83 |
/* put header */ |
84 |
newheader("RADIANCE", stdout); |
85 |
printargs(i, argv, rafp); |
86 |
fputformat(COLRFMT, rafp); |
87 |
putc('\n', rafp); |
88 |
fprtresolu(xmax, ymax, rafp); |
89 |
/* convert file */ |
90 |
bn2ra(); |
91 |
} else { |
92 |
if (i != argc-2) |
93 |
goto userr; |
94 |
if (!strcmp(argv[i], "-")) |
95 |
rafp = stdin; |
96 |
else if ((rafp = fopen(argv[i], "r")) == NULL) { |
97 |
sprintf(errmsg, "cannot open input \"%s\"", |
98 |
argv[i]); |
99 |
quiterr(errmsg); |
100 |
} |
101 |
/* get header */ |
102 |
if (checkheader(rafp, COLRFMT, NULL) < 0 || |
103 |
fgetresolu(&xmax, &ymax, rafp) < 0) |
104 |
quiterr("bad RADIANCE format"); |
105 |
if (openbarney(argv[i+1], "w") < 0) { |
106 |
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
107 |
quiterr(errmsg); |
108 |
} |
109 |
/* convert file */ |
110 |
ra2bn(); |
111 |
} |
112 |
quiterr(NULL); |
113 |
userr: |
114 |
fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n", |
115 |
progname); |
116 |
fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n", |
117 |
progname); |
118 |
exit(1); |
119 |
} |
120 |
|
121 |
|
122 |
static void |
123 |
quiterr( /* print message and exit */ |
124 |
char *err |
125 |
) |
126 |
{ |
127 |
if (err != NULL) { |
128 |
fprintf(stderr, "%s: %s\n", progname, err); |
129 |
exit(1); |
130 |
} |
131 |
exit(0); |
132 |
} |
133 |
|
134 |
|
135 |
static int |
136 |
openbarney( /* open barneyscan files */ |
137 |
char *fname, |
138 |
char *mode |
139 |
) |
140 |
{ |
141 |
static char suffix[3][4] = {"red", "grn", "blu"}; |
142 |
int i; |
143 |
char file[128]; |
144 |
|
145 |
for (i = 0; i < 3; i++) { |
146 |
sprintf(file, "%s.%s", fname, suffix[i]); |
147 |
if ((bnfp[i] = fopen(file, mode)) == NULL) |
148 |
return(-1); |
149 |
} |
150 |
if (mode[0] == 'r') { /* get header */ |
151 |
xmax = getint(bnfp[0]); |
152 |
ymax = getint(bnfp[0]); |
153 |
for (i = 1; i < 3; i++) |
154 |
if (getint(bnfp[i]) != xmax || getint(bnfp[i]) != ymax) |
155 |
return(-1); |
156 |
} else { /* put header */ |
157 |
for (i = 0; i < 3; i++) { |
158 |
putint(xmax, bnfp[i]); |
159 |
putint(ymax, bnfp[i]); |
160 |
} |
161 |
} |
162 |
return(0); |
163 |
} |
164 |
|
165 |
|
166 |
static int |
167 |
getint( /* get short int from barneyscan file */ |
168 |
register FILE *fp |
169 |
) |
170 |
{ |
171 |
register short val; |
172 |
|
173 |
val = getc(fp); |
174 |
val |= getc(fp) << 8; |
175 |
|
176 |
return(val); |
177 |
} |
178 |
|
179 |
|
180 |
static void |
181 |
putint( /* put short int to barneyscan file */ |
182 |
register int val, |
183 |
register FILE *fp |
184 |
) |
185 |
{ |
186 |
putc(val&0xff, fp); |
187 |
putc((val >> 8)&0xff, fp); |
188 |
} |
189 |
|
190 |
|
191 |
static void |
192 |
ra2bn(void) /* convert radiance to barneyscan */ |
193 |
{ |
194 |
register int i; |
195 |
register COLR *inl; |
196 |
int j; |
197 |
|
198 |
if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
199 |
quiterr("out of memory"); |
200 |
for (j = 0; j < ymax; j++) { |
201 |
if (freadcolrs(inl, xmax, rafp) < 0) |
202 |
quiterr("error reading RADIANCE file"); |
203 |
if (bradj) |
204 |
shiftcolrs(inl, xmax, bradj); |
205 |
colrs_gambs(inl, xmax); |
206 |
for (i = 0; i < xmax; i++) { |
207 |
putc(inl[i][RED], bnfp[0]); |
208 |
putc(inl[i][GRN], bnfp[1]); |
209 |
putc(inl[i][BLU], bnfp[2]); |
210 |
} |
211 |
if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2])) |
212 |
quiterr("error writing Barney files"); |
213 |
} |
214 |
free((void *)inl); |
215 |
} |
216 |
|
217 |
|
218 |
static void |
219 |
bn2ra(void) /* convert barneyscan to radiance */ |
220 |
{ |
221 |
register int i; |
222 |
register COLR *outline; |
223 |
int j; |
224 |
|
225 |
if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
226 |
quiterr("out of memory"); |
227 |
for (j = 0; j < ymax; j++) { |
228 |
for (i = 0; i < xmax; i++) { |
229 |
outline[i][RED] = getc(bnfp[0]); |
230 |
outline[i][GRN] = getc(bnfp[1]); |
231 |
outline[i][BLU] = getc(bnfp[2]); |
232 |
} |
233 |
if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2])) |
234 |
quiterr("error reading barney file"); |
235 |
gambs_colrs(outline, xmax); |
236 |
if (bradj) |
237 |
shiftcolrs(outline, xmax, bradj); |
238 |
if (fwritecolrs(outline, xmax, rafp) < 0) |
239 |
quiterr("error writing RADIANCE file"); |
240 |
} |
241 |
free((void *)outline); |
242 |
} |