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.21 by greg, Mon Aug 12 18:28:37 2019 UTC

# 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 121 | Line 123 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
123          cmi.err = "unexpected EOF in header";
124          if (getheader(fp, get_cminfo, &cmi) < 0)
125                  return(cmi.err);
126 <        if (dt != NULL) {                       /* get/check data type? */
126 >        if (dt) {                               /* get/check data type? */
127                  if (cmi.dtype == DTfromHeader) {
128                          if (*dt == DTfromHeader)
129                                  return("missing/unknown data format in header");
# Line 130 | Line 132 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
132                  else if (*dt != cmi.dtype)
133                          return("unexpected data format in header");
134          }
135 <        if (nr != NULL) {                       /* get/check #rows? */
135 >        if (nr) {                               /* get/check #rows? */
136                  if (*nr <= 0)
137                          *nr = cmi.nrows;
138                  else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
139                          return("unexpected row count in header");
140          }
141 <        if (nc != NULL) {                       /* get/check #columns? */
141 >        if (nc) {                               /* get/check #columns? */
142                  if (*nc <= 0)
143                          *nc = cmi.ncols;
144                  else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
# Line 153 | Line 155 | cm_load(const char *inspec, int nrows, int ncols, int
155          FILE            *fp = stdin;
156          CMATRIX         *cm;
157  
158 <        if (inspec == NULL)
158 >        if (!inspec)
159                  inspec = "<stdin>";
160          else if (inspec[0] == '!') {
161                  fp = popen(inspec+1, "r");
162 <                if (fp == NULL) {
162 >                if (!fp) {
163                          sprintf(errmsg, "cannot start command '%s'", inspec);
164                          error(SYSTEM, errmsg);
165                  }
166 <        } else if ((fp = fopen(inspec, "r")) == NULL) {
166 >        } else if (!(fp = fopen(inspec, "r"))) {
167                  sprintf(errmsg, "cannot open file '%s'", inspec);
168                  error(SYSTEM, errmsg);
169          }
# Line 172 | Line 174 | cm_load(const char *inspec, int nrows, int ncols, int
174                  SET_FILE_BINARY(fp);            /* doesn't really work */
175          if (!dtype | !ncols) {                  /* expecting header? */
176                  char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
177 <                if (err != NULL)
177 >                if (err)
178                          error(USER, err);
179                  if (ncols <= 0)
180                          error(USER, "unspecified number of columns");
# Line 213 | Line 215 | cm_load(const char *inspec, int nrows, int ncols, int
215                  cm = cm_alloc(guessrows, ncols);
216          } else
217                  cm = cm_alloc(nrows, ncols);
218 <        if (cm == NULL)                                 /* XXX never happens */
218 >        if (!cm)                                        /* XXX never happens */
219                  return(NULL);
220          if (dtype == DTascii) {                         /* read text file */
221                  int     maxrow = (nrows > 0 ? nrows : 32000);
# Line 323 | Line 325 | cm_column(const CMATRIX *cm, int c)
325          CMATRIX *cvr;
326          int     dr;
327  
328 +        if (!cm)
329 +                return(NULL);
330          if ((c < 0) | (c >= cm->ncols))
331                  error(INTERNAL, "column requested outside matrix");
332          cvr = cm_alloc(cm->nrows, 1);
333 <        if (cvr == NULL)
333 >        if (!cvr)
334                  return(NULL);
335          for (dr = 0; dr < cm->nrows; dr++) {
336                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 338 | Line 342 | cm_column(const CMATRIX *cm, int c)
342          return(cvr);
343   }
344  
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
345   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
346   CMATRIX *
347   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 367 | Line 350 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
350          CMATRIX *cmr;
351          int     dr, dc, i;
352  
353 +        if (!cm1 | !cm2)
354 +                return(NULL);
355          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
356                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
357          cmr = cm_alloc(cm1->nrows, cm2->ncols);
358 <        if (cmr == NULL)
358 >        if (!cmr)
359                  return(NULL);
360                                  /* optimization: check for zero rows & cols */
361          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 395 | Line 380 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
380                  COLORV  *dp = cm_lval(cmr,dr,dc);
381                  double  res[3];
382                  dp[0] = dp[1] = dp[2] = 0;
383 <                if (rowcheck != NULL && !rowcheck[dr])
383 >                if (rowcheck && !rowcheck[dr])
384                          continue;
385 <                if (colcheck != NULL && !colcheck[dc])
385 >                if (colcheck && !colcheck[dc])
386                          continue;
387                  res[0] = res[1] = res[2] = 0;
388                  for (i = 0; i < cm1->ncols; i++) {
# Line 409 | Line 394 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
394                  }
395                  copycolor(dp, res);
396              }
397 <        if (rowcheck != NULL) free(rowcheck);
398 <        if (colcheck != NULL) free(colcheck);
397 >        if (rowcheck) free(rowcheck);
398 >        if (colcheck) free(colcheck);
399          return(cmr);
400   }
401  
# Line 419 | Line 404 | int
404   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
405   {
406          static const char       tabEOL[2] = {'\t','\n'};
407 <        const COLORV            *mp = cm->cmem;
407 >        const COLORV            *mp;
408          int                     r, c;
409  
410 +        if (!cm)
411 +                return(0);
412 +        mp = cm->cmem;
413          switch (dtype) {
414          case DTascii:
415                  for (r = 0; r < cm->nrows; r++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines