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.29 by greg, Fri Jan 15 02:46:28 2021 UTC vs.
Revision 2.35 by greg, Wed Apr 27 01:31:56 2022 UTC

# Line 15 | Line 15 | static const char RCSid[] = "$Id$";
15   #include "paths.h"
16   #include "resolu.h"
17  
18 + const char      stdin_name[] = "<stdin>";
19 +
20   const char      *cm_fmt_id[] = {
21                          "unknown", COLRFMT, CIEFMT,
22                          "float", "ascii", "double"
# Line 65 | Line 67 | cm_resize(CMATRIX *cm, int nrows)
67                  cm_free(cm);
68                  return(NULL);
69          }
70 <        old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1);
70 >        old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1);
71          adjacent_ra_sizes(ra_bounds, old_size);
72 <        new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1);
72 >        new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1);
73          if (nrows < cm->nrows ? new_size <= ra_bounds[0] :
74                                  new_size > ra_bounds[1]) {
75                  adjacent_ra_sizes(ra_bounds, new_size);
# Line 212 | Line 214 | CMATRIX *
214   cm_load(const char *inspec, int nrows, int ncols, int dtype)
215   {
216          const int       ROWINC = 2048;
217 <        FILE            *fp = stdin;
217 >        int             rowsOK = (dtype == DTascii) | (nrows > 0);
218          int             swap = 0;
219 +        FILE            *fp;
220          COLOR           scale;
221          CMATRIX         *cm;
222  
223          if (!inspec)
224 <                inspec = "<stdin>";
225 <        else if (inspec[0] == '!') {
224 >                inspec = stdin_name;
225 >        else if (!*inspec)
226 >                return(NULL);
227 >        if (inspec == stdin_name) {             /* reading from stdin? */
228 >                fp = stdin;
229 >        } else if (inspec[0] == '!') {
230                  fp = popen(inspec+1, "r");
231                  if (!fp) {
232                          sprintf(errmsg, "cannot start command '%s'", inspec);
# Line 234 | Line 241 | cm_load(const char *inspec, int nrows, int ncols, int
241   #endif
242          if (dtype != DTascii)
243                  SET_FILE_BINARY(fp);            /* doesn't really work */
244 <        if (!dtype | !ncols) {                  /* expecting header? */
244 >        if (!dtype | !rowsOK | !ncols) {        /* expecting header? */
245                  char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
246                  if (err)
247                          error(USER, err);
248 +                rowsOK = (nrows > 0);
249          }
250 <        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
251 <                error(USER, "unspecified number of columns");
250 >        if (!rowsOK | !ncols && !fscnresolu(&ncols, &nrows, fp))
251 >                error(USER, "unspecified matrix size");
252          switch (dtype) {
253          case DTascii:
254          case DTfloat:
# Line 309 | Line 317 | cm_load(const char *inspec, int nrows, int ncols, int
317                          }
318          } else {                                        /* read binary file */
319                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
320 <                        int     nread = 0;
320 >                        size_t  nread = 0;
321                          do {                            /* read all we can */
322                                  nread += getbinary(cm->cmem + 3*nread,
323                                                  sizeof(COLOR),
324 <                                                cm->nrows*cm->ncols - nread,
324 >                                                (size_t)cm->nrows*cm->ncols - nread,
325                                                  fp);
326                                  if (nrows <= 0) {       /* unknown length */
327 <                                        if (nread == cm->nrows*cm->ncols)
327 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
328                                                          /* need more space? */
329                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
330                                          else if (nread && !(nread % cm->ncols))
# Line 324 | Line 332 | cm_load(const char *inspec, int nrows, int ncols, int
332                                                  cm = cm_resize(cm, nread/cm->ncols);
333                                          else            /* ended mid-row */
334                                                  goto EOFerror;
335 <                                } else if (nread < cm->nrows*cm->ncols)
335 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
336                                          goto EOFerror;
337 <                        } while (nread < cm->nrows*cm->ncols);
337 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
338  
339                          if (swap) {
340                                  if (sizeof(COLORV) == 4)
341                                          swap32((char *)cm->cmem,
342 <                                                        3*cm->nrows*cm->ncols);
342 >                                                        3*(size_t)cm->nrows*cm->ncols);
343                                  else /* sizeof(COLORV) == 8 */
344                                          swap64((char *)cm->cmem,
345 <                                                        3*cm->nrows*cm->ncols);
345 >                                                        3*(size_t)cm->nrows*cm->ncols);
346                          }
347                  } else if (dtype == DTdouble) {
348                          double  dc[3];                  /* load from double */
349                          COLORV  *cvp = cm->cmem;
350 <                        int     n = nrows*ncols;
350 >                        size_t  n = (size_t)nrows*ncols;
351  
352                          if (n <= 0)
353                                  goto not_handled;
# Line 353 | Line 361 | cm_load(const char *inspec, int nrows, int ncols, int
361                  } else /* dtype == DTfloat */ {
362                          float   fc[3];                  /* load from float */
363                          COLORV  *cvp = cm->cmem;
364 <                        int     n = nrows*ncols;
364 >                        size_t  n = (size_t)nrows*ncols;
365  
366                          if (n <= 0)
367                                  goto not_handled;
# Line 483 | Line 491 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
491          static const char       tabEOL[2] = {'\t','\n'};
492          const COLORV            *mp;
493          int                     r, c;
494 +        size_t                  n, rv;
495  
496          if (!cm)
497                  return(0);
# Line 498 | Line 507 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
507          case DTfloat:
508          case DTdouble:
509                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
510 <                        r = cm->ncols*cm->nrows;
511 <                        while (r > 0) {
512 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
513 <                                if (c <= 0)
510 >                        n = (size_t)cm->ncols*cm->nrows;
511 >                        while (n > 0) {
512 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
513 >                                if (rv <= 0)
514                                          return(0);
515 <                                mp += 3*c;
516 <                                r -= c;
515 >                                mp += 3*rv;
516 >                                n -= rv;
517                          }
518                  } else if (dtype == DTdouble) {
519                          double  dc[3];
520 <                        r = cm->ncols*cm->nrows;
521 <                        while (r--) {
520 >                        n = (size_t)cm->ncols*cm->nrows;
521 >                        while (n--) {
522                                  copycolor(dc, mp);
523                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
524                                          return(0);
# Line 517 | Line 526 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
526                          }
527                  } else /* dtype == DTfloat */ {
528                          float   fc[3];
529 <                        r = cm->ncols*cm->nrows;
530 <                        while (r--) {
529 >                        n = (size_t)cm->ncols*cm->nrows;
530 >                        while (n--) {
531                                  copycolor(fc, mp);
532                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
533                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines