1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* program to convert from RADIANCE RLE to flat format |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <math.h> |
13 |
#include "color.h" |
14 |
#include "resolu.h" |
15 |
|
16 |
#ifdef MSDOS |
17 |
#include <fcntl.h> |
18 |
#endif |
19 |
|
20 |
extern char *malloc(), *realloc(), *tempbuffer(); |
21 |
extern int addhline(); |
22 |
|
23 |
#define dumpheader(fp) fwrite(headlines, 1, headlen, fp) |
24 |
|
25 |
int bradj = 0; /* brightness adjustment */ |
26 |
|
27 |
int doflat = 1; /* produce flat file */ |
28 |
|
29 |
int force = 0; /* force file overwrite? */ |
30 |
|
31 |
int findframe = 0; /* find a specific frame? */ |
32 |
|
33 |
int frameno = 0; /* current frame number */ |
34 |
int fmterr = 0; /* got input format error */ |
35 |
char *headlines; /* current header info. */ |
36 |
int headlen; /* current header length */ |
37 |
|
38 |
char *progname; |
39 |
|
40 |
|
41 |
main(argc, argv) |
42 |
int argc; |
43 |
char *argv[]; |
44 |
{ |
45 |
char *ospec; |
46 |
int i; |
47 |
|
48 |
progname = argv[0]; |
49 |
|
50 |
for (i = 1; i < argc; i++) |
51 |
if (argv[i][0] == '-') |
52 |
switch (argv[i][1]) { |
53 |
case 'r': |
54 |
doflat = !doflat; |
55 |
break; |
56 |
case 'e': |
57 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
58 |
goto userr; |
59 |
bradj = atoi(argv[++i]); |
60 |
break; |
61 |
case 'n': |
62 |
findframe = atoi(argv[++i]); |
63 |
break; |
64 |
case 'f': |
65 |
force++; |
66 |
break; |
67 |
case '\0': |
68 |
goto gotfile; |
69 |
default: |
70 |
goto userr; |
71 |
} |
72 |
else |
73 |
break; |
74 |
gotfile: |
75 |
if (i < argc-2) |
76 |
goto userr; |
77 |
if (i <= argc-1 && strcmp(argv[i], "-") && |
78 |
freopen(argv[i], "r", stdin) == NULL) { |
79 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
80 |
progname, argv[i]); |
81 |
exit(1); |
82 |
} |
83 |
#ifdef MSDOS |
84 |
setmode(fileno(stdin), O_BINARY); |
85 |
#endif |
86 |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
87 |
while (transfer(ospec)) |
88 |
; |
89 |
exit(0); |
90 |
userr: |
91 |
fprintf(stderr, |
92 |
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
93 |
progname); |
94 |
exit(1); |
95 |
} |
96 |
|
97 |
|
98 |
transfer(ospec) /* transfer a Radiance picture */ |
99 |
char *ospec; |
100 |
{ |
101 |
char oname[128]; |
102 |
FILE *fp; |
103 |
int order; |
104 |
int xmax, ymax; |
105 |
COLR *scanin; |
106 |
int y; |
107 |
/* get header info. */ |
108 |
if (!(y = loadheader(stdin))) |
109 |
return(0); |
110 |
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { |
111 |
fprintf(stderr, "%s: bad input format\n", progname); |
112 |
exit(1); |
113 |
} |
114 |
/* did we pass the target frame? */ |
115 |
if (findframe && findframe < frameno) |
116 |
return(0); |
117 |
/* allocate scanline */ |
118 |
scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); |
119 |
if (scanin == NULL) { |
120 |
perror(progname); |
121 |
exit(1); |
122 |
} |
123 |
/* skip frame? */ |
124 |
if (findframe > frameno) { |
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 |
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 |
#ifdef MSDOS |
161 |
setmode(fileno(fp), O_BINARY); |
162 |
#endif |
163 |
dumpheader(fp); /* put out header */ |
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 |
fputc('\n', fp); |
173 |
fputresolu(order, xmax, ymax, fp); |
174 |
/* transfer picture */ |
175 |
for (y = ymax; y--; ) { |
176 |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
177 |
fprintf(stderr, "%s: error reading input picture\n", |
178 |
progname); |
179 |
exit(1); |
180 |
} |
181 |
if (bradj) |
182 |
shiftcolrs(scanin, xmax, bradj); |
183 |
if (doflat) |
184 |
fwrite((char *)scanin, sizeof(COLR), xmax, fp); |
185 |
else |
186 |
fwritecolrs(scanin, xmax, fp); |
187 |
if (ferror(fp)) { |
188 |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
189 |
progname, oname); |
190 |
exit(1); |
191 |
} |
192 |
} |
193 |
/* clean up */ |
194 |
if (oname[0] == '!') |
195 |
pclose(fp); |
196 |
else if (ospec != NULL) |
197 |
fclose(fp); |
198 |
return(1); |
199 |
} |
200 |
|
201 |
|
202 |
addhline(s) /* add a line to our info. header */ |
203 |
char *s; |
204 |
{ |
205 |
char fmt[32]; |
206 |
int n; |
207 |
|
208 |
if (formatval(fmt, s)) |
209 |
fmterr += !globmatch(PICFMT, fmt); |
210 |
else if (!strncmp(s, "FRAME=", 6)) |
211 |
frameno = atoi(s+6); |
212 |
n = strlen(s); |
213 |
if (headlen) |
214 |
headlines = (char *)realloc(headlines, headlen+n+1); |
215 |
else |
216 |
headlines = (char *)malloc(n+1); |
217 |
if (headlines == NULL) { |
218 |
perror(progname); |
219 |
exit(1); |
220 |
} |
221 |
strcpy(headlines+headlen, s); |
222 |
headlen += n; |
223 |
} |
224 |
|
225 |
|
226 |
loadheader(fp) /* load an info. header into memory */ |
227 |
FILE *fp; |
228 |
{ |
229 |
fmterr = 0; frameno = 0; |
230 |
if (headlen) { /* free old header */ |
231 |
free(headlines); |
232 |
headlen = 0; |
233 |
} |
234 |
if (getheader(fp, addhline, NULL) < 0) |
235 |
return(0); |
236 |
if (fmterr) |
237 |
return(-1); |
238 |
return(1); |
239 |
} |