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.25 by greg, Wed Mar 25 01:51:09 2020 UTC vs.
Revision 2.29 by greg, Fri Jan 15 02:46:28 2021 UTC

# Line 21 | Line 21 | const char     *cm_fmt_id[] = {
21                  };
22  
23   const int       cm_elem_size[] = {
24 <                        0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
24 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
25                  };
26  
27   /* Allocate a color coefficient matrix */
# 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 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 259 | Line 255 | cm_load(const char *inspec, int nrows, int ncols, int
255          }
256          if (nrows <= 0) {                       /* don't know length? */
257                  int     guessrows = 147;        /* usually big enough */
258 <                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
258 >                if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) {
259                          long    startpos = ftell(fp);
260                          if (fseek(fp, 0L, SEEK_END) == 0) {
261 +                                long    rowsiz = (long)ncols*cm_elem_size[dtype];
262                                  long    endpos = ftell(fp);
266                                long    elemsiz = 3*(dtype==DTfloat ?
267                                            sizeof(float) : sizeof(double));
263  
264 <                                if ((endpos - startpos) % (ncols*elemsiz)) {
264 >                                if ((endpos - startpos) % rowsiz) {
265                                          sprintf(errmsg,
266                                          "improper length for binary file '%s'",
267                                                          inspec);
268                                          error(USER, errmsg);
269                                  }
270 <                                guessrows = (endpos - startpos)/(ncols*elemsiz);
270 >                                guessrows = (endpos - startpos)/rowsiz;
271                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
272                                          sprintf(errmsg,
273                                                  "fseek() error on file '%s'",
# Line 333 | Line 328 | cm_load(const char *inspec, int nrows, int ncols, int
328                                          goto EOFerror;
329                          } while (nread < cm->nrows*cm->ncols);
330  
331 +                        if (swap) {
332 +                                if (sizeof(COLORV) == 4)
333 +                                        swap32((char *)cm->cmem,
334 +                                                        3*cm->nrows*cm->ncols);
335 +                                else /* sizeof(COLORV) == 8 */
336 +                                        swap64((char *)cm->cmem,
337 +                                                        3*cm->nrows*cm->ncols);
338 +                        }
339                  } else if (dtype == DTdouble) {
340                          double  dc[3];                  /* load from double */
341                          COLORV  *cvp = cm->cmem;
# Line 343 | Line 346 | cm_load(const char *inspec, int nrows, int ncols, int
346                          while (n--) {
347                                  if (getbinary(dc, sizeof(double), 3, fp) != 3)
348                                          goto EOFerror;
349 +                                if (swap) swap64((char *)dc, 3);
350                                  copycolor(cvp, dc);
351                                  cvp += 3;
352                          }
# Line 356 | Line 360 | cm_load(const char *inspec, int nrows, int ncols, int
360                          while (n--) {
361                                  if (getbinary(fc, sizeof(float), 3, fp) != 3)
362                                          goto EOFerror;
363 +                                if (swap) swap32((char *)fc, 3);
364                                  copycolor(cvp, fc);
365                                  cvp += 3;
366                          }
# Line 366 | Line 371 | cm_load(const char *inspec, int nrows, int ncols, int
371                                                  inspec);
372                                  error(WARNING, errmsg);
373                  }
369        }
370        if (swap) {
371                if (dtype == DTfloat)
372                        swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols);
373                else if (dtype == DTdouble)
374                        swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols);
374          }
375   cleanup:
376          if (fp != stdin) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines