7 |
|
* 7/4/19 Greg Ward |
8 |
|
*/ |
9 |
|
|
10 |
– |
#include <stdlib.h> |
10 |
|
#include <ctype.h> |
11 |
|
|
12 |
|
#include "rtio.h" |
13 |
|
#include "platform.h" |
14 |
+ |
#include "paths.h" |
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
|
#define DOHEADER 1 |
19 |
|
|
20 |
|
#define MAXFILE 512 /* maximum number of files */ |
21 |
|
|
22 |
+ |
static int swapped = 0; /* input is byte-swapped */ |
23 |
+ |
|
24 |
|
static FILE *output[MAXFILE]; |
25 |
|
static int bytsiz[MAXFILE]; |
26 |
|
static short hdrflags[MAXFILE]; |
28 |
|
static int termc[MAXFILE]; |
29 |
|
static int nfiles = 0; |
30 |
|
|
29 |
– |
static int outheader = 0; /* output header to each stream? */ |
30 |
– |
|
31 |
|
static RESOLU ourres = {PIXSTANDARD, 0, 0}; |
32 |
|
|
33 |
|
static char buf[16384]; /* input buffer used in scanOK() */ |
38 |
|
headline(char *s, void *p) |
39 |
|
{ |
40 |
|
extern const char FMTSTR[]; |
41 |
< |
int i = nfiles; |
41 |
> |
int i; |
42 |
|
|
43 |
|
if (strstr(s, FMTSTR) == s) |
44 |
|
return(0); /* don't copy format */ |
48 |
|
return(0); |
49 |
|
if (!strncmp(s, "NCOMP=", 6)) |
50 |
|
return(0); |
51 |
+ |
if ((i = isbigendian(s)) >= 0) { |
52 |
+ |
swapped = (nativebigendian() != i); |
53 |
+ |
return(0); |
54 |
+ |
} |
55 |
+ |
i = nfiles; |
56 |
|
while (i--) /* else copy line to output streams */ |
57 |
|
if (hdrflags[i] & DOHEADER) |
58 |
|
fputs(s, output[i]); |
83 |
|
int |
84 |
|
main(int argc, char *argv[]) |
85 |
|
{ |
86 |
< |
int inpheader = 0; |
87 |
< |
int inpres = 0; |
83 |
< |
int outres = 0; |
86 |
> |
int inpflags = 0; |
87 |
> |
int needres = 0; |
88 |
|
int force = 0; |
89 |
|
int append = 0; |
90 |
|
long outcnt = 0; |
105 |
|
case 'i': |
106 |
|
switch (argv[i][2]) { |
107 |
|
case 'h': |
108 |
< |
inpheader = !inpheader; |
108 |
> |
inpflags ^= DOHEADER; |
109 |
|
break; |
110 |
|
case 'H': |
111 |
< |
inpres = !inpres; |
111 |
> |
inpflags ^= DORESOLU; |
112 |
|
break; |
113 |
|
default: |
114 |
|
goto badopt; |
115 |
|
} |
116 |
|
break; |
117 |
|
case 'f': |
118 |
< |
++force; |
115 |
< |
append = 0; |
118 |
> |
force = !force; |
119 |
|
break; |
120 |
|
case 'a': |
121 |
< |
if (outheader | outres) { |
119 |
< |
fputs(argv[0], stderr); |
120 |
< |
fputs(": -a option incompatible with -oh and -oH\n", |
121 |
< |
stderr); |
122 |
< |
return(1); |
123 |
< |
} |
124 |
< |
append = 1; |
121 |
> |
append = !append; |
122 |
|
break; |
123 |
|
case 'x': |
124 |
|
ourres.xr = atoi(argv[++i]); |
128 |
– |
outres = 1; |
125 |
|
break; |
126 |
|
case 'y': |
127 |
|
ourres.yr = atoi(argv[++i]); |
132 |
– |
outres = 1; |
128 |
|
break; |
129 |
|
case 'o': |
130 |
|
switch (argv[i][2]) { |
175 |
|
bininp += (curbytes > 0); |
176 |
|
break; |
177 |
|
case '\0': |
178 |
< |
outres |= (curflags & DORESOLU); |
178 |
> |
needres |= (curflags & DORESOLU); |
179 |
|
termc[nfiles] = curterm; |
180 |
|
hdrflags[nfiles] = curflags; |
181 |
|
output[nfiles] = stdout; |
191 |
|
return(1); |
192 |
|
} |
193 |
|
} else if (argv[i][0] == '!') { |
194 |
< |
outres |= (curflags & DORESOLU); |
194 |
> |
needres |= (curflags & DORESOLU); |
195 |
|
termc[nfiles] = curterm; |
196 |
|
hdrflags[nfiles] = curflags; |
197 |
|
if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) { |
204 |
|
format[nfiles] = curfmt; |
205 |
|
bytsiz[nfiles++] = curbytes; |
206 |
|
} else { |
207 |
< |
outres |= (curflags & DORESOLU); |
207 |
> |
if (append & (curflags != 0)) { |
208 |
> |
fputs(argv[0], stderr); |
209 |
> |
fputs(": -a option incompatible with -oh and -oH\n", |
210 |
> |
stderr); |
211 |
> |
return(1); |
212 |
> |
} |
213 |
> |
needres |= (curflags & DORESOLU); |
214 |
|
termc[nfiles] = curterm; |
215 |
|
hdrflags[nfiles] = curflags; |
216 |
|
if (!append & !force && access(argv[i], F_OK) == 0) { |
249 |
|
flockfile(output[i]); |
250 |
|
#endif |
251 |
|
/* load/copy header */ |
252 |
< |
if (inpheader && getheader(stdin, headline, NULL) < 0) { |
252 |
> |
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
253 |
|
fputs(argv[0], stderr); |
254 |
|
fputs(": cannot get header from standard input\n", |
255 |
|
stderr); |
256 |
|
return(1); |
257 |
|
} |
258 |
|
/* handle resolution string */ |
259 |
< |
if (inpres && !fgetsresolu(&ourres, stdin)) { |
259 |
> |
if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) { |
260 |
|
fputs(argv[0], stderr); |
261 |
|
fputs(": cannot get resolution string from standard input\n", |
262 |
|
stderr); |
263 |
|
return(1); |
264 |
|
} |
265 |
< |
if (outres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
265 |
> |
if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
266 |
|
fputs(argv[0], stderr); |
267 |
|
fputs(": -oH option requires -iH or -x and -y options\n", stderr); |
268 |
|
return(1); |
278 |
|
} |
279 |
|
for (i = 0; i < nfiles; i++) { /* complete headers */ |
280 |
|
if (hdrflags[i] & DOHEADER) { |
281 |
< |
if (!inpheader) |
281 |
> |
if (!(inpflags & DOHEADER)) |
282 |
|
newheader("RADIANCE", output[i]); |
283 |
|
printargs(argc, argv, output[i]); |
284 |
< |
if (format[i] != NULL) |
284 |
> |
if (format[i] != NULL) { |
285 |
> |
extern const char BIGEND[]; |
286 |
> |
if ((format[i][0] == 'f') | |
287 |
> |
(format[i][0] == 'd')) { |
288 |
> |
fputs(BIGEND, output[i]); |
289 |
> |
fputs(nativebigendian() ^ swapped ? |
290 |
> |
"1\n" : "0\n", output[i]); |
291 |
> |
} |
292 |
|
fputformat(format[i], output[i]); |
293 |
+ |
} |
294 |
|
fputc('\n', output[i]); |
295 |
|
} |
296 |
|
if (hdrflags[i] & DORESOLU) |