6 |
|
* File i/o and recovery |
7 |
|
*/ |
8 |
|
|
9 |
+ |
#include <ctype.h> |
10 |
+ |
#include "platform.h" |
11 |
|
#include "rcontrib.h" |
12 |
|
#include "resolu.h" |
11 |
– |
#include "platform.h" |
13 |
|
|
14 |
+ |
/* Close output stream and free record */ |
15 |
+ |
static void |
16 |
+ |
closestream(void *p) |
17 |
+ |
{ |
18 |
+ |
STREAMOUT *sop = (STREAMOUT *)p; |
19 |
+ |
|
20 |
+ |
if (sop->ofp != NULL) { |
21 |
+ |
int status = 0; |
22 |
+ |
if (sop->outpipe) |
23 |
+ |
status = pclose(sop->ofp); |
24 |
+ |
else if (sop->ofp != stdout) |
25 |
+ |
status = fclose(sop->ofp); |
26 |
+ |
if (status) |
27 |
+ |
error(SYSTEM, "error closing output stream"); |
28 |
+ |
} |
29 |
+ |
free(p); |
30 |
+ |
} |
31 |
+ |
|
32 |
+ |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
33 |
+ |
|
34 |
|
#define OF_MODIFIER 01 |
35 |
|
#define OF_BIN 02 |
36 |
|
|
113 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
114 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
115 |
|
fputnow(fout); |
116 |
+ |
fputs("NCOMP=3\n", fout); /* always RGB */ |
117 |
|
if (info != NULL) /* add extra info if given */ |
118 |
|
fputs(info, fout); |
119 |
|
fputformat(formstr(outfmt), fout); |
134 |
|
STREAMOUT * |
135 |
|
getostream(const char *ospec, const char *mname, int bn, int noopen) |
136 |
|
{ |
115 |
– |
/* static const DCOLOR nocontrib = BLKCOLOR; */ |
137 |
|
static STREAMOUT stdos; |
138 |
+ |
char info[1024]; |
139 |
|
int ofl; |
140 |
|
char oname[1024]; |
141 |
|
LUENT *lep; |
142 |
|
STREAMOUT *sop; |
143 |
+ |
char *cp; |
144 |
|
|
145 |
|
if (ospec == NULL) { /* use stdout? */ |
146 |
< |
if (!noopen && !using_stdout) { |
146 |
> |
if (!noopen & !using_stdout) { |
147 |
|
if (outfmt != 'a') |
148 |
|
SET_FILE_BINARY(stdout); |
149 |
< |
if (header) |
150 |
< |
printheader(stdout, NULL); |
151 |
< |
printresolu(stdout, xres, yres); |
149 |
> |
if (header) { |
150 |
> |
cp = info; |
151 |
> |
if (yres > 0) { |
152 |
> |
sprintf(cp, "NROWS=%d\n", yres * |
153 |
> |
(xres + !xres) ); |
154 |
> |
while (*cp) ++cp; |
155 |
> |
} |
156 |
> |
if ((xres <= 0) | (stdos.reclen > 1)) |
157 |
> |
sprintf(cp, "NCOLS=%d\n", stdos.reclen); |
158 |
> |
printheader(stdout, info); |
159 |
> |
} |
160 |
> |
if (stdos.reclen == 1) |
161 |
> |
printresolu(stdout, xres, yres); |
162 |
|
if (waitflush > 0) |
163 |
|
fflush(stdout); |
164 |
|
stdos.xr = xres; stdos.yr = yres; |
196 |
|
} |
197 |
|
} |
198 |
|
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
166 |
– |
long i; |
199 |
|
if (oname[0] == '!') /* output to command */ |
200 |
|
sop->ofp = popen(oname+1, "w"); |
201 |
|
else /* else open file */ |
207 |
|
#ifdef getc_unlocked |
208 |
|
flockfile(sop->ofp); /* avoid lock/unlock overhead */ |
209 |
|
#endif |
210 |
+ |
if (accumulate > 0) { /* global resolution */ |
211 |
+ |
sop->xr = xres; sop->yr = yres; |
212 |
+ |
} |
213 |
|
if (header) { |
214 |
< |
char info[512]; |
180 |
< |
char *cp = info; |
214 |
> |
cp = info; |
215 |
|
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
216 |
|
sprintf(cp, "MODIFIER=%s\n", mname); |
217 |
|
while (*cp) ++cp; |
220 |
|
sprintf(cp, "BIN=%d\n", bn); |
221 |
|
while (*cp) ++cp; |
222 |
|
} |
223 |
< |
*cp = '\0'; |
223 |
> |
if (sop->yr > 0) { |
224 |
> |
sprintf(cp, "NROWS=%d\n", sop->yr * |
225 |
> |
(sop->xr + !sop->xr) ); |
226 |
> |
while (*cp) ++cp; |
227 |
> |
} |
228 |
> |
if ((sop->xr <= 0) | (sop->reclen > 1)) |
229 |
> |
sprintf(cp, "NCOLS=%d\n", sop->reclen); |
230 |
|
printheader(sop->ofp, info); |
231 |
|
} |
232 |
< |
if (accumulate > 0) { /* global resolution */ |
233 |
< |
sop->xr = xres; sop->yr = yres; |
194 |
< |
} |
195 |
< |
printresolu(sop->ofp, sop->xr, sop->yr); |
196 |
< |
#if 0 |
197 |
< |
/* play catch-up */ |
198 |
< |
for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) { |
199 |
< |
int j = sop->reclen; |
200 |
< |
if (j <= 0) j = 1; |
201 |
< |
while (j--) |
202 |
< |
put_contrib(nocontrib, sop->ofp); |
203 |
< |
if (outfmt == 'a') |
204 |
< |
putc('\n', sop->ofp); |
205 |
< |
} |
206 |
< |
#endif |
232 |
> |
if (sop->reclen == 1) |
233 |
> |
printresolu(sop->ofp, sop->xr, sop->yr); |
234 |
|
if (waitflush > 0) |
235 |
|
fflush(sop->ofp); |
236 |
|
} |
262 |
|
} |
263 |
|
break; |
264 |
|
case 'f': /* binary float */ |
265 |
< |
if (fread((char *)vf, sizeof(float), 3, stdin) != 3) |
265 |
> |
if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3) |
266 |
|
return(-1); |
267 |
|
VCOPY(vec, vf); |
268 |
|
break; |
269 |
|
case 'd': /* binary double */ |
270 |
< |
if (fread((char *)vd, sizeof(double), 3, stdin) != 3) |
270 |
> |
if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3) |
271 |
|
return(-1); |
272 |
|
VCOPY(vec, vd); |
273 |
|
break; |
303 |
|
scalecolor(fv, sf); |
304 |
|
} else |
305 |
|
copycolor(fv, cnt); |
306 |
< |
fwrite(fv, sizeof(float), 3, fout); |
306 |
> |
putbinary(fv, sizeof(float), 3, fout); |
307 |
|
break; |
308 |
|
case 'd': |
309 |
|
if (accumulate > 1) { |
310 |
|
DCOLOR dv; |
311 |
|
copycolor(dv, cnt); |
312 |
|
scalecolor(dv, sf); |
313 |
< |
fwrite(dv, sizeof(double), 3, fout); |
313 |
> |
putbinary(dv, sizeof(double), 3, fout); |
314 |
|
} else |
315 |
< |
fwrite(cnt, sizeof(double), 3, fout); |
315 |
> |
putbinary(cnt, sizeof(double), 3, fout); |
316 |
|
break; |
317 |
|
case 'c': |
318 |
|
if (accumulate > 1) |
319 |
|
setcolr(cv, sf*cnt[0], sf*cnt[1], sf*cnt[2]); |
320 |
|
else |
321 |
|
setcolr(cv, cnt[0], cnt[1], cnt[2]); |
322 |
< |
fwrite(cv, sizeof(cv), 1, fout); |
322 |
> |
putbinary(cv, sizeof(cv), 1, fout); |
323 |
|
break; |
324 |
|
default: |
325 |
|
error(INTERNAL, "botched output format"); |
336 |
|
|
337 |
|
put_contrib(mp->cbin[0], sop->ofp); |
338 |
|
if (mp->nbins > 3 && /* minor optimization */ |
339 |
< |
sop == getostream(mp->outspec, mp->modname, 1, 0)) |
339 |
> |
sop == getostream(mp->outspec, mp->modname, 1, 0)) { |
340 |
|
for (j = 1; j < mp->nbins; j++) |
341 |
|
put_contrib(mp->cbin[j], sop->ofp); |
342 |
< |
else |
342 |
> |
} else { |
343 |
|
for (j = 1; j < mp->nbins; j++) { |
344 |
|
sop = getostream(mp->outspec, mp->modname, j, 0); |
345 |
|
put_contrib(mp->cbin[j], sop->ofp); |
346 |
|
} |
347 |
+ |
} |
348 |
|
} |
349 |
|
|
350 |
|
|
371 |
|
end_record() |
372 |
|
{ |
373 |
|
--waitflush; |
374 |
< |
lu_doall(&ofiletab, puteol, NULL); |
374 |
> |
lu_doall(&ofiletab, &puteol, NULL); |
375 |
|
if (using_stdout & (outfmt == 'a')) |
376 |
|
putc('\n', stdout); |
377 |
|
if (!waitflush) { |
394 |
|
case 'a': |
395 |
|
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); |
396 |
|
case 'f': |
397 |
< |
if (fread(fv, sizeof(fv[0]), 3, finp) != 3) |
397 |
> |
if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3) |
398 |
|
return(0); |
399 |
|
copycolor(cnt, fv); |
400 |
|
return(1); |
401 |
|
case 'd': |
402 |
< |
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3); |
402 |
> |
return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3); |
403 |
|
case 'c': |
404 |
< |
if (fread(cv, sizeof(cv), 1, finp) != 1) |
404 |
> |
if (getbinary(cv, sizeof(cv), 1, finp) != 1) |
405 |
|
return(0); |
406 |
|
colr_color(fv, cv); |
407 |
|
copycolor(cnt, fv); |
483 |
|
oname); |
484 |
|
error(USER, errmsg); |
485 |
|
} |
486 |
< |
if ((sout.xr > 0) & (sout.yr > 0) && |
486 |
> |
if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && |
487 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
488 |
|
(xr != sout.xr) | |
489 |
|
(yr != sout.yr))) { |
521 |
|
*(STREAMOUT *)oent->data = sout; |
522 |
|
} |
523 |
|
} |
524 |
< |
lu_doall(&ofiletab, myclose, NULL); /* close all files */ |
524 |
> |
lu_doall(&ofiletab, &myclose, NULL); /* close all files */ |
525 |
|
} |
526 |
|
|
527 |
|
|
593 |
|
} else { |
594 |
|
sout.reclen = 0; |
595 |
|
sout.outpipe = 0; |
596 |
+ |
sout.xr = xres; |
597 |
+ |
sout.yr = yres; |
598 |
|
sout.ofp = NULL; |
599 |
|
} |
600 |
|
if (sout.ofp != NULL) { /* already open? */ |
635 |
|
oname); |
636 |
|
error(USER, errmsg); |
637 |
|
} |
638 |
< |
sout.xr = xres; sout.yr = yres; |
609 |
< |
if ((sout.xr > 0) & (sout.yr > 0) && |
638 |
> |
if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && |
639 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
640 |
|
(xr != sout.xr) | |
641 |
|
(yr != sout.yr))) { |
678 |
|
} |
679 |
|
/* seek on all files */ |
680 |
|
nvals = lastout * outvsiz; |
681 |
< |
lu_doall(&ofiletab, myseeko, &nvals); |
681 |
> |
lu_doall(&ofiletab, &myseeko, &nvals); |
682 |
|
/* skip repeated input */ |
683 |
+ |
lastout *= accumulate; |
684 |
|
for (nvals = 0; nvals < lastout; nvals++) { |
685 |
|
FVECT vdummy; |
686 |
|
if (getvec(vdummy) < 0 || getvec(vdummy) < 0) |
687 |
|
error(USER, "unexpected EOF on input"); |
688 |
|
} |
689 |
< |
lastray = lastdone = lastout * accumulate; |
689 |
> |
lastray = lastdone = (RNUMBER)lastout; |
690 |
|
if (raysleft) |
691 |
|
raysleft -= lastray; |
692 |
|
} |