ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rc2.c
(Generate patch)

Comparing ray/src/rt/rc2.c (file contents):
Revision 2.15 by greg, Sun Aug 23 00:17:12 2015 UTC vs.
Revision 2.25 by greg, Fri Apr 7 00:03:26 2023 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
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 <ctype.h>
13  
14   /* Close output stream and free record */
15   static void
# Line 115 | Line 116 | printheader(FILE *fout, const char *info)
116          fputs("NCOMP=3\n", fout);               /* always RGB */
117          if (info != NULL)                       /* add extra info if given */
118                  fputs(info, fout);
119 +        if ((outfmt == 'f') | (outfmt == 'd'))
120 +                fputendian(fout);
121          fputformat(formstr(outfmt), fout);
122          fputc('\n', fout);                      /* empty line ends header */
123   }
# Line 140 | Line 143 | getostream(const char *ospec, const char *mname, int b
143          LUENT                   *lep;
144          STREAMOUT               *sop;
145          char                    *cp;
146 <        
146 >
147 >        info[0] = '\0';
148          if (ospec == NULL) {                    /* use stdout? */
149                  if (!noopen & !using_stdout) {
150                          if (outfmt != 'a')
151                                  SET_FILE_BINARY(stdout);
152 + #ifdef getc_unlocked
153 +                        flockfile(stdout);      /* avoid lock/unlock overhead */
154 + #endif
155                          if (header) {
156                                  cp = info;
157                                  if (yres > 0) {
# Line 161 | Line 168 | getostream(const char *ospec, const char *mname, int b
168                          if (waitflush > 0)
169                                  fflush(stdout);
170                          stdos.xr = xres; stdos.yr = yres;
164 #ifdef getc_unlocked
165                        flockfile(stdout);      /* avoid lock/unlock overhead */
166 #endif
171                          using_stdout = 1;
172                  }
173                  stdos.ofp = stdout;
# Line 183 | Line 187 | getostream(const char *ospec, const char *mname, int b
187                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
188                  if (sop == NULL)
189                          error(SYSTEM, "out of memory in getostream");
190 <                sop->outpipe = oname[0] == '!';
190 >                sop->outpipe = (oname[0] == '!');
191                  sop->reclen = 0;
192                  sop->ofp = NULL;                /* open iff noopen==0 */
193                  sop->xr = xres; sop->yr = yres;
# Line 193 | Line 197 | getostream(const char *ospec, const char *mname, int b
197                          errno = EEXIST;         /* file exists */
198                          goto openerr;
199                  }
200 +        } else if (noopen && outfmt == 'c' &&   /* stream exists to picture? */
201 +                        (sop->xr > 0) & (sop->yr > 0)) {
202 +                if (ofl & OF_BIN)
203 +                        return(NULL);           /* let caller offset bins */
204 +                sprintf(errmsg, "output '%s' not a valid picture", oname);
205 +                error(WARNING, errmsg);
206          }
207          if (!noopen && sop->ofp == NULL) {      /* open output stream */
208                  if (oname[0] == '!')            /* output to command */
# Line 261 | Line 271 | getvec(FVECT vec)
271                  }
272                  break;
273          case 'f':                                       /* binary float */
274 <                if (fread((char *)vf, sizeof(float), 3, stdin) != 3)
274 >                if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3)
275                          return(-1);
276                  VCOPY(vec, vf);
277                  break;
278          case 'd':                                       /* binary double */
279 <                if (fread((char *)vd, sizeof(double), 3, stdin) != 3)
279 >                if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3)
280                          return(-1);
281                  VCOPY(vec, vd);
282                  break;
# Line 302 | Line 312 | put_contrib(const DCOLOR cnt, FILE *fout)
312                          scalecolor(fv, sf);
313                  } else
314                          copycolor(fv, cnt);
315 <                fwrite(fv, sizeof(float), 3, fout);
315 >                putbinary(fv, sizeof(float), 3, fout);
316                  break;
317          case 'd':
318                  if (accumulate > 1) {
319                          DCOLOR  dv;
320                          copycolor(dv, cnt);
321                          scalecolor(dv, sf);
322 <                        fwrite(dv, sizeof(double), 3, fout);
322 >                        putbinary(dv, sizeof(double), 3, fout);
323                  } else
324 <                        fwrite(cnt, sizeof(double), 3, fout);
324 >                        putbinary(cnt, sizeof(double), 3, fout);
325                  break;
326          case 'c':
327                  if (accumulate > 1)
328                          setcolr(cv, sf*cnt[0], sf*cnt[1], sf*cnt[2]);
329                  else
330                          setcolr(cv, cnt[0], cnt[1], cnt[2]);
331 <                fwrite(cv, sizeof(cv), 1, fout);
331 >                putbinary(cv, sizeof(cv), 1, fout);
332                  break;
333          default:
334                  error(INTERNAL, "botched output format");
# Line 330 | Line 340 | put_contrib(const DCOLOR cnt, FILE *fout)
340   void
341   mod_output(MODCONT *mp)
342   {
343 <        STREAMOUT       *sop = getostream(mp->outspec, mp->modname, 0, 0);
343 >        STREAMOUT       *sop = getostream(mp->outspec, mp->modname, mp->bin0, 0);
344          int             j;
345  
346          put_contrib(mp->cbin[0], sop->ofp);
347          if (mp->nbins > 3 &&    /* minor optimization */
348 <                        sop == getostream(mp->outspec, mp->modname, 1, 0)) {
348 >                        sop == getostream(mp->outspec, mp->modname, mp->bin0+1, 0)) {
349                  for (j = 1; j < mp->nbins; j++)
350                          put_contrib(mp->cbin[j], sop->ofp);
351          } else {
352                  for (j = 1; j < mp->nbins; j++) {
353 <                        sop = getostream(mp->outspec, mp->modname, j, 0);
353 >                        sop = getostream(mp->outspec, mp->modname, mp->bin0+j, 0);
354                          put_contrib(mp->cbin[j], sop->ofp);
355                  }
356          }
# Line 393 | Line 403 | get_contrib(DCOLOR cnt, FILE *finp)
403          case 'a':
404                  return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
405          case 'f':
406 <                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
406 >                if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3)
407                          return(0);
408                  copycolor(cnt, fv);
409                  return(1);
410          case 'd':
411 <                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
411 >                return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3);
412          case 'c':
413 <                if (fread(cv, sizeof(cv), 1, finp) != 1)
413 >                if (getbinary(cv, sizeof(cv), 1, finp) != 1)
414                          return(0);
415                  colr_color(fv, cv);
416                  copycolor(cnt, fv);
# Line 438 | Line 448 | reload_output()
448          char            *outvfmt;
449          LUENT           *oent;
450          int             xr, yr;
451 <        STREAMOUT       sout;
451 >        STREAMOUT       *sop;
452          DCOLOR          rgbv;
453  
454          if (outfmt == 'a')
# Line 451 | Line 461 | reload_output()
461                          error(USER, "cannot reload from stdout");
462                  if (mp->outspec[0] == '!')
463                          error(USER, "cannot reload from command");
464 <                for (j = 0; ; j++) {            /* load each modifier bin */
465 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
464 >                for (j = 0; j < mp->nbins; j++) { /* load each modifier bin */
465 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
466                          if (ofl < 0)
467                                  error(USER, "bad output file specification");
468                          oent = lu_find(&ofiletab, oname);
469 <                        if (oent->data != NULL) {
470 <                                sout = *(STREAMOUT *)oent->data;
471 <                        } else {
472 <                                sout.reclen = 0;
473 <                                sout.outpipe = 0;
474 <                                sout.xr = xres; sout.yr = yres;
465 <                                sout.ofp = NULL;
466 <                        }
467 <                        if (sout.ofp == NULL) { /* open output as input */
468 <                                sout.ofp = fopen(oname, fmode);
469 <                                if (sout.ofp == NULL) {
470 <                                        if (j == mp->nbins)
471 <                                                break;  /* assume end of modifier */
469 >                        if (oent->data == NULL)
470 >                                error(INTERNAL, "unallocated stream in reload_output()");
471 >                        sop = (STREAMOUT *)oent->data;
472 >                        if (sop->ofp == NULL) { /* open output as input */
473 >                                sop->ofp = fopen(oname, fmode);
474 >                                if (sop->ofp == NULL) {
475                                          sprintf(errmsg, "missing reload file '%s'",
476                                                          oname);
477                                          error(WARNING, errmsg);
478                                          break;
479                                  }
480   #ifdef getc_unlocked
481 <                                flockfile(sout.ofp);
481 >                                flockfile(sop->ofp);
482   #endif
483 <                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
483 >                                if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
484                                          sprintf(errmsg, "format mismatch for '%s'",
485                                                          oname);
486                                          error(USER, errmsg);
487                                  }
488 <                                if ((sout.xr > 0) & (sout.yr > 0) &&
489 <                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
490 <                                                        (xr != sout.xr) |
491 <                                                        (yr != sout.yr))) {
488 >                                if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
489 >                                                (!fscnresolu(&xr, &yr, sop->ofp) ||
490 >                                                        (xr != sop->xr) |
491 >                                                        (yr != sop->yr))) {
492                                          sprintf(errmsg, "resolution mismatch for '%s'",
493                                                          oname);
494                                          error(USER, errmsg);
495                                  }
496                          }
497                                                          /* read in RGB value */
498 <                        if (!get_contrib(rgbv, sout.ofp)) {
498 >                        if (!get_contrib(rgbv, sop->ofp)) {
499                                  if (!j) {
500 <                                        fclose(sout.ofp);
500 >                                        fclose(sop->ofp);
501                                          break;          /* ignore empty file */
502                                  }
503                                  if (j < mp->nbins) {
# Line 503 | Line 506 | reload_output()
506                                          error(USER, errmsg);
507                                  }
508                                  break;
509 <                        }
507 <                        if (j >= mp->nbins) {           /* check modifier size */
508 <                                sprintf(errmsg,
509 <                                "mismatched -bn setting for reloading '%s'",
510 <                                                modname[i]);
511 <                                error(USER, errmsg);
512 <                        }
513 <                                
509 >                        }                              
510                          copycolor(mp->cbin[j], rgbv);
515                        if (oent->key == NULL)          /* new file entry */
516                                oent->key = strcpy((char *)
517                                                malloc(strlen(oname)+1), oname);
518                        if (oent->data == NULL)
519                                oent->data = (char *)malloc(sizeof(STREAMOUT));
520                        *(STREAMOUT *)oent->data = sout;
511                  }
512          }
513          lu_doall(&ofiletab, &myclose, NULL);    /* close all files */
# Line 532 | Line 522 | myseeko(const LUENT *e, void *p)
522          off_t           nbytes = *(off_t *)p;
523          
524          if (sop->reclen > 1)
525 <                nbytes = nbytes * sop->reclen;
525 >                nbytes *= (off_t)sop->reclen;
526          if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
527                  sprintf(errmsg, "seek error on file '%s'", e->key);
528                  error(SYSTEM, errmsg);
# Line 553 | Line 543 | recover_output()
543          int             ofl;
544          char            oname[1024];
545          LUENT           *oent;
546 <        STREAMOUT       sout;
546 >        STREAMOUT       *sop;
547          off_t           nvals;
548          int             xr, yr;
549  
# Line 582 | Line 572 | recover_output()
572                          error(USER, "cannot recover from stdout");
573                  if (mp->outspec[0] == '!')
574                          error(USER, "cannot recover from command");
575 <                for (j = 0; ; j++) {            /* check each bin's file */
576 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
575 >                for (j = 0; j < mp->nbins; j++) { /* check each bin's file */
576 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
577                          if (ofl < 0)
578                                  error(USER, "bad output file specification");
579                          oent = lu_find(&ofiletab, oname);
580 <                        if (oent->data != NULL) {
581 <                                sout = *(STREAMOUT *)oent->data;
582 <                        } else {
583 <                                sout.reclen = 0;
594 <                                sout.outpipe = 0;
595 <                                sout.ofp = NULL;
596 <                        }
597 <                        if (sout.ofp != NULL) { /* already open? */
580 >                        if (oent->data == NULL)
581 >                                error(INTERNAL, "unallocated stream in recover_output()");
582 >                        sop = (STREAMOUT *)oent->data;
583 >                        if (sop->ofp != NULL) { /* already open? */
584                                  if (ofl & OF_BIN)
585                                          continue;
586                                  break;
587                          }
588                                                  /* open output */
589 <                        sout.ofp = fopen(oname, "rb+");
590 <                        if (sout.ofp == NULL) {
605 <                                if (j == mp->nbins)
606 <                                        break;  /* assume end of modifier */
589 >                        sop->ofp = fopen(oname, "rb+");
590 >                        if (sop->ofp == NULL) {
591                                  sprintf(errmsg, "missing recover file '%s'",
592                                                  oname);
593                                  error(WARNING, errmsg);
594 +                                lastout = 0;
595                                  break;
596                          }
597 <                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
597 >                        nvals = lseek(fileno(sop->ofp), 0, SEEK_END);
598                          if (nvals <= 0) {
599                                  lastout = 0;    /* empty output, quit here */
600 <                                fclose(sout.ofp);
600 >                                fclose(sop->ofp);
601                                  break;
602                          }
603 <                        if (!sout.reclen) {
619 <                                if (!(ofl & OF_BIN)) {
620 <                                        sprintf(errmsg,
621 <                                                "need -bn to recover file '%s'",
622 <                                                        oname);
623 <                                        error(USER, errmsg);
624 <                                }
625 <                                recsiz = outvsiz;
626 <                        } else
627 <                                recsiz = outvsiz * sout.reclen;
603 >                        recsiz = outvsiz * sop->reclen;
604  
605 <                        lseek(fileno(sout.ofp), 0, SEEK_SET);
606 <                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
605 >                        lseek(fileno(sop->ofp), 0, SEEK_SET);
606 >                        if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
607                                  sprintf(errmsg, "format mismatch for '%s'",
608                                                  oname);
609                                  error(USER, errmsg);
610                          }
611 <                        sout.xr = xres; sout.yr = yres;
612 <                        if ((sout.xr > 0) & (sout.yr > 0) &&
613 <                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
614 <                                                (xr != sout.xr) |
639 <                                                (yr != sout.yr))) {
611 >                        if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
612 >                                        (!fscnresolu(&xr, &yr, sop->ofp) ||
613 >                                                (xr != sop->xr) |
614 >                                                (yr != sop->yr))) {
615                                  sprintf(errmsg, "resolution mismatch for '%s'",
616                                                  oname);
617                                  error(USER, errmsg);
618                          }
619 <                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
619 >                        nvals = (nvals - (off_t)ftell(sop->ofp)) / recsiz;
620                          if ((lastout < 0) | (nvals < lastout))
621                                  lastout = nvals;
647                        if (oent->key == NULL)  /* new entry */
648                                oent->key = strcpy((char *)
649                                                malloc(strlen(oname)+1), oname);
650                        if (oent->data == NULL)
651                                oent->data = (char *)malloc(sizeof(STREAMOUT));
652                        *(STREAMOUT *)oent->data = sout;
622                          if (!(ofl & OF_BIN))
623                                  break;          /* no bin separation */
624                  }
625                  if (!lastout) {                 /* empty output */
626                          error(WARNING, "no previous data to recover");
627 <                        lu_done(&ofiletab);     /* reclose all outputs */
627 >                                                /* reclose all outputs */
628 >                        lu_doall(&ofiletab, &myclose, NULL);
629                          return;
660                }
661                if (j > mp->nbins) {            /* check modifier size */
662                        sprintf(errmsg,
663                                "mismatched -bn setting for recovering '%s'",
664                                        modname[i]);
665                        error(USER, errmsg);
630                  }
631          }
632          if (lastout < 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines