1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.21 |
static const char RCSid[] = "$Id: ra_rgbe.c,v 2.20 2018/08/02 18:33:47 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* program to convert from RADIANCE RLE to flat format |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
2.4 |
#include <math.h> |
9 |
schorsch |
2.12 |
|
10 |
|
|
#include "platform.h" |
11 |
greg |
2.19 |
#include "rtio.h" |
12 |
greg |
2.18 |
#include "paths.h" |
13 |
greg |
2.4 |
#include "color.h" |
14 |
|
|
#include "resolu.h" |
15 |
|
|
|
16 |
greg |
2.19 |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
17 |
gregl |
2.8 |
|
18 |
greg |
1.1 |
int bradj = 0; /* brightness adjustment */ |
19 |
|
|
int doflat = 1; /* produce flat file */ |
20 |
gregl |
2.8 |
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 |
greg |
1.1 |
char *progname; |
28 |
|
|
|
29 |
schorsch |
2.16 |
static gethfunc addhline; |
30 |
schorsch |
2.17 |
static int transfer(char *ospec); |
31 |
|
|
static int loadheader(FILE *fp); |
32 |
schorsch |
2.16 |
|
33 |
greg |
1.1 |
|
34 |
schorsch |
2.17 |
int |
35 |
|
|
main(int argc, char *argv[]) |
36 |
greg |
1.1 |
{ |
37 |
gregl |
2.8 |
char *ospec; |
38 |
greg |
1.1 |
int i; |
39 |
gregl |
2.7 |
|
40 |
greg |
1.1 |
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 |
gregl |
2.8 |
case 'n': |
54 |
|
|
findframe = atoi(argv[++i]); |
55 |
|
|
break; |
56 |
|
|
case 'f': |
57 |
|
|
force++; |
58 |
|
|
break; |
59 |
|
|
case '\0': |
60 |
|
|
goto gotfile; |
61 |
greg |
1.1 |
default: |
62 |
|
|
goto userr; |
63 |
|
|
} |
64 |
|
|
else |
65 |
|
|
break; |
66 |
gregl |
2.8 |
gotfile: |
67 |
greg |
1.1 |
if (i < argc-2) |
68 |
|
|
goto userr; |
69 |
gregl |
2.8 |
if (i <= argc-1 && strcmp(argv[i], "-") && |
70 |
|
|
freopen(argv[i], "r", stdin) == NULL) { |
71 |
greg |
1.1 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
72 |
|
|
progname, argv[i]); |
73 |
|
|
exit(1); |
74 |
|
|
} |
75 |
schorsch |
2.12 |
SET_FILE_BINARY(stdin); |
76 |
gregl |
2.8 |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
77 |
|
|
while (transfer(ospec)) |
78 |
|
|
; |
79 |
greg |
1.1 |
exit(0); |
80 |
|
|
userr: |
81 |
gregl |
2.8 |
fprintf(stderr, |
82 |
|
|
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
83 |
greg |
1.1 |
progname); |
84 |
|
|
exit(1); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
schorsch |
2.17 |
static int |
89 |
|
|
transfer( /* transfer a Radiance picture */ |
90 |
|
|
char *ospec |
91 |
|
|
) |
92 |
greg |
1.1 |
{ |
93 |
schorsch |
2.15 |
char oname[PATH_MAX]; |
94 |
gregl |
2.8 |
FILE *fp; |
95 |
greg |
1.2 |
int order; |
96 |
greg |
2.3 |
int xmax, ymax; |
97 |
greg |
1.1 |
COLR *scanin; |
98 |
|
|
int y; |
99 |
gregl |
2.8 |
/* 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 |
schorsch |
2.12 |
SET_FILE_BINARY(fp); |
153 |
gregl |
2.8 |
dumpheader(fp); /* put out header */ |
154 |
|
|
fputs(progname, fp); |
155 |
greg |
1.1 |
if (bradj) |
156 |
gregl |
2.8 |
fprintf(fp, " -e %+d", bradj); |
157 |
gregl |
2.7 |
if (!doflat) |
158 |
gregl |
2.8 |
fputs(" -r", fp); |
159 |
|
|
fputc('\n', fp); |
160 |
greg |
2.2 |
if (bradj) |
161 |
gregl |
2.8 |
fputexpos(pow(2.0, (double)bradj), fp); |
162 |
|
|
fputc('\n', fp); |
163 |
|
|
fputresolu(order, xmax, ymax, fp); |
164 |
|
|
/* transfer picture */ |
165 |
gregl |
2.7 |
for (y = ymax; y--; ) { |
166 |
gregl |
2.8 |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
167 |
|
|
fprintf(stderr, "%s: error reading input picture\n", |
168 |
|
|
progname); |
169 |
|
|
exit(1); |
170 |
|
|
} |
171 |
greg |
1.1 |
if (bradj) |
172 |
|
|
shiftcolrs(scanin, xmax, bradj); |
173 |
|
|
if (doflat) |
174 |
greg |
2.19 |
putbinary((char *)scanin, sizeof(COLR), xmax, fp); |
175 |
greg |
1.1 |
else |
176 |
gregl |
2.8 |
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 |
greg |
1.1 |
} |
183 |
gregl |
2.8 |
/* clean up */ |
184 |
|
|
if (oname[0] == '!') |
185 |
|
|
pclose(fp); |
186 |
|
|
else if (ospec != NULL) |
187 |
|
|
fclose(fp); |
188 |
|
|
return(1); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
|
192 |
schorsch |
2.16 |
static int |
193 |
|
|
addhline( /* add a line to our info. header */ |
194 |
|
|
char *s, |
195 |
|
|
void *p |
196 |
|
|
) |
197 |
gregl |
2.8 |
{ |
198 |
greg |
2.20 |
char fmt[MAXFMTLEN]; |
199 |
gregl |
2.8 |
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 |
greg |
2.11 |
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
208 |
gregl |
2.8 |
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 |
gwlarson |
2.9 |
return(0); |
217 |
gregl |
2.8 |
} |
218 |
|
|
|
219 |
|
|
|
220 |
schorsch |
2.17 |
static int |
221 |
|
|
loadheader( /* load an info. header into memory */ |
222 |
|
|
FILE *fp |
223 |
|
|
) |
224 |
gregl |
2.8 |
{ |
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 |
greg |
1.1 |
} |