--- ray/src/util/rmatrix.c 2023/12/05 21:13:38 2.71 +++ ray/src/util/rmatrix.c 2023/12/12 18:45:53 2.76 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rmatrix.c,v 2.71 2023/12/05 21:13:38 greg Exp $"; +static const char RCSid[] = "$Id: rmatrix.c,v 2.76 2023/12/12 18:45:53 greg Exp $"; #endif /* * General matrix operations. @@ -450,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); } @@ -539,7 +539,9 @@ rmx_write_header(const RMATRIX *rm, int dtype, FILE *f dtype = DTxyze; else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) dtype = DTrgbe; - if ((dtype == DTspec) & (rm->ncomp < 3)) + if ((dtype < DTspec) & (rm->ncomp > 3)) + dtype = DTspec; + else if ((dtype == DTspec) & (rm->ncomp <= 3)) return(0); if (dtype == DTascii) /* set file type (WINDOWS) */ @@ -560,8 +562,8 @@ rmx_write_header(const RMATRIX *rm, int dtype, FILE *f } if (dtype >= DTspec) { /* # components & split? */ fputncomp(rm->ncomp, fp); - if (dtype == DTspec || (rm->ncomp > 3 && - memcmp(rm->wlpart, WLPART, sizeof(WLPART)))) + if (rm->ncomp > 3 && + memcmp(rm->wlpart, WLPART, sizeof(WLPART))) fputwlsplit(rm->wlpart, fp); } else if ((rm->ncomp != 3) & (rm->ncomp != 1)) return(0); /* wrong # components */ @@ -666,6 +668,34 @@ 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); + } +#ifdef MAP_FILE /* just matrix data -- leave metadata */ + if (rdst->mapped) + munmap(rdst->mapped, mapped_size(rdst)); + else +#endif + if (rdst->mtx) + free(rdst->mtx); + rdst->mapped = rsrc->mapped; + rdst->mtx = rsrc->mtx; + rsrc->mapped = NULL; rsrc->mtx = NULL; + return(1); } /* Allocate and assign transposed matrix */