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.20 by greg, Thu Aug 2 18:33:49 2018 UTC vs.
Revision 2.24 by greg, Thu Nov 7 23:10:18 2019 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include "resolu.h"
17  
18   const char      *cm_fmt_id[] = {
19 <                        "unknown", "ascii", COLRFMT, CIEFMT,
20 <                        "float", "double"
19 >                        "unknown", COLRFMT, CIEFMT,
20 >                        "float", "ascii", "double"
21                  };
22  
23   const int       cm_elem_size[] = {
# Line 34 | 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 57 | 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 70 | 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 79 | 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 102 | 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 112 | 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 130 | 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 image data into matrix */
159 + static CMATRIX *
160 + cm_load_rgbe(FILE *fp, int nrows, int ncols)
161 + {
162 +        CMATRIX *cm;
163 +        COLORV  *mp;
164 +                                                /* header already loaded */
165 +        if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) {
166 +                error(USER, "bad picture resolution string");
167 +                return(NULL);
168 +        }
169 +        cm = cm_alloc(nrows, ncols);
170 +        if (!cm)
171 +                return(NULL);
172 +        mp = cm->cmem;
173 +        while (nrows--) {
174 +                if (freadscan((COLOR *)mp, ncols, fp) < 0) {
175 +                        error(USER, "error reading color picture as matrix");
176 +                        cm_free(cm);
177 +                        return(NULL);
178 +                }
179 +                mp += 3*ncols;
180 +        }                                       /* caller closes stream */
181 +        return(cm);
182 + }
183 +
184   /* Allocate and load a matrix from the given input (or stdin if NULL) */
185   CMATRIX *
186   cm_load(const char *inspec, int nrows, int ncols, int dtype)
187   {
188          const int       ROWINC = 2048;
189          FILE            *fp = stdin;
190 +        int             swap = 0;
191          CMATRIX         *cm;
192  
193 <        if (inspec == NULL)
193 >        if (!inspec)
194                  inspec = "<stdin>";
195          else if (inspec[0] == '!') {
196                  fp = popen(inspec+1, "r");
197 <                if (fp == NULL) {
197 >                if (!fp) {
198                          sprintf(errmsg, "cannot start command '%s'", inspec);
199                          error(SYSTEM, errmsg);
200                  }
201 <        } else if ((fp = fopen(inspec, "r")) == NULL) {
201 >        } else if (!(fp = fopen(inspec, "r"))) {
202                  sprintf(errmsg, "cannot open file '%s'", inspec);
203                  error(SYSTEM, errmsg);
204          }
# Line 171 | Line 208 | cm_load(const char *inspec, int nrows, int ncols, int
208          if (dtype != DTascii)
209                  SET_FILE_BINARY(fp);            /* doesn't really work */
210          if (!dtype | !ncols) {                  /* expecting header? */
211 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
212 <                if (err != NULL)
211 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp);
212 >                if (err)
213                          error(USER, err);
214                  if (ncols <= 0)
215                          error(USER, "unspecified number of columns");
# Line 182 | Line 219 | cm_load(const char *inspec, int nrows, int ncols, int
219          case DTfloat:
220          case DTdouble:
221                  break;
222 +        case DTrgbe:
223 +        case DTxyze:
224 +                cm = cm_load_rgbe(fp, nrows, ncols);
225 +                goto cleanup;
226          default:
227                  error(USER, "unexpected data type in cm_load()");
228          }
# Line 213 | Line 254 | cm_load(const char *inspec, int nrows, int ncols, int
254                  cm = cm_alloc(guessrows, ncols);
255          } else
256                  cm = cm_alloc(nrows, ncols);
257 <        if (cm == NULL)                                 /* XXX never happens */
257 >        if (!cm)                                        /* XXX never happens */
258                  return(NULL);
259          if (dtype == DTascii) {                         /* read text file */
260                  int     maxrow = (nrows > 0 ? nrows : 32000);
# Line 295 | Line 336 | cm_load(const char *inspec, int nrows, int ncols, int
336                                  error(WARNING, errmsg);
337                  }
338          }
339 +        if (swap) {
340 +                if (dtype == DTfloat)
341 +                        swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols);
342 +                else if (dtype == DTdouble)
343 +                        swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols);
344 +        }
345 + cleanup:
346          if (fp != stdin) {
347                  if (inspec[0] != '!')
348                          fclose(fp);
# Line 311 | Line 359 | cm_load(const char *inspec, int nrows, int ncols, int
359   EOFerror:
360          sprintf(errmsg, "unexpected EOF reading %s", inspec);
361          error(USER, errmsg);
362 +        return(NULL);
363   not_handled:
364          error(INTERNAL, "unhandled data size or length in cm_load()");
365          return(NULL);   /* gratis return */
# Line 323 | Line 372 | cm_column(const CMATRIX *cm, int c)
372          CMATRIX *cvr;
373          int     dr;
374  
375 +        if (!cm)
376 +                return(NULL);
377          if ((c < 0) | (c >= cm->ncols))
378                  error(INTERNAL, "column requested outside matrix");
379          cvr = cm_alloc(cm->nrows, 1);
380 <        if (cvr == NULL)
380 >        if (!cvr)
381                  return(NULL);
382          for (dr = 0; dr < cm->nrows; dr++) {
383                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 338 | Line 389 | cm_column(const CMATRIX *cm, int c)
389          return(cvr);
390   }
391  
341 /* Scale a matrix by a single value */
342 CMATRIX *
343 cm_scale(const CMATRIX *cm1, const COLOR sca)
344 {
345        CMATRIX *cmr;
346        int     dr, dc;
347
348        cmr = cm_alloc(cm1->nrows, cm1->ncols);
349        if (cmr == NULL)
350                return(NULL);
351        for (dr = 0; dr < cmr->nrows; dr++)
352            for (dc = 0; dc < cmr->ncols; dc++) {
353                const COLORV    *sp = cm_lval(cm1,dr,dc);
354                COLORV          *dp = cm_lval(cmr,dr,dc);
355                dp[0] = sp[0] * sca[0];
356                dp[1] = sp[1] * sca[1];
357                dp[2] = sp[2] * sca[2];
358            }
359        return(cmr);
360 }
361
392   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
393   CMATRIX *
394   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 367 | Line 397 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
397          CMATRIX *cmr;
398          int     dr, dc, i;
399  
400 +        if (!cm1 | !cm2)
401 +                return(NULL);
402          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
403                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
404          cmr = cm_alloc(cm1->nrows, cm2->ncols);
405 <        if (cmr == NULL)
405 >        if (!cmr)
406                  return(NULL);
407                                  /* optimization: check for zero rows & cols */
408          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 395 | Line 427 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
427                  COLORV  *dp = cm_lval(cmr,dr,dc);
428                  double  res[3];
429                  dp[0] = dp[1] = dp[2] = 0;
430 <                if (rowcheck != NULL && !rowcheck[dr])
430 >                if (rowcheck && !rowcheck[dr])
431                          continue;
432 <                if (colcheck != NULL && !colcheck[dc])
432 >                if (colcheck && !colcheck[dc])
433                          continue;
434                  res[0] = res[1] = res[2] = 0;
435                  for (i = 0; i < cm1->ncols; i++) {
# Line 409 | Line 441 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
441                  }
442                  copycolor(dp, res);
443              }
444 <        if (rowcheck != NULL) free(rowcheck);
445 <        if (colcheck != NULL) free(colcheck);
444 >        if (rowcheck) free(rowcheck);
445 >        if (colcheck) free(colcheck);
446          return(cmr);
447   }
448  
# Line 419 | Line 451 | int
451   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
452   {
453          static const char       tabEOL[2] = {'\t','\n'};
454 <        const COLORV            *mp = cm->cmem;
454 >        const COLORV            *mp;
455          int                     r, c;
456  
457 +        if (!cm)
458 +                return(0);
459 +        mp = cm->cmem;
460          switch (dtype) {
461          case DTascii:
462                  for (r = 0; r < cm->nrows; r++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines