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.19 by greg, Mon Sep 12 20:31:34 2016 UTC vs.
Revision 2.26 by greg, Wed Nov 15 18:02:53 2023 UTC

# Line 113 | Line 113 | printheader(FILE *fout, const char *info)
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 */
116 >        fputncomp(NCSAMP, fout);
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 141 | 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 162 | 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;
165 #ifdef getc_unlocked
166                        flockfile(stdout);      /* avoid lock/unlock overhead */
167 #endif
171                          using_stdout = 1;
172                  }
173                  stdos.ofp = stdout;
# Line 268 | Line 271 | getvec(FVECT vec)
271                  }
272                  break;
273          case 'f':                                       /* binary float */
274 <                if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3)
274 >                if (getbinary(vf, sizeof(float), 3, stdin) != 3)
275                          return(-1);
276                  VCOPY(vec, vf);
277                  break;
278          case 'd':                                       /* binary double */
279 <                if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3)
279 >                if (getbinary(vd, sizeof(double), 3, stdin) != 3)
280                          return(-1);
281                  VCOPY(vec, vd);
282                  break;
# Line 286 | Line 289 | getvec(FVECT vec)
289  
290   /* Put out ray contribution to file */
291   static void
292 < put_contrib(const DCOLOR cnt, FILE *fout)
292 > put_contrib(const DCOLORV *cnt, FILE *fout)
293   {
294          double  sf = 1;
295 <        COLOR   fv;
296 <        COLR    cv;
295 >        SCOLOR  fv;
296 >        SCOLR   cv;
297 >        int     i;
298  
299          if (accumulate > 1)
300                  sf = 1./(double)accumulate;
301          switch (outfmt) {
302          case 'a':
303 <                if (accumulate > 1)
304 <                        fprintf(fout, "%.6e\t%.6e\t%.6e\t",
301 <                                        sf*cnt[0], sf*cnt[1], sf*cnt[2]);
302 <                else
303 <                        fprintf(fout, "%.6e\t%.6e\t%.6e\t",
304 <                                        cnt[0], cnt[1], cnt[2]);
303 >                for (i = 0; i < NCSAMP; i++)
304 >                        fprintf(fout, "%.6e\t", sf*cnt[i]);
305                  break;
306          case 'f':
307 <                if (accumulate > 1) {
308 <                        copycolor(fv, cnt);
309 <                        scalecolor(fv, sf);
310 <                } else
311 <                        copycolor(fv, cnt);
312 <                putbinary(fv, sizeof(float), 3, fout);
307 >                for (i = NCSAMP; i-- > 0; )
308 >                        fv[i] = cnt[i];
309 >                if (accumulate > 1)
310 >                        scalescolor(fv, sf);
311 >                putbinary(fv, sizeof(COLORV), NCSAMP, fout);
312                  break;
313          case 'd':
314                  if (accumulate > 1) {
315 <                        DCOLOR  dv;
316 <                        copycolor(dv, cnt);
317 <                        scalecolor(dv, sf);
318 <                        putbinary(dv, sizeof(double), 3, fout);
315 >                        DCOLORV dv[MAXCSAMP];
316 >                        for (i = NCSAMP; i-- > 0; )
317 >                                dv[i] = sf*cnt[i];
318 >                        putbinary(dv, sizeof(DCOLORV), NCSAMP, fout);
319                  } else
320 <                        putbinary(cnt, sizeof(double), 3, fout);
320 >                        putbinary(cnt, sizeof(DCOLORV), NCSAMP, fout);
321                  break;
322          case 'c':
323 +                for (i = NCSAMP; i-- > 0; )
324 +                        fv[i] = cnt[i];
325                  if (accumulate > 1)
326 <                        setcolr(cv, sf*cnt[0], sf*cnt[1], sf*cnt[2]);
327 <                else
328 <                        setcolr(cv, cnt[0], cnt[1], cnt[2]);
328 <                putbinary(cv, sizeof(cv), 1, fout);
326 >                        scalescolor(fv, sf);
327 >                scolor_scolr(cv, fv);
328 >                putbinary(cv, 1, LSCOLR, fout);
329                  break;
330          default:
331                  error(INTERNAL, "botched output format");
# Line 340 | Line 340 | mod_output(MODCONT *mp)
340          STREAMOUT       *sop = getostream(mp->outspec, mp->modname, mp->bin0, 0);
341          int             j;
342  
343 <        put_contrib(mp->cbin[0], sop->ofp);
343 >        put_contrib(mp->cbin, sop->ofp);
344          if (mp->nbins > 3 &&    /* minor optimization */
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);
347 >                        put_contrib(mcbin(mp,j), sop->ofp);
348          } else {
349                  for (j = 1; j < mp->nbins; j++) {
350                          sop = getostream(mp->outspec, mp->modname, mp->bin0+j, 0);
351 <                        put_contrib(mp->cbin[j], sop->ofp);
351 >                        put_contrib(mcbin(mp,j), sop->ofp);
352                  }
353          }
354   }
# Line 391 | Line 391 | end_record()
391  
392   /* Get ray contribution from previous file */
393   static int
394 < get_contrib(DCOLOR cnt, FILE *finp)
394 > get_contrib(DCOLORV *cnt, FILE *finp)
395   {
396 <        COLOR   fv;
397 <        COLR    cv;
396 >        SCOLOR  fv;
397 >        SCOLR   cv;
398 >        int     i;
399  
400          switch (outfmt) {
401          case 'a':
402 <                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
403 <        case 'f':
404 <                if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3)
404 <                        return(0);
405 <                copycolor(cnt, fv);
402 >                for (i = 0; i < NCSAMP; i++)
403 >                        if (fscanf(finp, "%lf", &cnt[i]) != 1)
404 >                                return(0);
405                  return(1);
406          case 'd':
407 <                return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3);
407 >                return(getbinary(cnt, sizeof(DCOLORV), NCSAMP, finp) == NCSAMP);
408 >        case 'f':
409 >                if (getbinary(fv, sizeof(COLORV), NCSAMP, finp) != NCSAMP)
410 >                        return(0);
411 >                break;
412          case 'c':
413 <                if (getbinary(cv, sizeof(cv), 1, finp) != 1)
413 >                if (getbinary(cv, 1, LSCOLR, finp) != LSCOLR)
414                          return(0);
415 <                colr_color(fv, cv);
416 <                copycolor(cnt, fv);
414 <                return(1);
415 >                scolr_scolor(fv, cv);
416 >                break;
417          default:
418                  error(INTERNAL, "botched output format");
419          }
420 <        return(0);      /* pro forma return */
420 >                        /* copy result from SCOLOR */
421 >        for (i = NCSAMP; i-- > 0; )
422 >                cnt[i] = fv[i];
423 >        return(1);
424   }
425  
426  
# Line 445 | Line 450 | reload_output()
450          char            *outvfmt;
451          LUENT           *oent;
452          int             xr, yr;
453 <        STREAMOUT       sout;
454 <        DCOLOR          rgbv;
453 >        STREAMOUT       *sop;
454 >        DCOLORV         contr[MAXCSAMP];
455  
456          if (outfmt == 'a')
457                  fmode = "r";
458 <        outvfmt = formstr(outfmt);
458 >        outvfmt = (char *)formstr(outfmt);
459                                                  /* reload modifier values */
460          for (i = 0; i < nmods; i++) {
461                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
# Line 458 | Line 463 | reload_output()
463                          error(USER, "cannot reload from stdout");
464                  if (mp->outspec[0] == '!')
465                          error(USER, "cannot reload from command");
466 <                for (j = 0; ; j++) {            /* load each modifier bin */
467 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
466 >                for (j = 0; j < mp->nbins; j++) { /* load each modifier bin */
467 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
468                          if (ofl < 0)
469                                  error(USER, "bad output file specification");
470                          oent = lu_find(&ofiletab, oname);
471 <                        if (oent->data != NULL) {
472 <                                sout = *(STREAMOUT *)oent->data;
473 <                        } else {
474 <                                sout.reclen = 0;
475 <                                sout.outpipe = 0;
476 <                                sout.xr = xres; sout.yr = yres;
472 <                                sout.ofp = NULL;
473 <                        }
474 <                        if (sout.ofp == NULL) { /* open output as input */
475 <                                sout.ofp = fopen(oname, fmode);
476 <                                if (sout.ofp == NULL) {
477 <                                        if (j == mp->nbins)
478 <                                                break;  /* assume end of modifier */
471 >                        if (oent->data == NULL)
472 >                                error(INTERNAL, "unallocated stream in reload_output()");
473 >                        sop = (STREAMOUT *)oent->data;
474 >                        if (sop->ofp == NULL) { /* open output as input */
475 >                                sop->ofp = fopen(oname, fmode);
476 >                                if (sop->ofp == NULL) {
477                                          sprintf(errmsg, "missing reload file '%s'",
478                                                          oname);
479                                          error(WARNING, errmsg);
480                                          break;
481                                  }
482   #ifdef getc_unlocked
483 <                                flockfile(sout.ofp);
483 >                                flockfile(sop->ofp);
484   #endif
485 <                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
485 >                                if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
486                                          sprintf(errmsg, "format mismatch for '%s'",
487                                                          oname);
488                                          error(USER, errmsg);
489                                  }
490 <                                if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) &&
491 <                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
492 <                                                        (xr != sout.xr) |
493 <                                                        (yr != sout.yr))) {
490 >                                if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
491 >                                                (!fscnresolu(&xr, &yr, sop->ofp) ||
492 >                                                        (xr != sop->xr) |
493 >                                                        (yr != sop->yr))) {
494                                          sprintf(errmsg, "resolution mismatch for '%s'",
495                                                          oname);
496                                          error(USER, errmsg);
497                                  }
498                          }
499 <                                                        /* read in RGB value */
500 <                        if (!get_contrib(rgbv, sout.ofp)) {
499 >                                                        /* read in spectral value */
500 >                        if (!get_contrib(contr, sop->ofp)) {
501                                  if (!j) {
502 <                                        fclose(sout.ofp);
502 >                                        fclose(sop->ofp);
503                                          break;          /* ignore empty file */
504                                  }
505                                  if (j < mp->nbins) {
# Line 511 | Line 509 | reload_output()
509                                  }
510                                  break;
511                          }
512 <                        if (j >= mp->nbins) {           /* check modifier size */
515 <                                sprintf(errmsg,
516 <                                "mismatched -bn setting for reloading '%s'",
517 <                                                modname[i]);
518 <                                error(USER, errmsg);
519 <                        }
520 <                                
521 <                        copycolor(mp->cbin[j], rgbv);
522 <                        if (oent->key == NULL)          /* new file entry */
523 <                                oent->key = strcpy((char *)
524 <                                                malloc(strlen(oname)+1), oname);
525 <                        if (oent->data == NULL)
526 <                                oent->data = (char *)malloc(sizeof(STREAMOUT));
527 <                        *(STREAMOUT *)oent->data = sout;
512 >                        memcpy(mcbin(mp,j), contr, DCOLORSIZ);
513                  }
514          }
515          lu_doall(&ofiletab, &myclose, NULL);    /* close all files */
# Line 539 | Line 524 | myseeko(const LUENT *e, void *p)
524          off_t           nbytes = *(off_t *)p;
525          
526          if (sop->reclen > 1)
527 <                nbytes = nbytes * sop->reclen;
527 >                nbytes *= (off_t)sop->reclen;
528          if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
529                  sprintf(errmsg, "seek error on file '%s'", e->key);
530                  error(SYSTEM, errmsg);
# Line 560 | Line 545 | recover_output()
545          int             ofl;
546          char            oname[1024];
547          LUENT           *oent;
548 <        STREAMOUT       sout;
548 >        STREAMOUT       *sop;
549          off_t           nvals;
550          int             xr, yr;
551  
# Line 569 | Line 554 | recover_output()
554                  error(USER, "cannot recover ASCII output");
555                  return;
556          case 'f':
557 <                outvsiz = sizeof(float)*3;
557 >                outvsiz = sizeof(float)*NCSAMP;
558                  break;
559          case 'd':
560 <                outvsiz = sizeof(double)*3;
560 >                outvsiz = sizeof(double)*NCSAMP;
561                  break;
562          case 'c':
563 <                outvsiz = sizeof(COLR);
563 >                outvsiz = LSCOLR;
564                  break;
565          default:
566                  error(INTERNAL, "botched output format");
567                  return;
568          }
569 <        outvfmt = formstr(outfmt);
569 >        outvfmt = (char *)formstr(outfmt);
570                                                  /* check modifier outputs */
571          for (i = 0; i < nmods; i++) {
572                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
# Line 589 | Line 574 | recover_output()
574                          error(USER, "cannot recover from stdout");
575                  if (mp->outspec[0] == '!')
576                          error(USER, "cannot recover from command");
577 <                for (j = 0; ; j++) {            /* check each bin's file */
578 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
577 >                for (j = 0; j < mp->nbins; j++) { /* check each bin's file */
578 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
579                          if (ofl < 0)
580                                  error(USER, "bad output file specification");
581                          oent = lu_find(&ofiletab, oname);
582 <                        if (oent->data != NULL) {
583 <                                sout = *(STREAMOUT *)oent->data;
584 <                        } else {
585 <                                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? */
582 >                        if (oent->data == NULL)
583 >                                error(INTERNAL, "unallocated stream in recover_output()");
584 >                        sop = (STREAMOUT *)oent->data;
585 >                        if (sop->ofp != NULL) { /* already open? */
586                                  if (ofl & OF_BIN)
587                                          continue;
588                                  break;
589                          }
590                                                  /* open output */
591 <                        sout.ofp = fopen(oname, "rb+");
592 <                        if (sout.ofp == NULL) {
614 <                                if (j == mp->nbins)
615 <                                        break;  /* assume end of modifier */
591 >                        sop->ofp = fopen(oname, "rb+");
592 >                        if (sop->ofp == NULL) {
593                                  sprintf(errmsg, "missing recover file '%s'",
594                                                  oname);
595                                  error(WARNING, errmsg);
596 +                                lastout = 0;
597                                  break;
598                          }
599 <                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
599 >                        nvals = lseek(fileno(sop->ofp), 0, SEEK_END);
600                          if (nvals <= 0) {
601                                  lastout = 0;    /* empty output, quit here */
602 <                                fclose(sout.ofp);
602 >                                fclose(sop->ofp);
603                                  break;
604                          }
605 <                        if (!sout.reclen) {
628 <                                if (!(ofl & OF_BIN)) {
629 <                                        sprintf(errmsg,
630 <                                                "need -bn to recover file '%s'",
631 <                                                        oname);
632 <                                        error(USER, errmsg);
633 <                                }
634 <                                recsiz = outvsiz;
635 <                        } else
636 <                                recsiz = outvsiz * sout.reclen;
605 >                        recsiz = outvsiz * sop->reclen;
606  
607 <                        lseek(fileno(sout.ofp), 0, SEEK_SET);
608 <                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
607 >                        lseek(fileno(sop->ofp), 0, SEEK_SET);
608 >                        if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
609                                  sprintf(errmsg, "format mismatch for '%s'",
610                                                  oname);
611                                  error(USER, errmsg);
612                          }
613 <                        if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) &&
614 <                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
615 <                                                (xr != sout.xr) |
616 <                                                (yr != sout.yr))) {
613 >                        if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
614 >                                        (!fscnresolu(&xr, &yr, sop->ofp) ||
615 >                                                (xr != sop->xr) |
616 >                                                (yr != sop->yr))) {
617                                  sprintf(errmsg, "resolution mismatch for '%s'",
618                                                  oname);
619                                  error(USER, errmsg);
620                          }
621 <                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
621 >                        nvals = (nvals - (off_t)ftell(sop->ofp)) / recsiz;
622                          if ((lastout < 0) | (nvals < lastout))
623                                  lastout = nvals;
655                        if (oent->key == NULL)  /* new entry */
656                                oent->key = strcpy((char *)
657                                                malloc(strlen(oname)+1), oname);
658                        if (oent->data == NULL)
659                                oent->data = (char *)malloc(sizeof(STREAMOUT));
660                        *(STREAMOUT *)oent->data = sout;
624                          if (!(ofl & OF_BIN))
625                                  break;          /* no bin separation */
626                  }
627                  if (!lastout) {                 /* empty output */
628                          error(WARNING, "no previous data to recover");
629 <                        lu_done(&ofiletab);     /* reclose all outputs */
629 >                                                /* reclose all outputs */
630 >                        lu_doall(&ofiletab, &myclose, NULL);
631                          return;
668                }
669                if (j > mp->nbins) {            /* check modifier size */
670                        sprintf(errmsg,
671                                "mismatched -bn setting for recovering '%s'",
672                                        modname[i]);
673                        error(USER, errmsg);
632                  }
633          }
634          if (lastout < 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines