--- ray/src/util/cmatrix.c 2022/03/11 17:15:42 2.34 +++ ray/src/util/cmatrix.c 2023/11/21 01:30:20 2.38 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.34 2022/03/11 17:15:42 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.38 2023/11/21 01:30:20 greg Exp $"; #endif /* * Color matrix routines. @@ -18,12 +18,12 @@ static const char RCSid[] = "$Id: cmatrix.c,v 2.34 202 const char stdin_name[] = ""; const char *cm_fmt_id[] = { - "unknown", COLRFMT, CIEFMT, + "unknown", COLRFMT, CIEFMT, SPECFMT, "float", "ascii", "double" }; const int cm_elem_size[] = { - 0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double) + 0, 4, 4, 0, 3*sizeof(float), 0, 3*sizeof(double) }; /* Allocate a color coefficient matrix */ @@ -177,18 +177,14 @@ cm_getheader(int *dt, int *nr, int *nc, int *swp, COLO /* Allocate and load image data into matrix */ static CMATRIX * -cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale) +cm_load_rgbe(FILE *fp, int nrows, int ncols) { - int doscale; CMATRIX *cm; COLORV *mp; /* header already loaded */ cm = cm_alloc(nrows, ncols); if (!cm) return(NULL); - doscale = (scale[0] < .99) | (scale[0] > 1.01) | - (scale[1] < .99) | (scale[1] > 1.01) | - (scale[2] < .99) | (scale[2] > 1.01) ; mp = cm->cmem; while (nrows--) { if (freadscan((COLOR *)mp, ncols, fp) < 0) { @@ -196,15 +192,7 @@ cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR sca cm_free(cm); return(NULL); } - if (doscale) { - int i = ncols; - while (i--) { - *mp++ *= scale[0]; - *mp++ *= scale[1]; - *mp++ *= scale[2]; - } - } else - mp += 3*ncols; + mp += 3*ncols; } /* caller closes stream */ return(cm); } @@ -214,6 +202,7 @@ CMATRIX * cm_load(const char *inspec, int nrows, int ncols, int dtype) { const int ROWINC = 2048; + int dimsOK = (dtype == DTascii) | (nrows > 0) && ncols; int swap = 0; FILE *fp; COLOR scale; @@ -240,12 +229,14 @@ cm_load(const char *inspec, int nrows, int ncols, int #endif if (dtype != DTascii) SET_FILE_BINARY(fp); /* doesn't really work */ - if (!dtype | !nrows | !ncols) { /* expecting header? */ + if (!dtype | !dimsOK) { /* expecting header? */ char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); if (err) error(USER, err); + dimsOK = ncols > 0 && ( nrows > 0 || + (dtype != DTrgbe) & (dtype != DTxyze) ); } - if (!nrows | !ncols && !fscnresolu(&ncols, &nrows, fp)) + if (!dimsOK && !fscnresolu(&ncols, &nrows, fp)) error(USER, "unspecified matrix size"); switch (dtype) { case DTascii: @@ -254,7 +245,7 @@ cm_load(const char *inspec, int nrows, int ncols, int break; case DTrgbe: case DTxyze: - cm = cm_load_rgbe(fp, nrows, ncols, scale); + cm = cm_load_rgbe(fp, nrows, ncols); goto cleanup; default: error(USER, "unexpected data type in cm_load()"); @@ -391,6 +382,17 @@ cleanup: else funlockfile(fp); #endif + if ((scale[0] < .99) | (scale[0] > 1.01) | + (scale[1] < .99) | (scale[1] > 1.01) | + (scale[2] < .99) | (scale[2] > 1.01)) { + size_t n = (size_t)ncols*nrows; + COLORV *mp = cm->cmem; + while (n--) { /* apply exposure scaling */ + *mp++ *= scale[0]; + *mp++ *= scale[1]; + *mp++ *= scale[2]; + } + } return(cm); EOFerror: sprintf(errmsg, "unexpected EOF reading %s", inspec);