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.7 by greg, Tue Jul 8 16:39:41 2014 UTC vs.
Revision 2.22 by greg, Wed Aug 14 18:20:02 2019 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <ctype.h>
11 + #include "platform.h"
12   #include "standard.h"
13   #include "cmatrix.h"
14   #include "platform.h"
15 + #include "paths.h"
16   #include "resolu.h"
17  
18   const char      *cm_fmt_id[] = {
19 <                        "unknown", "ascii", "float", "double",
20 <                        COLRFMT, CIEFMT
19 >                        "unknown", "ascii", COLRFMT, CIEFMT,
20 >                        "float", "double"
21                  };
22  
23   const int       cm_elem_size[] = {
24 <                        0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4
24 >                        0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
25                  };
26  
27   /* Allocate a color coefficient matrix */
# Line 32 | Line 34 | cm_alloc(int nrows, int ncols)
34                  error(USER, "attempt to create empty matrix");
35          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
36                                  sizeof(COLOR)*(nrows*ncols - 1));
37 <        if (cm == NULL)
37 >        if (!cm)
38                  error(SYSTEM, "out of memory in cm_alloc()");
39          cm->nrows = nrows;
40          cm->ncols = ncols;
# Line 55 | Line 57 | cm_resize(CMATRIX *cm, int nrows)
57   {
58          size_t  old_size, new_size, ra_bounds[2];
59  
60 +        if (!cm)
61 +                return(NULL);
62          if (nrows == cm->nrows)
63                  return(cm);
64          if (nrows <= 0) {
# Line 68 | Line 72 | cm_resize(CMATRIX *cm, int nrows)
72                                  new_size > ra_bounds[1]) {
73                  adjacent_ra_sizes(ra_bounds, new_size);
74                  cm = (CMATRIX *)realloc(cm, ra_bounds[1]);
75 <                if (cm == NULL)
75 >                if (!cm)
76                          error(SYSTEM, "out of memory in cm_resize()");
77          }
78          cm->nrows = nrows;
# Line 77 | Line 81 | cm_resize(CMATRIX *cm, int nrows)
81  
82   typedef struct {
83          int     dtype;          /* data type */
84 +        int     need2swap;      /* need byte swap? */
85          int     nrows, ncols;   /* matrix size */
86          char    *err;           /* error message */
87   } CMINFO;               /* header info record */
# Line 85 | Line 90 | static int
90   get_cminfo(char *s, void *p)
91   {
92          CMINFO  *ip = (CMINFO *)p;
93 <        char    fmt[32];
93 >        char    fmt[MAXFMTLEN];
94          int     i;
95  
96          if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) {
# Line 100 | Line 105 | get_cminfo(char *s, void *p)
105                  ip->ncols = atoi(s+6);
106                  return(0);
107          }
108 +        if ((i = isbigendian(s)) >= 0) {
109 +                ip->need2swap = (nativebigendian() != i);
110 +                return(0);
111 +        }
112          if (!formatval(fmt, s))
113                  return(0);
114          for (i = 1; i < DTend; i++)
# Line 110 | Line 119 | get_cminfo(char *s, void *p)
119  
120   /* Load header to obtain/check data type and number of columns */
121   char *
122 < cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
122 > cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp)
123   {
124          CMINFO  cmi;
125                                                  /* read header */
126          cmi.dtype = DTfromHeader;
127 +        cmi.need2swap = 0;
128          cmi.nrows = cmi.ncols = 0;
129          cmi.err = "unexpected EOF in header";
130          if (getheader(fp, get_cminfo, &cmi) < 0)
131                  return(cmi.err);
132 <        if (dt != NULL) {                       /* get/check data type? */
132 >        if (dt) {                               /* get/check data type? */
133                  if (cmi.dtype == DTfromHeader) {
134                          if (*dt == DTfromHeader)
135                                  return("missing/unknown data format in header");
# Line 128 | Line 138 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
138                  else if (*dt != cmi.dtype)
139                          return("unexpected data format in header");
140          }
141 <        if (nr != NULL) {                       /* get/check #rows? */
141 >        if (nr) {                               /* get/check #rows? */
142                  if (*nr <= 0)
143                          *nr = cmi.nrows;
144                  else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
145                          return("unexpected row count in header");
146          }
147 <        if (nc != NULL) {                       /* get/check #columns? */
147 >        if (nc) {                               /* get/check #columns? */
148                  if (*nc <= 0)
149                          *nc = cmi.ncols;
150                  else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
151                          return("unexpected column count in header");
152          }
153 +        if (swp)                                /* get/check swap? */
154 +                *swp = cmi.need2swap;
155          return(NULL);
156   }
157  
158 < /* Allocate and load a matrix from the given file (or stdin if NULL) */
158 > /* Allocate and load a matrix from the given input (or stdin if NULL) */
159   CMATRIX *
160 < cm_load(const char *fname, int nrows, int ncols, int dtype)
160 > cm_load(const char *inspec, int nrows, int ncols, int dtype)
161   {
162 <        FILE    *fp = stdin;
163 <        CMATRIX *cm;
162 >        const int       ROWINC = 2048;
163 >        FILE            *fp = stdin;
164 >        int             swap = 0;
165 >        CMATRIX         *cm;
166  
167 <        if (fname == NULL)
168 <                fname = "<stdin>";
169 <        else if ((fp = fopen(fname, "r")) == NULL) {
170 <                sprintf(errmsg, "cannot open file '%s'", fname);
167 >        if (!inspec)
168 >                inspec = "<stdin>";
169 >        else if (inspec[0] == '!') {
170 >                fp = popen(inspec+1, "r");
171 >                if (!fp) {
172 >                        sprintf(errmsg, "cannot start command '%s'", inspec);
173 >                        error(SYSTEM, errmsg);
174 >                }
175 >        } else if (!(fp = fopen(inspec, "r"))) {
176 >                sprintf(errmsg, "cannot open file '%s'", inspec);
177                  error(SYSTEM, errmsg);
178          }
179   #ifdef getc_unlocked
# Line 162 | Line 182 | cm_load(const char *fname, int nrows, int ncols, int d
182          if (dtype != DTascii)
183                  SET_FILE_BINARY(fp);            /* doesn't really work */
184          if (!dtype | !ncols) {                  /* expecting header? */
185 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
186 <                if (err != NULL)
185 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp);
186 >                if (err)
187                          error(USER, err);
188                  if (ncols <= 0)
189                          error(USER, "unspecified number of columns");
# Line 178 | Line 198 | cm_load(const char *fname, int nrows, int ncols, int d
198          }
199          if (nrows <= 0) {                       /* don't know length? */
200                  int     guessrows = 147;        /* usually big enough */
201 <                if ((dtype != DTascii) & (fp != stdin)) {
201 >                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
202                          long    startpos = ftell(fp);
203                          if (fseek(fp, 0L, SEEK_END) == 0) {
204                                  long    endpos = ftell(fp);
# Line 188 | Line 208 | cm_load(const char *fname, int nrows, int ncols, int d
208                                  if ((endpos - startpos) % (ncols*elemsiz)) {
209                                          sprintf(errmsg,
210                                          "improper length for binary file '%s'",
211 <                                                        fname);
211 >                                                        inspec);
212                                          error(USER, errmsg);
213                                  }
214                                  guessrows = (endpos - startpos)/(ncols*elemsiz);
215                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
216                                          sprintf(errmsg,
217                                                  "fseek() error on file '%s'",
218 <                                                        fname);
218 >                                                        inspec);
219                                          error(SYSTEM, errmsg);
220                                  }
221                                  nrows = guessrows;      /* we're confident */
# Line 204 | Line 224 | cm_load(const char *fname, int nrows, int ncols, int d
224                  cm = cm_alloc(guessrows, ncols);
225          } else
226                  cm = cm_alloc(nrows, ncols);
227 <        if (cm == NULL)                                 /* XXX never happens */
227 >        if (!cm)                                        /* XXX never happens */
228                  return(NULL);
229          if (dtype == DTascii) {                         /* read text file */
230                  int     maxrow = (nrows > 0 ? nrows : 32000);
231                  int     r, c;
232                  for (r = 0; r < maxrow; r++) {
233                      if (r >= cm->nrows)                 /* need more space? */
234 <                        cm = cm_resize(cm, 2*cm->nrows);
234 >                        cm = cm_resize(cm, cm->nrows+ROWINC);
235                      for (c = 0; c < ncols; c++) {
236                          COLORV  *cv = cm_lval(cm,r,c);
237 <                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
237 >                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) {
238                                  if ((nrows <= 0) & (r > 0) & !c) {
239                                          cm = cm_resize(cm, maxrow=r);
240                                          break;
241                                  } else
242                                          goto EOFerror;
243 +                        }
244                      }
245                  }
246                  while ((c = getc(fp)) != EOF)
247                          if (!isspace(c)) {
248                                  sprintf(errmsg,
249 <                                "unexpected data at end of ascii file %s",
250 <                                                fname);
249 >                                "unexpected data at end of ascii input '%s'",
250 >                                                inspec);
251                                  error(WARNING, errmsg);
252                                  break;
253                          }
# Line 234 | Line 255 | cm_load(const char *fname, int nrows, int ncols, int d
255                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
256                          int     nread = 0;
257                          do {                            /* read all we can */
258 <                                nread += fread(cm->cmem + 3*nread,
258 >                                nread += getbinary(cm->cmem + 3*nread,
259                                                  sizeof(COLOR),
260                                                  cm->nrows*cm->ncols - nread,
261                                                  fp);
262                                  if (nrows <= 0) {       /* unknown length */
263                                          if (nread == cm->nrows*cm->ncols)
264                                                          /* need more space? */
265 <                                                cm = cm_resize(cm, 2*cm->nrows);
265 >                                                cm = cm_resize(cm, cm->nrows+ROWINC);
266                                          else if (nread && !(nread % cm->ncols))
267                                                          /* seem to be  done */
268                                                  cm = cm_resize(cm, nread/cm->ncols);
# Line 259 | Line 280 | cm_load(const char *fname, int nrows, int ncols, int d
280                          if (n <= 0)
281                                  goto not_handled;
282                          while (n--) {
283 <                                if (fread(dc, sizeof(double), 3, fp) != 3)
283 >                                if (getbinary(dc, sizeof(double), 3, fp) != 3)
284                                          goto EOFerror;
285                                  copycolor(cvp, dc);
286                                  cvp += 3;
# Line 272 | Line 293 | cm_load(const char *fname, int nrows, int ncols, int d
293                          if (n <= 0)
294                                  goto not_handled;
295                          while (n--) {
296 <                                if (fread(fc, sizeof(float), 3, fp) != 3)
296 >                                if (getbinary(fc, sizeof(float), 3, fp) != 3)
297                                          goto EOFerror;
298                                  copycolor(cvp, fc);
299                                  cvp += 3;
# Line 280 | Line 301 | cm_load(const char *fname, int nrows, int ncols, int d
301                  }
302                  if (fgetc(fp) != EOF) {
303                                  sprintf(errmsg,
304 <                                "unexpected data at end of binary file %s",
305 <                                                fname);
304 >                                "unexpected data at end of binary input '%s'",
305 >                                                inspec);
306                                  error(WARNING, errmsg);
307                  }
308          }
309 <        if (fp != stdin)
310 <                fclose(fp);
309 >        if (swap) {
310 >                if (dtype == DTfloat)
311 >                        swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols);
312 >                else if (dtype == DTdouble)
313 >                        swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols);
314 >        }
315 >        if (fp != stdin) {
316 >                if (inspec[0] != '!')
317 >                        fclose(fp);
318 >                else if (pclose(fp)) {
319 >                        sprintf(errmsg, "error running command '%s'", inspec);
320 >                        error(WARNING, errmsg);
321 >                }
322 >        }
323   #ifdef getc_unlocked
324          else
325                  funlockfile(fp);
326   #endif
327          return(cm);
328   EOFerror:
329 <        sprintf(errmsg, "unexpected EOF reading %s", fname);
329 >        sprintf(errmsg, "unexpected EOF reading %s", inspec);
330          error(USER, errmsg);
331   not_handled:
332          error(INTERNAL, "unhandled data size or length in cm_load()");
# Line 307 | Line 340 | cm_column(const CMATRIX *cm, int c)
340          CMATRIX *cvr;
341          int     dr;
342  
343 +        if (!cm)
344 +                return(NULL);
345          if ((c < 0) | (c >= cm->ncols))
346                  error(INTERNAL, "column requested outside matrix");
347          cvr = cm_alloc(cm->nrows, 1);
348 <        if (cvr == NULL)
348 >        if (!cvr)
349                  return(NULL);
350          for (dr = 0; dr < cm->nrows; dr++) {
351                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 322 | Line 357 | cm_column(const CMATRIX *cm, int c)
357          return(cvr);
358   }
359  
325 /* Scale a matrix by a single value */
326 CMATRIX *
327 cm_scale(const CMATRIX *cm1, const COLOR sca)
328 {
329        CMATRIX *cmr;
330        int     dr, dc;
331
332        cmr = cm_alloc(cm1->nrows, cm1->ncols);
333        if (cmr == NULL)
334                return(NULL);
335        for (dr = 0; dr < cmr->nrows; dr++)
336            for (dc = 0; dc < cmr->ncols; dc++) {
337                const COLORV    *sp = cm_lval(cm1,dr,dc);
338                COLORV          *dp = cm_lval(cmr,dr,dc);
339                dp[0] = sp[0] * sca[0];
340                dp[1] = sp[1] * sca[1];
341                dp[2] = sp[2] * sca[2];
342            }
343        return(cmr);
344 }
345
360   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
361   CMATRIX *
362   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 351 | Line 365 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
365          CMATRIX *cmr;
366          int     dr, dc, i;
367  
368 +        if (!cm1 | !cm2)
369 +                return(NULL);
370          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
371                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
372          cmr = cm_alloc(cm1->nrows, cm2->ncols);
373 <        if (cmr == NULL)
373 >        if (!cmr)
374                  return(NULL);
375                                  /* optimization: check for zero rows & cols */
376          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 379 | Line 395 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
395                  COLORV  *dp = cm_lval(cmr,dr,dc);
396                  double  res[3];
397                  dp[0] = dp[1] = dp[2] = 0;
398 <                if (rowcheck != NULL && !rowcheck[dr])
398 >                if (rowcheck && !rowcheck[dr])
399                          continue;
400 <                if (colcheck != NULL && !colcheck[dc])
400 >                if (colcheck && !colcheck[dc])
401                          continue;
402                  res[0] = res[1] = res[2] = 0;
403                  for (i = 0; i < cm1->ncols; i++) {
404                      const COLORV        *cp1 = cm_lval(cm1,dr,i);
405                      const COLORV        *cp2 = cm_lval(cm2,i,dc);
406 <                    res[0] += cp1[0] * cp2[0];
407 <                    res[1] += cp1[1] * cp2[1];
408 <                    res[2] += cp1[2] * cp2[2];
406 >                    res[0] += (double)cp1[0] * cp2[0];
407 >                    res[1] += (double)cp1[1] * cp2[1];
408 >                    res[2] += (double)cp1[2] * cp2[2];
409                  }
410                  copycolor(dp, res);
411              }
412 <        if (rowcheck != NULL) free(rowcheck);
413 <        if (colcheck != NULL) free(colcheck);
412 >        if (rowcheck) free(rowcheck);
413 >        if (colcheck) free(colcheck);
414          return(cmr);
415   }
416  
# Line 403 | Line 419 | int
419   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
420   {
421          static const char       tabEOL[2] = {'\t','\n'};
422 <        const COLORV            *mp = cm->cmem;
422 >        const COLORV            *mp;
423          int                     r, c;
424  
425 +        if (!cm)
426 +                return(0);
427 +        mp = cm->cmem;
428          switch (dtype) {
429          case DTascii:
430                  for (r = 0; r < cm->nrows; r++)
# Line 419 | Line 438 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
438                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
439                          r = cm->ncols*cm->nrows;
440                          while (r > 0) {
441 <                                c = fwrite(mp, sizeof(COLOR), r, fp);
441 >                                c = putbinary(mp, sizeof(COLOR), r, fp);
442                                  if (c <= 0)
443                                          return(0);
444                                  mp += 3*c;
# Line 430 | Line 449 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
449                          r = cm->ncols*cm->nrows;
450                          while (r--) {
451                                  copycolor(dc, mp);
452 <                                if (fwrite(dc, sizeof(double), 3, fp) != 3)
452 >                                if (putbinary(dc, sizeof(double), 3, fp) != 3)
453                                          return(0);
454                                  mp += 3;
455                          }
# Line 439 | Line 458 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
458                          r = cm->ncols*cm->nrows;
459                          while (r--) {
460                                  copycolor(fc, mp);
461 <                                if (fwrite(fc, sizeof(float), 3, fp) != 3)
461 >                                if (putbinary(fc, sizeof(float), 3, fp) != 3)
462                                          return(0);
463                                  mp += 3;
464                          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines