--- ray/src/util/cmatrix.c 2019/08/12 18:28:37 2.21 +++ ray/src/util/cmatrix.c 2021/01/15 18:31:38 2.31 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.21 2019/08/12 18:28:37 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.31 2021/01/15 18:31:38 greg Exp $"; #endif /* * Color matrix routines. @@ -16,12 +16,12 @@ static const char RCSid[] = "$Id: cmatrix.c,v 2.21 201 #include "resolu.h" const char *cm_fmt_id[] = { - "unknown", "ascii", COLRFMT, CIEFMT, - "float", "double" + "unknown", COLRFMT, CIEFMT, + "float", "ascii", "double" }; const int cm_elem_size[] = { - 0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) + 0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double) }; /* Allocate a color coefficient matrix */ @@ -33,7 +33,7 @@ cm_alloc(int nrows, int ncols) if ((nrows <= 0) | (ncols <= 0)) error(USER, "attempt to create empty matrix"); cm = (CMATRIX *)malloc(sizeof(CMATRIX) + - sizeof(COLOR)*(nrows*ncols - 1)); + sizeof(COLOR)*((size_t)nrows*ncols - 1)); if (!cm) error(SYSTEM, "out of memory in cm_alloc()"); cm->nrows = nrows; @@ -65,9 +65,9 @@ cm_resize(CMATRIX *cm, int nrows) cm_free(cm); return(NULL); } - old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1); + old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1); adjacent_ra_sizes(ra_bounds, old_size); - new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1); + new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1); if (nrows < cm->nrows ? new_size <= ra_bounds[0] : new_size > ra_bounds[1]) { adjacent_ra_sizes(ra_bounds, new_size); @@ -81,7 +81,9 @@ cm_resize(CMATRIX *cm, int nrows) typedef struct { int dtype; /* data type */ + int need2swap; /* need byte swap? */ int nrows, ncols; /* matrix size */ + COLOR expos; /* exposure value */ char *err; /* error message */ } CMINFO; /* header info record */ @@ -104,6 +106,21 @@ get_cminfo(char *s, void *p) ip->ncols = atoi(s+6); return(0); } + if ((i = isbigendian(s)) >= 0) { + ip->need2swap = (nativebigendian() != i); + return(0); + } + if (isexpos(s)) { + double d = exposval(s); + scalecolor(ip->expos, d); + return(0); + } + if (iscolcor(s)) { + COLOR ctmp; + colcorval(ctmp, s); + multcolor(ip->expos, ctmp); + return(0); + } if (!formatval(fmt, s)) return(0); for (i = 1; i < DTend; i++) @@ -114,11 +131,13 @@ get_cminfo(char *s, void *p) /* Load header to obtain/check data type and number of columns */ char * -cm_getheader(int *dt, int *nr, int *nc, FILE *fp) +cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp) { CMINFO cmi; /* read header */ cmi.dtype = DTfromHeader; + cmi.need2swap = 0; + cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f; cmi.nrows = cmi.ncols = 0; cmi.err = "unexpected EOF in header"; if (getheader(fp, get_cminfo, &cmi) < 0) @@ -144,15 +163,58 @@ cm_getheader(int *dt, int *nr, int *nc, FILE *fp) else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) return("unexpected column count in header"); } + if (swp) /* get/check swap? */ + *swp = cmi.need2swap; + if (scale) { /* transfer exposure comp. */ + scale[0] = 1.f/cmi.expos[0]; + scale[1] = 1.f/cmi.expos[1]; + scale[2] = 1.f/cmi.expos[2]; + } return(NULL); } +/* Allocate and load image data into matrix */ +static CMATRIX * +cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale) +{ + 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) { + error(USER, "error reading color picture as matrix"); + 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; + } /* caller closes stream */ + return(cm); +} + /* Allocate and load a matrix from the given input (or stdin if NULL) */ CMATRIX * cm_load(const char *inspec, int nrows, int ncols, int dtype) { const int ROWINC = 2048; FILE *fp = stdin; + int swap = 0; + COLOR scale; CMATRIX *cm; if (!inspec) @@ -173,36 +235,39 @@ cm_load(const char *inspec, int nrows, int ncols, int if (dtype != DTascii) SET_FILE_BINARY(fp); /* doesn't really work */ if (!dtype | !ncols) { /* expecting header? */ - char *err = cm_getheader(&dtype, &nrows, &ncols, fp); + char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp); if (err) error(USER, err); - if (ncols <= 0) - error(USER, "unspecified number of columns"); } + if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp)) + error(USER, "unspecified number of columns"); switch (dtype) { case DTascii: case DTfloat: case DTdouble: break; + case DTrgbe: + case DTxyze: + cm = cm_load_rgbe(fp, nrows, ncols, scale); + goto cleanup; default: error(USER, "unexpected data type in cm_load()"); } if (nrows <= 0) { /* don't know length? */ int guessrows = 147; /* usually big enough */ - if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { + if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) { long startpos = ftell(fp); if (fseek(fp, 0L, SEEK_END) == 0) { + long rowsiz = (long)ncols*cm_elem_size[dtype]; long endpos = ftell(fp); - long elemsiz = 3*(dtype==DTfloat ? - sizeof(float) : sizeof(double)); - if ((endpos - startpos) % (ncols*elemsiz)) { + if ((endpos - startpos) % rowsiz) { sprintf(errmsg, "improper length for binary file '%s'", inspec); error(USER, errmsg); } - guessrows = (endpos - startpos)/(ncols*elemsiz); + guessrows = (endpos - startpos)/rowsiz; if (fseek(fp, startpos, SEEK_SET) < 0) { sprintf(errmsg, "fseek() error on file '%s'", @@ -244,14 +309,14 @@ cm_load(const char *inspec, int nrows, int ncols, int } } else { /* read binary file */ if (sizeof(COLOR) == cm_elem_size[dtype]) { - int nread = 0; + size_t nread = 0; do { /* read all we can */ nread += getbinary(cm->cmem + 3*nread, sizeof(COLOR), - cm->nrows*cm->ncols - nread, + (size_t)cm->nrows*cm->ncols - nread, fp); if (nrows <= 0) { /* unknown length */ - if (nread == cm->nrows*cm->ncols) + if (nread == (size_t)cm->nrows*cm->ncols) /* need more space? */ cm = cm_resize(cm, cm->nrows+ROWINC); else if (nread && !(nread % cm->ncols)) @@ -259,33 +324,43 @@ cm_load(const char *inspec, int nrows, int ncols, int cm = cm_resize(cm, nread/cm->ncols); else /* ended mid-row */ goto EOFerror; - } else if (nread < cm->nrows*cm->ncols) + } else if (nread < (size_t)cm->nrows*cm->ncols) goto EOFerror; - } while (nread < cm->nrows*cm->ncols); + } while (nread < (size_t)cm->nrows*cm->ncols); + if (swap) { + if (sizeof(COLORV) == 4) + swap32((char *)cm->cmem, + 3*(size_t)cm->nrows*cm->ncols); + else /* sizeof(COLORV) == 8 */ + swap64((char *)cm->cmem, + 3*(size_t)cm->nrows*cm->ncols); + } } else if (dtype == DTdouble) { double dc[3]; /* load from double */ COLORV *cvp = cm->cmem; - int n = nrows*ncols; + size_t n = (size_t)nrows*ncols; if (n <= 0) goto not_handled; while (n--) { if (getbinary(dc, sizeof(double), 3, fp) != 3) goto EOFerror; + if (swap) swap64((char *)dc, 3); copycolor(cvp, dc); cvp += 3; } } else /* dtype == DTfloat */ { float fc[3]; /* load from float */ COLORV *cvp = cm->cmem; - int n = nrows*ncols; + size_t n = (size_t)nrows*ncols; if (n <= 0) goto not_handled; while (n--) { if (getbinary(fc, sizeof(float), 3, fp) != 3) goto EOFerror; + if (swap) swap32((char *)fc, 3); copycolor(cvp, fc); cvp += 3; } @@ -297,6 +372,7 @@ cm_load(const char *inspec, int nrows, int ncols, int error(WARNING, errmsg); } } +cleanup: if (fp != stdin) { if (inspec[0] != '!') fclose(fp); @@ -313,6 +389,7 @@ cm_load(const char *inspec, int nrows, int ncols, int EOFerror: sprintf(errmsg, "unexpected EOF reading %s", inspec); error(USER, errmsg); + return(NULL); not_handled: error(INTERNAL, "unhandled data size or length in cm_load()"); return(NULL); /* gratis return */ @@ -406,6 +483,7 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) static const char tabEOL[2] = {'\t','\n'}; const COLORV *mp; int r, c; + size_t n, rv; if (!cm) return(0); @@ -421,18 +499,18 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) case DTfloat: case DTdouble: if (sizeof(COLOR) == cm_elem_size[dtype]) { - r = cm->ncols*cm->nrows; - while (r > 0) { - c = putbinary(mp, sizeof(COLOR), r, fp); - if (c <= 0) + n = (size_t)cm->ncols*cm->nrows; + while (n > 0) { + rv = fwrite(mp, sizeof(COLOR), n, fp); + if (rv <= 0) return(0); - mp += 3*c; - r -= c; + mp += 3*rv; + n -= rv; } } else if (dtype == DTdouble) { double dc[3]; - r = cm->ncols*cm->nrows; - while (r--) { + n = (size_t)cm->ncols*cm->nrows; + while (n--) { copycolor(dc, mp); if (putbinary(dc, sizeof(double), 3, fp) != 3) return(0); @@ -440,8 +518,8 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) } } else /* dtype == DTfloat */ { float fc[3]; - r = cm->ncols*cm->nrows; - while (r--) { + n = (size_t)cm->ncols*cm->nrows; + while (n--) { copycolor(fc, mp); if (putbinary(fc, sizeof(float), 3, fp) != 3) return(0);