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.20 by greg, Thu Aug 2 18:33:49 2018 UTC vs.
Revision 2.32 by greg, Tue Jan 19 23:32:00 2021 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", "ascii", COLRFMT, CIEFMT,
22 <                        "float", "double"
21 >                        "unknown", COLRFMT, CIEFMT,
22 >                        "float", "ascii", "double"
23                  };
24  
25   const int       cm_elem_size[] = {
26 <                        0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
26 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
27                  };
28  
29   /* Allocate a color coefficient matrix */
# Line 33 | Line 35 | cm_alloc(int nrows, int ncols)
35          if ((nrows <= 0) | (ncols <= 0))
36                  error(USER, "attempt to create empty matrix");
37          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
38 <                                sizeof(COLOR)*(nrows*ncols - 1));
39 <        if (cm == NULL)
38 >                                sizeof(COLOR)*((size_t)nrows*ncols - 1));
39 >        if (!cm)
40                  error(SYSTEM, "out of memory in cm_alloc()");
41          cm->nrows = nrows;
42          cm->ncols = ncols;
# Line 57 | Line 59 | cm_resize(CMATRIX *cm, int nrows)
59   {
60          size_t  old_size, new_size, ra_bounds[2];
61  
62 +        if (!cm)
63 +                return(NULL);
64          if (nrows == cm->nrows)
65                  return(cm);
66          if (nrows <= 0) {
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);
76                  cm = (CMATRIX *)realloc(cm, ra_bounds[1]);
77 <                if (cm == NULL)
77 >                if (!cm)
78                          error(SYSTEM, "out of memory in cm_resize()");
79          }
80          cm->nrows = nrows;
# Line 79 | Line 83 | cm_resize(CMATRIX *cm, int nrows)
83  
84   typedef struct {
85          int     dtype;          /* data type */
86 +        int     need2swap;      /* need byte swap? */
87          int     nrows, ncols;   /* matrix size */
88 +        COLOR   expos;          /* exposure value */
89          char    *err;           /* error message */
90   } CMINFO;               /* header info record */
91  
# Line 102 | Line 108 | get_cminfo(char *s, void *p)
108                  ip->ncols = atoi(s+6);
109                  return(0);
110          }
111 +        if ((i = isbigendian(s)) >= 0) {
112 +                ip->need2swap = (nativebigendian() != i);
113 +                return(0);
114 +        }
115 +        if (isexpos(s)) {
116 +                double  d = exposval(s);
117 +                scalecolor(ip->expos, d);
118 +                return(0);
119 +        }
120 +        if (iscolcor(s)) {
121 +                COLOR   ctmp;
122 +                colcorval(ctmp, s);
123 +                multcolor(ip->expos, ctmp);
124 +                return(0);
125 +        }
126          if (!formatval(fmt, s))
127                  return(0);
128          for (i = 1; i < DTend; i++)
# Line 112 | Line 133 | get_cminfo(char *s, void *p)
133  
134   /* Load header to obtain/check data type and number of columns */
135   char *
136 < cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
136 > cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp)
137   {
138          CMINFO  cmi;
139                                                  /* read header */
140          cmi.dtype = DTfromHeader;
141 +        cmi.need2swap = 0;
142 +        cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f;
143          cmi.nrows = cmi.ncols = 0;
144          cmi.err = "unexpected EOF in header";
145          if (getheader(fp, get_cminfo, &cmi) < 0)
146                  return(cmi.err);
147 <        if (dt != NULL) {                       /* get/check data type? */
147 >        if (dt) {                               /* get/check data type? */
148                  if (cmi.dtype == DTfromHeader) {
149                          if (*dt == DTfromHeader)
150                                  return("missing/unknown data format in header");
# Line 130 | Line 153 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
153                  else if (*dt != cmi.dtype)
154                          return("unexpected data format in header");
155          }
156 <        if (nr != NULL) {                       /* get/check #rows? */
156 >        if (nr) {                               /* get/check #rows? */
157                  if (*nr <= 0)
158                          *nr = cmi.nrows;
159                  else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
160                          return("unexpected row count in header");
161          }
162 <        if (nc != NULL) {                       /* get/check #columns? */
162 >        if (nc) {                               /* get/check #columns? */
163                  if (*nc <= 0)
164                          *nc = cmi.ncols;
165                  else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
166                          return("unexpected column count in header");
167          }
168 +        if (swp)                                /* get/check swap? */
169 +                *swp = cmi.need2swap;
170 +        if (scale) {                            /* transfer exposure comp. */
171 +                scale[0] = 1.f/cmi.expos[0];
172 +                scale[1] = 1.f/cmi.expos[1];
173 +                scale[2] = 1.f/cmi.expos[2];
174 +        }
175          return(NULL);
176   }
177  
178 + /* Allocate and load image data into matrix */
179 + static CMATRIX *
180 + cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
181 + {
182 +        int     doscale;
183 +        CMATRIX *cm;
184 +        COLORV  *mp;
185 +                                                /* header already loaded */
186 +        cm = cm_alloc(nrows, ncols);
187 +        if (!cm)
188 +                return(NULL);
189 +        doscale = (scale[0] < .99) | (scale[0] > 1.01) |
190 +                        (scale[1] < .99) | (scale[1] > 1.01) |
191 +                        (scale[2] < .99) | (scale[2] > 1.01) ;
192 +        mp = cm->cmem;
193 +        while (nrows--) {
194 +                if (freadscan((COLOR *)mp, ncols, fp) < 0) {
195 +                        error(USER, "error reading color picture as matrix");
196 +                        cm_free(cm);
197 +                        return(NULL);
198 +                }
199 +                if (doscale) {
200 +                        int     i = ncols;
201 +                        while (i--) {
202 +                                *mp++ *= scale[0];
203 +                                *mp++ *= scale[1];
204 +                                *mp++ *= scale[2];
205 +                        }
206 +                } else
207 +                        mp += 3*ncols;
208 +        }                                       /* caller closes stream */
209 +        return(cm);
210 + }
211 +
212   /* Allocate and load a matrix from the given input (or stdin if NULL) */
213   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             swap = 0;
218 >        FILE            *fp;
219 >        COLOR           scale;
220          CMATRIX         *cm;
221  
222 <        if (inspec == NULL)
223 <                inspec = "<stdin>";
222 >        if (!inspec || !*inspec)
223 >                return(NULL);
224 >        if (inspec == stdin_name)
225 >                fp = stdin;
226          else if (inspec[0] == '!') {
227                  fp = popen(inspec+1, "r");
228 <                if (fp == NULL) {
228 >                if (!fp) {
229                          sprintf(errmsg, "cannot start command '%s'", inspec);
230                          error(SYSTEM, errmsg);
231                  }
232 <        } else if ((fp = fopen(inspec, "r")) == NULL) {
232 >        } else if (!(fp = fopen(inspec, "r"))) {
233                  sprintf(errmsg, "cannot open file '%s'", inspec);
234                  error(SYSTEM, errmsg);
235          }
# Line 171 | Line 239 | cm_load(const char *inspec, int nrows, int ncols, int
239          if (dtype != DTascii)
240                  SET_FILE_BINARY(fp);            /* doesn't really work */
241          if (!dtype | !ncols) {                  /* expecting header? */
242 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
243 <                if (err != NULL)
242 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
243 >                if (err)
244                          error(USER, err);
177                if (ncols <= 0)
178                        error(USER, "unspecified number of columns");
245          }
246 +        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
247 +                error(USER, "unspecified number of columns");
248          switch (dtype) {
249          case DTascii:
250          case DTfloat:
251          case DTdouble:
252                  break;
253 +        case DTrgbe:
254 +        case DTxyze:
255 +                cm = cm_load_rgbe(fp, nrows, ncols, scale);
256 +                goto cleanup;
257          default:
258                  error(USER, "unexpected data type in cm_load()");
259          }
260          if (nrows <= 0) {                       /* don't know length? */
261                  int     guessrows = 147;        /* usually big enough */
262 <                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
262 >                if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) {
263                          long    startpos = ftell(fp);
264                          if (fseek(fp, 0L, SEEK_END) == 0) {
265 +                                long    rowsiz = (long)ncols*cm_elem_size[dtype];
266                                  long    endpos = ftell(fp);
194                                long    elemsiz = 3*(dtype==DTfloat ?
195                                            sizeof(float) : sizeof(double));
267  
268 <                                if ((endpos - startpos) % (ncols*elemsiz)) {
268 >                                if ((endpos - startpos) % rowsiz) {
269                                          sprintf(errmsg,
270                                          "improper length for binary file '%s'",
271                                                          inspec);
272                                          error(USER, errmsg);
273                                  }
274 <                                guessrows = (endpos - startpos)/(ncols*elemsiz);
274 >                                guessrows = (endpos - startpos)/rowsiz;
275                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
276                                          sprintf(errmsg,
277                                                  "fseek() error on file '%s'",
# Line 213 | Line 284 | cm_load(const char *inspec, int nrows, int ncols, int
284                  cm = cm_alloc(guessrows, ncols);
285          } else
286                  cm = cm_alloc(nrows, ncols);
287 <        if (cm == NULL)                                 /* XXX never happens */
287 >        if (!cm)                                        /* XXX never happens */
288                  return(NULL);
289          if (dtype == DTascii) {                         /* read text file */
290                  int     maxrow = (nrows > 0 ? nrows : 32000);
# Line 242 | Line 313 | cm_load(const char *inspec, int nrows, int ncols, int
313                          }
314          } else {                                        /* read binary file */
315                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
316 <                        int     nread = 0;
316 >                        size_t  nread = 0;
317                          do {                            /* read all we can */
318                                  nread += getbinary(cm->cmem + 3*nread,
319                                                  sizeof(COLOR),
320 <                                                cm->nrows*cm->ncols - nread,
320 >                                                (size_t)cm->nrows*cm->ncols - nread,
321                                                  fp);
322                                  if (nrows <= 0) {       /* unknown length */
323 <                                        if (nread == cm->nrows*cm->ncols)
323 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
324                                                          /* need more space? */
325                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
326                                          else if (nread && !(nread % cm->ncols))
# Line 257 | Line 328 | cm_load(const char *inspec, int nrows, int ncols, int
328                                                  cm = cm_resize(cm, nread/cm->ncols);
329                                          else            /* ended mid-row */
330                                                  goto EOFerror;
331 <                                } else if (nread < cm->nrows*cm->ncols)
331 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
332                                          goto EOFerror;
333 <                        } while (nread < cm->nrows*cm->ncols);
333 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
334  
335 +                        if (swap) {
336 +                                if (sizeof(COLORV) == 4)
337 +                                        swap32((char *)cm->cmem,
338 +                                                        3*(size_t)cm->nrows*cm->ncols);
339 +                                else /* sizeof(COLORV) == 8 */
340 +                                        swap64((char *)cm->cmem,
341 +                                                        3*(size_t)cm->nrows*cm->ncols);
342 +                        }
343                  } else if (dtype == DTdouble) {
344                          double  dc[3];                  /* load from double */
345                          COLORV  *cvp = cm->cmem;
346 <                        int     n = nrows*ncols;
346 >                        size_t  n = (size_t)nrows*ncols;
347  
348                          if (n <= 0)
349                                  goto not_handled;
350                          while (n--) {
351                                  if (getbinary(dc, sizeof(double), 3, fp) != 3)
352                                          goto EOFerror;
353 +                                if (swap) swap64((char *)dc, 3);
354                                  copycolor(cvp, dc);
355                                  cvp += 3;
356                          }
357                  } else /* dtype == DTfloat */ {
358                          float   fc[3];                  /* load from float */
359                          COLORV  *cvp = cm->cmem;
360 <                        int     n = nrows*ncols;
360 >                        size_t  n = (size_t)nrows*ncols;
361  
362                          if (n <= 0)
363                                  goto not_handled;
364                          while (n--) {
365                                  if (getbinary(fc, sizeof(float), 3, fp) != 3)
366                                          goto EOFerror;
367 +                                if (swap) swap32((char *)fc, 3);
368                                  copycolor(cvp, fc);
369                                  cvp += 3;
370                          }
# Line 295 | Line 376 | cm_load(const char *inspec, int nrows, int ncols, int
376                                  error(WARNING, errmsg);
377                  }
378          }
379 + cleanup:
380          if (fp != stdin) {
381                  if (inspec[0] != '!')
382                          fclose(fp);
# Line 311 | Line 393 | cm_load(const char *inspec, int nrows, int ncols, int
393   EOFerror:
394          sprintf(errmsg, "unexpected EOF reading %s", inspec);
395          error(USER, errmsg);
396 +        return(NULL);
397   not_handled:
398          error(INTERNAL, "unhandled data size or length in cm_load()");
399          return(NULL);   /* gratis return */
# Line 323 | Line 406 | cm_column(const CMATRIX *cm, int c)
406          CMATRIX *cvr;
407          int     dr;
408  
409 +        if (!cm)
410 +                return(NULL);
411          if ((c < 0) | (c >= cm->ncols))
412                  error(INTERNAL, "column requested outside matrix");
413          cvr = cm_alloc(cm->nrows, 1);
414 <        if (cvr == NULL)
414 >        if (!cvr)
415                  return(NULL);
416          for (dr = 0; dr < cm->nrows; dr++) {
417                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 338 | Line 423 | cm_column(const CMATRIX *cm, int c)
423          return(cvr);
424   }
425  
341 /* Scale a matrix by a single value */
342 CMATRIX *
343 cm_scale(const CMATRIX *cm1, const COLOR sca)
344 {
345        CMATRIX *cmr;
346        int     dr, dc;
347
348        cmr = cm_alloc(cm1->nrows, cm1->ncols);
349        if (cmr == NULL)
350                return(NULL);
351        for (dr = 0; dr < cmr->nrows; dr++)
352            for (dc = 0; dc < cmr->ncols; dc++) {
353                const COLORV    *sp = cm_lval(cm1,dr,dc);
354                COLORV          *dp = cm_lval(cmr,dr,dc);
355                dp[0] = sp[0] * sca[0];
356                dp[1] = sp[1] * sca[1];
357                dp[2] = sp[2] * sca[2];
358            }
359        return(cmr);
360 }
361
426   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
427   CMATRIX *
428   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 367 | Line 431 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
431          CMATRIX *cmr;
432          int     dr, dc, i;
433  
434 +        if (!cm1 | !cm2)
435 +                return(NULL);
436          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
437                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
438          cmr = cm_alloc(cm1->nrows, cm2->ncols);
439 <        if (cmr == NULL)
439 >        if (!cmr)
440                  return(NULL);
441                                  /* optimization: check for zero rows & cols */
442          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 395 | Line 461 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
461                  COLORV  *dp = cm_lval(cmr,dr,dc);
462                  double  res[3];
463                  dp[0] = dp[1] = dp[2] = 0;
464 <                if (rowcheck != NULL && !rowcheck[dr])
464 >                if (rowcheck && !rowcheck[dr])
465                          continue;
466 <                if (colcheck != NULL && !colcheck[dc])
466 >                if (colcheck && !colcheck[dc])
467                          continue;
468                  res[0] = res[1] = res[2] = 0;
469                  for (i = 0; i < cm1->ncols; i++) {
# Line 409 | Line 475 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
475                  }
476                  copycolor(dp, res);
477              }
478 <        if (rowcheck != NULL) free(rowcheck);
479 <        if (colcheck != NULL) free(colcheck);
478 >        if (rowcheck) free(rowcheck);
479 >        if (colcheck) free(colcheck);
480          return(cmr);
481   }
482  
# Line 419 | Line 485 | int
485   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
486   {
487          static const char       tabEOL[2] = {'\t','\n'};
488 <        const COLORV            *mp = cm->cmem;
488 >        const COLORV            *mp;
489          int                     r, c;
490 +        size_t                  n, rv;
491  
492 +        if (!cm)
493 +                return(0);
494 +        mp = cm->cmem;
495          switch (dtype) {
496          case DTascii:
497                  for (r = 0; r < cm->nrows; r++)
# Line 433 | Line 503 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
503          case DTfloat:
504          case DTdouble:
505                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
506 <                        r = cm->ncols*cm->nrows;
507 <                        while (r > 0) {
508 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
509 <                                if (c <= 0)
506 >                        n = (size_t)cm->ncols*cm->nrows;
507 >                        while (n > 0) {
508 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
509 >                                if (rv <= 0)
510                                          return(0);
511 <                                mp += 3*c;
512 <                                r -= c;
511 >                                mp += 3*rv;
512 >                                n -= rv;
513                          }
514                  } else if (dtype == DTdouble) {
515                          double  dc[3];
516 <                        r = cm->ncols*cm->nrows;
517 <                        while (r--) {
516 >                        n = (size_t)cm->ncols*cm->nrows;
517 >                        while (n--) {
518                                  copycolor(dc, mp);
519                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
520                                          return(0);
# Line 452 | Line 522 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
522                          }
523                  } else /* dtype == DTfloat */ {
524                          float   fc[3];
525 <                        r = cm->ncols*cm->nrows;
526 <                        while (r--) {
525 >                        n = (size_t)cm->ncols*cm->nrows;
526 >                        while (n--) {
527                                  copycolor(fc, mp);
528                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
529                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines