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.38 by greg, Tue Nov 21 01:30:20 2023 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,
21 >                        "unknown", COLRFMT, CIEFMT, SPECFMT,
22                          "float", "ascii", "double"
23                  };
24  
25   const int       cm_elem_size[] = {
26 <                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
26 >                        0, 4, 4, 0, 3*sizeof(float), 0, 3*sizeof(double)
27                  };
28  
29   /* Allocate a color coefficient matrix */
# 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 175 | Line 177 | cm_getheader(int *dt, int *nr, int *nc, int *swp, COLO
177  
178   /* Allocate and load image data into matrix */
179   static CMATRIX *
180 < cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
180 > cm_load_rgbe(FILE *fp, int nrows, int ncols)
181   {
180        int     doscale;
182          CMATRIX *cm;
183          COLORV  *mp;
184                                                  /* header already loaded */
185          cm = cm_alloc(nrows, ncols);
186          if (!cm)
187                  return(NULL);
187        doscale = (scale[0] < .99) | (scale[0] > 1.01) |
188                        (scale[1] < .99) | (scale[1] > 1.01) |
189                        (scale[2] < .99) | (scale[2] > 1.01) ;
188          mp = cm->cmem;
189          while (nrows--) {
190                  if (freadscan((COLOR *)mp, ncols, fp) < 0) {
# Line 194 | Line 192 | cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR sca
192                          cm_free(cm);
193                          return(NULL);
194                  }
195 <                if (doscale) {
198 <                        int     i = ncols;
199 <                        while (i--) {
200 <                                *mp++ *= scale[0];
201 <                                *mp++ *= scale[1];
202 <                                *mp++ *= scale[2];
203 <                        }
204 <                } else
205 <                        mp += 3*ncols;
195 >                mp += 3*ncols;
196          }                                       /* caller closes stream */
197          return(cm);
198   }
# Line 212 | Line 202 | CMATRIX *
202   cm_load(const char *inspec, int nrows, int ncols, int dtype)
203   {
204          const int       ROWINC = 2048;
205 <        FILE            *fp = stdin;
205 >        int             dimsOK = (dtype == DTascii) | (nrows > 0) && ncols;
206          int             swap = 0;
207 +        FILE            *fp;
208          COLOR           scale;
209          CMATRIX         *cm;
210  
211          if (!inspec)
212 <                inspec = "<stdin>";
213 <        else if (inspec[0] == '!') {
212 >                inspec = stdin_name;
213 >        else if (!*inspec)
214 >                return(NULL);
215 >        if (inspec == stdin_name) {             /* reading from stdin? */
216 >                fp = stdin;
217 >        } else if (inspec[0] == '!') {
218                  fp = popen(inspec+1, "r");
219                  if (!fp) {
220                          sprintf(errmsg, "cannot start command '%s'", inspec);
# Line 234 | Line 229 | cm_load(const char *inspec, int nrows, int ncols, int
229   #endif
230          if (dtype != DTascii)
231                  SET_FILE_BINARY(fp);            /* doesn't really work */
232 <        if (!dtype | !ncols) {                  /* expecting header? */
232 >        if (!dtype | !dimsOK) {                 /* expecting header? */
233                  char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
234                  if (err)
235                          error(USER, err);
236 +                dimsOK = ncols > 0 && ( nrows > 0 ||
237 +                                (dtype != DTrgbe) & (dtype != DTxyze) );
238          }
239 <        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
240 <                error(USER, "unspecified number of columns");
239 >        if (!dimsOK && !fscnresolu(&ncols, &nrows, fp))
240 >                error(USER, "unspecified matrix size");
241          switch (dtype) {
242          case DTascii:
243          case DTfloat:
# Line 248 | Line 245 | cm_load(const char *inspec, int nrows, int ncols, int
245                  break;
246          case DTrgbe:
247          case DTxyze:
248 <                cm = cm_load_rgbe(fp, nrows, ncols, scale);
248 >                cm = cm_load_rgbe(fp, nrows, ncols);
249                  goto cleanup;
250          default:
251                  error(USER, "unexpected data type in cm_load()");
# Line 309 | Line 306 | cm_load(const char *inspec, int nrows, int ncols, int
306                          }
307          } else {                                        /* read binary file */
308                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
309 <                        int     nread = 0;
309 >                        size_t  nread = 0;
310                          do {                            /* read all we can */
311                                  nread += getbinary(cm->cmem + 3*nread,
312                                                  sizeof(COLOR),
313 <                                                cm->nrows*cm->ncols - nread,
313 >                                                (size_t)cm->nrows*cm->ncols - nread,
314                                                  fp);
315                                  if (nrows <= 0) {       /* unknown length */
316 <                                        if (nread == cm->nrows*cm->ncols)
316 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
317                                                          /* need more space? */
318                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
319                                          else if (nread && !(nread % cm->ncols))
# Line 324 | Line 321 | cm_load(const char *inspec, int nrows, int ncols, int
321                                                  cm = cm_resize(cm, nread/cm->ncols);
322                                          else            /* ended mid-row */
323                                                  goto EOFerror;
324 <                                } else if (nread < cm->nrows*cm->ncols)
324 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
325                                          goto EOFerror;
326 <                        } while (nread < cm->nrows*cm->ncols);
326 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
327  
328                          if (swap) {
329                                  if (sizeof(COLORV) == 4)
330                                          swap32((char *)cm->cmem,
331 <                                                        3*cm->nrows*cm->ncols);
331 >                                                        3*(size_t)cm->nrows*cm->ncols);
332                                  else /* sizeof(COLORV) == 8 */
333                                          swap64((char *)cm->cmem,
334 <                                                        3*cm->nrows*cm->ncols);
334 >                                                        3*(size_t)cm->nrows*cm->ncols);
335                          }
336                  } else if (dtype == DTdouble) {
337                          double  dc[3];                  /* load from double */
338                          COLORV  *cvp = cm->cmem;
339 <                        int     n = nrows*ncols;
339 >                        size_t  n = (size_t)nrows*ncols;
340  
341                          if (n <= 0)
342                                  goto not_handled;
# Line 353 | Line 350 | cm_load(const char *inspec, int nrows, int ncols, int
350                  } else /* dtype == DTfloat */ {
351                          float   fc[3];                  /* load from float */
352                          COLORV  *cvp = cm->cmem;
353 <                        int     n = nrows*ncols;
353 >                        size_t  n = (size_t)nrows*ncols;
354  
355                          if (n <= 0)
356                                  goto not_handled;
# Line 385 | Line 382 | cleanup:
382          else
383                  funlockfile(fp);
384   #endif
385 +        if ((scale[0] < .99) | (scale[0] > 1.01) |
386 +                        (scale[1] < .99) | (scale[1] > 1.01) |
387 +                        (scale[2] < .99) | (scale[2] > 1.01)) {
388 +                size_t  n = (size_t)ncols*nrows;
389 +                COLORV  *mp = cm->cmem;
390 +                while (n--) {           /* apply exposure scaling */
391 +                        *mp++ *= scale[0];
392 +                        *mp++ *= scale[1];
393 +                        *mp++ *= scale[2];
394 +                }
395 +        }
396          return(cm);
397   EOFerror:
398          sprintf(errmsg, "unexpected EOF reading %s", inspec);
# 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