5 |
|
* program to convert from RADIANCE RLE to flat format |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <stdio.h> |
8 |
|
#include <math.h> |
9 |
< |
#include <time.h> |
9 |
> |
|
10 |
> |
#include "platform.h" |
11 |
> |
#include "rtio.h" |
12 |
> |
#include "paths.h" |
13 |
|
#include "color.h" |
14 |
|
#include "resolu.h" |
15 |
|
|
16 |
< |
#ifdef MSDOS |
15 |
< |
#include <fcntl.h> |
16 |
< |
#endif |
16 |
> |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
17 |
|
|
18 |
– |
extern int addhline(); |
19 |
– |
|
20 |
– |
#define dumpheader(fp) fwrite(headlines, 1, headlen, fp) |
21 |
– |
|
18 |
|
int bradj = 0; /* brightness adjustment */ |
23 |
– |
|
19 |
|
int doflat = 1; /* produce flat file */ |
25 |
– |
|
20 |
|
int force = 0; /* force file overwrite? */ |
27 |
– |
|
21 |
|
int findframe = 0; /* find a specific frame? */ |
29 |
– |
|
22 |
|
int frameno = 0; /* current frame number */ |
23 |
|
int fmterr = 0; /* got input format error */ |
24 |
< |
char *headlines; /* current header info. */ |
25 |
< |
int headlen; /* current header length */ |
24 |
> |
char *headlines = NULL; /* current header info. */ |
25 |
> |
int headlen1 = 0; /* length of initial frame header */ |
26 |
> |
int headlen = 0; /* current header length */ |
27 |
> |
char fmt[MAXFMTLEN]; /* input format */ |
28 |
|
|
29 |
|
char *progname; |
30 |
|
|
31 |
+ |
static gethfunc addhline; |
32 |
+ |
static int transfer(char *ospec); |
33 |
+ |
static int loadheader(FILE *fp); |
34 |
|
|
35 |
< |
main(argc, argv) |
36 |
< |
int argc; |
37 |
< |
char *argv[]; |
35 |
> |
|
36 |
> |
int |
37 |
> |
main(int argc, char *argv[]) |
38 |
|
{ |
39 |
|
char *ospec; |
40 |
|
int i; |
74 |
|
progname, argv[i]); |
75 |
|
exit(1); |
76 |
|
} |
77 |
< |
#ifdef MSDOS |
81 |
< |
setmode(fileno(stdin), O_BINARY); |
82 |
< |
#endif |
77 |
> |
SET_FILE_BINARY(stdin); |
78 |
|
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
79 |
|
while (transfer(ospec)) |
80 |
|
; |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
transfer(ospec) /* transfer a Radiance picture */ |
91 |
< |
char *ospec; |
90 |
> |
static int |
91 |
> |
transfer( /* transfer a Radiance picture */ |
92 |
> |
char *ospec |
93 |
> |
) |
94 |
|
{ |
95 |
< |
char oname[128]; |
95 |
> |
char oname[PATH_MAX]; |
96 |
|
FILE *fp; |
97 |
|
int order; |
98 |
|
int xmax, ymax; |
151 |
|
} |
152 |
|
} |
153 |
|
} |
154 |
< |
#ifdef MSDOS |
155 |
< |
setmode(fileno(fp), O_BINARY); |
156 |
< |
#endif |
160 |
< |
dumpheader(fp); /* put out header */ |
154 |
> |
SET_FILE_BINARY(fp); |
155 |
> |
newheader("RADIANCE", fp); /* put out header */ |
156 |
> |
dumpheader(fp); |
157 |
|
fputs(progname, fp); |
158 |
|
if (bradj) |
159 |
|
fprintf(fp, " -e %+d", bradj); |
162 |
|
fputc('\n', fp); |
163 |
|
if (bradj) |
164 |
|
fputexpos(pow(2.0, (double)bradj), fp); |
165 |
+ |
if (frameno) |
166 |
+ |
fprintf(fp, "FRAME=%d\n", frameno); |
167 |
+ |
if (fmt[0]) |
168 |
+ |
fputformat(fmt, fp); |
169 |
|
fputc('\n', fp); |
170 |
|
fputresolu(order, xmax, ymax, fp); |
171 |
|
/* transfer picture */ |
177 |
|
} |
178 |
|
if (bradj) |
179 |
|
shiftcolrs(scanin, xmax, bradj); |
180 |
< |
if (doflat) |
181 |
< |
fwrite((char *)scanin, sizeof(COLR), xmax, fp); |
182 |
< |
else |
183 |
< |
fwritecolrs(scanin, xmax, fp); |
184 |
< |
if (ferror(fp)) { |
185 |
< |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
186 |
< |
progname, oname); |
187 |
< |
exit(1); |
188 |
< |
} |
180 |
> |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : |
181 |
> |
(fwritecolrs(scanin, xmax, fp) < 0)) |
182 |
> |
goto writerr; |
183 |
|
} |
184 |
< |
/* clean up */ |
184 |
> |
if (fflush(fp) == EOF) /* clean up */ |
185 |
> |
goto writerr; |
186 |
|
if (oname[0] == '!') |
187 |
|
pclose(fp); |
188 |
|
else if (ospec != NULL) |
189 |
|
fclose(fp); |
190 |
|
return(1); |
191 |
+ |
writerr: |
192 |
+ |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
193 |
+ |
progname, oname); |
194 |
+ |
exit(1); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
< |
int |
199 |
< |
addhline(s) /* add a line to our info. header */ |
200 |
< |
char *s; |
198 |
> |
static int |
199 |
> |
addhline( /* add a line to our info. header */ |
200 |
> |
char *s, |
201 |
> |
void *p |
202 |
> |
) |
203 |
|
{ |
203 |
– |
char fmt[32]; |
204 |
|
int n; |
205 |
|
|
206 |
< |
if (formatval(fmt, s)) |
207 |
< |
fmterr += !globmatch(PICFMT, fmt); |
208 |
< |
else if (!strncmp(s, "FRAME=", 6)) |
206 |
> |
if (isheadid(s)) |
207 |
> |
return(0); |
208 |
> |
if (!strncmp(s, "FRAME=", 6)) { |
209 |
|
frameno = atoi(s+6); |
210 |
+ |
return(0); |
211 |
+ |
} |
212 |
+ |
if (formatval(fmt, s)) { |
213 |
+ |
fmterr += !globmatch(PICFMT, fmt); |
214 |
+ |
return(0); |
215 |
+ |
} |
216 |
|
n = strlen(s); |
217 |
|
if (headlen) |
218 |
< |
headlines = (char *)realloc(headlines, headlen+n+1); |
218 |
> |
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
219 |
|
else |
220 |
|
headlines = (char *)malloc(n+1); |
221 |
|
if (headlines == NULL) { |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
< |
loadheader(fp) /* load an info. header into memory */ |
232 |
< |
FILE *fp; |
231 |
> |
static int |
232 |
> |
loadheader( /* load an info. header into memory */ |
233 |
> |
FILE *fp |
234 |
> |
) |
235 |
|
{ |
236 |
|
fmterr = 0; frameno = 0; |
237 |
< |
if (headlen) { /* free old header */ |
238 |
< |
free(headlines); |
239 |
< |
headlen = 0; |
240 |
< |
} |
237 |
> |
/* revert to initial header length */ |
238 |
> |
if (!headlen1) headlen1 = headlen; |
239 |
> |
else headlen = headlen1; |
240 |
> |
|
241 |
|
if (getheader(fp, addhline, NULL) < 0) |
242 |
|
return(0); |
243 |
|
if (fmterr) |