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.27 by greg, Thu Mar 26 16:56:10 2020 UTC vs.
Revision 2.31 by greg, Fri Jan 15 18:31:38 2021 UTC

# Line 33 | Line 33 | cm_alloc(int nrows, int ncols)
33          if ((nrows <= 0) | (ncols <= 0))
34                  error(USER, "attempt to create empty matrix");
35          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
36 <                                sizeof(COLOR)*(nrows*ncols - 1));
36 >                                sizeof(COLOR)*((size_t)nrows*ncols - 1));
37          if (!cm)
38                  error(SYSTEM, "out of memory in cm_alloc()");
39          cm->nrows = nrows;
# Line 65 | Line 65 | cm_resize(CMATRIX *cm, int nrows)
65                  cm_free(cm);
66                  return(NULL);
67          }
68 <        old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1);
68 >        old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1);
69          adjacent_ra_sizes(ra_bounds, old_size);
70 <        new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1);
70 >        new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1);
71          if (nrows < cm->nrows ? new_size <= ra_bounds[0] :
72                                  new_size > ra_bounds[1]) {
73                  adjacent_ra_sizes(ra_bounds, new_size);
# Line 181 | Line 181 | cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR sca
181          CMATRIX *cm;
182          COLORV  *mp;
183                                                  /* header already loaded */
184        if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) {
185                error(USER, "bad picture resolution string");
186                return(NULL);
187        }
184          cm = cm_alloc(nrows, ncols);
185          if (!cm)
186                  return(NULL);
# Line 242 | Line 238 | cm_load(const char *inspec, int nrows, int ncols, int
238                  char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
239                  if (err)
240                          error(USER, err);
245                if (ncols <= 0)
246                        error(USER, "unspecified number of columns");
241          }
242 +        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
243 +                error(USER, "unspecified number of columns");
244          switch (dtype) {
245          case DTascii:
246          case DTfloat:
# Line 313 | Line 309 | cm_load(const char *inspec, int nrows, int ncols, int
309                          }
310          } else {                                        /* read binary file */
311                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
312 <                        int     nread = 0;
312 >                        size_t  nread = 0;
313                          do {                            /* read all we can */
314                                  nread += getbinary(cm->cmem + 3*nread,
315                                                  sizeof(COLOR),
316 <                                                cm->nrows*cm->ncols - nread,
316 >                                                (size_t)cm->nrows*cm->ncols - nread,
317                                                  fp);
318                                  if (nrows <= 0) {       /* unknown length */
319 <                                        if (nread == cm->nrows*cm->ncols)
319 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
320                                                          /* need more space? */
321                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
322                                          else if (nread && !(nread % cm->ncols))
# Line 328 | Line 324 | cm_load(const char *inspec, int nrows, int ncols, int
324                                                  cm = cm_resize(cm, nread/cm->ncols);
325                                          else            /* ended mid-row */
326                                                  goto EOFerror;
327 <                                } else if (nread < cm->nrows*cm->ncols)
327 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
328                                          goto EOFerror;
329 <                        } while (nread < cm->nrows*cm->ncols);
329 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
330  
331                          if (swap) {
332                                  if (sizeof(COLORV) == 4)
333                                          swap32((char *)cm->cmem,
334 <                                                        3*cm->nrows*cm->ncols);
334 >                                                        3*(size_t)cm->nrows*cm->ncols);
335                                  else /* sizeof(COLORV) == 8 */
336                                          swap64((char *)cm->cmem,
337 <                                                        3*cm->nrows*cm->ncols);
337 >                                                        3*(size_t)cm->nrows*cm->ncols);
338                          }
339                  } else if (dtype == DTdouble) {
340                          double  dc[3];                  /* load from double */
341                          COLORV  *cvp = cm->cmem;
342 <                        int     n = nrows*ncols;
342 >                        size_t  n = (size_t)nrows*ncols;
343  
344                          if (n <= 0)
345                                  goto not_handled;
# Line 357 | Line 353 | cm_load(const char *inspec, int nrows, int ncols, int
353                  } else /* dtype == DTfloat */ {
354                          float   fc[3];                  /* load from float */
355                          COLORV  *cvp = cm->cmem;
356 <                        int     n = nrows*ncols;
356 >                        size_t  n = (size_t)nrows*ncols;
357  
358                          if (n <= 0)
359                                  goto not_handled;
# Line 487 | Line 483 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
483          static const char       tabEOL[2] = {'\t','\n'};
484          const COLORV            *mp;
485          int                     r, c;
486 +        size_t                  n, rv;
487  
488          if (!cm)
489                  return(0);
# Line 502 | Line 499 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
499          case DTfloat:
500          case DTdouble:
501                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
502 <                        r = cm->ncols*cm->nrows;
503 <                        while (r > 0) {
504 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
505 <                                if (c <= 0)
502 >                        n = (size_t)cm->ncols*cm->nrows;
503 >                        while (n > 0) {
504 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
505 >                                if (rv <= 0)
506                                          return(0);
507 <                                mp += 3*c;
508 <                                r -= c;
507 >                                mp += 3*rv;
508 >                                n -= rv;
509                          }
510                  } else if (dtype == DTdouble) {
511                          double  dc[3];
512 <                        r = cm->ncols*cm->nrows;
513 <                        while (r--) {
512 >                        n = (size_t)cm->ncols*cm->nrows;
513 >                        while (n--) {
514                                  copycolor(dc, mp);
515                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
516                                          return(0);
# Line 521 | Line 518 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
518                          }
519                  } else /* dtype == DTfloat */ {
520                          float   fc[3];
521 <                        r = cm->ncols*cm->nrows;
522 <                        while (r--) {
521 >                        n = (size_t)cm->ncols*cm->nrows;
522 >                        while (n--) {
523                                  copycolor(fc, mp);
524                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
525                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines