ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/rsplit.c
Revision: 1.12
Committed: Tue Mar 31 16:39:01 2020 UTC (4 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +18 -11 lines
Log Message:
Added NCOMP= line to header output

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.12 static const char RCSid[] = "$Id: rsplit.c,v 1.11 2019/11/07 23:13:12 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * rsplit.c - split input into multiple output streams
6     *
7     * 7/4/19 Greg Ward
8     */
9    
10     #include <ctype.h>
11    
12 greg 1.6 #include "rtio.h"
13 greg 1.1 #include "platform.h"
14 greg 1.9 #include "paths.h"
15 greg 1.1 #include "resolu.h"
16    
17     #define DOHEADER 1
18     #define DORESOLU 2
19    
20     #define MAXFILE 512 /* maximum number of files */
21    
22 greg 1.10 static int swapped = 0; /* input is byte-swapped */
23    
24 greg 1.7 static FILE *output[MAXFILE];
25 greg 1.12 static int ncomp[MAXFILE];
26 greg 1.7 static int bytsiz[MAXFILE];
27 greg 1.12 static int hdrflags[MAXFILE];
28 greg 1.7 static const char *format[MAXFILE];
29     static int termc[MAXFILE];
30     static int nfiles = 0;
31 greg 1.1
32 greg 1.7 static RESOLU ourres = {PIXSTANDARD, 0, 0};
33 greg 1.1
34 greg 1.7 static char buf[16384]; /* input buffer used in scanOK() */
35 greg 1.1
36    
37     /* process header line */
38     static int
39     headline(char *s, void *p)
40     {
41     extern const char FMTSTR[];
42 greg 1.10 int i;
43 greg 1.1
44     if (strstr(s, FMTSTR) == s)
45     return(0); /* don't copy format */
46     if (!strncmp(s, "NROWS=", 6))
47     return(0);
48     if (!strncmp(s, "NCOLS=", 6))
49     return(0);
50     if (!strncmp(s, "NCOMP=", 6))
51     return(0);
52 greg 1.10 if ((i = isbigendian(s)) >= 0) {
53     swapped = (nativebigendian() != i);
54     return(0);
55     }
56     i = nfiles;
57 greg 1.1 while (i--) /* else copy line to output streams */
58     if (hdrflags[i] & DOHEADER)
59     fputs(s, output[i]);
60     return(1);
61     }
62    
63    
64     /* scan field into buffer up to and including terminating byte */
65     static int
66     scanOK(int termc)
67     {
68 greg 1.11 int skip_white = (termc == ' ');
69 greg 1.1 char *cp = buf;
70     int c;
71    
72     while ((c = getchar()) != EOF) {
73 greg 1.11 if (skip_white && isspace(c))
74     continue;
75     skip_white = 0;
76     if (c == '\n' && isspace(termc))
77     c = termc; /* forgiving assumption */
78 greg 1.1 *cp++ = c;
79     if (cp-buf >= sizeof(buf))
80     break;
81 greg 1.11 if ((termc == ' ') ? isspace(c) : (c == termc)) {
82 greg 1.1 *cp = '\0';
83     return(cp-buf);
84     }
85     }
86     return(0);
87     }
88    
89    
90     int
91     main(int argc, char *argv[])
92     {
93 greg 1.8 int inpflags = 0;
94     int needres = 0;
95 greg 1.1 int force = 0;
96     int append = 0;
97     long outcnt = 0;
98     int bininp = 0;
99     int curterm = '\n';
100 greg 1.12 int curncomp = 1;
101 greg 1.1 int curbytes = 0;
102     int curflags = 0;
103     const char *curfmt = "ascii";
104     int i;
105    
106     for (i = 1; i < argc; i++) {
107     if (argv[i][0] == '-') {
108     switch (argv[i][1]) {
109     case 't':
110     curterm = argv[i][2];
111 greg 1.5 if (!curterm) curterm = '\n';
112 greg 1.1 break;
113     case 'i':
114     switch (argv[i][2]) {
115     case 'h':
116 greg 1.8 inpflags ^= DOHEADER;
117 greg 1.1 break;
118     case 'H':
119 greg 1.8 inpflags ^= DORESOLU;
120 greg 1.1 break;
121     default:
122     goto badopt;
123     }
124     break;
125     case 'f':
126 greg 1.8 force = !force;
127 greg 1.1 break;
128     case 'a':
129 greg 1.8 append = !append;
130 greg 1.1 break;
131     case 'x':
132     ourres.xr = atoi(argv[++i]);
133     break;
134     case 'y':
135     ourres.yr = atoi(argv[++i]);
136     break;
137     case 'o':
138     switch (argv[i][2]) {
139     case 'n':
140     outcnt = atol(argv[++i]);
141     continue;
142     case 'h':
143     curflags ^= DOHEADER;
144     continue;
145     case 'H':
146     curflags ^= DORESOLU;
147     continue;
148     case 'f':
149     curfmt = "float";
150     curbytes = sizeof(float);
151     break;
152     case 'd':
153     curfmt = "double";
154     curbytes = sizeof(double);
155     break;
156     case 'i':
157     curfmt = "int";
158     curbytes = sizeof(int);
159     break;
160     case 'w':
161     curfmt = "16-bit";
162     curbytes = 2;
163     break;
164     case 'b':
165     curfmt = "byte";
166     curbytes = 1;
167     break;
168     case 'a':
169     curfmt = "ascii";
170 greg 1.12 curbytes = 0;
171 greg 1.1 break;
172     default:
173     goto badopt;
174     }
175 greg 1.12 curncomp = isdigit(argv[i][3]) ?
176     atoi(argv[i]+3) : 1 ;
177     if (curbytes*curncomp > (int)sizeof(buf)) {
178 greg 1.1 fputs(argv[0], stderr);
179     fputs(": output size too big\n", stderr);
180     return(1);
181     }
182 greg 1.7 bininp += (curbytes > 0);
183 greg 1.1 break;
184     case '\0':
185 greg 1.8 needres |= (curflags & DORESOLU);
186 greg 1.1 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 greg 1.12 ncomp[nfiles] = curncomp;
193 greg 1.1 bytsiz[nfiles++] = curbytes;
194     break;
195     badopt:;
196     default:
197     fputs(argv[0], stderr);
198     fputs(": bad option\n", stderr);
199     return(1);
200     }
201     } else if (argv[i][0] == '!') {
202 greg 1.8 needres |= (curflags & DORESOLU);
203 greg 1.1 termc[nfiles] = curterm;
204     hdrflags[nfiles] = curflags;
205     if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) {
206     fputs(argv[i], stderr);
207     fputs(": cannot start command\n", stderr);
208     return(1);
209     }
210     if (curbytes > 0)
211     SET_FILE_BINARY(output[nfiles]);
212     format[nfiles] = curfmt;
213 greg 1.12 ncomp[nfiles] = curncomp;
214 greg 1.1 bytsiz[nfiles++] = curbytes;
215     } else {
216 greg 1.8 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 greg 1.1 termc[nfiles] = curterm;
224     hdrflags[nfiles] = curflags;
225     if (!append & !force && access(argv[i], F_OK) == 0) {
226     fputs(argv[i], stderr);
227     fputs(": file exists -- use -f to overwrite\n",
228     stderr);
229     return(1);
230     }
231     output[nfiles] = fopen(argv[i], append ? "a" : "w");
232     if (output[nfiles] == NULL) {
233     fputs(argv[i], stderr);
234     fputs(": cannot open for output\n", stderr);
235     return(1);
236     }
237     if (curbytes > 0)
238     SET_FILE_BINARY(output[nfiles]);
239     format[nfiles] = curfmt;
240 greg 1.12 ncomp[nfiles] = curncomp;
241 greg 1.1 bytsiz[nfiles++] = curbytes;
242     }
243     if (nfiles >= MAXFILE) {
244     fputs(argv[0], stderr);
245     fputs(": too many output streams\n", stderr);
246     return(1);
247     }
248     }
249     if (!nfiles) {
250     fputs(argv[0], stderr);
251     fputs(": no output streams\n", stderr);
252     return(1);
253     }
254     if (bininp) /* binary input? */
255     SET_FILE_BINARY(stdin);
256     #ifdef getc_unlocked /* avoid lock/unlock overhead */
257     flockfile(stdin);
258     for (i = nfiles; i--; )
259     flockfile(output[i]);
260     #endif
261     /* load/copy header */
262 greg 1.8 if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) {
263 greg 1.1 fputs(argv[0], stderr);
264     fputs(": cannot get header from standard input\n",
265     stderr);
266     return(1);
267     }
268     /* handle resolution string */
269 greg 1.8 if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) {
270 greg 1.1 fputs(argv[0], stderr);
271     fputs(": cannot get resolution string from standard input\n",
272     stderr);
273     return(1);
274     }
275 greg 1.8 if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) {
276 greg 1.1 fputs(argv[0], stderr);
277     fputs(": -oH option requires -iH or -x and -y options\n", stderr);
278     return(1);
279     }
280     if ((ourres.xr > 0) & (ourres.yr > 0)) {
281     if (outcnt <= 0) {
282     outcnt = ourres.xr * ourres.yr;
283     } else if (outcnt != ourres.xr*ourres.yr) {
284     fputs(argv[0], stderr);
285     fputs(": warning: -on option does not agree with resolution\n",
286     stderr);
287     }
288     }
289     for (i = 0; i < nfiles; i++) { /* complete headers */
290     if (hdrflags[i] & DOHEADER) {
291 greg 1.8 if (!(inpflags & DOHEADER))
292 greg 1.1 newheader("RADIANCE", output[i]);
293     printargs(argc, argv, output[i]);
294 greg 1.12 fprintf(output[i], "NCOMP=%d\n", ncomp[i]);
295 greg 1.10 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 greg 1.1 fputformat(format[i], output[i]);
304 greg 1.10 }
305 greg 1.1 fputc('\n', output[i]);
306     }
307     if (hdrflags[i] & DORESOLU)
308     fputsresolu(&ourres, output[i]);
309     }
310     do { /* main loop */
311     for (i = 0; i < nfiles; i++) {
312     if (bytsiz[i] > 0) { /* binary output */
313 greg 1.12 if (getbinary(buf, bytsiz[i], ncomp[i],
314     stdin) < ncomp[i])
315 greg 1.1 break;
316 greg 1.12 if (putbinary(buf, bytsiz[i], ncomp[i],
317     output[i]) < ncomp[i])
318 greg 1.4 break;
319 greg 1.12 } else if (ncomp[i] > 1) { /* N-field output */
320     int n = ncomp[i];
321 greg 1.1 while (n--) {
322     if (!scanOK(termc[i]))
323     break;
324 greg 1.4 if (fputs(buf, output[i]) == EOF)
325     break;
326 greg 1.1 }
327     if (n >= 0) /* fell short? */
328     break;
329 greg 1.4 if (termc[i] != '\n') /* add EOL if none */
330     fputc('\n', output[i]);
331 greg 1.1 } else { /* 1-field output */
332     if (!scanOK(termc[i]))
333     break;
334 greg 1.4 if (fputs(buf, output[i]) == EOF)
335     break;
336     if (termc[i] != '\n') /* add EOL if none */
337     fputc('\n', output[i]);
338 greg 1.1 }
339 greg 1.7 /* 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 greg 1.1 }
346     if (i < nfiles)
347     break;
348     } while (--outcnt);
349 greg 1.2 /* check ending */
350 greg 1.3 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 greg 1.4 if (outcnt > 0) {
356     fputs(argv[0], stderr);
357     fputs(": warning: premature EOD\n", stderr);
358     }
359 greg 1.1 return(0);
360     }