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

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.67 by greg, Fri Dec 1 02:05:00 2023 UTC vs.
Revision 2.77 by greg, Thu Dec 21 23:58:05 2023 UTC

# Line 31 | Line 31 | rmx_new(int nr, int nc, int n)
31                  return(NULL);
32  
33          dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
34 <        if (dnew) {
35 <                dnew->dtype = DTdouble;
36 <                dnew->nrows = nr;
37 <                dnew->ncols = nc;
38 <                dnew->ncomp = n;
39 <                setcolor(dnew->cexp, 1.f, 1.f, 1.f);
40 <                memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
41 <        }
34 >        if (!dnew)
35 >                return(NULL);
36 >
37 >        dnew->dtype = DTdouble;
38 >        dnew->nrows = nr;
39 >        dnew->ncols = nc;
40 >        dnew->ncomp = n;
41 >        setcolor(dnew->cexp, 1.f, 1.f, 1.f);
42 >        memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
43 >
44          return(dnew);
45   }
46  
# Line 292 | Line 294 | rmx_load_header(RMATRIX *rm, FILE *fp)
294                  fputs("Unrecognized matrix format\n", stderr);
295                  return(0);
296          }
297 <                                                /* resolution string? */
298 <        if ((rm->nrows <= 0) | (rm->ncols <= 0)) {
299 <                if (!fscnresolu(&rm->ncols, &rm->nrows, fp))
300 <                        return(0);
301 <                if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
302 <                                rm->ncomp != 3)
303 <                        return(0);
302 <        }
303 <        if (rm->dtype == DTascii)               /* set input type (WINDOWS) */
297 >        if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
298 >                        rm->ncomp != 3)
299 >                return(0);
300 >        if (rm->ncols <= 0 &&                   /* resolution string? */
301 >                        !fscnresolu(&rm->ncols, &rm->nrows, fp))
302 >                return(0);
303 >        if (rm->dtype == DTascii)               /* set file type (WINDOWS) */
304                  SET_FILE_TEXT(fp);
305          else
306                  SET_FILE_BINARY(fp);
# Line 374 | Line 374 | rmx_load(const char *inspec, RMPref rmp)
374                  fp = stdin;
375          else if (inspec[0] == '!')
376                  fp = popen(inspec+1, "r");
377 <        else if (rmp != RMPnone) {
377 >        else {
378                  const char      *sp = inspec;   /* check suffix */
379                  while (*sp)
380                          ++sp;
381                  while (sp > inspec && sp[-1] != '.')
382                          --sp;
383                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
384 <                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
384 >                        CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL :
385 >                                        rmp==RMPtrans ? cm_loadBTDF(inspec) :
386                                          cm_loadBRDF(inspec, rmp==RMPreflB) ;
387                          if (!cm)
388                                  return(NULL);
# Line 392 | Line 393 | rmx_load(const char *inspec, RMPref rmp)
393                  }                               /* else open it ourselves */
394                  fp = fopen(inspec, "r");
395          }
396 <        if (!fp)
396 >        if (!fp) {
397 >                fprintf(stderr, "Cannot open for reading: %s\n", inspec);
398                  return(NULL);
399 +        }
400   #ifdef getc_unlocked
401          flockfile(fp);
402   #endif
# Line 449 | Line 452 | rmx_write_ascii(const double *dp, int nc, int len, FIL
452   {
453          while (len-- > 0) {
454                  int     k = nc;
455 <                while (nc-- > 0)
455 >                while (k-- > 0)
456                          fprintf(fp, " %.7e", *dp++);
457                  fputc('\t', fp);
458          }
# Line 527 | Line 530 | findCIEprims(const char *info)
530   int
531   rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
532   {
533 <        if (!rm | !fp || !rm->mtx)
533 >        if (!rm | !fp || rm->ncols <= 0)
534                  return(0);
535          if (rm->info)
536                  fputs(rm->info, fp);
# Line 538 | Line 541 | rmx_write_header(const RMATRIX *rm, int dtype, FILE *f
541                  dtype = DTxyze;
542          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
543                  dtype = DTrgbe;
544 +        if ((dtype < DTspec) & (rm->ncomp > 3))
545 +                dtype = DTspec;
546 +        else if ((dtype == DTspec) & (rm->ncomp <= 3))
547 +                return(0);
548  
549 <        if (dtype == DTascii)                   /* set output type (WINDOWS) */
549 >        if (dtype == DTascii)                   /* set file type (WINDOWS) */
550                  SET_FILE_TEXT(fp);
551          else
552                  SET_FILE_BINARY(fp);
# Line 549 | Line 556 | rmx_write_header(const RMATRIX *rm, int dtype, FILE *f
556                  fputcolcor(rm->cexp, fp);
557          else if (rm->cexp[GRN] != 1.f)
558                  fputexpos(rm->cexp[GRN], fp);
559 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
560 <                if (dtype != DTspec) {
559 >                                                /* matrix size? */
560 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
561 >                if (rm->nrows > 0)
562                          fprintf(fp, "NROWS=%d\n", rm->nrows);
563 <                        fprintf(fp, "NCOLS=%d\n", rm->ncols);
564 <                } else if (rm->ncomp < 3)
565 <                        return(0);              /* bad # components */
563 >                fprintf(fp, "NCOLS=%d\n", rm->ncols);
564 >        }
565 >        if (dtype >= DTspec) {                  /* # components & split? */
566                  fputncomp(rm->ncomp, fp);
567 <                if (dtype == DTspec || (rm->ncomp > 3 &&
568 <                                memcmp(rm->wlpart, WLPART, sizeof(WLPART))))
567 >                if (rm->ncomp > 3 &&
568 >                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
569                          fputwlsplit(rm->wlpart, fp);
570          } else if ((rm->ncomp != 3) & (rm->ncomp != 1))
571                  return(0);                      /* wrong # components */
# Line 565 | Line 573 | rmx_write_header(const RMATRIX *rm, int dtype, FILE *f
573                  fputendian(fp);                 /* important to record */
574          fputformat(cm_fmt_id[dtype], fp);
575          fputc('\n', fp);                        /* end of header */
576 <        if (dtype <= DTspec)
576 >        if ((dtype <= DTspec) & (rm->nrows > 0))
577                  fprtresolu(rm->ncols, rm->nrows, fp);
578          return(dtype);
579   }
# Line 639 | Line 647 | rmx_identity(const int dim, const int n)
647          return(rid);
648   }
649  
650 < /* Duplicate the given matrix */
650 > /* Duplicate the given matrix (may be unallocated) */
651   RMATRIX *
652   rmx_copy(const RMATRIX *rm)
653   {
654          RMATRIX *dnew;
655  
656 <        if (!rm || !rm->mtx)
656 >        if (!rm)
657                  return(NULL);
658 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
658 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
659          if (!dnew)
660                  return(NULL);
661 +        if (rm->mtx) {
662 +                if (!rmx_prepare(dnew)) {
663 +                        rmx_free(dnew);
664 +                        return(NULL);
665 +                }
666 +                memcpy(dnew->mtx, rm->mtx, array_size(dnew));
667 +        }
668          rmx_addinfo(dnew, rm->info);
669          dnew->dtype = rm->dtype;
670          copycolor(dnew->cexp, rm->cexp);
671          memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
657        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
672          return(dnew);
673 + }
674 +
675 + /* Replace data in first matrix with data from second */
676 + int
677 + rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
678 + {
679 +        if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) |
680 +                        (rdst->ncols != rsrc->ncols) |
681 +                        (rdst->ncomp != rsrc->ncomp))
682 +                return(0);
683 +
684 +        if (dometa) {           /* transfer everything? */
685 +                rmx_reset(rdst);
686 +                *rdst = *rsrc;
687 +                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
688 +                return(1);
689 +        }
690 + #ifdef MAP_FILE                 /* just matrix data -- leave metadata */
691 +        if (rdst->mapped)
692 +                munmap(rdst->mapped, mapped_size(rdst));
693 +        else
694 + #endif
695 +        if (rdst->mtx)
696 +                free(rdst->mtx);
697 +        rdst->mapped = rsrc->mapped;
698 +        rdst->mtx = rsrc->mtx;
699 +        rsrc->mapped = NULL; rsrc->mtx = NULL;
700 +        return(1);
701   }
702  
703   /* Allocate and assign transposed matrix */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines