1 |
greg |
2.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id$"; |
3 |
|
|
#endif |
4 |
|
|
/* |
5 |
|
|
* program to convert between RADIANCE and Windows BMP file |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include "platform.h" |
10 |
|
|
#include "color.h" |
11 |
|
|
#include "resolu.h" |
12 |
|
|
#include "bmpfile.h" |
13 |
|
|
|
14 |
|
|
int bradj = 0; /* brightness adjustment */ |
15 |
|
|
|
16 |
|
|
double gamcor = 2.2; /* gamma correction value */ |
17 |
|
|
|
18 |
|
|
char *progname; |
19 |
|
|
|
20 |
|
|
void quiterr(const char *); |
21 |
|
|
void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry); |
22 |
|
|
void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
int |
26 |
|
|
main(int argc, char *argv[]) |
27 |
|
|
{ |
28 |
|
|
char *inpfile=NULL, *outfile=NULL; |
29 |
|
|
int gryflag = 0; |
30 |
|
|
int reverse = 0; |
31 |
|
|
RESOLU rs; |
32 |
|
|
int i; |
33 |
|
|
|
34 |
|
|
progname = argv[0]; |
35 |
|
|
|
36 |
|
|
for (i = 1; i < argc; i++) |
37 |
|
|
if (argv[i][0] == '-') |
38 |
|
|
switch (argv[i][1]) { |
39 |
|
|
case 'b': |
40 |
|
|
gryflag = 1; |
41 |
|
|
break; |
42 |
|
|
case 'g': |
43 |
|
|
gamcor = atof(argv[++i]); |
44 |
|
|
break; |
45 |
|
|
case 'e': |
46 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
47 |
|
|
goto userr; |
48 |
|
|
bradj = atoi(argv[++i]); |
49 |
|
|
break; |
50 |
|
|
case 'r': |
51 |
|
|
reverse = !reverse; |
52 |
|
|
break; |
53 |
|
|
case '\0': |
54 |
|
|
break; |
55 |
|
|
default: |
56 |
|
|
goto userr; |
57 |
|
|
} |
58 |
|
|
else |
59 |
|
|
break; |
60 |
|
|
|
61 |
|
|
if (i < argc-2) |
62 |
|
|
goto userr; |
63 |
|
|
|
64 |
|
|
SET_FILE_BINARY(stdin); |
65 |
|
|
SET_FILE_BINARY(stdout); |
66 |
|
|
SET_DEFAULT_BINARY(); |
67 |
|
|
|
68 |
|
|
if (i <= argc-1 && strcmp(argv[i], "-")) |
69 |
|
|
inpfile = argv[i]; |
70 |
|
|
|
71 |
|
|
if (i == argc-2 && strcmp(argv[i+1], "-")) |
72 |
|
|
outfile = argv[i+1]; |
73 |
|
|
|
74 |
|
|
setcolrgam(gamcor); /* set up conversion */ |
75 |
|
|
|
76 |
|
|
if (reverse) { |
77 |
|
|
BMPReader *rdr; |
78 |
|
|
/* open BMP file or stream */ |
79 |
|
|
if (inpfile != NULL) |
80 |
|
|
rdr = BMPopenInputFile(inpfile); |
81 |
|
|
else |
82 |
|
|
rdr = BMPopenInputStream(stdin); |
83 |
|
|
|
84 |
|
|
if (rdr == NULL) { |
85 |
|
|
fprintf(stderr, "%s: cannot open or recognize BMP\n", |
86 |
|
|
inpfile != NULL ? inpfile : "<stdin>"); |
87 |
|
|
exit(1); |
88 |
|
|
} |
89 |
|
|
/* open Radiance output */ |
90 |
|
|
if (outfile != NULL && freopen(outfile, "w", stdout) == NULL) { |
91 |
|
|
fprintf(stderr, "%s: cannot open for output\n", |
92 |
|
|
outfile); |
93 |
|
|
exit(1); |
94 |
|
|
} |
95 |
|
|
/* put Radiance header */ |
96 |
|
|
newheader("RADIANCE", stdout); |
97 |
|
|
printargs(i, argv, stdout); |
98 |
|
|
fputformat(COLRFMT, stdout); |
99 |
|
|
putchar('\n'); |
100 |
|
|
rs.xr = rdr->hdr->width; |
101 |
|
|
rs.yr = rdr->hdr->height; |
102 |
|
|
rs.rt = YMAJOR; |
103 |
|
|
if (rdr->hdr->yIsDown || inpfile != NULL) |
104 |
|
|
rs.rt |= YDECR; |
105 |
|
|
fputsresolu(&rs, stdout); |
106 |
|
|
/* convert file */ |
107 |
|
|
bmp2rad(rdr, stdout, !rdr->hdr->yIsDown && inpfile!=NULL); |
108 |
|
|
/* flush output */ |
109 |
|
|
BMPcloseInput(rdr); /* COMMENT OUT LATER */ |
110 |
|
|
if (fflush(stdout) < 0) |
111 |
|
|
quiterr("error writing Radiance output"); |
112 |
|
|
} else { |
113 |
|
|
BMPHeader *hdr; |
114 |
|
|
BMPWriter *wtr; |
115 |
|
|
/* open Radiance input */ |
116 |
|
|
if (inpfile != NULL && freopen(inpfile, "r", stdin) == NULL) { |
117 |
|
|
fprintf(stderr, "%s: cannot open input file\n", |
118 |
|
|
inpfile); |
119 |
|
|
exit(1); |
120 |
|
|
} |
121 |
|
|
/* get header info. */ |
122 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
123 |
|
|
!fgetsresolu(&rs, stdin)) |
124 |
|
|
quiterr("bad Radiance picture format"); |
125 |
|
|
/* initialize BMP header */ |
126 |
|
|
if (gryflag) |
127 |
|
|
hdr = BMPmappedHeader(numscans(&rs), |
128 |
|
|
scanlen(&rs), 0, 256); |
129 |
|
|
else |
130 |
|
|
hdr = BMPtruecolorHeader(numscans(&rs), |
131 |
|
|
scanlen(&rs), 0); |
132 |
|
|
if (hdr == NULL) |
133 |
|
|
quiterr("cannot initialize BMP header"); |
134 |
|
|
hdr->yIsDown = (rs.rt & YDECR) && outfile == NULL; |
135 |
|
|
/* open BMP output */ |
136 |
|
|
if (outfile != NULL) |
137 |
|
|
wtr = BMPopenOutputFile(outfile, hdr); |
138 |
|
|
else |
139 |
|
|
wtr = BMPopenOutputStream(stdout, hdr); |
140 |
|
|
/* convert file */ |
141 |
|
|
rad2bmp(stdin, wtr, !hdr->yIsDown && (rs.rt&YDECR), gryflag); |
142 |
|
|
/* flush output */ |
143 |
|
|
if (fflush((FILE *)wtr->c_data) < 0) |
144 |
|
|
quiterr("error writing BMP output"); |
145 |
|
|
BMPcloseOutput(wtr); /* COMMENT OUT LATER */ |
146 |
|
|
} |
147 |
|
|
exit(0); /* success */ |
148 |
|
|
userr: |
149 |
|
|
fprintf(stderr, |
150 |
|
|
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
151 |
|
|
progname); |
152 |
|
|
exit(1); |
153 |
|
|
return(1); /* to keep compiler happy */ |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/* print message and exit */ |
157 |
|
|
void |
158 |
|
|
quiterr(const char *err) |
159 |
|
|
{ |
160 |
|
|
if (err != NULL) { |
161 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
162 |
|
|
exit(1); |
163 |
|
|
} |
164 |
|
|
exit(0); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/* convert Radiance picture to BMP */ |
168 |
|
|
void |
169 |
|
|
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) |
170 |
|
|
{ |
171 |
|
|
COLR *scanin; |
172 |
|
|
int y, yend, ystp; |
173 |
|
|
int x; |
174 |
|
|
/* allocate scanline */ |
175 |
|
|
scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); |
176 |
|
|
if (scanin == NULL) |
177 |
|
|
quiterr("out of memory in rad2bmp"); |
178 |
|
|
/* convert image */ |
179 |
|
|
if (inv) { |
180 |
|
|
y = bwr->hdr->height - 1; |
181 |
|
|
ystp = -1; yend = -1; |
182 |
|
|
} else { |
183 |
|
|
y = 0; |
184 |
|
|
ystp = 1; yend = bwr->hdr->height; |
185 |
|
|
} |
186 |
|
|
/* convert each scanline */ |
187 |
|
|
for ( ; y != yend; y += ystp) { |
188 |
|
|
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
189 |
|
|
quiterr("error reading Radiance picture"); |
190 |
|
|
if (bradj) |
191 |
|
|
shiftcolrs(scanin, bwr->hdr->width, bradj); |
192 |
|
|
for (x = gry ? bwr->hdr->width : 0; x--; ) |
193 |
|
|
scanin[x][GRN] = normbright(scanin[x]); |
194 |
|
|
colrs_gambs(scanin, bwr->hdr->width); |
195 |
|
|
if (gry) |
196 |
|
|
for (x = bwr->hdr->width; x--; ) |
197 |
|
|
bwr->scanline[x] = scanin[x][GRN]; |
198 |
|
|
else |
199 |
|
|
for (x = bwr->hdr->width; x--; ) { |
200 |
|
|
bwr->scanline[3*x] = scanin[x][BLU]; |
201 |
|
|
bwr->scanline[3*x+1] = scanin[x][GRN]; |
202 |
|
|
bwr->scanline[3*x+2] = scanin[x][RED]; |
203 |
|
|
} |
204 |
|
|
bwr->yscan = y; |
205 |
|
|
x = BMPwriteScanline(bwr); |
206 |
|
|
if (x != BIR_OK) |
207 |
|
|
quiterr(BMPerrorMessage(x)); |
208 |
|
|
} |
209 |
|
|
/* free scanline */ |
210 |
|
|
free((void *)scanin); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/* convert BMP file to Radiance */ |
214 |
|
|
void |
215 |
|
|
bmp2rad(BMPReader *brd, FILE *rfp, int inv) |
216 |
|
|
{ |
217 |
|
|
COLR *scanout; |
218 |
|
|
int y, yend, ystp; |
219 |
|
|
int x; |
220 |
|
|
/* allocate scanline */ |
221 |
|
|
scanout = (COLR *)malloc(brd->hdr->width*sizeof(COLR)); |
222 |
|
|
if (scanout == NULL) |
223 |
|
|
quiterr("out of memory in bmp2rad"); |
224 |
|
|
/* convert image */ |
225 |
|
|
if (inv) { |
226 |
|
|
y = brd->hdr->height - 1; |
227 |
|
|
ystp = -1; yend = -1; |
228 |
|
|
} else { |
229 |
|
|
y = 0; |
230 |
|
|
ystp = 1; yend = brd->hdr->height; |
231 |
|
|
} |
232 |
|
|
/* convert each scanline */ |
233 |
|
|
for ( ; y != yend; y += ystp) { |
234 |
|
|
x = BMPseekScanline(y, brd); |
235 |
|
|
if (x != BIR_OK) |
236 |
|
|
quiterr(BMPerrorMessage(x)); |
237 |
|
|
for (x = brd->hdr->width; x--; ) { |
238 |
|
|
RGBquad rgbq = BMPdecodePixel(x, brd); |
239 |
|
|
scanout[x][RED] = rgbq.r; |
240 |
|
|
scanout[x][GRN] = rgbq.g; |
241 |
|
|
scanout[x][BLU] = rgbq.b; |
242 |
|
|
} |
243 |
|
|
gambs_colrs(scanout, brd->hdr->width); |
244 |
|
|
if (bradj) |
245 |
|
|
shiftcolrs(scanout, brd->hdr->width, bradj); |
246 |
|
|
if (fwritecolrs(scanout, brd->hdr->width, rfp) < 0) |
247 |
|
|
quiterr("error writing Radiance picture"); |
248 |
|
|
} |
249 |
|
|
/* clean up */ |
250 |
|
|
free((void *)scanout); |
251 |
|
|
} |