--- ray/src/util/rmatrix.c 2024/05/19 15:32:24 2.79 +++ ray/src/util/rmatrix.c 2024/10/08 00:43:57 2.83 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rmatrix.c,v 2.79 2024/05/19 15:32:24 greg Exp $"; +static const char RCSid[] = "$Id: rmatrix.c,v 2.83 2024/10/08 00:43:57 greg Exp $"; #endif /* * General matrix operations. @@ -31,7 +31,7 @@ rmx_new(int nr, int nc, int n) if (!dnew) return(NULL); - dnew->dtype = DTdouble; + dnew->dtype = DTrmx_native; dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; @@ -46,12 +46,12 @@ int rmx_prepare(RMATRIX *rm) { if (!rm) return(0); - if (rm->mtx) + if (rm->mtx) /* assume it's right size */ return(1); if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) return(0); - rm->mtx = (double *)malloc(rmx_array_size(rm)); - rm->pflags |= RMF_OURMEM; + rm->mtx = (rmx_dtype *)malloc(rmx_array_size(rm)); + rm->pflags |= RMF_FREEMEM; return(rm->mtx != NULL); } @@ -61,9 +61,9 @@ rmx_alloc(int nr, int nc, int n) { RMATRIX *dnew = rmx_new(nr, nc, n); - if (dnew && !rmx_prepare(dnew)) { + if (!rmx_prepare(dnew)) { rmx_free(dnew); - dnew = NULL; + return(NULL); } return(dnew); } @@ -77,15 +77,15 @@ rmx_reset(RMATRIX *rm) free(rm->info); rm->info = NULL; } - if (rm->mtx && rm->pflags & RMF_OURMEM) { #ifdef MAP_FILE - if (rm->mapped) { - munmap(rm->mapped, rmx_mapped_size(rm)); - rm->mapped = NULL; - } else + if (rm->mapped) { + munmap(rm->mapped, rmx_mapped_size(rm)); + rm->mapped = NULL; + } else #endif - free(rm->mtx); - rm->pflags &= ~RMF_OURMEM; + if (rm->pflags & RMF_FREEMEM) { + free(rm->mtx); + rm->pflags &= ~RMF_FREEMEM; } rm->mtx = NULL; } @@ -116,13 +116,12 @@ rmx_newtype(int dtyp1, int dtyp2) int rmx_addinfo(RMATRIX *rm, const char *info) { - int oldlen = 0; + size_t oldlen = 0; if (!rm || !info || !*info) return(0); if (!rm->info) { rm->info = (char *)malloc(strlen(info)+1); - if (rm->info) rm->info[0] = '\0'; } else { oldlen = strlen(rm->info); rm->info = (char *)realloc(rm->info, @@ -141,19 +140,19 @@ get_dminfo(char *s, void *p) char fmt[MAXFMTLEN]; int i; - if (headidval(NULL, s)) + if (isheadid(s)) return(0); if (isncomp(s)) { ip->ncomp = ncompval(s); - return(0); + return(ip->ncomp - 1); } if (!strncmp(s, "NROWS=", 6)) { ip->nrows = atoi(s+6); - return(0); + return(ip->nrows - 1); } if (!strncmp(s, "NCOLS=", 6)) { ip->ncols = atoi(s+6); - return(0); + return(ip->ncols - 1); } if ((i = isbigendian(s)) >= 0) { if (nativebigendian() != i) @@ -165,18 +164,17 @@ get_dminfo(char *s, void *p) if (isexpos(s)) { float f = exposval(s); scalecolor(ip->cexp, f); - return(0); + return(f > .0 ? 0 : -1); } if (iscolcor(s)) { COLOR ctmp; - colcorval(ctmp, s); + if (!colcorval(ctmp, s)) return(-1); multcolor(ip->cexp, ctmp); return(0); } - if (iswlsplit(s)) { - wlsplitval(ip->wlpart, s); - return(0); - } + if (iswlsplit(s)) + return(wlsplitval(ip->wlpart, s) - 1); + if (!formatval(fmt, s)) { rmx_addinfo(ip, s); return(0); @@ -186,11 +184,11 @@ get_dminfo(char *s, void *p) ip->dtype = i; return(0); } - return(-1); + return(-1); /* bad format */ } static int -rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_ascii(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { int j, k; @@ -202,7 +200,7 @@ rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *f } static int -rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_float(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { int j, k; float val[100]; @@ -223,7 +221,7 @@ rmx_load_float(double *drp, const RMATRIX *rm, FILE *f } static int -rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_double(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) return(0); @@ -233,7 +231,7 @@ rmx_load_double(double *drp, const RMATRIX *rm, FILE * } static int -rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_rgbe(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { COLR *scan; COLOR col; @@ -256,7 +254,7 @@ rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp } static int -rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { uby8 *scan; SCOLOR scol; @@ -293,7 +291,7 @@ rmx_load_header(RMATRIX *rm, FILE *fp) } rm->dtype = DTascii; /* assumed w/o FORMAT */ if (getheader(fp, get_dminfo, rm) < 0) { - fputs("Unrecognized matrix format\n", stderr); + fputs("Bad matrix header\n", stderr); return(0); } if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && @@ -309,9 +307,9 @@ rmx_load_header(RMATRIX *rm, FILE *fp) return(1); } -/* Load next row as double (cannot be XML) */ +/* Load next row as rmx_dtype (cannot be XML) */ int -rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp) +rmx_load_row(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) { switch (rm->dtype) { case DTascii: @@ -338,14 +336,16 @@ rmx_load_data(RMATRIX *rm, FILE *fp) int i; #ifdef MAP_FILE long pos; /* map memory for file > 1MB if possible */ - if ((rm->dtype == DTdouble) & !(rm->pflags & RMF_SWAPIN) && - rmx_array_size(rm) >= 1L<<20 && - (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { + if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) & + (rmx_array_size(rm) >= 1L<<20) && + (pos = ftell(fp)) >= 0 && !(pos % sizeof(rmx_dtype))) { rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(fp), 0); if (rm->mapped != MAP_FAILED) { - rm->mtx = (double *)rm->mapped + pos/sizeof(double); - rm->pflags |= RMF_OURMEM; + if (rm->pflags & RMF_FREEMEM) + free(rm->mtx); + rm->mtx = (rmx_dtype *)rm->mapped + pos/sizeof(rmx_dtype); + rm->pflags &= ~RMF_FREEMEM; return(1); } /* else fall back on reading into memory */ rm->mapped = NULL; @@ -434,17 +434,17 @@ rmx_load(const char *inspec, RMPref rmp) (dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { double cmlt[MAXCSAMP]; int i; - cmlt[0] = 1./dnew->cexp[0]; - cmlt[1] = 1./dnew->cexp[1]; - cmlt[2] = 1./dnew->cexp[2]; if (dnew->ncomp > MAXCSAMP) { fprintf(stderr, "Excess spectral components in: %s\n", inspec); rmx_free(dnew); return(NULL); } + cmlt[0] = 1./dnew->cexp[0]; + cmlt[1] = 1./dnew->cexp[1]; + cmlt[2] = 1./dnew->cexp[2]; for (i = dnew->ncomp; i-- > 3; ) - cmlt[i] = cmlt[1]; + cmlt[i] = cmlt[1]; /* XXX hack! */ rmx_scale(dnew, cmlt); setcolor(dnew->cexp, 1.f, 1.f, 1.f); } @@ -452,7 +452,7 @@ rmx_load(const char *inspec, RMPref rmp) } static int -rmx_write_ascii(const double *dp, int nc, int len, FILE *fp) +rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp) { while (len-- > 0) { int k = nc; @@ -464,7 +464,7 @@ rmx_write_ascii(const double *dp, int nc, int len, FIL } static int -rmx_write_float(const double *dp, int len, FILE *fp) +rmx_write_float(const rmx_dtype *dp, int len, FILE *fp) { float val; @@ -477,7 +477,7 @@ rmx_write_float(const double *dp, int len, FILE *fp) } static int -rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp) +rmx_write_rgbe(const rmx_dtype *dp, int nc, int len, FILE *fp) { COLR *scan; int j; @@ -496,7 +496,7 @@ rmx_write_rgbe(const double *dp, int nc, int len, FILE } static int -rmx_write_spec(const double *dp, int nc, int len, FILE *fp) +rmx_write_spec(const rmx_dtype *dp, int nc, int len, FILE *fp) { uby8 *scan; SCOLOR scol; @@ -584,14 +584,14 @@ rmx_write_header(const RMATRIX *rm, int dtype, FILE *f /* Write out matrix data (usually by row) */ int -rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp) +rmx_write_data(const rmx_dtype *dp, int nc, int len, int dtype, FILE *fp) { switch (dtype) { case DTascii: return(rmx_write_ascii(dp, nc, len, fp)); case DTfloat: return(rmx_write_float(dp, nc*len, fp)); - case DTdouble: + case DTrmx_native: return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len); case DTrgbe: case DTxyze: @@ -615,7 +615,7 @@ rmx_write(const RMATRIX *rm, int dtype, FILE *fp) #ifdef getc_unlocked flockfile(fp); #endif - if (dtype == DTdouble) /* write all at once? */ + if (dtype == DTrmx_native) /* write all at once? */ ok = rmx_write_data(rm->mtx, rm->ncomp, rm->nrows*rm->ncols, dtype, fp); else /* else row by row */ @@ -644,7 +644,7 @@ rmx_identity(const int dim, const int n) return(NULL); memset(rid->mtx, 0, rmx_array_size(rid)); for (i = dim; i--; ) { - double *dp = rmx_lval(rid,i,i); + rmx_dtype *dp = rmx_lval(rid,i,i); for (k = n; k--; ) dp[k] = 1.; } @@ -691,21 +691,18 @@ rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int do rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; return(1); } - if (rdst->pflags & RMF_OURMEM) { #ifdef MAP_FILE /* just matrix data -- leave metadata */ - if (rdst->mapped) - munmap(rdst->mapped, rmx_mapped_size(rdst)); - else + if (rdst->mapped) + munmap(rdst->mapped, rmx_mapped_size(rdst)); + else #endif - if (rdst->mtx) - free(rdst->mtx); + if (rdst->pflags & RMF_FREEMEM) { + free(rdst->mtx); + rdst->pflags &= ~RMF_FREEMEM; } rdst->mapped = rsrc->mapped; rdst->mtx = rsrc->mtx; - if (rsrc->pflags & RMF_OURMEM) - rdst->pflags |= RMF_OURMEM; - else - rdst->pflags &= ~RMF_OURMEM; + rdst->pflags |= rsrc->pflags & RMF_FREEMEM; rsrc->mapped = NULL; rsrc->mtx = NULL; return(1); } @@ -740,7 +737,7 @@ rmx_transpose(const RMATRIX *rm) for (j = dnew->ncols; j--; ) for (i = dnew->nrows; i--; ) memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), - sizeof(double)*dnew->ncomp); + sizeof(rmx_dtype)*dnew->ncomp); return(dnew); } @@ -765,7 +762,7 @@ rmx_multiply(const RMATRIX *m1, const RMATRIX *m2) for (i = mres->nrows; i--; ) for (j = mres->ncols; j--; ) for (k = mres->ncomp; k--; ) { - double d = 0; + rmx_dtype d = 0; for (h = m1->ncols; h--; ) d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; rmx_lval(mres,i,j)[k] = d; @@ -793,7 +790,7 @@ rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide for (i = m1->nrows; i--; ) for (j = m1->ncols; j--; ) if (divide) { - double d; + rmx_dtype d; if (m2->ncomp == 1) { d = rmx_val(m2,i,j)[0]; if (d == 0) { @@ -816,7 +813,7 @@ rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide } } else { if (m2->ncomp == 1) { - const double d = rmx_val(m2,i,j)[0]; + const rmx_dtype d = rmx_val(m2,i,j)[0]; for (k = m1->ncomp; k--; ) rmx_lval(m1,i,j)[k] *= d; } else @@ -857,8 +854,8 @@ rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub rmx_addinfo(msum, rmx_mismatch_warn); for (i = msum->nrows; i--; ) for (j = msum->ncols; j--; ) { - const double *da = rmx_val(madd,i,j); - double *ds = rmx_lval(msum,i,j); + const rmx_dtype *da = rmx_val(madd,i,j); + rmx_dtype *ds = rmx_lval(msum,i,j); for (k = msum->ncomp; k--; ) ds[k] += sf[k] * da[k]; } @@ -877,7 +874,7 @@ rmx_scale(RMATRIX *rm, const double sf[]) return(0); for (i = rm->nrows; i--; ) for (j = rm->ncols; j--; ) { - double *dp = rmx_lval(rm,i,j); + rmx_dtype *dp = rmx_lval(rm,i,j); for (k = rm->ncomp; k--; ) dp[k] *= sf[k]; } @@ -909,9 +906,9 @@ rmx_transform(const RMATRIX *msrc, int n, const double dnew->dtype = msrc->dtype; for (i = dnew->nrows; i--; ) for (j = dnew->ncols; j--; ) { - const double *ds = rmx_val(msrc,i,j); + const rmx_dtype *ds = rmx_val(msrc,i,j); for (kd = dnew->ncomp; kd--; ) { - double d = 0; + rmx_dtype d = 0; for (ks = msrc->ncomp; ks--; ) d += cmat[kd*msrc->ncomp + ks] * ds[ks]; rmx_lval(dnew,i,j)[kd] = d; @@ -936,7 +933,7 @@ rmx_from_cmatrix(const CMATRIX *cm) for (i = dnew->nrows; i--; ) for (j = dnew->ncols; j--; ) { const COLORV *cv = cm_lval(cm,i,j); - double *dp = rmx_lval(dnew,i,j); + rmx_dtype *dp = rmx_lval(dnew,i,j); dp[0] = cv[0]; dp[1] = cv[1]; dp[2] = cv[2]; @@ -958,7 +955,7 @@ cm_from_rmatrix(const RMATRIX *rm) return(NULL); for (i = cnew->nrows; i--; ) for (j = cnew->ncols; j--; ) { - const double *dp = rmx_val(rm,i,j); + const rmx_dtype *dp = rmx_val(rm,i,j); COLORV *cv = cm_lval(cnew,i,j); switch (rm->ncomp) { case 3: