--- ray/src/util/cmatrix.c 2014/07/08 16:39:41 2.7 +++ ray/src/util/cmatrix.c 2018/04/10 22:11:30 2.19 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cmatrix.c,v 2.7 2014/07/08 16:39:41 greg Exp $"; +static const char RCSid[] = "$Id: cmatrix.c,v 2.19 2018/04/10 22:11:30 greg Exp $"; #endif /* * Color matrix routines. @@ -8,18 +8,20 @@ static const char RCSid[] = "$Id: cmatrix.c,v 2.7 2014 */ #include +#include "platform.h" #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[] = { - 0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 + 0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) }; /* Allocate a color coefficient matrix */ @@ -143,17 +145,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 @@ -178,7 +187,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); @@ -188,14 +197,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 */ @@ -211,22 +220,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; } @@ -234,14 +244,14 @@ cm_load(const char *fname, int nrows, int ncols, int d if (sizeof(COLOR) == cm_elem_size[dtype]) { int nread = 0; do { /* read all we can */ - nread += fread(cm->cmem + 3*nread, + nread += getbinary(cm->cmem + 3*nread, sizeof(COLOR), cm->nrows*cm->ncols - nread, fp); 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); @@ -259,7 +269,7 @@ cm_load(const char *fname, int nrows, int ncols, int d if (n <= 0) goto not_handled; while (n--) { - if (fread(dc, sizeof(double), 3, fp) != 3) + if (getbinary(dc, sizeof(double), 3, fp) != 3) goto EOFerror; copycolor(cvp, dc); cvp += 3; @@ -272,7 +282,7 @@ cm_load(const char *fname, int nrows, int ncols, int d if (n <= 0) goto not_handled; while (n--) { - if (fread(fc, sizeof(float), 3, fp) != 3) + if (getbinary(fc, sizeof(float), 3, fp) != 3) goto EOFerror; copycolor(cvp, fc); cvp += 3; @@ -280,20 +290,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()"); @@ -387,9 +403,9 @@ cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) for (i = 0; i < cm1->ncols; i++) { const COLORV *cp1 = cm_lval(cm1,dr,i); const COLORV *cp2 = cm_lval(cm2,i,dc); - res[0] += cp1[0] * cp2[0]; - res[1] += cp1[1] * cp2[1]; - res[2] += cp1[2] * cp2[2]; + res[0] += (double)cp1[0] * cp2[0]; + res[1] += (double)cp1[1] * cp2[1]; + res[2] += (double)cp1[2] * cp2[2]; } copycolor(dp, res); } @@ -419,7 +435,7 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) if (sizeof(COLOR) == cm_elem_size[dtype]) { r = cm->ncols*cm->nrows; while (r > 0) { - c = fwrite(mp, sizeof(COLOR), r, fp); + c = putbinary(mp, sizeof(COLOR), r, fp); if (c <= 0) return(0); mp += 3*c; @@ -430,7 +446,7 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) r = cm->ncols*cm->nrows; while (r--) { copycolor(dc, mp); - if (fwrite(dc, sizeof(double), 3, fp) != 3) + if (putbinary(dc, sizeof(double), 3, fp) != 3) return(0); mp += 3; } @@ -439,7 +455,7 @@ cm_write(const CMATRIX *cm, int dtype, FILE *fp) r = cm->ncols*cm->nrows; while (r--) { copycolor(fc, mp); - if (fwrite(fc, sizeof(float), 3, fp) != 3) + if (putbinary(fc, sizeof(float), 3, fp) != 3) return(0); mp += 3; }