--- ray/src/util/cmatrix.c 2014/04/08 23:45:33 2.3 +++ ray/src/util/cmatrix.c 2015/05/04 20:53:21 2.9 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.3 2014/04/08 23:45:33 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.9 2015/05/04 20:53:21 greg Exp $"; #endif /* * Color matrix routines. @@ -14,8 +14,8 @@ static const char RCSid[] = "$Id: cmatrix.c,v 2.3 2014 #include "resolu.h" const char *cm_fmt_id[] = { - "unknown", "ascii", "float", "double", - COLRFMT, CIEFMT + "unknown", "ascii", COLRFMT, CIEFMT, + "float", "double" }; const int cm_elem_size[] = { @@ -31,7 +31,7 @@ cm_alloc(int nrows, int ncols) if ((nrows <= 0) | (ncols <= 0)) error(USER, "attempt to create empty matrix"); cm = (CMATRIX *)malloc(sizeof(CMATRIX) + - 3*sizeof(COLORV)*(nrows*ncols - 1)); + sizeof(COLOR)*(nrows*ncols - 1)); if (cm == NULL) error(SYSTEM, "out of memory in cm_alloc()"); cm->nrows = nrows; @@ -39,64 +39,127 @@ cm_alloc(int nrows, int ncols) return(cm); } +static void +adjacent_ra_sizes(size_t bounds[2], size_t target) +{ + bounds[0] = 0; bounds[1] = 2048; + while (bounds[1] < target) { + bounds[0] = bounds[1]; + bounds[1] += bounds[1]>>1; + } +} + /* Resize color coefficient matrix */ CMATRIX * cm_resize(CMATRIX *cm, int nrows) { + size_t old_size, new_size, ra_bounds[2]; + if (nrows == cm->nrows) return(cm); if (nrows <= 0) { cm_free(cm); return(NULL); } - cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) + - 3*sizeof(COLORV)*(nrows*cm->ncols - 1)); - if (cm == NULL) - error(SYSTEM, "out of memory in cm_resize()"); + old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1); + adjacent_ra_sizes(ra_bounds, old_size); + new_size = sizeof(CMATRIX) + sizeof(COLOR)*(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); + cm = (CMATRIX *)realloc(cm, ra_bounds[1]); + if (cm == NULL) + error(SYSTEM, "out of memory in cm_resize()"); + } cm->nrows = nrows; return(cm); } +typedef struct { + int dtype; /* data type */ + int nrows, ncols; /* matrix size */ + char *err; /* error message */ +} CMINFO; /* header info record */ + static int -getDT(char *s, void *p) +get_cminfo(char *s, void *p) { + CMINFO *ip = (CMINFO *)p; char fmt[32]; int i; - + + if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { + ip->err = "unexpected # components (must be 3)"; + return(-1); + } + if (!strncmp(s, "NROWS=", 6)) { + ip->nrows = atoi(s+6); + return(0); + } + if (!strncmp(s, "NCOLS=", 6)) { + ip->ncols = atoi(s+6); + return(0); + } if (!formatval(fmt, s)) return(0); for (i = 1; i < DTend; i++) if (!strcmp(fmt, cm_fmt_id[i])) - *((int *)p) = i; + ip->dtype = i; return(0); } -/* Load header to obtain data type */ -int -getDTfromHeader(FILE *fp) +/* Load header to obtain/check data type and number of columns */ +char * +cm_getheader(int *dt, int *nr, int *nc, FILE *fp) { - int dt = DTfromHeader; - - if (getheader(fp, getDT, &dt) < 0) - error(SYSTEM, "header read error"); - if (dt == DTfromHeader) - error(USER, "missing data format in header"); - return(dt); + CMINFO cmi; + /* read header */ + cmi.dtype = DTfromHeader; + cmi.nrows = cmi.ncols = 0; + cmi.err = "unexpected EOF in header"; + if (getheader(fp, get_cminfo, &cmi) < 0) + return(cmi.err); + if (dt != NULL) { /* get/check data type? */ + if (cmi.dtype == DTfromHeader) { + if (*dt == DTfromHeader) + return("missing/unknown data format in header"); + } else if (*dt == DTfromHeader) + *dt = cmi.dtype; + else if (*dt != cmi.dtype) + return("unexpected data format in header"); + } + if (nr != NULL) { /* get/check #rows? */ + if (*nr <= 0) + *nr = cmi.nrows; + else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) + return("unexpected row count in header"); + } + if (nc != NULL) { /* get/check #columns? */ + if (*nc <= 0) + *nc = cmi.ncols; + else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) + return("unexpected column count in header"); + } + return(NULL); } -/* Allocate and load a matrix from the given file (or stdin if NULL) */ +/* Allocate and load a matrix from the given input (or stdin if NULL) */ CMATRIX * -cm_load(const char *fname, int nrows, int ncols, int dtype) +cm_load(const char *inspec, int nrows, int ncols, int dtype) { FILE *fp = stdin; CMATRIX *cm; - if (ncols <= 0) - error(USER, "Non-positive number of columns"); - if (fname == NULL) - fname = ""; - else if ((fp = fopen(fname, "r")) == NULL) { - sprintf(errmsg, "cannot open file '%s'", fname); + if (inspec == NULL) + inspec = ""; + else if (inspec[0] == '!') { + fp = popen(inspec+1, "r"); + if (fp == NULL) { + sprintf(errmsg, "cannot start command '%s'", inspec); + error(SYSTEM, errmsg); + } + } else if ((fp = fopen(inspec, "r")) == NULL) { + sprintf(errmsg, "cannot open file '%s'", inspec); error(SYSTEM, errmsg); } #ifdef getc_unlocked @@ -104,8 +167,13 @@ cm_load(const char *fname, int nrows, int ncols, int d #endif if (dtype != DTascii) SET_FILE_BINARY(fp); /* doesn't really work */ - if (dtype == DTfromHeader) - dtype = getDTfromHeader(fp); + if (!dtype | !ncols) { /* expecting header? */ + char *err = cm_getheader(&dtype, &nrows, &ncols, fp); + if (err != NULL) + error(USER, err); + if (ncols <= 0) + error(USER, "unspecified number of columns"); + } switch (dtype) { case DTascii: case DTfloat: @@ -116,7 +184,7 @@ cm_load(const char *fname, int nrows, int ncols, int d } if (nrows <= 0) { /* don't know length? */ int guessrows = 147; /* usually big enough */ - if ((dtype != DTascii) & (fp != stdin)) { + if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { long startpos = ftell(fp); if (fseek(fp, 0L, SEEK_END) == 0) { long endpos = ftell(fp); @@ -126,14 +194,14 @@ cm_load(const char *fname, int nrows, int ncols, int d if ((endpos - startpos) % (ncols*elemsiz)) { sprintf(errmsg, "improper length for binary file '%s'", - fname); + inspec); error(USER, errmsg); } guessrows = (endpos - startpos)/(ncols*elemsiz); if (fseek(fp, startpos, SEEK_SET) < 0) { sprintf(errmsg, "fseek() error on file '%s'", - fname); + inspec); error(SYSTEM, errmsg); } nrows = guessrows; /* we're confident */ @@ -163,8 +231,8 @@ cm_load(const char *fname, int nrows, int ncols, int d while ((c = getc(fp)) != EOF) if (!isspace(c)) { sprintf(errmsg, - "unexpected data at end of ascii file %s", - fname); + "unexpected data at end of ascii input '%s'", + inspec); error(WARNING, errmsg); break; } @@ -173,7 +241,7 @@ cm_load(const char *fname, int nrows, int ncols, int d int nread = 0; do { /* read all we can */ nread += fread(cm->cmem + 3*nread, - 3*sizeof(COLORV), + sizeof(COLOR), cm->nrows*cm->ncols - nread, fp); if (nrows <= 0) { /* unknown length */ @@ -218,20 +286,26 @@ cm_load(const char *fname, int nrows, int ncols, int d } if (fgetc(fp) != EOF) { sprintf(errmsg, - "unexpected data at end of binary file %s", - fname); + "unexpected data at end of binary input '%s'", + inspec); error(WARNING, errmsg); } } - if (fp != stdin) - fclose(fp); + if (fp != stdin) { + if (inspec[0] != '!') + fclose(fp); + else if (pclose(fp)) { + sprintf(errmsg, "error running command '%s'", inspec); + error(WARNING, errmsg); + } + } #ifdef getc_unlocked else funlockfile(fp); #endif return(cm); EOFerror: - sprintf(errmsg, "unexpected EOF reading %s", fname); + sprintf(errmsg, "unexpected EOF reading %s", inspec); error(USER, errmsg); not_handled: error(INTERNAL, "unhandled data size or length in cm_load()"); @@ -315,18 +389,21 @@ cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) for (dr = 0; dr < cmr->nrows; dr++) for (dc = 0; dc < cmr->ncols; dc++) { COLORV *dp = cm_lval(cmr,dr,dc); + double res[3]; dp[0] = dp[1] = dp[2] = 0; if (rowcheck != NULL && !rowcheck[dr]) continue; if (colcheck != NULL && !colcheck[dc]) continue; + res[0] = res[1] = res[2] = 0; for (i = 0; i < cm1->ncols; i++) { const COLORV *cp1 = cm_lval(cm1,dr,i); const COLORV *cp2 = cm_lval(cm2,i,dc); - dp[0] += cp1[0] * cp2[0]; - dp[1] += cp1[1] * cp2[1]; - dp[2] += cp1[2] * cp2[2]; + res[0] += cp1[0] * cp2[0]; + res[1] += cp1[1] * cp2[1]; + res[2] += cp1[2] * cp2[2]; } + copycolor(dp, res); } if (rowcheck != NULL) free(rowcheck); if (colcheck != NULL) free(colcheck);