1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* program to convert from RADIANCE RLE to flat format |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include <stdio.h> |
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 |
< |
int bradj = 0; /* brightness adjustment */ |
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 = 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; |
41 |
< |
|
41 |
> |
|
42 |
|
progname = argv[0]; |
43 |
|
|
44 |
|
for (i = 1; i < argc; i++) |
52 |
|
goto userr; |
53 |
|
bradj = atoi(argv[++i]); |
54 |
|
break; |
55 |
+ |
case 'n': |
56 |
+ |
findframe = atoi(argv[++i]); |
57 |
+ |
break; |
58 |
+ |
case 'f': |
59 |
+ |
force++; |
60 |
+ |
break; |
61 |
+ |
case '\0': |
62 |
+ |
goto gotfile; |
63 |
|
default: |
64 |
|
goto userr; |
65 |
|
} |
66 |
|
else |
67 |
|
break; |
68 |
< |
|
68 |
> |
gotfile: |
69 |
|
if (i < argc-2) |
70 |
|
goto userr; |
71 |
< |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
71 |
> |
if (i <= argc-1 && strcmp(argv[i], "-") && |
72 |
> |
freopen(argv[i], "r", stdin) == NULL) { |
73 |
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
74 |
|
progname, argv[i]); |
75 |
|
exit(1); |
76 |
|
} |
77 |
< |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
78 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
79 |
< |
progname, argv[i+1]); |
80 |
< |
exit(1); |
59 |
< |
} |
60 |
< |
transfer(); |
77 |
> |
SET_FILE_BINARY(stdin); |
78 |
> |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
79 |
> |
while (transfer(ospec)) |
80 |
> |
; |
81 |
|
exit(0); |
82 |
|
userr: |
83 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n", |
83 |
> |
fprintf(stderr, |
84 |
> |
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
85 |
|
progname); |
86 |
|
exit(1); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
quiterr(err) /* print message and exit */ |
91 |
< |
char *err; |
90 |
> |
static int |
91 |
> |
transfer( /* transfer a Radiance picture */ |
92 |
> |
char *ospec |
93 |
> |
) |
94 |
|
{ |
95 |
< |
if (err != NULL) { |
96 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
74 |
< |
exit(1); |
75 |
< |
} |
76 |
< |
exit(0); |
77 |
< |
} |
78 |
< |
|
79 |
< |
|
80 |
< |
transfer() /* transfer Radiance picture */ |
81 |
< |
{ |
82 |
< |
extern double pow(); |
95 |
> |
char oname[PATH_MAX]; |
96 |
> |
FILE *fp; |
97 |
|
int order; |
98 |
< |
int xmax, ymax; |
98 |
> |
int xmax, ymax; |
99 |
|
COLR *scanin; |
86 |
– |
register int x; |
100 |
|
int y; |
101 |
< |
/* get header info. */ |
102 |
< |
if (checkheader(stdin, COLRFMT, stdout) < 0 || |
103 |
< |
(order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
104 |
< |
quiterr("bad picture format"); |
105 |
< |
if (bradj) |
106 |
< |
fputexpos(pow(2.0, (double)bradj), stdout); |
107 |
< |
if (!doflat) { |
108 |
< |
fputformat(COLRFMT, stdout); |
109 |
< |
printf("%s -r\n\n", progname); |
110 |
< |
} else |
111 |
< |
printf("%s\n\n", progname); |
99 |
< |
fputresolu(order, xmax, ymax, stdout); |
100 |
< |
/* allocate scanline */ |
101 |
> |
/* get header info. */ |
102 |
> |
if (!(y = loadheader(stdin))) |
103 |
> |
return(0); |
104 |
> |
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { |
105 |
> |
fprintf(stderr, "%s: bad input format\n", progname); |
106 |
> |
exit(1); |
107 |
> |
} |
108 |
> |
/* did we pass the target frame? */ |
109 |
> |
if (findframe && findframe < frameno) |
110 |
> |
return(0); |
111 |
> |
/* allocate scanline */ |
112 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
113 |
< |
if (scanin == NULL) |
114 |
< |
quiterr("out of memory in transfer"); |
115 |
< |
/* convert image */ |
116 |
< |
for (y = ymax-1; y >= 0; y--) { |
117 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) |
118 |
< |
quiterr("error reading input picture"); |
113 |
> |
if (scanin == NULL) { |
114 |
> |
perror(progname); |
115 |
> |
exit(1); |
116 |
> |
} |
117 |
> |
/* skip frame? */ |
118 |
> |
if (findframe > frameno) { |
119 |
> |
if (NCSAMP > 3) { |
120 |
> |
if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) { |
121 |
> |
perror(progname); |
122 |
> |
exit(1); |
123 |
> |
} |
124 |
> |
} else |
125 |
> |
for (y = ymax; y--; ) |
126 |
> |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
127 |
> |
fprintf(stderr, |
128 |
> |
"%s: error reading input picture\n", |
129 |
> |
progname); |
130 |
> |
exit(1); |
131 |
> |
} |
132 |
> |
free(scanin); |
133 |
> |
return(1); |
134 |
> |
} |
135 |
> |
/* open output file/command */ |
136 |
> |
if (ospec == NULL) { |
137 |
> |
strcpy(oname, "<stdout>"); |
138 |
> |
fp = stdout; |
139 |
> |
} else { |
140 |
> |
sprintf(oname, ospec, frameno); |
141 |
> |
if (oname[0] == '!') { |
142 |
> |
if ((fp = popen(oname+1, "w")) == NULL) { |
143 |
> |
fprintf(stderr, "%s: cannot start \"%s\"\n", |
144 |
> |
progname, oname); |
145 |
> |
exit(1); |
146 |
> |
} |
147 |
> |
} else { |
148 |
> |
if (!force && access(oname, 0) >= 0) { |
149 |
> |
fprintf(stderr, |
150 |
> |
"%s: output file \"%s\" exists\n", |
151 |
> |
progname, oname); |
152 |
> |
exit(1); |
153 |
> |
} |
154 |
> |
if ((fp = fopen(oname, "w")) == NULL) { |
155 |
> |
fprintf(stderr, "%s: ", progname); |
156 |
> |
perror(oname); |
157 |
> |
exit(1); |
158 |
> |
} |
159 |
> |
} |
160 |
> |
} |
161 |
> |
SET_FILE_BINARY(fp); |
162 |
> |
newheader("RADIANCE", fp); /* put out header */ |
163 |
> |
dumpheader(fp); |
164 |
> |
fputs(progname, fp); |
165 |
> |
if (bradj) |
166 |
> |
fprintf(fp, " -e %+d", bradj); |
167 |
> |
if (!doflat) |
168 |
> |
fputs(" -r", fp); |
169 |
> |
fputc('\n', fp); |
170 |
> |
if (bradj) |
171 |
> |
fputexpos(pow(2.0, (double)bradj), fp); |
172 |
> |
if (frameno) |
173 |
> |
fprintf(fp, "FRAME=%d\n", frameno); |
174 |
> |
if (fmt[0]) |
175 |
> |
fputformat(fmt, fp); |
176 |
> |
fputc('\n', fp); |
177 |
> |
fputresolu(order, xmax, ymax, fp); |
178 |
> |
/* transfer picture */ |
179 |
> |
for (y = ymax; y--; ) { |
180 |
> |
if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) { |
181 |
> |
fprintf(stderr, "%s: error reading input picture\n", |
182 |
> |
progname); |
183 |
> |
exit(1); |
184 |
> |
} |
185 |
|
if (bradj) |
186 |
|
shiftcolrs(scanin, xmax, bradj); |
187 |
< |
if (doflat) |
188 |
< |
fwrite((char *)scanin, sizeof(COLR), xmax, stdout); |
187 |
> |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : |
188 |
> |
(fwritecolrs(scanin, xmax, fp) < 0)) |
189 |
> |
goto writerr; |
190 |
> |
} |
191 |
> |
free(scanin); /* clean up */ |
192 |
> |
if (fflush(fp) == EOF) |
193 |
> |
goto writerr; |
194 |
> |
if (oname[0] == '!') { |
195 |
> |
if (pclose(fp) != 0) { |
196 |
> |
fprintf(stderr, "%s: bad status from \"%s\"\n", |
197 |
> |
progname, oname); |
198 |
> |
exit(1); |
199 |
> |
} |
200 |
> |
} else if (ospec != NULL) |
201 |
> |
fclose(fp); |
202 |
> |
return(1); |
203 |
> |
writerr: |
204 |
> |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
205 |
> |
progname, oname); |
206 |
> |
exit(1); |
207 |
> |
} |
208 |
> |
|
209 |
> |
|
210 |
> |
static int |
211 |
> |
addhline( /* add a line to our info. header */ |
212 |
> |
char *s, |
213 |
> |
void *p |
214 |
> |
) |
215 |
> |
{ |
216 |
> |
int n; |
217 |
> |
|
218 |
> |
if (isheadid(s)) |
219 |
> |
return(0); |
220 |
> |
if (!strncmp(s, "FRAME=", 6)) { |
221 |
> |
frameno = atoi(s+6); |
222 |
> |
return(0); |
223 |
> |
} |
224 |
> |
if (formatval(fmt, s)) { |
225 |
> |
if (!strcmp(fmt, SPECFMT)) |
226 |
> |
strcpy(fmt, COLRFMT); |
227 |
|
else |
228 |
< |
fwritecolrs(scanin, xmax, stdout); |
229 |
< |
if (ferror(stdout)) |
115 |
< |
quiterr("error writing rasterfile"); |
228 |
> |
fmterr += !globmatch(PICFMT, fmt); |
229 |
> |
return(0); |
230 |
|
} |
231 |
< |
/* free scanline */ |
232 |
< |
free((char *)scanin); |
231 |
> |
if (isncomp(s)) { |
232 |
> |
NCSAMP = ncompval(s); |
233 |
> |
return(NCSAMP - 3); |
234 |
> |
} |
235 |
> |
if (iswlsplit(s)) { |
236 |
> |
wlsplitval(WLPART, s); |
237 |
> |
return(0); |
238 |
> |
} |
239 |
> |
n = strlen(s); |
240 |
> |
if (headlen) |
241 |
> |
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
242 |
> |
else |
243 |
> |
headlines = (char *)malloc(n+1); |
244 |
> |
if (headlines == NULL) { |
245 |
> |
perror(progname); |
246 |
> |
exit(1); |
247 |
> |
} |
248 |
> |
strcpy(headlines+headlen, s); |
249 |
> |
headlen += n; |
250 |
> |
return(0); |
251 |
> |
} |
252 |
> |
|
253 |
> |
|
254 |
> |
static int |
255 |
> |
loadheader( /* load an info. header into memory */ |
256 |
> |
FILE *fp |
257 |
> |
) |
258 |
> |
{ |
259 |
> |
fmterr = 0; frameno = 0; |
260 |
> |
/* revert to initial header length */ |
261 |
> |
if (!headlen1) headlen1 = headlen; |
262 |
> |
else headlen = headlen1; |
263 |
> |
|
264 |
> |
if (getheader(fp, addhline, NULL) < 0) |
265 |
> |
return(0); |
266 |
> |
if (fmterr) |
267 |
> |
return(-1); |
268 |
> |
return(1); |
269 |
|
} |