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.27 by greg, Wed May 22 21:44:03 2024 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 (NCSAMP > 3)
118 >                fputwlsplit(WLPART, fout);
119          if (info != NULL)                       /* add extra info if given */
120                  fputs(info, fout);
121 +        if ((outfmt == 'f') | (outfmt == 'd'))
122 +                fputendian(fout);
123          fputformat(formstr(outfmt), fout);
124          fputc('\n', fout);                      /* empty line ends header */
125   }
# Line 141 | Line 145 | getostream(const char *ospec, const char *mname, int b
145          LUENT                   *lep;
146          STREAMOUT               *sop;
147          char                    *cp;
148 <        
148 >
149 >        info[0] = '\0';
150          if (ospec == NULL) {                    /* use stdout? */
151                  if (!noopen & !using_stdout) {
152                          if (outfmt != 'a')
153                                  SET_FILE_BINARY(stdout);
154 + #ifdef getc_unlocked
155 +                        flockfile(stdout);      /* avoid lock/unlock overhead */
156 + #endif
157                          if (header) {
158                                  cp = info;
159                                  if (yres > 0) {
# Line 162 | Line 170 | getostream(const char *ospec, const char *mname, int b
170                          if (waitflush > 0)
171                                  fflush(stdout);
172                          stdos.xr = xres; stdos.yr = yres;
165 #ifdef getc_unlocked
166                        flockfile(stdout);      /* avoid lock/unlock overhead */
167 #endif
173                          using_stdout = 1;
174                  }
175                  stdos.ofp = stdout;
# Line 268 | Line 273 | getvec(FVECT vec)
273                  }
274                  break;
275          case 'f':                                       /* binary float */
276 <                if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3)
276 >                if (getbinary(vf, sizeof(float), 3, stdin) != 3)
277                          return(-1);
278                  VCOPY(vec, vf);
279                  break;
280          case 'd':                                       /* binary double */
281 <                if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3)
281 >                if (getbinary(vd, sizeof(double), 3, stdin) != 3)
282                          return(-1);
283                  VCOPY(vec, vd);
284                  break;
# Line 286 | Line 291 | getvec(FVECT vec)
291  
292   /* Put out ray contribution to file */
293   static void
294 < put_contrib(const DCOLOR cnt, FILE *fout)
294 > put_contrib(const DCOLORV *cnt, FILE *fout)
295   {
296          double  sf = 1;
297 <        COLOR   fv;
298 <        COLR    cv;
297 >        SCOLOR  fv;
298 >        SCOLR   cv;
299 >        int     i;
300  
301          if (accumulate > 1)
302                  sf = 1./(double)accumulate;
303          switch (outfmt) {
304          case 'a':
305 <                if (accumulate > 1)
306 <                        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]);
305 >                for (i = 0; i < NCSAMP; i++)
306 >                        fprintf(fout, "%.6e\t", sf*cnt[i]);
307                  break;
308          case 'f':
309 <                if (accumulate > 1) {
310 <                        copycolor(fv, cnt);
311 <                        scalecolor(fv, sf);
312 <                } else
313 <                        copycolor(fv, cnt);
312 <                putbinary(fv, sizeof(float), 3, fout);
309 >                for (i = NCSAMP; i-- > 0; )
310 >                        fv[i] = cnt[i];
311 >                if (accumulate > 1)
312 >                        scalescolor(fv, sf);
313 >                putbinary(fv, sizeof(COLORV), NCSAMP, fout);
314                  break;
315          case 'd':
316                  if (accumulate > 1) {
317 <                        DCOLOR  dv;
318 <                        copycolor(dv, cnt);
319 <                        scalecolor(dv, sf);
320 <                        putbinary(dv, sizeof(double), 3, fout);
317 >                        DCOLORV dv[MAXCSAMP];
318 >                        for (i = NCSAMP; i-- > 0; )
319 >                                dv[i] = sf*cnt[i];
320 >                        putbinary(dv, sizeof(DCOLORV), NCSAMP, fout);
321                  } else
322 <                        putbinary(cnt, sizeof(double), 3, fout);
322 >                        putbinary(cnt, sizeof(DCOLORV), NCSAMP, fout);
323                  break;
324          case 'c':
325 +                for (i = NCSAMP; i-- > 0; )
326 +                        fv[i] = cnt[i];
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]);
328 <                putbinary(cv, sizeof(cv), 1, fout);
328 >                        scalescolor(fv, sf);
329 >                scolor_scolr(cv, fv);
330 >                putbinary(cv, 1, LSCOLR, fout);
331                  break;
332          default:
333                  error(INTERNAL, "botched output format");
# Line 340 | Line 342 | mod_output(MODCONT *mp)
342          STREAMOUT       *sop = getostream(mp->outspec, mp->modname, mp->bin0, 0);
343          int             j;
344  
345 <        put_contrib(mp->cbin[0], sop->ofp);
345 >        put_contrib(mp->cbin, sop->ofp);
346          if (mp->nbins > 3 &&    /* minor optimization */
347                          sop == getostream(mp->outspec, mp->modname, mp->bin0+1, 0)) {
348                  for (j = 1; j < mp->nbins; j++)
349 <                        put_contrib(mp->cbin[j], sop->ofp);
349 >                        put_contrib(mcbin(mp,j), sop->ofp);
350          } else {
351                  for (j = 1; j < mp->nbins; j++) {
352                          sop = getostream(mp->outspec, mp->modname, mp->bin0+j, 0);
353 <                        put_contrib(mp->cbin[j], sop->ofp);
353 >                        put_contrib(mcbin(mp,j), sop->ofp);
354                  }
355          }
356   }
# Line 391 | Line 393 | end_record()
393  
394   /* Get ray contribution from previous file */
395   static int
396 < get_contrib(DCOLOR cnt, FILE *finp)
396 > get_contrib(DCOLORV *cnt, FILE *finp)
397   {
398 <        COLOR   fv;
399 <        COLR    cv;
398 >        SCOLOR  fv;
399 >        SCOLR   cv;
400 >        int     i;
401  
402          switch (outfmt) {
403          case 'a':
404 <                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
405 <        case 'f':
406 <                if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3)
404 <                        return(0);
405 <                copycolor(cnt, fv);
404 >                for (i = 0; i < NCSAMP; i++)
405 >                        if (fscanf(finp, "%lf", &cnt[i]) != 1)
406 >                                return(0);
407                  return(1);
408          case 'd':
409 <                return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3);
409 >                return(getbinary(cnt, sizeof(DCOLORV), NCSAMP, finp) == NCSAMP);
410 >        case 'f':
411 >                if (getbinary(fv, sizeof(COLORV), NCSAMP, finp) != NCSAMP)
412 >                        return(0);
413 >                break;
414          case 'c':
415 <                if (getbinary(cv, sizeof(cv), 1, finp) != 1)
415 >                if (getbinary(cv, 1, LSCOLR, finp) != LSCOLR)
416                          return(0);
417 <                colr_color(fv, cv);
418 <                copycolor(cnt, fv);
414 <                return(1);
417 >                scolr_scolor(fv, cv);
418 >                break;
419          default:
420                  error(INTERNAL, "botched output format");
421          }
422 <        return(0);      /* pro forma return */
422 >                        /* copy result from SCOLOR */
423 >        for (i = NCSAMP; i-- > 0; )
424 >                cnt[i] = fv[i];
425 >        return(1);
426   }
427  
428  
# Line 445 | Line 452 | reload_output()
452          char            *outvfmt;
453          LUENT           *oent;
454          int             xr, yr;
455 <        STREAMOUT       sout;
456 <        DCOLOR          rgbv;
455 >        STREAMOUT       *sop;
456 >        DCOLORV         contr[MAXCSAMP];
457  
458          if (outfmt == 'a')
459                  fmode = "r";
460 <        outvfmt = formstr(outfmt);
460 >        outvfmt = (char *)formstr(outfmt);
461                                                  /* reload modifier values */
462          for (i = 0; i < nmods; i++) {
463                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
# Line 458 | Line 465 | reload_output()
465                          error(USER, "cannot reload from stdout");
466                  if (mp->outspec[0] == '!')
467                          error(USER, "cannot reload from command");
468 <                for (j = 0; ; j++) {            /* load each modifier bin */
469 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
468 >                for (j = 0; j < mp->nbins; j++) { /* load each modifier bin */
469 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
470                          if (ofl < 0)
471                                  error(USER, "bad output file specification");
472                          oent = lu_find(&ofiletab, oname);
473 <                        if (oent->data != NULL) {
474 <                                sout = *(STREAMOUT *)oent->data;
475 <                        } else {
476 <                                sout.reclen = 0;
477 <                                sout.outpipe = 0;
478 <                                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 */
473 >                        if (oent->data == NULL)
474 >                                error(INTERNAL, "unallocated stream in reload_output()");
475 >                        sop = (STREAMOUT *)oent->data;
476 >                        if (sop->ofp == NULL) { /* open output as input */
477 >                                sop->ofp = fopen(oname, fmode);
478 >                                if (sop->ofp == NULL) {
479                                          sprintf(errmsg, "missing reload file '%s'",
480                                                          oname);
481                                          error(WARNING, errmsg);
482                                          break;
483                                  }
484   #ifdef getc_unlocked
485 <                                flockfile(sout.ofp);
485 >                                flockfile(sop->ofp);
486   #endif
487 <                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
487 >                                if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
488                                          sprintf(errmsg, "format mismatch for '%s'",
489                                                          oname);
490                                          error(USER, errmsg);
491                                  }
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))) {
492 >                                if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
493 >                                                (!fscnresolu(&xr, &yr, sop->ofp) ||
494 >                                                        (xr != sop->xr) |
495 >                                                        (yr != sop->yr))) {
496                                          sprintf(errmsg, "resolution mismatch for '%s'",
497                                                          oname);
498                                          error(USER, errmsg);
499                                  }
500                          }
501 <                                                        /* read in RGB value */
502 <                        if (!get_contrib(rgbv, sout.ofp)) {
501 >                                                        /* read in spectral value */
502 >                        if (!get_contrib(contr, sop->ofp)) {
503                                  if (!j) {
504 <                                        fclose(sout.ofp);
504 >                                        fclose(sop->ofp);
505                                          break;          /* ignore empty file */
506                                  }
507                                  if (j < mp->nbins) {
# Line 511 | Line 511 | reload_output()
511                                  }
512                                  break;
513                          }
514 <                        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;
514 >                        memcpy(mcbin(mp,j), contr, DCOLORSIZ);
515                  }
516          }
517          lu_doall(&ofiletab, &myclose, NULL);    /* close all files */
# Line 539 | Line 526 | myseeko(const LUENT *e, void *p)
526          off_t           nbytes = *(off_t *)p;
527          
528          if (sop->reclen > 1)
529 <                nbytes = nbytes * sop->reclen;
529 >                nbytes *= (off_t)sop->reclen;
530          if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
531                  sprintf(errmsg, "seek error on file '%s'", e->key);
532                  error(SYSTEM, errmsg);
# Line 560 | Line 547 | recover_output()
547          int             ofl;
548          char            oname[1024];
549          LUENT           *oent;
550 <        STREAMOUT       sout;
550 >        STREAMOUT       *sop;
551          off_t           nvals;
552          int             xr, yr;
553  
# Line 569 | Line 556 | recover_output()
556                  error(USER, "cannot recover ASCII output");
557                  return;
558          case 'f':
559 <                outvsiz = sizeof(float)*3;
559 >                outvsiz = sizeof(float)*NCSAMP;
560                  break;
561          case 'd':
562 <                outvsiz = sizeof(double)*3;
562 >                outvsiz = sizeof(double)*NCSAMP;
563                  break;
564          case 'c':
565 <                outvsiz = sizeof(COLR);
565 >                outvsiz = LSCOLR;
566                  break;
567          default:
568                  error(INTERNAL, "botched output format");
569                  return;
570          }
571 <        outvfmt = formstr(outfmt);
571 >        outvfmt = (char *)formstr(outfmt);
572                                                  /* check modifier outputs */
573          for (i = 0; i < nmods; i++) {
574                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
# Line 589 | Line 576 | recover_output()
576                          error(USER, "cannot recover from stdout");
577                  if (mp->outspec[0] == '!')
578                          error(USER, "cannot recover from command");
579 <                for (j = 0; ; j++) {            /* check each bin's file */
580 <                        ofl = ofname(oname, mp->outspec, mp->modname, j);
579 >                for (j = 0; j < mp->nbins; j++) { /* check each bin's file */
580 >                        ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j);
581                          if (ofl < 0)
582                                  error(USER, "bad output file specification");
583                          oent = lu_find(&ofiletab, oname);
584 <                        if (oent->data != NULL) {
585 <                                sout = *(STREAMOUT *)oent->data;
586 <                        } else {
587 <                                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? */
584 >                        if (oent->data == NULL)
585 >                                error(INTERNAL, "unallocated stream in recover_output()");
586 >                        sop = (STREAMOUT *)oent->data;
587 >                        if (sop->ofp != NULL) { /* already open? */
588                                  if (ofl & OF_BIN)
589                                          continue;
590                                  break;
591                          }
592                                                  /* open output */
593 <                        sout.ofp = fopen(oname, "rb+");
594 <                        if (sout.ofp == NULL) {
614 <                                if (j == mp->nbins)
615 <                                        break;  /* assume end of modifier */
593 >                        sop->ofp = fopen(oname, "rb+");
594 >                        if (sop->ofp == NULL) {
595                                  sprintf(errmsg, "missing recover file '%s'",
596                                                  oname);
597                                  error(WARNING, errmsg);
598 +                                lastout = 0;
599                                  break;
600                          }
601 <                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
601 >                        nvals = lseek(fileno(sop->ofp), 0, SEEK_END);
602                          if (nvals <= 0) {
603                                  lastout = 0;    /* empty output, quit here */
604 <                                fclose(sout.ofp);
604 >                                fclose(sop->ofp);
605                                  break;
606                          }
607 <                        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;
607 >                        recsiz = outvsiz * sop->reclen;
608  
609 <                        lseek(fileno(sout.ofp), 0, SEEK_SET);
610 <                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
609 >                        lseek(fileno(sop->ofp), 0, SEEK_SET);
610 >                        if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) {
611                                  sprintf(errmsg, "format mismatch for '%s'",
612                                                  oname);
613                                  error(USER, errmsg);
614                          }
615 <                        if ((sout.reclen == 1) & (sout.xr > 0) & (sout.yr > 0) &&
616 <                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
617 <                                                (xr != sout.xr) |
618 <                                                (yr != sout.yr))) {
615 >                        if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) &&
616 >                                        (!fscnresolu(&xr, &yr, sop->ofp) ||
617 >                                                (xr != sop->xr) |
618 >                                                (yr != sop->yr))) {
619                                  sprintf(errmsg, "resolution mismatch for '%s'",
620                                                  oname);
621                                  error(USER, errmsg);
622                          }
623 <                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
623 >                        nvals = (nvals - (off_t)ftell(sop->ofp)) / recsiz;
624                          if ((lastout < 0) | (nvals < lastout))
625                                  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;
626                          if (!(ofl & OF_BIN))
627                                  break;          /* no bin separation */
628                  }
629                  if (!lastout) {                 /* empty output */
630                          error(WARNING, "no previous data to recover");
631 <                        lu_done(&ofiletab);     /* reclose all outputs */
631 >                                                /* reclose all outputs */
632 >                        lu_doall(&ofiletab, &myclose, NULL);
633                          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);
634                  }
635          }
636          if (lastout < 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines