1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_rgbe.c,v 2.20 2018/08/02 18:33:47 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* program to convert from RADIANCE RLE to flat format |
6 |
*/ |
7 |
|
8 |
#include <math.h> |
9 |
|
10 |
#include "platform.h" |
11 |
#include "rtio.h" |
12 |
#include "paths.h" |
13 |
#include "color.h" |
14 |
#include "resolu.h" |
15 |
|
16 |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
17 |
|
18 |
int bradj = 0; /* brightness adjustment */ |
19 |
int doflat = 1; /* produce flat file */ |
20 |
int force = 0; /* force file overwrite? */ |
21 |
int findframe = 0; /* find a specific frame? */ |
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 */ |
26 |
|
27 |
char *progname; |
28 |
|
29 |
static gethfunc addhline; |
30 |
static int transfer(char *ospec); |
31 |
static int loadheader(FILE *fp); |
32 |
|
33 |
|
34 |
int |
35 |
main(int argc, char *argv[]) |
36 |
{ |
37 |
char *ospec; |
38 |
int i; |
39 |
|
40 |
progname = argv[0]; |
41 |
|
42 |
for (i = 1; i < argc; i++) |
43 |
if (argv[i][0] == '-') |
44 |
switch (argv[i][1]) { |
45 |
case 'r': |
46 |
doflat = !doflat; |
47 |
break; |
48 |
case 'e': |
49 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
50 |
goto userr; |
51 |
bradj = atoi(argv[++i]); |
52 |
break; |
53 |
case 'n': |
54 |
findframe = atoi(argv[++i]); |
55 |
break; |
56 |
case 'f': |
57 |
force++; |
58 |
break; |
59 |
case '\0': |
60 |
goto gotfile; |
61 |
default: |
62 |
goto userr; |
63 |
} |
64 |
else |
65 |
break; |
66 |
gotfile: |
67 |
if (i < argc-2) |
68 |
goto userr; |
69 |
if (i <= argc-1 && strcmp(argv[i], "-") && |
70 |
freopen(argv[i], "r", stdin) == NULL) { |
71 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
72 |
progname, argv[i]); |
73 |
exit(1); |
74 |
} |
75 |
SET_FILE_BINARY(stdin); |
76 |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
77 |
while (transfer(ospec)) |
78 |
; |
79 |
exit(0); |
80 |
userr: |
81 |
fprintf(stderr, |
82 |
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
83 |
progname); |
84 |
exit(1); |
85 |
} |
86 |
|
87 |
|
88 |
static int |
89 |
transfer( /* transfer a Radiance picture */ |
90 |
char *ospec |
91 |
) |
92 |
{ |
93 |
char oname[PATH_MAX]; |
94 |
FILE *fp; |
95 |
int order; |
96 |
int xmax, ymax; |
97 |
COLR *scanin; |
98 |
int y; |
99 |
/* get header info. */ |
100 |
if (!(y = loadheader(stdin))) |
101 |
return(0); |
102 |
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { |
103 |
fprintf(stderr, "%s: bad input format\n", progname); |
104 |
exit(1); |
105 |
} |
106 |
/* did we pass the target frame? */ |
107 |
if (findframe && findframe < frameno) |
108 |
return(0); |
109 |
/* allocate scanline */ |
110 |
scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); |
111 |
if (scanin == NULL) { |
112 |
perror(progname); |
113 |
exit(1); |
114 |
} |
115 |
/* skip frame? */ |
116 |
if (findframe > frameno) { |
117 |
for (y = ymax; y--; ) |
118 |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
119 |
fprintf(stderr, |
120 |
"%s: error reading input picture\n", |
121 |
progname); |
122 |
exit(1); |
123 |
} |
124 |
return(1); |
125 |
} |
126 |
/* open output file/command */ |
127 |
if (ospec == NULL) { |
128 |
strcpy(oname, "<stdout>"); |
129 |
fp = stdout; |
130 |
} else { |
131 |
sprintf(oname, ospec, frameno); |
132 |
if (oname[0] == '!') { |
133 |
if ((fp = popen(oname+1, "w")) == NULL) { |
134 |
fprintf(stderr, "%s: cannot start \"%s\"\n", |
135 |
progname, oname); |
136 |
exit(1); |
137 |
} |
138 |
} else { |
139 |
if (!force && access(oname, 0) >= 0) { |
140 |
fprintf(stderr, |
141 |
"%s: output file \"%s\" exists\n", |
142 |
progname, oname); |
143 |
exit(1); |
144 |
} |
145 |
if ((fp = fopen(oname, "w")) == NULL) { |
146 |
fprintf(stderr, "%s: ", progname); |
147 |
perror(oname); |
148 |
exit(1); |
149 |
} |
150 |
} |
151 |
} |
152 |
SET_FILE_BINARY(fp); |
153 |
dumpheader(fp); /* put out header */ |
154 |
fputs(progname, fp); |
155 |
if (bradj) |
156 |
fprintf(fp, " -e %+d", bradj); |
157 |
if (!doflat) |
158 |
fputs(" -r", fp); |
159 |
fputc('\n', fp); |
160 |
if (bradj) |
161 |
fputexpos(pow(2.0, (double)bradj), fp); |
162 |
fputc('\n', fp); |
163 |
fputresolu(order, xmax, ymax, fp); |
164 |
/* transfer picture */ |
165 |
for (y = ymax; y--; ) { |
166 |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
167 |
fprintf(stderr, "%s: error reading input picture\n", |
168 |
progname); |
169 |
exit(1); |
170 |
} |
171 |
if (bradj) |
172 |
shiftcolrs(scanin, xmax, bradj); |
173 |
if (doflat) |
174 |
putbinary((char *)scanin, sizeof(COLR), xmax, fp); |
175 |
else |
176 |
fwritecolrs(scanin, xmax, fp); |
177 |
if (ferror(fp)) { |
178 |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
179 |
progname, oname); |
180 |
exit(1); |
181 |
} |
182 |
} |
183 |
/* clean up */ |
184 |
if (oname[0] == '!') |
185 |
pclose(fp); |
186 |
else if (ospec != NULL) |
187 |
fclose(fp); |
188 |
return(1); |
189 |
} |
190 |
|
191 |
|
192 |
static int |
193 |
addhline( /* add a line to our info. header */ |
194 |
char *s, |
195 |
void *p |
196 |
) |
197 |
{ |
198 |
char fmt[MAXFMTLEN]; |
199 |
int n; |
200 |
|
201 |
if (formatval(fmt, s)) |
202 |
fmterr += !globmatch(PICFMT, fmt); |
203 |
else if (!strncmp(s, "FRAME=", 6)) |
204 |
frameno = atoi(s+6); |
205 |
n = strlen(s); |
206 |
if (headlen) |
207 |
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
208 |
else |
209 |
headlines = (char *)malloc(n+1); |
210 |
if (headlines == NULL) { |
211 |
perror(progname); |
212 |
exit(1); |
213 |
} |
214 |
strcpy(headlines+headlen, s); |
215 |
headlen += n; |
216 |
return(0); |
217 |
} |
218 |
|
219 |
|
220 |
static int |
221 |
loadheader( /* load an info. header into memory */ |
222 |
FILE *fp |
223 |
) |
224 |
{ |
225 |
fmterr = 0; frameno = 0; |
226 |
if (headlen) { /* free old header */ |
227 |
free(headlines); |
228 |
headlen = 0; |
229 |
} |
230 |
if (getheader(fp, addhline, NULL) < 0) |
231 |
return(0); |
232 |
if (fmterr) |
233 |
return(-1); |
234 |
return(1); |
235 |
} |