| 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; |
| 184 |
|
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
| 185 |
|
if (sop == NULL) |
| 186 |
|
error(SYSTEM, "out of memory in getostream"); |
| 187 |
< |
sop->outpipe = oname[0] == '!'; |
| 187 |
> |
sop->outpipe = (oname[0] == '!'); |
| 188 |
|
sop->reclen = 0; |
| 189 |
|
sop->ofp = NULL; /* open iff noopen==0 */ |
| 190 |
|
sop->xr = xres; sop->yr = yres; |
| 194 |
|
errno = EEXIST; /* file exists */ |
| 195 |
|
goto openerr; |
| 196 |
|
} |
| 197 |
+ |
} else if (noopen && outfmt == 'c' && /* stream exists to picture? */ |
| 198 |
+ |
(sop->xr > 0) & (sop->yr > 0)) { |
| 199 |
+ |
if (ofl & OF_BIN) |
| 200 |
+ |
return(NULL); /* let caller offset bins */ |
| 201 |
+ |
sprintf(errmsg, "output '%s' not a valid picture", oname); |
| 202 |
+ |
error(WARNING, errmsg); |
| 203 |
|
} |
| 204 |
|
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
| 166 |
– |
long i; |
| 205 |
|
if (oname[0] == '!') /* output to command */ |
| 206 |
|
sop->ofp = popen(oname+1, "w"); |
| 207 |
|
else /* else open file */ |
| 213 |
|
#ifdef getc_unlocked |
| 214 |
|
flockfile(sop->ofp); /* avoid lock/unlock overhead */ |
| 215 |
|
#endif |
| 216 |
+ |
if (accumulate > 0) { /* global resolution */ |
| 217 |
+ |
sop->xr = xres; sop->yr = yres; |
| 218 |
+ |
} |
| 219 |
|
if (header) { |
| 220 |
< |
char info[512]; |
| 180 |
< |
char *cp = info; |
| 220 |
> |
cp = info; |
| 221 |
|
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
| 222 |
|
sprintf(cp, "MODIFIER=%s\n", mname); |
| 223 |
|
while (*cp) ++cp; |
| 226 |
|
sprintf(cp, "BIN=%d\n", bn); |
| 227 |
|
while (*cp) ++cp; |
| 228 |
|
} |
| 229 |
< |
*cp = '\0'; |
| 229 |
> |
if (sop->yr > 0) { |
| 230 |
> |
sprintf(cp, "NROWS=%d\n", sop->yr * |
| 231 |
> |
(sop->xr + !sop->xr) ); |
| 232 |
> |
while (*cp) ++cp; |
| 233 |
> |
} |
| 234 |
> |
if ((sop->xr <= 0) | (sop->reclen > 1)) |
| 235 |
> |
sprintf(cp, "NCOLS=%d\n", sop->reclen); |
| 236 |
|
printheader(sop->ofp, info); |
| 237 |
|
} |
| 238 |
< |
if (accumulate > 0) { /* global resolution */ |
| 239 |
< |
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 |
| 238 |
> |
if (sop->reclen == 1) |
| 239 |
> |
printresolu(sop->ofp, sop->xr, sop->yr); |
| 240 |
|
if (waitflush > 0) |
| 241 |
|
fflush(sop->ofp); |
| 242 |
|
} |
| 268 |
|
} |
| 269 |
|
break; |
| 270 |
|
case 'f': /* binary float */ |
| 271 |
< |
if (fread((char *)vf, sizeof(float), 3, stdin) != 3) |
| 271 |
> |
if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3) |
| 272 |
|
return(-1); |
| 273 |
|
VCOPY(vec, vf); |
| 274 |
|
break; |
| 275 |
|
case 'd': /* binary double */ |
| 276 |
< |
if (fread((char *)vd, sizeof(double), 3, stdin) != 3) |
| 276 |
> |
if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3) |
| 277 |
|
return(-1); |
| 278 |
|
VCOPY(vec, vd); |
| 279 |
|
break; |
| 309 |
|
scalecolor(fv, sf); |
| 310 |
|
} else |
| 311 |
|
copycolor(fv, cnt); |
| 312 |
< |
fwrite(fv, sizeof(float), 3, fout); |
| 312 |
> |
putbinary(fv, sizeof(float), 3, fout); |
| 313 |
|
break; |
| 314 |
|
case 'd': |
| 315 |
|
if (accumulate > 1) { |
| 316 |
|
DCOLOR dv; |
| 317 |
|
copycolor(dv, cnt); |
| 318 |
|
scalecolor(dv, sf); |
| 319 |
< |
fwrite(dv, sizeof(double), 3, fout); |
| 319 |
> |
putbinary(dv, sizeof(double), 3, fout); |
| 320 |
|
} else |
| 321 |
< |
fwrite(cnt, sizeof(double), 3, fout); |
| 321 |
> |
putbinary(cnt, sizeof(double), 3, fout); |
| 322 |
|
break; |
| 323 |
|
case 'c': |
| 324 |
|
if (accumulate > 1) |
| 325 |
|
setcolr(cv, sf*cnt[0], sf*cnt[1], sf*cnt[2]); |
| 326 |
|
else |
| 327 |
|
setcolr(cv, cnt[0], cnt[1], cnt[2]); |
| 328 |
< |
fwrite(cv, sizeof(cv), 1, fout); |
| 328 |
> |
putbinary(cv, sizeof(cv), 1, fout); |
| 329 |
|
break; |
| 330 |
|
default: |
| 331 |
|
error(INTERNAL, "botched output format"); |
| 337 |
|
void |
| 338 |
|
mod_output(MODCONT *mp) |
| 339 |
|
{ |
| 340 |
< |
STREAMOUT *sop = getostream(mp->outspec, mp->modname, 0, 0); |
| 340 |
> |
STREAMOUT *sop = getostream(mp->outspec, mp->modname, mp->bin0, 0); |
| 341 |
|
int j; |
| 342 |
|
|
| 343 |
|
put_contrib(mp->cbin[0], sop->ofp); |
| 344 |
|
if (mp->nbins > 3 && /* minor optimization */ |
| 345 |
< |
sop == getostream(mp->outspec, mp->modname, 1, 0)) |
| 345 |
> |
sop == getostream(mp->outspec, mp->modname, mp->bin0+1, 0)) { |
| 346 |
|
for (j = 1; j < mp->nbins; j++) |
| 347 |
|
put_contrib(mp->cbin[j], sop->ofp); |
| 348 |
< |
else |
| 348 |
> |
} else { |
| 349 |
|
for (j = 1; j < mp->nbins; j++) { |
| 350 |
< |
sop = getostream(mp->outspec, mp->modname, j, 0); |
| 350 |
> |
sop = getostream(mp->outspec, mp->modname, mp->bin0+j, 0); |
| 351 |
|
put_contrib(mp->cbin[j], sop->ofp); |
| 352 |
|
} |
| 353 |
+ |
} |
| 354 |
|
} |
| 355 |
|
|
| 356 |
|
|
| 377 |
|
end_record() |
| 378 |
|
{ |
| 379 |
|
--waitflush; |
| 380 |
< |
lu_doall(&ofiletab, puteol, NULL); |
| 380 |
> |
lu_doall(&ofiletab, &puteol, NULL); |
| 381 |
|
if (using_stdout & (outfmt == 'a')) |
| 382 |
|
putc('\n', stdout); |
| 383 |
|
if (!waitflush) { |
| 400 |
|
case 'a': |
| 401 |
|
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); |
| 402 |
|
case 'f': |
| 403 |
< |
if (fread(fv, sizeof(fv[0]), 3, finp) != 3) |
| 403 |
> |
if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3) |
| 404 |
|
return(0); |
| 405 |
|
copycolor(cnt, fv); |
| 406 |
|
return(1); |
| 407 |
|
case 'd': |
| 408 |
< |
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3); |
| 408 |
> |
return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3); |
| 409 |
|
case 'c': |
| 410 |
< |
if (fread(cv, sizeof(cv), 1, finp) != 1) |
| 410 |
> |
if (getbinary(cv, sizeof(cv), 1, finp) != 1) |
| 411 |
|
return(0); |
| 412 |
|
colr_color(fv, cv); |
| 413 |
|
copycolor(cnt, fv); |
| 489 |
|
oname); |
| 490 |
|
error(USER, errmsg); |
| 491 |
|
} |
| 492 |
< |
if ((sout.xr > 0) & (sout.yr > 0) && |
| 492 |
> |
if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && |
| 493 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
| 494 |
|
(xr != sout.xr) | |
| 495 |
|
(yr != sout.yr))) { |
| 527 |
|
*(STREAMOUT *)oent->data = sout; |
| 528 |
|
} |
| 529 |
|
} |
| 530 |
< |
lu_doall(&ofiletab, myclose, NULL); /* close all files */ |
| 530 |
> |
lu_doall(&ofiletab, &myclose, NULL); /* close all files */ |
| 531 |
|
} |
| 532 |
|
|
| 533 |
|
|
| 599 |
|
} else { |
| 600 |
|
sout.reclen = 0; |
| 601 |
|
sout.outpipe = 0; |
| 602 |
+ |
sout.xr = xres; |
| 603 |
+ |
sout.yr = yres; |
| 604 |
|
sout.ofp = NULL; |
| 605 |
|
} |
| 606 |
|
if (sout.ofp != NULL) { /* already open? */ |
| 641 |
|
oname); |
| 642 |
|
error(USER, errmsg); |
| 643 |
|
} |
| 644 |
< |
sout.xr = xres; sout.yr = yres; |
| 609 |
< |
if ((sout.xr > 0) & (sout.yr > 0) && |
| 644 |
> |
if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) && |
| 645 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
| 646 |
|
(xr != sout.xr) | |
| 647 |
|
(yr != sout.yr))) { |
| 684 |
|
} |
| 685 |
|
/* seek on all files */ |
| 686 |
|
nvals = lastout * outvsiz; |
| 687 |
< |
lu_doall(&ofiletab, myseeko, &nvals); |
| 687 |
> |
lu_doall(&ofiletab, &myseeko, &nvals); |
| 688 |
|
/* skip repeated input */ |
| 689 |
+ |
lastout *= accumulate; |
| 690 |
|
for (nvals = 0; nvals < lastout; nvals++) { |
| 691 |
|
FVECT vdummy; |
| 692 |
|
if (getvec(vdummy) < 0 || getvec(vdummy) < 0) |
| 693 |
|
error(USER, "unexpected EOF on input"); |
| 694 |
|
} |
| 695 |
< |
lastray = lastdone = lastout * accumulate; |
| 695 |
> |
lastray = lastdone = (RNUMBER)lastout; |
| 696 |
|
if (raysleft) |
| 697 |
|
raysleft -= lastray; |
| 698 |
|
} |