--- ray/src/util/rmatrix.c 2023/12/04 22:02:40 2.69 +++ ray/src/util/rmatrix.c 2023/12/06 17:57:34 2.73 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rmatrix.c,v 2.69 2023/12/04 22:02:40 greg Exp $"; +static const char RCSid[] = "$Id: rmatrix.c,v 2.73 2023/12/06 17:57:34 greg Exp $"; #endif /* * General matrix operations. @@ -374,14 +374,15 @@ rmx_load(const char *inspec, RMPref rmp) fp = stdin; else if (inspec[0] == '!') fp = popen(inspec+1, "r"); - else if (rmp != RMPnone) { + else { const char *sp = inspec; /* check suffix */ while (*sp) ++sp; while (sp > inspec && sp[-1] != '.') --sp; if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ - CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : + CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL : + rmp==RMPtrans ? cm_loadBTDF(inspec) : cm_loadBRDF(inspec, rmp==RMPreflB) ; if (!cm) return(NULL); @@ -449,7 +450,7 @@ rmx_write_ascii(const double *dp, int nc, int len, FIL { while (len-- > 0) { int k = nc; - while (nc-- > 0) + while (k-- > 0) fprintf(fp, " %.7e", *dp++); fputc('\t', fp); } @@ -527,7 +528,7 @@ findCIEprims(const char *info) int rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp) { - if (!rm | !fp || !rm->mtx | (rm->ncols <= 0)) + if (!rm | !fp || rm->ncols <= 0) return(0); if (rm->info) fputs(rm->info, fp); @@ -665,6 +666,30 @@ rmx_copy(const RMATRIX *rm) copycolor(dnew->cexp, rm->cexp); memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); return(dnew); +} + +/* Replace data in first matrix with data from second */ +int +rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) +{ + if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | + (rdst->ncols != rsrc->ncols) | + (rdst->ncomp != rsrc->ncomp)) + return(0); + + if (dometa) { /* transfer everything? */ + rmx_reset(rdst); + *rdst = *rsrc; + rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; + return(1); + } + if (rdst->mapped) + return(0); /* XXX can't handle this case */ + /* just matrix data -- leave metadata */ + if (rdst->mtx) free(rdst->mtx); + rdst->mtx = rsrc->mtx; + rsrc->mtx = NULL; + return(1); } /* Allocate and assign transposed matrix */