1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.26 |
static const char RCSid[] = "$Id: ra_rgbe.c,v 2.25 2025/04/22 14:51:29 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.4 |
#include "color.h" |
13 |
|
|
#include "resolu.h" |
14 |
|
|
|
15 |
greg |
2.19 |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
16 |
gregl |
2.8 |
|
17 |
greg |
1.1 |
int bradj = 0; /* brightness adjustment */ |
18 |
|
|
int doflat = 1; /* produce flat file */ |
19 |
gregl |
2.8 |
int force = 0; /* force file overwrite? */ |
20 |
|
|
int findframe = 0; /* find a specific frame? */ |
21 |
|
|
int frameno = 0; /* current frame number */ |
22 |
|
|
int fmterr = 0; /* got input format error */ |
23 |
greg |
2.22 |
char *headlines = NULL; /* current header info. */ |
24 |
|
|
int headlen1 = 0; /* length of initial frame header */ |
25 |
|
|
int headlen = 0; /* current header length */ |
26 |
|
|
char fmt[MAXFMTLEN]; /* input format */ |
27 |
gregl |
2.8 |
|
28 |
greg |
1.1 |
char *progname; |
29 |
|
|
|
30 |
schorsch |
2.16 |
static gethfunc addhline; |
31 |
schorsch |
2.17 |
static int transfer(char *ospec); |
32 |
|
|
static int loadheader(FILE *fp); |
33 |
schorsch |
2.16 |
|
34 |
greg |
1.1 |
|
35 |
schorsch |
2.17 |
int |
36 |
|
|
main(int argc, char *argv[]) |
37 |
greg |
1.1 |
{ |
38 |
gregl |
2.8 |
char *ospec; |
39 |
greg |
1.1 |
int i; |
40 |
gregl |
2.7 |
|
41 |
greg |
1.1 |
progname = argv[0]; |
42 |
|
|
|
43 |
|
|
for (i = 1; i < argc; i++) |
44 |
|
|
if (argv[i][0] == '-') |
45 |
|
|
switch (argv[i][1]) { |
46 |
|
|
case 'r': |
47 |
|
|
doflat = !doflat; |
48 |
|
|
break; |
49 |
|
|
case 'e': |
50 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
51 |
|
|
goto userr; |
52 |
|
|
bradj = atoi(argv[++i]); |
53 |
|
|
break; |
54 |
gregl |
2.8 |
case 'n': |
55 |
|
|
findframe = atoi(argv[++i]); |
56 |
|
|
break; |
57 |
|
|
case 'f': |
58 |
|
|
force++; |
59 |
|
|
break; |
60 |
|
|
case '\0': |
61 |
|
|
goto gotfile; |
62 |
greg |
1.1 |
default: |
63 |
|
|
goto userr; |
64 |
|
|
} |
65 |
|
|
else |
66 |
|
|
break; |
67 |
gregl |
2.8 |
gotfile: |
68 |
greg |
1.1 |
if (i < argc-2) |
69 |
|
|
goto userr; |
70 |
gregl |
2.8 |
if (i <= argc-1 && strcmp(argv[i], "-") && |
71 |
|
|
freopen(argv[i], "r", stdin) == NULL) { |
72 |
greg |
1.1 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
73 |
|
|
progname, argv[i]); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
schorsch |
2.12 |
SET_FILE_BINARY(stdin); |
77 |
gregl |
2.8 |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
78 |
|
|
while (transfer(ospec)) |
79 |
|
|
; |
80 |
greg |
1.1 |
exit(0); |
81 |
|
|
userr: |
82 |
gregl |
2.8 |
fprintf(stderr, |
83 |
|
|
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
84 |
greg |
1.1 |
progname); |
85 |
|
|
exit(1); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
|
89 |
schorsch |
2.17 |
static int |
90 |
|
|
transfer( /* transfer a Radiance picture */ |
91 |
|
|
char *ospec |
92 |
|
|
) |
93 |
greg |
1.1 |
{ |
94 |
schorsch |
2.15 |
char oname[PATH_MAX]; |
95 |
gregl |
2.8 |
FILE *fp; |
96 |
greg |
1.2 |
int order; |
97 |
greg |
2.3 |
int xmax, ymax; |
98 |
greg |
1.1 |
COLR *scanin; |
99 |
|
|
int y; |
100 |
gregl |
2.8 |
/* get header info. */ |
101 |
|
|
if (!(y = loadheader(stdin))) |
102 |
|
|
return(0); |
103 |
|
|
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { |
104 |
|
|
fprintf(stderr, "%s: bad input format\n", progname); |
105 |
|
|
exit(1); |
106 |
|
|
} |
107 |
|
|
/* did we pass the target frame? */ |
108 |
|
|
if (findframe && findframe < frameno) |
109 |
|
|
return(0); |
110 |
|
|
/* allocate scanline */ |
111 |
greg |
2.23 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
112 |
gregl |
2.8 |
if (scanin == NULL) { |
113 |
|
|
perror(progname); |
114 |
|
|
exit(1); |
115 |
|
|
} |
116 |
|
|
/* skip frame? */ |
117 |
|
|
if (findframe > frameno) { |
118 |
greg |
2.23 |
if (NCSAMP > 3) { |
119 |
|
|
if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) { |
120 |
|
|
perror(progname); |
121 |
|
|
exit(1); |
122 |
|
|
} |
123 |
|
|
} else |
124 |
|
|
for (y = ymax; y--; ) |
125 |
gregl |
2.8 |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
126 |
|
|
fprintf(stderr, |
127 |
|
|
"%s: error reading input picture\n", |
128 |
|
|
progname); |
129 |
|
|
exit(1); |
130 |
|
|
} |
131 |
greg |
2.23 |
free(scanin); |
132 |
gregl |
2.8 |
return(1); |
133 |
|
|
} |
134 |
|
|
/* open output file/command */ |
135 |
|
|
if (ospec == NULL) { |
136 |
|
|
strcpy(oname, "<stdout>"); |
137 |
|
|
fp = stdout; |
138 |
|
|
} else { |
139 |
|
|
sprintf(oname, ospec, frameno); |
140 |
|
|
if (oname[0] == '!') { |
141 |
|
|
if ((fp = popen(oname+1, "w")) == NULL) { |
142 |
|
|
fprintf(stderr, "%s: cannot start \"%s\"\n", |
143 |
|
|
progname, oname); |
144 |
|
|
exit(1); |
145 |
|
|
} |
146 |
|
|
} else { |
147 |
|
|
if (!force && access(oname, 0) >= 0) { |
148 |
|
|
fprintf(stderr, |
149 |
|
|
"%s: output file \"%s\" exists\n", |
150 |
|
|
progname, oname); |
151 |
|
|
exit(1); |
152 |
|
|
} |
153 |
|
|
if ((fp = fopen(oname, "w")) == NULL) { |
154 |
|
|
fprintf(stderr, "%s: ", progname); |
155 |
|
|
perror(oname); |
156 |
|
|
exit(1); |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
} |
160 |
schorsch |
2.12 |
SET_FILE_BINARY(fp); |
161 |
greg |
2.22 |
newheader("RADIANCE", fp); /* put out header */ |
162 |
|
|
dumpheader(fp); |
163 |
gregl |
2.8 |
fputs(progname, fp); |
164 |
greg |
1.1 |
if (bradj) |
165 |
gregl |
2.8 |
fprintf(fp, " -e %+d", bradj); |
166 |
gregl |
2.7 |
if (!doflat) |
167 |
gregl |
2.8 |
fputs(" -r", fp); |
168 |
|
|
fputc('\n', fp); |
169 |
greg |
2.2 |
if (bradj) |
170 |
gregl |
2.8 |
fputexpos(pow(2.0, (double)bradj), fp); |
171 |
greg |
2.22 |
if (frameno) |
172 |
|
|
fprintf(fp, "FRAME=%d\n", frameno); |
173 |
|
|
if (fmt[0]) |
174 |
|
|
fputformat(fmt, fp); |
175 |
gregl |
2.8 |
fputc('\n', fp); |
176 |
|
|
fputresolu(order, xmax, ymax, fp); |
177 |
|
|
/* transfer picture */ |
178 |
gregl |
2.7 |
for (y = ymax; y--; ) { |
179 |
greg |
2.23 |
if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) { |
180 |
gregl |
2.8 |
fprintf(stderr, "%s: error reading input picture\n", |
181 |
|
|
progname); |
182 |
|
|
exit(1); |
183 |
|
|
} |
184 |
greg |
1.1 |
if (bradj) |
185 |
|
|
shiftcolrs(scanin, xmax, bradj); |
186 |
greg |
2.22 |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : |
187 |
|
|
(fwritecolrs(scanin, xmax, fp) < 0)) |
188 |
|
|
goto writerr; |
189 |
greg |
1.1 |
} |
190 |
greg |
2.23 |
free(scanin); /* clean up */ |
191 |
|
|
if (fflush(fp) == EOF) |
192 |
greg |
2.22 |
goto writerr; |
193 |
greg |
2.24 |
if (oname[0] == '!') { |
194 |
greg |
2.25 |
if (pclose(fp) != 0) |
195 |
|
|
fprintf(stderr, "%s: warning - bad status from \"%s\"\n", |
196 |
greg |
2.24 |
progname, oname); |
197 |
|
|
} else if (ospec != NULL) |
198 |
gregl |
2.8 |
fclose(fp); |
199 |
|
|
return(1); |
200 |
greg |
2.22 |
writerr: |
201 |
|
|
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
202 |
|
|
progname, oname); |
203 |
|
|
exit(1); |
204 |
gregl |
2.8 |
} |
205 |
|
|
|
206 |
|
|
|
207 |
schorsch |
2.16 |
static int |
208 |
|
|
addhline( /* add a line to our info. header */ |
209 |
|
|
char *s, |
210 |
|
|
void *p |
211 |
|
|
) |
212 |
gregl |
2.8 |
{ |
213 |
|
|
int n; |
214 |
|
|
|
215 |
greg |
2.22 |
if (isheadid(s)) |
216 |
|
|
return(0); |
217 |
|
|
if (!strncmp(s, "FRAME=", 6)) { |
218 |
|
|
frameno = atoi(s+6); |
219 |
|
|
return(0); |
220 |
|
|
} |
221 |
|
|
if (formatval(fmt, s)) { |
222 |
greg |
2.23 |
if (!strcmp(fmt, SPECFMT)) |
223 |
|
|
strcpy(fmt, COLRFMT); |
224 |
|
|
else |
225 |
|
|
fmterr += !globmatch(PICFMT, fmt); |
226 |
|
|
return(0); |
227 |
|
|
} |
228 |
|
|
if (isncomp(s)) { |
229 |
|
|
NCSAMP = ncompval(s); |
230 |
|
|
return(NCSAMP - 3); |
231 |
|
|
} |
232 |
|
|
if (iswlsplit(s)) { |
233 |
|
|
wlsplitval(WLPART, s); |
234 |
greg |
2.22 |
return(0); |
235 |
|
|
} |
236 |
gregl |
2.8 |
n = strlen(s); |
237 |
|
|
if (headlen) |
238 |
greg |
2.11 |
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
239 |
gregl |
2.8 |
else |
240 |
|
|
headlines = (char *)malloc(n+1); |
241 |
|
|
if (headlines == NULL) { |
242 |
|
|
perror(progname); |
243 |
|
|
exit(1); |
244 |
|
|
} |
245 |
|
|
strcpy(headlines+headlen, s); |
246 |
|
|
headlen += n; |
247 |
gwlarson |
2.9 |
return(0); |
248 |
gregl |
2.8 |
} |
249 |
|
|
|
250 |
|
|
|
251 |
schorsch |
2.17 |
static int |
252 |
|
|
loadheader( /* load an info. header into memory */ |
253 |
|
|
FILE *fp |
254 |
|
|
) |
255 |
gregl |
2.8 |
{ |
256 |
|
|
fmterr = 0; frameno = 0; |
257 |
greg |
2.22 |
/* revert to initial header length */ |
258 |
|
|
if (!headlen1) headlen1 = headlen; |
259 |
|
|
else headlen = headlen1; |
260 |
|
|
|
261 |
gregl |
2.8 |
if (getheader(fp, addhline, NULL) < 0) |
262 |
|
|
return(0); |
263 |
|
|
if (fmterr) |
264 |
|
|
return(-1); |
265 |
|
|
return(1); |
266 |
greg |
1.1 |
} |