1 |
– |
/* Copyright (c) 1992 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> |
12 |
< |
#ifdef MSDOS |
13 |
< |
#include <fcntl.h> |
14 |
< |
#endif |
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 |
< |
extern char *malloc(); |
16 |
> |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
17 |
|
|
18 |
|
int bradj = 0; /* brightness adjustment */ |
22 |
– |
|
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 |
< |
#ifdef MSDOS |
34 |
< |
extern int _fmode; |
35 |
< |
_fmode = O_BINARY; |
36 |
< |
setmode(fileno(stdin), O_BINARY); |
37 |
< |
setmode(fileno(stdout), O_BINARY); |
38 |
< |
#endif |
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); |
69 |
< |
} |
70 |
< |
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); |
84 |
< |
exit(1); |
85 |
< |
} |
86 |
< |
exit(0); |
87 |
< |
} |
88 |
< |
|
89 |
< |
|
90 |
< |
transfer() /* transfer Radiance picture */ |
91 |
< |
{ |
92 |
< |
extern double pow(); |
95 |
> |
char oname[PATH_MAX]; |
96 |
> |
FILE *fp; |
97 |
|
int order; |
98 |
|
int xmax, ymax; |
99 |
|
COLR *scanin; |
96 |
– |
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 |
< |
fputs(progname, stdout); |
106 |
< |
if (bradj) |
104 |
< |
printf(" -e %+d", bradj); |
105 |
< |
if (doflat) |
106 |
< |
fputs("\n", stdout); |
107 |
< |
else { |
108 |
< |
fputs(" -r\n", stdout); |
109 |
< |
fputformat(COLRFMT, stdout); |
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 *)tempbuffer(xmax*sizeof(COLR)); |
113 |
+ |
if (scanin == NULL) { |
114 |
+ |
perror(progname); |
115 |
+ |
exit(1); |
116 |
+ |
} |
117 |
+ |
/* skip frame? */ |
118 |
+ |
if (findframe > frameno) { |
119 |
+ |
for (y = ymax; y--; ) |
120 |
+ |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
121 |
+ |
fprintf(stderr, |
122 |
+ |
"%s: error reading input picture\n", |
123 |
+ |
progname); |
124 |
+ |
exit(1); |
125 |
+ |
} |
126 |
+ |
return(1); |
127 |
+ |
} |
128 |
+ |
/* open output file/command */ |
129 |
+ |
if (ospec == NULL) { |
130 |
+ |
strcpy(oname, "<stdout>"); |
131 |
+ |
fp = stdout; |
132 |
+ |
} else { |
133 |
+ |
sprintf(oname, ospec, frameno); |
134 |
+ |
if (oname[0] == '!') { |
135 |
+ |
if ((fp = popen(oname+1, "w")) == NULL) { |
136 |
+ |
fprintf(stderr, "%s: cannot start \"%s\"\n", |
137 |
+ |
progname, oname); |
138 |
+ |
exit(1); |
139 |
+ |
} |
140 |
+ |
} else { |
141 |
+ |
if (!force && access(oname, 0) >= 0) { |
142 |
+ |
fprintf(stderr, |
143 |
+ |
"%s: output file \"%s\" exists\n", |
144 |
+ |
progname, oname); |
145 |
+ |
exit(1); |
146 |
+ |
} |
147 |
+ |
if ((fp = fopen(oname, "w")) == NULL) { |
148 |
+ |
fprintf(stderr, "%s: ", progname); |
149 |
+ |
perror(oname); |
150 |
+ |
exit(1); |
151 |
+ |
} |
152 |
+ |
} |
153 |
+ |
} |
154 |
+ |
SET_FILE_BINARY(fp); |
155 |
+ |
newheader("RADIANCE", fp); /* put out header */ |
156 |
+ |
dumpheader(fp); |
157 |
+ |
fputs(progname, fp); |
158 |
|
if (bradj) |
159 |
< |
fputexpos(pow(2.0, (double)bradj), stdout); |
160 |
< |
fputs("\n", stdout); |
161 |
< |
fputresolu(order, xmax, ymax, stdout); |
162 |
< |
/* allocate scanline */ |
163 |
< |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
164 |
< |
if (scanin == NULL) |
165 |
< |
quiterr("out of memory in transfer"); |
166 |
< |
/* convert image */ |
167 |
< |
for (y = ymax-1; y >= 0; y--) { |
168 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) |
169 |
< |
quiterr("error reading input picture"); |
159 |
> |
fprintf(fp, " -e %+d", bradj); |
160 |
> |
if (!doflat) |
161 |
> |
fputs(" -r", fp); |
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 */ |
172 |
> |
for (y = ymax; y--; ) { |
173 |
> |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
174 |
> |
fprintf(stderr, "%s: error reading input picture\n", |
175 |
> |
progname); |
176 |
> |
exit(1); |
177 |
> |
} |
178 |
|
if (bradj) |
179 |
|
shiftcolrs(scanin, xmax, bradj); |
180 |
< |
if (doflat) |
181 |
< |
fwrite((char *)scanin, sizeof(COLR), xmax, stdout); |
182 |
< |
else |
128 |
< |
fwritecolrs(scanin, xmax, stdout); |
129 |
< |
if (ferror(stdout)) |
130 |
< |
quiterr("error writing rasterfile"); |
180 |
> |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : |
181 |
> |
(fwritecolrs(scanin, xmax, fp) < 0)) |
182 |
> |
goto writerr; |
183 |
|
} |
184 |
< |
/* free scanline */ |
185 |
< |
free((char *)scanin); |
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 |
> |
static int |
199 |
> |
addhline( /* add a line to our info. header */ |
200 |
> |
char *s, |
201 |
> |
void *p |
202 |
> |
) |
203 |
> |
{ |
204 |
> |
int n; |
205 |
> |
|
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((void *)headlines, headlen+n+1); |
219 |
> |
else |
220 |
> |
headlines = (char *)malloc(n+1); |
221 |
> |
if (headlines == NULL) { |
222 |
> |
perror(progname); |
223 |
> |
exit(1); |
224 |
> |
} |
225 |
> |
strcpy(headlines+headlen, s); |
226 |
> |
headlen += n; |
227 |
> |
return(0); |
228 |
> |
} |
229 |
> |
|
230 |
> |
|
231 |
> |
static int |
232 |
> |
loadheader( /* load an info. header into memory */ |
233 |
> |
FILE *fp |
234 |
> |
) |
235 |
> |
{ |
236 |
> |
fmterr = 0; frameno = 0; |
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) |
244 |
> |
return(-1); |
245 |
> |
return(1); |
246 |
|
} |