8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdlib.h> |
11 |
– |
#include <string.h> |
12 |
– |
#include <stdio.h> |
11 |
|
#include <ctype.h> |
12 |
|
|
13 |
+ |
#include "rtio.h" |
14 |
|
#include "platform.h" |
15 |
|
#include "resolu.h" |
17 |
– |
#include "rtio.h" |
16 |
|
|
17 |
|
#define DOHEADER 1 |
18 |
|
#define DORESOLU 2 |
19 |
|
|
20 |
|
#define MAXFILE 512 /* maximum number of files */ |
21 |
|
|
22 |
< |
FILE *output[MAXFILE]; |
23 |
< |
int bytsiz[MAXFILE]; |
24 |
< |
short hdrflags[MAXFILE]; |
25 |
< |
const char *format[MAXFILE]; |
26 |
< |
int termc[MAXFILE]; |
27 |
< |
int nfiles = 0; |
22 |
> |
static FILE *output[MAXFILE]; |
23 |
> |
static int bytsiz[MAXFILE]; |
24 |
> |
static short hdrflags[MAXFILE]; |
25 |
> |
static const char *format[MAXFILE]; |
26 |
> |
static int termc[MAXFILE]; |
27 |
> |
static int nfiles = 0; |
28 |
|
|
29 |
< |
int outheader = 0; /* output header to each stream? */ |
29 |
> |
static RESOLU ourres = {PIXSTANDARD, 0, 0}; |
30 |
|
|
31 |
< |
RESOLU ourres = {PIXSTANDARD, 0, 0}; |
31 |
> |
static char buf[16384]; /* input buffer used in scanOK() */ |
32 |
|
|
35 |
– |
char buf[16384]; |
33 |
|
|
37 |
– |
|
34 |
|
/* process header line */ |
35 |
|
static int |
36 |
|
headline(char *s, void *p) |
76 |
|
int |
77 |
|
main(int argc, char *argv[]) |
78 |
|
{ |
79 |
< |
int inpheader = 0; |
80 |
< |
int inpres = 0; |
85 |
< |
int outres = 0; |
79 |
> |
int inpflags = 0; |
80 |
> |
int needres = 0; |
81 |
|
int force = 0; |
82 |
|
int append = 0; |
83 |
|
long outcnt = 0; |
93 |
|
switch (argv[i][1]) { |
94 |
|
case 't': |
95 |
|
curterm = argv[i][2]; |
96 |
+ |
if (!curterm) curterm = '\n'; |
97 |
|
break; |
98 |
|
case 'i': |
99 |
|
switch (argv[i][2]) { |
100 |
|
case 'h': |
101 |
< |
inpheader = !inpheader; |
101 |
> |
inpflags ^= DOHEADER; |
102 |
|
break; |
103 |
|
case 'H': |
104 |
< |
inpres = !inpres; |
104 |
> |
inpflags ^= DORESOLU; |
105 |
|
break; |
106 |
|
default: |
107 |
|
goto badopt; |
108 |
|
} |
109 |
|
break; |
110 |
|
case 'f': |
111 |
< |
++force; |
116 |
< |
append = 0; |
111 |
> |
force = !force; |
112 |
|
break; |
113 |
|
case 'a': |
114 |
< |
if (outheader | outres) { |
120 |
< |
fputs(argv[0], stderr); |
121 |
< |
fputs(": -a option incompatible with -oh and -oH\n", |
122 |
< |
stderr); |
123 |
< |
return(1); |
124 |
< |
} |
125 |
< |
append = 1; |
114 |
> |
append = !append; |
115 |
|
break; |
116 |
|
case 'x': |
117 |
|
ourres.xr = atoi(argv[++i]); |
129 |
– |
outres = 1; |
118 |
|
break; |
119 |
|
case 'y': |
120 |
|
ourres.yr = atoi(argv[++i]); |
133 |
– |
outres = 1; |
121 |
|
break; |
122 |
|
case 'o': |
123 |
|
switch (argv[i][2]) { |
152 |
|
break; |
153 |
|
case 'a': |
154 |
|
curfmt = "ascii"; |
155 |
< |
curbytes = argv[i][3] ? -1 : 0; |
155 |
> |
curbytes = -1; |
156 |
|
break; |
157 |
|
default: |
158 |
|
goto badopt; |
165 |
|
fputs(": output size too big\n", stderr); |
166 |
|
return(1); |
167 |
|
} |
168 |
< |
if (curbytes > 0) { |
182 |
< |
curterm = '\0'; |
183 |
< |
++bininp; |
184 |
< |
} |
168 |
> |
bininp += (curbytes > 0); |
169 |
|
break; |
170 |
|
case '\0': |
171 |
< |
outres |= (curflags & DORESOLU); |
171 |
> |
needres |= (curflags & DORESOLU); |
172 |
|
termc[nfiles] = curterm; |
173 |
|
hdrflags[nfiles] = curflags; |
174 |
|
output[nfiles] = stdout; |
184 |
|
return(1); |
185 |
|
} |
186 |
|
} else if (argv[i][0] == '!') { |
187 |
< |
outres |= (curflags & DORESOLU); |
187 |
> |
needres |= (curflags & DORESOLU); |
188 |
|
termc[nfiles] = curterm; |
189 |
|
hdrflags[nfiles] = curflags; |
190 |
|
if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) { |
197 |
|
format[nfiles] = curfmt; |
198 |
|
bytsiz[nfiles++] = curbytes; |
199 |
|
} else { |
200 |
< |
outres |= (curflags & DORESOLU); |
200 |
> |
if (append & (curflags != 0)) { |
201 |
> |
fputs(argv[0], stderr); |
202 |
> |
fputs(": -a option incompatible with -oh and -oH\n", |
203 |
> |
stderr); |
204 |
> |
return(1); |
205 |
> |
} |
206 |
> |
needres |= (curflags & DORESOLU); |
207 |
|
termc[nfiles] = curterm; |
208 |
|
hdrflags[nfiles] = curflags; |
209 |
|
if (!append & !force && access(argv[i], F_OK) == 0) { |
242 |
|
flockfile(output[i]); |
243 |
|
#endif |
244 |
|
/* load/copy header */ |
245 |
< |
if (inpheader && getheader(stdin, headline, NULL) < 0) { |
245 |
> |
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
246 |
|
fputs(argv[0], stderr); |
247 |
|
fputs(": cannot get header from standard input\n", |
248 |
|
stderr); |
249 |
|
return(1); |
250 |
|
} |
251 |
|
/* handle resolution string */ |
252 |
< |
if (inpres && !fgetsresolu(&ourres, stdin)) { |
252 |
> |
if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) { |
253 |
|
fputs(argv[0], stderr); |
254 |
|
fputs(": cannot get resolution string from standard input\n", |
255 |
|
stderr); |
256 |
|
return(1); |
257 |
|
} |
258 |
< |
if (outres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
258 |
> |
if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
259 |
|
fputs(argv[0], stderr); |
260 |
|
fputs(": -oH option requires -iH or -x and -y options\n", stderr); |
261 |
|
return(1); |
271 |
|
} |
272 |
|
for (i = 0; i < nfiles; i++) { /* complete headers */ |
273 |
|
if (hdrflags[i] & DOHEADER) { |
274 |
< |
if (!inpheader) |
274 |
> |
if (!(inpflags & DOHEADER)) |
275 |
|
newheader("RADIANCE", output[i]); |
276 |
|
printargs(argc, argv, output[i]); |
277 |
|
if (format[i] != NULL) |
286 |
|
if (bytsiz[i] > 0) { /* binary output */ |
287 |
|
if (getbinary(buf, bytsiz[i], 1, stdin) < 1) |
288 |
|
break; |
289 |
< |
if (putbinary(buf, bytsiz[i], 1, output[i]) != 1) |
289 |
> |
if (putbinary(buf, bytsiz[i], 1, output[i]) < 1) |
290 |
|
break; |
291 |
|
} else if (bytsiz[i] < 0) { /* N-field output */ |
292 |
|
int n = -bytsiz[i]; |
307 |
|
break; |
308 |
|
if (termc[i] != '\n') /* add EOL if none */ |
309 |
|
fputc('\n', output[i]); |
310 |
+ |
} |
311 |
+ |
/* skip input EOL? */ |
312 |
+ |
if (!bininp && termc[nfiles-1] != '\n') { |
313 |
+ |
int c = getchar(); |
314 |
+ |
if ((c != '\n') & (c != EOF)) |
315 |
+ |
ungetc(c, stdin); |
316 |
|
} |
317 |
|
} |
318 |
|
if (i < nfiles) |