7 |
|
* 7/4/19 Greg Ward |
8 |
|
*/ |
9 |
|
|
10 |
– |
#include <stdlib.h> |
11 |
– |
#include <string.h> |
12 |
– |
#include <stdio.h> |
10 |
|
#include <ctype.h> |
11 |
|
|
12 |
+ |
#include "rtio.h" |
13 |
|
#include "platform.h" |
14 |
+ |
#include "paths.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]; |
25 |
< |
int bytsiz[MAXFILE]; |
26 |
< |
short hdrflags[MAXFILE]; |
27 |
< |
const char *format[MAXFILE]; |
28 |
< |
int termc[MAXFILE]; |
29 |
< |
int nfiles = 0; |
22 |
> |
static int swapped = 0; /* input is byte-swapped */ |
23 |
|
|
24 |
< |
int outheader = 0; /* output header to each stream? */ |
24 |
> |
static FILE *output[MAXFILE]; |
25 |
> |
static int ncomp[MAXFILE]; |
26 |
> |
static int bytsiz[MAXFILE]; |
27 |
> |
static int hdrflags[MAXFILE]; |
28 |
> |
static const char *format[MAXFILE]; |
29 |
> |
static int termc[MAXFILE]; |
30 |
> |
static int nfiles = 0; |
31 |
|
|
32 |
< |
RESOLU ourres = {PIXSTANDARD, 0, 0}; |
32 |
> |
static RESOLU ourres = {PIXSTANDARD, 0, 0}; |
33 |
|
|
34 |
< |
char buf[16384]; |
34 |
> |
static char buf[16384]; /* input buffer used in scanOK() */ |
35 |
|
|
36 |
|
|
37 |
|
/* process header line */ |
39 |
|
headline(char *s, void *p) |
40 |
|
{ |
41 |
|
extern const char FMTSTR[]; |
42 |
< |
int i = nfiles; |
42 |
> |
int i; |
43 |
|
|
44 |
|
if (strstr(s, FMTSTR) == s) |
45 |
|
return(0); /* don't copy format */ |
49 |
|
return(0); |
50 |
|
if (!strncmp(s, "NCOMP=", 6)) |
51 |
|
return(0); |
52 |
+ |
if ((i = isbigendian(s)) >= 0) { |
53 |
+ |
swapped = (nativebigendian() != i); |
54 |
+ |
return(0); |
55 |
+ |
} |
56 |
+ |
i = nfiles; |
57 |
|
while (i--) /* else copy line to output streams */ |
58 |
|
if (hdrflags[i] & DOHEADER) |
59 |
|
fputs(s, output[i]); |
65 |
|
static int |
66 |
|
scanOK(int termc) |
67 |
|
{ |
68 |
+ |
int skip_white = (termc == ' '); |
69 |
|
char *cp = buf; |
70 |
|
int c; |
71 |
|
|
72 |
|
while ((c = getchar()) != EOF) { |
73 |
+ |
if (skip_white && isspace(c)) |
74 |
+ |
continue; |
75 |
+ |
skip_white = 0; |
76 |
+ |
if (c == '\n' && isspace(termc)) |
77 |
+ |
c = termc; /* forgiving assumption */ |
78 |
|
*cp++ = c; |
79 |
|
if (cp-buf >= sizeof(buf)) |
80 |
|
break; |
81 |
< |
if (c == termc) { |
81 |
> |
if ((termc == ' ') ? isspace(c) : (c == termc)) { |
82 |
|
*cp = '\0'; |
83 |
|
return(cp-buf); |
84 |
|
} |
90 |
|
int |
91 |
|
main(int argc, char *argv[]) |
92 |
|
{ |
93 |
< |
int inpheader = 0; |
94 |
< |
int inpres = 0; |
85 |
< |
int outres = 0; |
93 |
> |
int inpflags = 0; |
94 |
> |
int needres = 0; |
95 |
|
int force = 0; |
96 |
|
int append = 0; |
97 |
|
long outcnt = 0; |
98 |
|
int bininp = 0; |
99 |
|
int curterm = '\n'; |
100 |
+ |
int curncomp = 1; |
101 |
|
int curbytes = 0; |
102 |
|
int curflags = 0; |
103 |
|
const char *curfmt = "ascii"; |
108 |
|
switch (argv[i][1]) { |
109 |
|
case 't': |
110 |
|
curterm = argv[i][2]; |
111 |
+ |
if (!curterm) curterm = '\n'; |
112 |
|
break; |
113 |
|
case 'i': |
114 |
|
switch (argv[i][2]) { |
115 |
|
case 'h': |
116 |
< |
inpheader = !inpheader; |
116 |
> |
inpflags ^= DOHEADER; |
117 |
|
break; |
118 |
|
case 'H': |
119 |
< |
inpres = !inpres; |
119 |
> |
inpflags ^= DORESOLU; |
120 |
|
break; |
121 |
|
default: |
122 |
|
goto badopt; |
123 |
|
} |
124 |
|
break; |
125 |
|
case 'f': |
126 |
< |
++force; |
116 |
< |
append = 0; |
126 |
> |
force = !force; |
127 |
|
break; |
128 |
|
case 'a': |
129 |
< |
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; |
129 |
> |
append = !append; |
130 |
|
break; |
131 |
|
case 'x': |
132 |
|
ourres.xr = atoi(argv[++i]); |
129 |
– |
outres = 1; |
133 |
|
break; |
134 |
|
case 'y': |
135 |
|
ourres.yr = atoi(argv[++i]); |
133 |
– |
outres = 1; |
136 |
|
break; |
137 |
|
case 'o': |
138 |
|
switch (argv[i][2]) { |
167 |
|
break; |
168 |
|
case 'a': |
169 |
|
curfmt = "ascii"; |
170 |
< |
curbytes = argv[i][3] ? -1 : 0; |
170 |
> |
curbytes = 0; |
171 |
|
break; |
172 |
|
default: |
173 |
|
goto badopt; |
174 |
|
} |
175 |
< |
if (isdigit(argv[i][3])) |
176 |
< |
curbytes *= atoi(argv[i]+3); |
177 |
< |
curbytes += (curbytes == -1); |
176 |
< |
if (curbytes > (int)sizeof(buf)) { |
175 |
> |
curncomp = isdigit(argv[i][3]) ? |
176 |
> |
atoi(argv[i]+3) : 1 ; |
177 |
> |
if (curbytes*curncomp > (int)sizeof(buf)) { |
178 |
|
fputs(argv[0], stderr); |
179 |
|
fputs(": output size too big\n", stderr); |
180 |
|
return(1); |
181 |
|
} |
182 |
< |
if (curbytes > 0) { |
182 |
< |
curterm = '\0'; |
183 |
< |
++bininp; |
184 |
< |
} |
182 |
> |
bininp += (curbytes > 0); |
183 |
|
break; |
184 |
|
case '\0': |
185 |
< |
outres |= (curflags & DORESOLU); |
185 |
> |
needres |= (curflags & DORESOLU); |
186 |
|
termc[nfiles] = curterm; |
187 |
|
hdrflags[nfiles] = curflags; |
188 |
|
output[nfiles] = stdout; |
189 |
|
if (curbytes > 0) |
190 |
|
SET_FILE_BINARY(output[nfiles]); |
191 |
|
format[nfiles] = curfmt; |
192 |
+ |
ncomp[nfiles] = curncomp; |
193 |
|
bytsiz[nfiles++] = curbytes; |
194 |
|
break; |
195 |
|
badopt:; |
199 |
|
return(1); |
200 |
|
} |
201 |
|
} else if (argv[i][0] == '!') { |
202 |
< |
outres |= (curflags & DORESOLU); |
202 |
> |
needres |= (curflags & DORESOLU); |
203 |
|
termc[nfiles] = curterm; |
204 |
|
hdrflags[nfiles] = curflags; |
205 |
|
if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) { |
210 |
|
if (curbytes > 0) |
211 |
|
SET_FILE_BINARY(output[nfiles]); |
212 |
|
format[nfiles] = curfmt; |
213 |
+ |
ncomp[nfiles] = curncomp; |
214 |
|
bytsiz[nfiles++] = curbytes; |
215 |
|
} else { |
216 |
< |
outres |= (curflags & DORESOLU); |
216 |
> |
if (append & (curflags != 0)) { |
217 |
> |
fputs(argv[0], stderr); |
218 |
> |
fputs(": -a option incompatible with -oh and -oH\n", |
219 |
> |
stderr); |
220 |
> |
return(1); |
221 |
> |
} |
222 |
> |
needres |= (curflags & DORESOLU); |
223 |
|
termc[nfiles] = curterm; |
224 |
|
hdrflags[nfiles] = curflags; |
225 |
|
if (!append & !force && access(argv[i], F_OK) == 0) { |
237 |
|
if (curbytes > 0) |
238 |
|
SET_FILE_BINARY(output[nfiles]); |
239 |
|
format[nfiles] = curfmt; |
240 |
+ |
ncomp[nfiles] = curncomp; |
241 |
|
bytsiz[nfiles++] = curbytes; |
242 |
|
} |
243 |
|
if (nfiles >= MAXFILE) { |
259 |
|
flockfile(output[i]); |
260 |
|
#endif |
261 |
|
/* load/copy header */ |
262 |
< |
if (inpheader && getheader(stdin, headline, NULL) < 0) { |
262 |
> |
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
263 |
|
fputs(argv[0], stderr); |
264 |
|
fputs(": cannot get header from standard input\n", |
265 |
|
stderr); |
266 |
|
return(1); |
267 |
|
} |
268 |
|
/* handle resolution string */ |
269 |
< |
if (inpres && !fgetsresolu(&ourres, stdin)) { |
269 |
> |
if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) { |
270 |
|
fputs(argv[0], stderr); |
271 |
|
fputs(": cannot get resolution string from standard input\n", |
272 |
|
stderr); |
273 |
|
return(1); |
274 |
|
} |
275 |
< |
if (outres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
275 |
> |
if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
276 |
|
fputs(argv[0], stderr); |
277 |
|
fputs(": -oH option requires -iH or -x and -y options\n", stderr); |
278 |
|
return(1); |
288 |
|
} |
289 |
|
for (i = 0; i < nfiles; i++) { /* complete headers */ |
290 |
|
if (hdrflags[i] & DOHEADER) { |
291 |
< |
if (!inpheader) |
291 |
> |
if (!(inpflags & DOHEADER)) |
292 |
|
newheader("RADIANCE", output[i]); |
293 |
|
printargs(argc, argv, output[i]); |
294 |
< |
if (format[i] != NULL) |
294 |
> |
fprintf(output[i], "NCOMP=%d\n", ncomp[i]); |
295 |
> |
if (format[i] != NULL) { |
296 |
> |
extern const char BIGEND[]; |
297 |
> |
if ((format[i][0] == 'f') | |
298 |
> |
(format[i][0] == 'd')) { |
299 |
> |
fputs(BIGEND, output[i]); |
300 |
> |
fputs(nativebigendian() ^ swapped ? |
301 |
> |
"1\n" : "0\n", output[i]); |
302 |
> |
} |
303 |
|
fputformat(format[i], output[i]); |
304 |
+ |
} |
305 |
|
fputc('\n', output[i]); |
306 |
|
} |
307 |
|
if (hdrflags[i] & DORESOLU) |
310 |
|
do { /* main loop */ |
311 |
|
for (i = 0; i < nfiles; i++) { |
312 |
|
if (bytsiz[i] > 0) { /* binary output */ |
313 |
< |
if (getbinary(buf, bytsiz[i], 1, stdin) < 1) |
313 |
> |
if (getbinary(buf, bytsiz[i], ncomp[i], |
314 |
> |
stdin) < ncomp[i]) |
315 |
|
break; |
316 |
< |
putbinary(buf, bytsiz[i], 1, output[i]); |
317 |
< |
} else if (bytsiz[i] < 0) { /* N-field output */ |
318 |
< |
int n = -bytsiz[i]; |
316 |
> |
if (putbinary(buf, bytsiz[i], ncomp[i], |
317 |
> |
output[i]) < ncomp[i]) |
318 |
> |
break; |
319 |
> |
} else if (ncomp[i] > 1) { /* N-field output */ |
320 |
> |
int n = ncomp[i]; |
321 |
|
while (n--) { |
322 |
|
if (!scanOK(termc[i])) |
323 |
|
break; |
324 |
< |
fputs(buf, output[i]); |
324 |
> |
if (fputs(buf, output[i]) == EOF) |
325 |
> |
break; |
326 |
|
} |
327 |
|
if (n >= 0) /* fell short? */ |
328 |
|
break; |
329 |
+ |
if (termc[i] != '\n') /* add EOL if none */ |
330 |
+ |
fputc('\n', output[i]); |
331 |
|
} else { /* 1-field output */ |
332 |
|
if (!scanOK(termc[i])) |
333 |
|
break; |
334 |
< |
fputs(buf, output[i]); |
334 |
> |
if (fputs(buf, output[i]) == EOF) |
335 |
> |
break; |
336 |
> |
if (termc[i] != '\n') /* add EOL if none */ |
337 |
> |
fputc('\n', output[i]); |
338 |
|
} |
339 |
+ |
/* skip input EOL? */ |
340 |
+ |
if (!bininp && termc[nfiles-1] != '\n') { |
341 |
+ |
int c = getchar(); |
342 |
+ |
if ((c != '\n') & (c != EOF)) |
343 |
+ |
ungetc(c, stdin); |
344 |
+ |
} |
345 |
|
} |
346 |
|
if (i < nfiles) |
347 |
|
break; |
348 |
|
} while (--outcnt); |
349 |
|
/* check ending */ |
319 |
– |
if (outcnt > 0) { |
320 |
– |
fputs(argv[0], stderr); |
321 |
– |
fputs(": warning: premature EOD\n", stderr); |
322 |
– |
} |
350 |
|
if (fflush(NULL) == EOF) { |
351 |
|
fputs(argv[0], stderr); |
352 |
|
fputs(": write error on one or more outputs\n", stderr); |
353 |
|
return(1); |
354 |
+ |
} |
355 |
+ |
if (outcnt > 0) { |
356 |
+ |
fputs(argv[0], stderr); |
357 |
+ |
fputs(": warning: premature EOD\n", stderr); |
358 |
|
} |
359 |
|
return(0); |
360 |
|
} |