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.36 by greg, Sun Dec 4 16:58:08 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             dimsOK = (dtype == DTascii) | (nrows > 0) && ncols;
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 | !dimsOK) {                 /* expecting header? */
245                  char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
246                  if (err)
247                          error(USER, err);
248 +                dimsOK = ncols > 0 && ( nrows > 0 ||
249 +                                (dtype != DTrgbe) & (dtype != DTxyze) );
250          }
251 <        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
252 <                error(USER, "unspecified number of columns");
251 >        if (!dimsOK && !fscnresolu(&ncols, &nrows, fp))
252 >                error(USER, "unspecified matrix size");
253          switch (dtype) {
254          case DTascii:
255          case DTfloat:
# Line 309 | Line 318 | cm_load(const char *inspec, int nrows, int ncols, int
318                          }
319          } else {                                        /* read binary file */
320                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
321 <                        int     nread = 0;
321 >                        size_t  nread = 0;
322                          do {                            /* read all we can */
323                                  nread += getbinary(cm->cmem + 3*nread,
324                                                  sizeof(COLOR),
325 <                                                cm->nrows*cm->ncols - nread,
325 >                                                (size_t)cm->nrows*cm->ncols - nread,
326                                                  fp);
327                                  if (nrows <= 0) {       /* unknown length */
328 <                                        if (nread == cm->nrows*cm->ncols)
328 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
329                                                          /* need more space? */
330                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
331                                          else if (nread && !(nread % cm->ncols))
# Line 324 | Line 333 | cm_load(const char *inspec, int nrows, int ncols, int
333                                                  cm = cm_resize(cm, nread/cm->ncols);
334                                          else            /* ended mid-row */
335                                                  goto EOFerror;
336 <                                } else if (nread < cm->nrows*cm->ncols)
336 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
337                                          goto EOFerror;
338 <                        } while (nread < cm->nrows*cm->ncols);
338 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
339  
340                          if (swap) {
341                                  if (sizeof(COLORV) == 4)
342                                          swap32((char *)cm->cmem,
343 <                                                        3*cm->nrows*cm->ncols);
343 >                                                        3*(size_t)cm->nrows*cm->ncols);
344                                  else /* sizeof(COLORV) == 8 */
345                                          swap64((char *)cm->cmem,
346 <                                                        3*cm->nrows*cm->ncols);
346 >                                                        3*(size_t)cm->nrows*cm->ncols);
347                          }
348                  } else if (dtype == DTdouble) {
349                          double  dc[3];                  /* load from double */
350                          COLORV  *cvp = cm->cmem;
351 <                        int     n = nrows*ncols;
351 >                        size_t  n = (size_t)nrows*ncols;
352  
353                          if (n <= 0)
354                                  goto not_handled;
# Line 353 | Line 362 | cm_load(const char *inspec, int nrows, int ncols, int
362                  } else /* dtype == DTfloat */ {
363                          float   fc[3];                  /* load from float */
364                          COLORV  *cvp = cm->cmem;
365 <                        int     n = nrows*ncols;
365 >                        size_t  n = (size_t)nrows*ncols;
366  
367                          if (n <= 0)
368                                  goto not_handled;
# Line 483 | Line 492 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
492          static const char       tabEOL[2] = {'\t','\n'};
493          const COLORV            *mp;
494          int                     r, c;
495 +        size_t                  n, rv;
496  
497          if (!cm)
498                  return(0);
# Line 498 | Line 508 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
508          case DTfloat:
509          case DTdouble:
510                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
511 <                        r = cm->ncols*cm->nrows;
512 <                        while (r > 0) {
513 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
514 <                                if (c <= 0)
511 >                        n = (size_t)cm->ncols*cm->nrows;
512 >                        while (n > 0) {
513 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
514 >                                if (rv <= 0)
515                                          return(0);
516 <                                mp += 3*c;
517 <                                r -= c;
516 >                                mp += 3*rv;
517 >                                n -= rv;
518                          }
519                  } else if (dtype == DTdouble) {
520                          double  dc[3];
521 <                        r = cm->ncols*cm->nrows;
522 <                        while (r--) {
521 >                        n = (size_t)cm->ncols*cm->nrows;
522 >                        while (n--) {
523                                  copycolor(dc, mp);
524                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
525                                          return(0);
# Line 517 | Line 527 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
527                          }
528                  } else /* dtype == DTfloat */ {
529                          float   fc[3];
530 <                        r = cm->ncols*cm->nrows;
531 <                        while (r--) {
530 >                        n = (size_t)cm->ncols*cm->nrows;
531 >                        while (n--) {
532                                  copycolor(fc, mp);
533                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
534                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines