ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/cmatrix.c
(Generate patch)

Comparing ray/src/util/cmatrix.c (file contents):
Revision 2.4 by greg, Thu May 29 17:28:09 2014 UTC vs.
Revision 2.9 by greg, Mon May 4 20:53:21 2015 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
14   #include "resolu.h"
15  
16   const char      *cm_fmt_id[] = {
17 <                        "unknown", "ascii", "float", "double",
18 <                        COLRFMT, CIEFMT
17 >                        "unknown", "ascii", COLRFMT, CIEFMT,
18 >                        "float", "double"
19                  };
20  
21   const int       cm_elem_size[] = {
# Line 39 | Line 39 | cm_alloc(int nrows, int ncols)
39          return(cm);
40   }
41  
42 + static void
43 + adjacent_ra_sizes(size_t bounds[2], size_t target)
44 + {
45 +        bounds[0] = 0; bounds[1] = 2048;
46 +        while (bounds[1] < target) {
47 +                bounds[0] = bounds[1];
48 +                bounds[1] += bounds[1]>>1;
49 +        }
50 + }
51 +
52   /* Resize color coefficient matrix */
53   CMATRIX *
54   cm_resize(CMATRIX *cm, int nrows)
55   {
56 +        size_t  old_size, new_size, ra_bounds[2];
57 +
58          if (nrows == cm->nrows)
59                  return(cm);
60          if (nrows <= 0) {
61                  cm_free(cm);
62                  return(NULL);
63          }
64 <        cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) +
65 <                        sizeof(COLOR)*(nrows*cm->ncols - 1));
66 <        if (cm == NULL)
67 <                error(SYSTEM, "out of memory in cm_resize()");
64 >        old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1);
65 >        adjacent_ra_sizes(ra_bounds, old_size);
66 >        new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1);
67 >        if (nrows < cm->nrows ? new_size <= ra_bounds[0] :
68 >                                new_size > ra_bounds[1]) {
69 >                adjacent_ra_sizes(ra_bounds, new_size);
70 >                cm = (CMATRIX *)realloc(cm, ra_bounds[1]);
71 >                if (cm == NULL)
72 >                        error(SYSTEM, "out of memory in cm_resize()");
73 >        }
74          cm->nrows = nrows;
75          return(cm);
76   }
77  
78 + typedef struct {
79 +        int     dtype;          /* data type */
80 +        int     nrows, ncols;   /* matrix size */
81 +        char    *err;           /* error message */
82 + } CMINFO;               /* header info record */
83 +
84   static int
85 < getDT(char *s, void *p)
85 > get_cminfo(char *s, void *p)
86   {
87 +        CMINFO  *ip = (CMINFO *)p;
88          char    fmt[32];
89          int     i;
90 <        
90 >
91 >        if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) {
92 >                ip->err = "unexpected # components (must be 3)";
93 >                return(-1);
94 >        }
95 >        if (!strncmp(s, "NROWS=", 6)) {
96 >                ip->nrows = atoi(s+6);
97 >                return(0);
98 >        }
99 >        if (!strncmp(s, "NCOLS=", 6)) {
100 >                ip->ncols = atoi(s+6);
101 >                return(0);
102 >        }
103          if (!formatval(fmt, s))
104                  return(0);
105          for (i = 1; i < DTend; i++)
106                  if (!strcmp(fmt, cm_fmt_id[i]))
107 <                        *((int *)p) = i;
107 >                        ip->dtype = i;
108          return(0);
109   }
110  
111 < /* Load header to obtain data type */
112 < int
113 < getDTfromHeader(FILE *fp)
111 > /* Load header to obtain/check data type and number of columns */
112 > char *
113 > cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
114   {
115 <        int     dt = DTfromHeader;
116 <        
117 <        if (getheader(fp, getDT, &dt) < 0)
118 <                error(SYSTEM, "header read error");
119 <        if (dt == DTfromHeader)
120 <                error(USER, "missing data format in header");
121 <        return(dt);
115 >        CMINFO  cmi;
116 >                                                /* read header */
117 >        cmi.dtype = DTfromHeader;
118 >        cmi.nrows = cmi.ncols = 0;
119 >        cmi.err = "unexpected EOF in header";
120 >        if (getheader(fp, get_cminfo, &cmi) < 0)
121 >                return(cmi.err);
122 >        if (dt != NULL) {                       /* get/check data type? */
123 >                if (cmi.dtype == DTfromHeader) {
124 >                        if (*dt == DTfromHeader)
125 >                                return("missing/unknown data format in header");
126 >                } else if (*dt == DTfromHeader)
127 >                        *dt = cmi.dtype;
128 >                else if (*dt != cmi.dtype)
129 >                        return("unexpected data format in header");
130 >        }
131 >        if (nr != NULL) {                       /* get/check #rows? */
132 >                if (*nr <= 0)
133 >                        *nr = cmi.nrows;
134 >                else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
135 >                        return("unexpected row count in header");
136 >        }
137 >        if (nc != NULL) {                       /* get/check #columns? */
138 >                if (*nc <= 0)
139 >                        *nc = cmi.ncols;
140 >                else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
141 >                        return("unexpected column count in header");
142 >        }
143 >        return(NULL);
144   }
145  
146 < /* Allocate and load a matrix from the given file (or stdin if NULL) */
146 > /* Allocate and load a matrix from the given input (or stdin if NULL) */
147   CMATRIX *
148 < cm_load(const char *fname, int nrows, int ncols, int dtype)
148 > cm_load(const char *inspec, int nrows, int ncols, int dtype)
149   {
150          FILE    *fp = stdin;
151          CMATRIX *cm;
152  
153 <        if (ncols <= 0)
154 <                error(USER, "Non-positive number of columns");
155 <        if (fname == NULL)
156 <                fname = "<stdin>";
157 <        else if ((fp = fopen(fname, "r")) == NULL) {
158 <                sprintf(errmsg, "cannot open file '%s'", fname);
153 >        if (inspec == NULL)
154 >                inspec = "<stdin>";
155 >        else if (inspec[0] == '!') {
156 >                fp = popen(inspec+1, "r");
157 >                if (fp == NULL) {
158 >                        sprintf(errmsg, "cannot start command '%s'", inspec);
159 >                        error(SYSTEM, errmsg);
160 >                }
161 >        } else if ((fp = fopen(inspec, "r")) == NULL) {
162 >                sprintf(errmsg, "cannot open file '%s'", inspec);
163                  error(SYSTEM, errmsg);
164          }
165   #ifdef getc_unlocked
# Line 104 | Line 167 | cm_load(const char *fname, int nrows, int ncols, int d
167   #endif
168          if (dtype != DTascii)
169                  SET_FILE_BINARY(fp);            /* doesn't really work */
170 <        if (dtype == DTfromHeader)
171 <                dtype = getDTfromHeader(fp);
170 >        if (!dtype | !ncols) {                  /* expecting header? */
171 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
172 >                if (err != NULL)
173 >                        error(USER, err);
174 >                if (ncols <= 0)
175 >                        error(USER, "unspecified number of columns");
176 >        }
177          switch (dtype) {
178          case DTascii:
179          case DTfloat:
# Line 116 | Line 184 | cm_load(const char *fname, int nrows, int ncols, int d
184          }
185          if (nrows <= 0) {                       /* don't know length? */
186                  int     guessrows = 147;        /* usually big enough */
187 <                if ((dtype != DTascii) & (fp != stdin)) {
187 >                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
188                          long    startpos = ftell(fp);
189                          if (fseek(fp, 0L, SEEK_END) == 0) {
190                                  long    endpos = ftell(fp);
# Line 126 | Line 194 | cm_load(const char *fname, int nrows, int ncols, int d
194                                  if ((endpos - startpos) % (ncols*elemsiz)) {
195                                          sprintf(errmsg,
196                                          "improper length for binary file '%s'",
197 <                                                        fname);
197 >                                                        inspec);
198                                          error(USER, errmsg);
199                                  }
200                                  guessrows = (endpos - startpos)/(ncols*elemsiz);
201                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
202                                          sprintf(errmsg,
203                                                  "fseek() error on file '%s'",
204 <                                                        fname);
204 >                                                        inspec);
205                                          error(SYSTEM, errmsg);
206                                  }
207                                  nrows = guessrows;      /* we're confident */
# Line 163 | Line 231 | cm_load(const char *fname, int nrows, int ncols, int d
231                  while ((c = getc(fp)) != EOF)
232                          if (!isspace(c)) {
233                                  sprintf(errmsg,
234 <                                "unexpected data at end of ascii file %s",
235 <                                                fname);
234 >                                "unexpected data at end of ascii input '%s'",
235 >                                                inspec);
236                                  error(WARNING, errmsg);
237                                  break;
238                          }
# Line 218 | Line 286 | cm_load(const char *fname, int nrows, int ncols, int d
286                  }
287                  if (fgetc(fp) != EOF) {
288                                  sprintf(errmsg,
289 <                                "unexpected data at end of binary file %s",
290 <                                                fname);
289 >                                "unexpected data at end of binary input '%s'",
290 >                                                inspec);
291                                  error(WARNING, errmsg);
292                  }
293          }
294 <        if (fp != stdin)
295 <                fclose(fp);
294 >        if (fp != stdin) {
295 >                if (inspec[0] != '!')
296 >                        fclose(fp);
297 >                else if (pclose(fp)) {
298 >                        sprintf(errmsg, "error running command '%s'", inspec);
299 >                        error(WARNING, errmsg);
300 >                }
301 >        }
302   #ifdef getc_unlocked
303          else
304                  funlockfile(fp);
305   #endif
306          return(cm);
307   EOFerror:
308 <        sprintf(errmsg, "unexpected EOF reading %s", fname);
308 >        sprintf(errmsg, "unexpected EOF reading %s", inspec);
309          error(USER, errmsg);
310   not_handled:
311          error(INTERNAL, "unhandled data size or length in cm_load()");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines