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