--- ray/src/util/cmatrix.c 2020/03/25 01:51:09 2.25 +++ ray/src/util/cmatrix.c 2021/01/15 17:22:23 2.30 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.25 2020/03/25 01:51:09 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.30 2021/01/15 17:22:23 greg Exp $"; #endif /* * Color matrix routines. @@ -21,7 +21,7 @@ const char *cm_fmt_id[] = { }; 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); @@ -181,10 +181,6 @@ cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR sca CMATRIX *cm; COLORV *mp; /* header already loaded */ - if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) { - error(USER, "bad picture resolution string"); - return(NULL); - } cm = cm_alloc(nrows, ncols); if (!cm) return(NULL); @@ -242,9 +238,9 @@ cm_load(const char *inspec, int nrows, int ncols, int 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: @@ -259,20 +255,19 @@ cm_load(const char *inspec, int nrows, int ncols, int } 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'", @@ -333,6 +328,14 @@ cm_load(const char *inspec, int nrows, int ncols, int goto EOFerror; } while (nread < cm->nrows*cm->ncols); + if (swap) { + if (sizeof(COLORV) == 4) + swap32((char *)cm->cmem, + 3*cm->nrows*cm->ncols); + else /* sizeof(COLORV) == 8 */ + swap64((char *)cm->cmem, + 3*cm->nrows*cm->ncols); + } } else if (dtype == DTdouble) { double dc[3]; /* load from double */ COLORV *cvp = cm->cmem; @@ -343,6 +346,7 @@ cm_load(const char *inspec, int nrows, int ncols, int while (n--) { if (getbinary(dc, sizeof(double), 3, fp) != 3) goto EOFerror; + if (swap) swap64((char *)dc, 3); copycolor(cvp, dc); cvp += 3; } @@ -356,6 +360,7 @@ cm_load(const char *inspec, int nrows, int ncols, int while (n--) { if (getbinary(fc, sizeof(float), 3, fp) != 3) goto EOFerror; + if (swap) swap32((char *)fc, 3); copycolor(cvp, fc); cvp += 3; } @@ -367,12 +372,6 @@ cm_load(const char *inspec, int nrows, int ncols, int error(WARNING, errmsg); } } - if (swap) { - if (dtype == DTfloat) - swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols); - else if (dtype == DTdouble) - swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols); - } cleanup: if (fp != stdin) { if (inspec[0] != '!') @@ -484,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); @@ -499,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); @@ -518,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);