--- ray/src/util/cmatrix.c 2021/01/19 23:32:00 2.32 +++ ray/src/util/cmatrix.c 2023/05/18 23:55:10 2.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.32 2021/01/19 23:32:00 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.37 2023/05/18 23:55:10 greg Exp $"; #endif /* * Color matrix routines. @@ -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,16 +202,19 @@ 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; CMATRIX *cm; - if (!inspec || !*inspec) + if (!inspec) + inspec = stdin_name; + else if (!*inspec) return(NULL); - if (inspec == stdin_name) + if (inspec == stdin_name) { /* reading from stdin? */ fp = stdin; - else if (inspec[0] == '!') { + } else if (inspec[0] == '!') { fp = popen(inspec+1, "r"); if (!fp) { sprintf(errmsg, "cannot start command '%s'", inspec); @@ -238,13 +229,15 @@ cm_load(const char *inspec, int nrows, int ncols, int #endif if (dtype != DTascii) SET_FILE_BINARY(fp); /* doesn't really work */ - if (!dtype | !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 (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp)) - error(USER, "unspecified number of columns"); + if (!dimsOK && !fscnresolu(&ncols, &nrows, fp)) + error(USER, "unspecified matrix size"); switch (dtype) { case DTascii: case DTfloat: @@ -252,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()"); @@ -389,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);