--- ray/src/util/cmatrix.c 2014/05/30 00:00:54 2.5 +++ ray/src/util/cmatrix.c 2016/02/17 23:26:06 2.15 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.5 2014/05/30 00:00:54 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.15 2016/02/17 23:26:06 greg Exp $"; #endif /* * Color matrix routines. @@ -11,11 +11,12 @@ static const char RCSid[] = "$Id: cmatrix.c,v 2.5 2014 #include "standard.h" #include "cmatrix.h" #include "platform.h" +#include "paths.h" #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[] = { @@ -39,20 +40,38 @@ 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) + - sizeof(COLOR)*(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); } @@ -99,7 +118,7 @@ cm_getheader(int *dt, int *nr, int *nc, FILE *fp) cmi.dtype = DTfromHeader; cmi.nrows = cmi.ncols = 0; cmi.err = "unexpected EOF in header"; - if (getheader(fp, &get_cminfo, &cmi) < 0) + if (getheader(fp, get_cminfo, &cmi) < 0) return(cmi.err); if (dt != NULL) { /* get/check data type? */ if (cmi.dtype == DTfromHeader) { @@ -125,17 +144,24 @@ cm_getheader(int *dt, int *nr, int *nc, FILE *fp) 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; + const int ROWINC = 2048; + FILE *fp = stdin; + CMATRIX *cm; - 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 @@ -160,7 +186,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); @@ -170,14 +196,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 */ @@ -193,22 +219,23 @@ cm_load(const char *fname, int nrows, int ncols, int d int r, c; for (r = 0; r < maxrow; r++) { if (r >= cm->nrows) /* need more space? */ - cm = cm_resize(cm, 2*cm->nrows); + cm = cm_resize(cm, cm->nrows+ROWINC); for (c = 0; c < ncols; c++) { COLORV *cv = cm_lval(cm,r,c); - if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) + if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { if ((nrows <= 0) & (r > 0) & !c) { cm = cm_resize(cm, maxrow=r); break; } else goto EOFerror; + } } } 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; } @@ -223,7 +250,7 @@ cm_load(const char *fname, int nrows, int ncols, int d if (nrows <= 0) { /* unknown length */ if (nread == cm->nrows*cm->ncols) /* need more space? */ - cm = cm_resize(cm, 2*cm->nrows); + cm = cm_resize(cm, cm->nrows+ROWINC); else if (nread && !(nread % cm->ncols)) /* seem to be done */ cm = cm_resize(cm, nread/cm->ncols); @@ -262,20 +289,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()");