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.10 by greg, Fri May 8 18:25:03 2015 UTC vs.
Revision 2.31 by greg, Fri Jan 15 18:31:38 2021 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <ctype.h>
11 + #include "platform.h"
12   #include "standard.h"
13   #include "cmatrix.h"
14   #include "platform.h"
15 < #include "rtprocess.h"
15 > #include "paths.h"
16   #include "resolu.h"
17  
18   const char      *cm_fmt_id[] = {
19 <                        "unknown", "ascii", COLRFMT, CIEFMT,
20 <                        "float", "double"
19 >                        "unknown", COLRFMT, CIEFMT,
20 >                        "float", "ascii", "double"
21                  };
22  
23   const int       cm_elem_size[] = {
24 <                        0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4
24 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
25                  };
26  
27   /* Allocate a color coefficient matrix */
# Line 32 | 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));
37 <        if (cm == NULL)
36 >                                sizeof(COLOR)*((size_t)nrows*ncols - 1));
37 >        if (!cm)
38                  error(SYSTEM, "out of memory in cm_alloc()");
39          cm->nrows = nrows;
40          cm->ncols = ncols;
# Line 56 | Line 57 | cm_resize(CMATRIX *cm, int nrows)
57   {
58          size_t  old_size, new_size, ra_bounds[2];
59  
60 +        if (!cm)
61 +                return(NULL);
62          if (nrows == cm->nrows)
63                  return(cm);
64          if (nrows <= 0) {
65                  cm_free(cm);
66                  return(NULL);
67          }
68 <        old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1);
68 >        old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1);
69          adjacent_ra_sizes(ra_bounds, old_size);
70 <        new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1);
70 >        new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1);
71          if (nrows < cm->nrows ? new_size <= ra_bounds[0] :
72                                  new_size > ra_bounds[1]) {
73                  adjacent_ra_sizes(ra_bounds, new_size);
74                  cm = (CMATRIX *)realloc(cm, ra_bounds[1]);
75 <                if (cm == NULL)
75 >                if (!cm)
76                          error(SYSTEM, "out of memory in cm_resize()");
77          }
78          cm->nrows = nrows;
# Line 78 | Line 81 | cm_resize(CMATRIX *cm, int nrows)
81  
82   typedef struct {
83          int     dtype;          /* data type */
84 +        int     need2swap;      /* need byte swap? */
85          int     nrows, ncols;   /* matrix size */
86 +        COLOR   expos;          /* exposure value */
87          char    *err;           /* error message */
88   } CMINFO;               /* header info record */
89  
# Line 86 | Line 91 | static int
91   get_cminfo(char *s, void *p)
92   {
93          CMINFO  *ip = (CMINFO *)p;
94 <        char    fmt[32];
94 >        char    fmt[MAXFMTLEN];
95          int     i;
96  
97          if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) {
# Line 101 | Line 106 | get_cminfo(char *s, void *p)
106                  ip->ncols = atoi(s+6);
107                  return(0);
108          }
109 +        if ((i = isbigendian(s)) >= 0) {
110 +                ip->need2swap = (nativebigendian() != i);
111 +                return(0);
112 +        }
113 +        if (isexpos(s)) {
114 +                double  d = exposval(s);
115 +                scalecolor(ip->expos, d);
116 +                return(0);
117 +        }
118 +        if (iscolcor(s)) {
119 +                COLOR   ctmp;
120 +                colcorval(ctmp, s);
121 +                multcolor(ip->expos, ctmp);
122 +                return(0);
123 +        }
124          if (!formatval(fmt, s))
125                  return(0);
126          for (i = 1; i < DTend; i++)
# Line 111 | Line 131 | get_cminfo(char *s, void *p)
131  
132   /* Load header to obtain/check data type and number of columns */
133   char *
134 < cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
134 > cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp)
135   {
136          CMINFO  cmi;
137                                                  /* read header */
138          cmi.dtype = DTfromHeader;
139 +        cmi.need2swap = 0;
140 +        cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f;
141          cmi.nrows = cmi.ncols = 0;
142          cmi.err = "unexpected EOF in header";
143          if (getheader(fp, get_cminfo, &cmi) < 0)
144                  return(cmi.err);
145 <        if (dt != NULL) {                       /* get/check data type? */
145 >        if (dt) {                               /* get/check data type? */
146                  if (cmi.dtype == DTfromHeader) {
147                          if (*dt == DTfromHeader)
148                                  return("missing/unknown data format in header");
# Line 129 | Line 151 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
151                  else if (*dt != cmi.dtype)
152                          return("unexpected data format in header");
153          }
154 <        if (nr != NULL) {                       /* get/check #rows? */
154 >        if (nr) {                               /* get/check #rows? */
155                  if (*nr <= 0)
156                          *nr = cmi.nrows;
157                  else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
158                          return("unexpected row count in header");
159          }
160 <        if (nc != NULL) {                       /* get/check #columns? */
160 >        if (nc) {                               /* get/check #columns? */
161                  if (*nc <= 0)
162                          *nc = cmi.ncols;
163                  else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
164                          return("unexpected column count in header");
165          }
166 +        if (swp)                                /* get/check swap? */
167 +                *swp = cmi.need2swap;
168 +        if (scale) {                            /* transfer exposure comp. */
169 +                scale[0] = 1.f/cmi.expos[0];
170 +                scale[1] = 1.f/cmi.expos[1];
171 +                scale[2] = 1.f/cmi.expos[2];
172 +        }
173          return(NULL);
174   }
175  
176 + /* Allocate and load image data into matrix */
177 + static CMATRIX *
178 + cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
179 + {
180 +        int     doscale;
181 +        CMATRIX *cm;
182 +        COLORV  *mp;
183 +                                                /* header already loaded */
184 +        cm = cm_alloc(nrows, ncols);
185 +        if (!cm)
186 +                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) ;
190 +        mp = cm->cmem;
191 +        while (nrows--) {
192 +                if (freadscan((COLOR *)mp, ncols, fp) < 0) {
193 +                        error(USER, "error reading color picture as matrix");
194 +                        cm_free(cm);
195 +                        return(NULL);
196 +                }
197 +                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;
206 +        }                                       /* caller closes stream */
207 +        return(cm);
208 + }
209 +
210   /* Allocate and load a matrix from the given input (or stdin if NULL) */
211   CMATRIX *
212   cm_load(const char *inspec, int nrows, int ncols, int dtype)
213   {
214 <        FILE    *fp = stdin;
215 <        CMATRIX *cm;
214 >        const int       ROWINC = 2048;
215 >        FILE            *fp = stdin;
216 >        int             swap = 0;
217 >        COLOR           scale;
218 >        CMATRIX         *cm;
219  
220 <        if (inspec == NULL)
220 >        if (!inspec)
221                  inspec = "<stdin>";
222          else if (inspec[0] == '!') {
223                  fp = popen(inspec+1, "r");
224 <                if (fp == NULL) {
224 >                if (!fp) {
225                          sprintf(errmsg, "cannot start command '%s'", inspec);
226                          error(SYSTEM, errmsg);
227                  }
228 <        } else if ((fp = fopen(inspec, "r")) == NULL) {
228 >        } else if (!(fp = fopen(inspec, "r"))) {
229                  sprintf(errmsg, "cannot open file '%s'", inspec);
230                  error(SYSTEM, errmsg);
231          }
# Line 169 | Line 235 | cm_load(const char *inspec, int nrows, int ncols, int
235          if (dtype != DTascii)
236                  SET_FILE_BINARY(fp);            /* doesn't really work */
237          if (!dtype | !ncols) {                  /* expecting header? */
238 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, fp);
239 <                if (err != NULL)
238 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
239 >                if (err)
240                          error(USER, err);
175                if (ncols <= 0)
176                        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:
247          case DTdouble:
248                  break;
249 +        case DTrgbe:
250 +        case DTxyze:
251 +                cm = cm_load_rgbe(fp, nrows, ncols, scale);
252 +                goto cleanup;
253          default:
254                  error(USER, "unexpected data type in cm_load()");
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);
192                                long    elemsiz = 3*(dtype==DTfloat ?
193                                            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 211 | Line 280 | cm_load(const char *inspec, int nrows, int ncols, int
280                  cm = cm_alloc(guessrows, ncols);
281          } else
282                  cm = cm_alloc(nrows, ncols);
283 <        if (cm == NULL)                                 /* XXX never happens */
283 >        if (!cm)                                        /* XXX never happens */
284                  return(NULL);
285          if (dtype == DTascii) {                         /* read text file */
286                  int     maxrow = (nrows > 0 ? nrows : 32000);
287                  int     r, c;
288                  for (r = 0; r < maxrow; r++) {
289                      if (r >= cm->nrows)                 /* need more space? */
290 <                        cm = cm_resize(cm, 2*cm->nrows);
290 >                        cm = cm_resize(cm, cm->nrows+ROWINC);
291                      for (c = 0; c < ncols; c++) {
292                          COLORV  *cv = cm_lval(cm,r,c);
293 <                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
293 >                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) {
294                                  if ((nrows <= 0) & (r > 0) & !c) {
295                                          cm = cm_resize(cm, maxrow=r);
296                                          break;
297                                  } else
298                                          goto EOFerror;
299 +                        }
300                      }
301                  }
302                  while ((c = getc(fp)) != EOF)
# Line 239 | Line 309 | cm_load(const char *inspec, int nrows, int ncols, int
309                          }
310          } else {                                        /* read binary file */
311                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
312 <                        int     nread = 0;
312 >                        size_t  nread = 0;
313                          do {                            /* read all we can */
314 <                                nread += fread(cm->cmem + 3*nread,
314 >                                nread += getbinary(cm->cmem + 3*nread,
315                                                  sizeof(COLOR),
316 <                                                cm->nrows*cm->ncols - nread,
316 >                                                (size_t)cm->nrows*cm->ncols - nread,
317                                                  fp);
318                                  if (nrows <= 0) {       /* unknown length */
319 <                                        if (nread == cm->nrows*cm->ncols)
319 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
320                                                          /* need more space? */
321 <                                                cm = cm_resize(cm, 2*cm->nrows);
321 >                                                cm = cm_resize(cm, cm->nrows+ROWINC);
322                                          else if (nread && !(nread % cm->ncols))
323                                                          /* seem to be  done */
324                                                  cm = cm_resize(cm, nread/cm->ncols);
325                                          else            /* ended mid-row */
326                                                  goto EOFerror;
327 <                                } else if (nread < cm->nrows*cm->ncols)
327 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
328                                          goto EOFerror;
329 <                        } while (nread < cm->nrows*cm->ncols);
329 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
330  
331 +                        if (swap) {
332 +                                if (sizeof(COLORV) == 4)
333 +                                        swap32((char *)cm->cmem,
334 +                                                        3*(size_t)cm->nrows*cm->ncols);
335 +                                else /* sizeof(COLORV) == 8 */
336 +                                        swap64((char *)cm->cmem,
337 +                                                        3*(size_t)cm->nrows*cm->ncols);
338 +                        }
339                  } else if (dtype == DTdouble) {
340                          double  dc[3];                  /* load from double */
341                          COLORV  *cvp = cm->cmem;
342 <                        int     n = nrows*ncols;
342 >                        size_t  n = (size_t)nrows*ncols;
343  
344                          if (n <= 0)
345                                  goto not_handled;
346                          while (n--) {
347 <                                if (fread(dc, sizeof(double), 3, fp) != 3)
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                          }
353                  } else /* dtype == DTfloat */ {
354                          float   fc[3];                  /* load from float */
355                          COLORV  *cvp = cm->cmem;
356 <                        int     n = nrows*ncols;
356 >                        size_t  n = (size_t)nrows*ncols;
357  
358                          if (n <= 0)
359                                  goto not_handled;
360                          while (n--) {
361 <                                if (fread(fc, sizeof(float), 3, fp) != 3)
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 292 | Line 372 | cm_load(const char *inspec, int nrows, int ncols, int
372                                  error(WARNING, errmsg);
373                  }
374          }
375 + cleanup:
376          if (fp != stdin) {
377                  if (inspec[0] != '!')
378                          fclose(fp);
# Line 308 | Line 389 | cm_load(const char *inspec, int nrows, int ncols, int
389   EOFerror:
390          sprintf(errmsg, "unexpected EOF reading %s", inspec);
391          error(USER, errmsg);
392 +        return(NULL);
393   not_handled:
394          error(INTERNAL, "unhandled data size or length in cm_load()");
395          return(NULL);   /* gratis return */
# Line 320 | Line 402 | cm_column(const CMATRIX *cm, int c)
402          CMATRIX *cvr;
403          int     dr;
404  
405 +        if (!cm)
406 +                return(NULL);
407          if ((c < 0) | (c >= cm->ncols))
408                  error(INTERNAL, "column requested outside matrix");
409          cvr = cm_alloc(cm->nrows, 1);
410 <        if (cvr == NULL)
410 >        if (!cvr)
411                  return(NULL);
412          for (dr = 0; dr < cm->nrows; dr++) {
413                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 335 | Line 419 | cm_column(const CMATRIX *cm, int c)
419          return(cvr);
420   }
421  
338 /* Scale a matrix by a single value */
339 CMATRIX *
340 cm_scale(const CMATRIX *cm1, const COLOR sca)
341 {
342        CMATRIX *cmr;
343        int     dr, dc;
344
345        cmr = cm_alloc(cm1->nrows, cm1->ncols);
346        if (cmr == NULL)
347                return(NULL);
348        for (dr = 0; dr < cmr->nrows; dr++)
349            for (dc = 0; dc < cmr->ncols; dc++) {
350                const COLORV    *sp = cm_lval(cm1,dr,dc);
351                COLORV          *dp = cm_lval(cmr,dr,dc);
352                dp[0] = sp[0] * sca[0];
353                dp[1] = sp[1] * sca[1];
354                dp[2] = sp[2] * sca[2];
355            }
356        return(cmr);
357 }
358
422   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
423   CMATRIX *
424   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 364 | Line 427 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
427          CMATRIX *cmr;
428          int     dr, dc, i;
429  
430 +        if (!cm1 | !cm2)
431 +                return(NULL);
432          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
433                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
434          cmr = cm_alloc(cm1->nrows, cm2->ncols);
435 <        if (cmr == NULL)
435 >        if (!cmr)
436                  return(NULL);
437                                  /* optimization: check for zero rows & cols */
438          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 392 | Line 457 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
457                  COLORV  *dp = cm_lval(cmr,dr,dc);
458                  double  res[3];
459                  dp[0] = dp[1] = dp[2] = 0;
460 <                if (rowcheck != NULL && !rowcheck[dr])
460 >                if (rowcheck && !rowcheck[dr])
461                          continue;
462 <                if (colcheck != NULL && !colcheck[dc])
462 >                if (colcheck && !colcheck[dc])
463                          continue;
464                  res[0] = res[1] = res[2] = 0;
465                  for (i = 0; i < cm1->ncols; i++) {
466                      const COLORV        *cp1 = cm_lval(cm1,dr,i);
467                      const COLORV        *cp2 = cm_lval(cm2,i,dc);
468 <                    res[0] += cp1[0] * cp2[0];
469 <                    res[1] += cp1[1] * cp2[1];
470 <                    res[2] += cp1[2] * cp2[2];
468 >                    res[0] += (double)cp1[0] * cp2[0];
469 >                    res[1] += (double)cp1[1] * cp2[1];
470 >                    res[2] += (double)cp1[2] * cp2[2];
471                  }
472                  copycolor(dp, res);
473              }
474 <        if (rowcheck != NULL) free(rowcheck);
475 <        if (colcheck != NULL) free(colcheck);
474 >        if (rowcheck) free(rowcheck);
475 >        if (colcheck) free(colcheck);
476          return(cmr);
477   }
478  
# Line 416 | Line 481 | int
481   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
482   {
483          static const char       tabEOL[2] = {'\t','\n'};
484 <        const COLORV            *mp = cm->cmem;
484 >        const COLORV            *mp;
485          int                     r, c;
486 +        size_t                  n, rv;
487  
488 +        if (!cm)
489 +                return(0);
490 +        mp = cm->cmem;
491          switch (dtype) {
492          case DTascii:
493                  for (r = 0; r < cm->nrows; r++)
# Line 430 | Line 499 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
499          case DTfloat:
500          case DTdouble:
501                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
502 <                        r = cm->ncols*cm->nrows;
503 <                        while (r > 0) {
504 <                                c = fwrite(mp, sizeof(COLOR), r, fp);
505 <                                if (c <= 0)
502 >                        n = (size_t)cm->ncols*cm->nrows;
503 >                        while (n > 0) {
504 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
505 >                                if (rv <= 0)
506                                          return(0);
507 <                                mp += 3*c;
508 <                                r -= c;
507 >                                mp += 3*rv;
508 >                                n -= rv;
509                          }
510                  } else if (dtype == DTdouble) {
511                          double  dc[3];
512 <                        r = cm->ncols*cm->nrows;
513 <                        while (r--) {
512 >                        n = (size_t)cm->ncols*cm->nrows;
513 >                        while (n--) {
514                                  copycolor(dc, mp);
515 <                                if (fwrite(dc, sizeof(double), 3, fp) != 3)
515 >                                if (putbinary(dc, sizeof(double), 3, fp) != 3)
516                                          return(0);
517                                  mp += 3;
518                          }
519                  } else /* dtype == DTfloat */ {
520                          float   fc[3];
521 <                        r = cm->ncols*cm->nrows;
522 <                        while (r--) {
521 >                        n = (size_t)cm->ncols*cm->nrows;
522 >                        while (n--) {
523                                  copycolor(fc, mp);
524 <                                if (fwrite(fc, sizeof(float), 3, fp) != 3)
524 >                                if (putbinary(fc, sizeof(float), 3, fp) != 3)
525                                          return(0);
526                                  mp += 3;
527                          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines