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.16 by schorsch, Sun Mar 6 01:13:18 2016 UTC vs.
Revision 2.26 by greg, Thu Mar 26 02:48:31 2020 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
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 34 | Line 34 | cm_alloc(int nrows, int ncols)
34                  error(USER, "attempt to create empty matrix");
35          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
36                                  sizeof(COLOR)*(nrows*ncols - 1));
37 <        if (cm == NULL)
37 >        if (!cm)
38                  error(SYSTEM, "out of memory in cm_alloc()");
39          cm->nrows = nrows;
40          cm->ncols = ncols;
# Line 57 | 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) {
# Line 70 | Line 72 | cm_resize(CMATRIX *cm, int nrows)
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 79 | 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 87 | 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 102 | 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 112 | 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 130 | 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 +        if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) {
185 +                error(USER, "bad picture resolution string");
186 +                return(NULL);
187 +        }
188 +        cm = cm_alloc(nrows, ncols);
189 +        if (!cm)
190 +                return(NULL);
191 +        doscale = (scale[0] < .99) | (scale[0] > 1.01) |
192 +                        (scale[1] < .99) | (scale[1] > 1.01) |
193 +                        (scale[2] < .99) | (scale[2] > 1.01) ;
194 +        mp = cm->cmem;
195 +        while (nrows--) {
196 +                if (freadscan((COLOR *)mp, ncols, fp) < 0) {
197 +                        error(USER, "error reading color picture as matrix");
198 +                        cm_free(cm);
199 +                        return(NULL);
200 +                }
201 +                if (doscale) {
202 +                        int     i = ncols;
203 +                        while (i--) {
204 +                                *mp++ *= scale[0];
205 +                                *mp++ *= scale[1];
206 +                                *mp++ *= scale[2];
207 +                        }
208 +                } else
209 +                        mp += 3*ncols;
210 +        }                                       /* caller closes stream */
211 +        return(cm);
212 + }
213 +
214   /* Allocate and load a matrix from the given input (or stdin if NULL) */
215   CMATRIX *
216   cm_load(const char *inspec, int nrows, int ncols, int dtype)
217   {
218          const int       ROWINC = 2048;
219          FILE            *fp = stdin;
220 +        int             swap = 0;
221 +        COLOR           scale;
222          CMATRIX         *cm;
223  
224 <        if (inspec == NULL)
224 >        if (!inspec)
225                  inspec = "<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);
245                  if (ncols <= 0)
246                          error(USER, "unspecified number of columns");
# Line 182 | Line 250 | cm_load(const char *inspec, int nrows, int ncols, int
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          }
# Line 213 | Line 285 | cm_load(const char *inspec, int nrows, int ncols, int
285                  cm = cm_alloc(guessrows, ncols);
286          } else
287                  cm = cm_alloc(nrows, ncols);
288 <        if (cm == NULL)                                 /* XXX never happens */
288 >        if (!cm)                                        /* XXX never happens */
289                  return(NULL);
290          if (dtype == DTascii) {                         /* read text file */
291                  int     maxrow = (nrows > 0 ? nrows : 32000);
# Line 244 | Line 316 | cm_load(const char *inspec, int nrows, int ncols, int
316                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
317                          int     nread = 0;
318                          do {                            /* read all we can */
319 <                                nread += fread(cm->cmem + 3*nread,
319 >                                nread += getbinary(cm->cmem + 3*nread,
320                                                  sizeof(COLOR),
321                                                  cm->nrows*cm->ncols - nread,
322                                                  fp);
# Line 261 | Line 333 | cm_load(const char *inspec, int nrows, int ncols, int
333                                          goto EOFerror;
334                          } while (nread < cm->nrows*cm->ncols);
335  
336 +                        if (swap) {
337 +                                if (sizeof(COLORV) == 4)
338 +                                        swap32((char *)cm->cmem,
339 +                                                        3*cm->nrows*cm->ncols);
340 +                                else /* sizeof(COLORV) == 8 */
341 +                                        swap64((char *)cm->cmem,
342 +                                                        3*cm->nrows*cm->ncols);
343 +                        }
344                  } else if (dtype == DTdouble) {
345                          double  dc[3];                  /* load from double */
346                          COLORV  *cvp = cm->cmem;
# Line 269 | Line 349 | cm_load(const char *inspec, int nrows, int ncols, int
349                          if (n <= 0)
350                                  goto not_handled;
351                          while (n--) {
352 <                                if (fread(dc, sizeof(double), 3, fp) != 3)
352 >                                if (getbinary(dc, sizeof(double), 3, fp) != 3)
353                                          goto EOFerror;
354 +                                if (swap) swap64((char *)dc, 3);
355                                  copycolor(cvp, dc);
356                                  cvp += 3;
357                          }
# Line 282 | Line 363 | cm_load(const char *inspec, int nrows, int ncols, int
363                          if (n <= 0)
364                                  goto not_handled;
365                          while (n--) {
366 <                                if (fread(fc, sizeof(float), 3, fp) != 3)
366 >                                if (getbinary(fc, sizeof(float), 3, fp) != 3)
367                                          goto EOFerror;
368 +                                if (swap) swap32((char *)fc, 3);
369                                  copycolor(cvp, fc);
370                                  cvp += 3;
371                          }
# Line 295 | Line 377 | cm_load(const char *inspec, int nrows, int ncols, int
377                                  error(WARNING, errmsg);
378                  }
379          }
380 + cleanup:
381          if (fp != stdin) {
382                  if (inspec[0] != '!')
383                          fclose(fp);
# Line 311 | Line 394 | cm_load(const char *inspec, int nrows, int ncols, int
394   EOFerror:
395          sprintf(errmsg, "unexpected EOF reading %s", inspec);
396          error(USER, errmsg);
397 +        return(NULL);
398   not_handled:
399          error(INTERNAL, "unhandled data size or length in cm_load()");
400          return(NULL);   /* gratis return */
# Line 323 | Line 407 | cm_column(const CMATRIX *cm, int c)
407          CMATRIX *cvr;
408          int     dr;
409  
410 +        if (!cm)
411 +                return(NULL);
412          if ((c < 0) | (c >= cm->ncols))
413                  error(INTERNAL, "column requested outside matrix");
414          cvr = cm_alloc(cm->nrows, 1);
415 <        if (cvr == NULL)
415 >        if (!cvr)
416                  return(NULL);
417          for (dr = 0; dr < cm->nrows; dr++) {
418                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 338 | Line 424 | cm_column(const CMATRIX *cm, int c)
424          return(cvr);
425   }
426  
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
427   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
428   CMATRIX *
429   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 367 | Line 432 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
432          CMATRIX *cmr;
433          int     dr, dc, i;
434  
435 +        if (!cm1 | !cm2)
436 +                return(NULL);
437          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
438                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
439          cmr = cm_alloc(cm1->nrows, cm2->ncols);
440 <        if (cmr == NULL)
440 >        if (!cmr)
441                  return(NULL);
442                                  /* optimization: check for zero rows & cols */
443          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 395 | Line 462 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
462                  COLORV  *dp = cm_lval(cmr,dr,dc);
463                  double  res[3];
464                  dp[0] = dp[1] = dp[2] = 0;
465 <                if (rowcheck != NULL && !rowcheck[dr])
465 >                if (rowcheck && !rowcheck[dr])
466                          continue;
467 <                if (colcheck != NULL && !colcheck[dc])
467 >                if (colcheck && !colcheck[dc])
468                          continue;
469                  res[0] = res[1] = res[2] = 0;
470                  for (i = 0; i < cm1->ncols; i++) {
471                      const COLORV        *cp1 = cm_lval(cm1,dr,i);
472                      const COLORV        *cp2 = cm_lval(cm2,i,dc);
473 <                    res[0] += cp1[0] * cp2[0];
474 <                    res[1] += cp1[1] * cp2[1];
475 <                    res[2] += cp1[2] * cp2[2];
473 >                    res[0] += (double)cp1[0] * cp2[0];
474 >                    res[1] += (double)cp1[1] * cp2[1];
475 >                    res[2] += (double)cp1[2] * cp2[2];
476                  }
477                  copycolor(dp, res);
478              }
479 <        if (rowcheck != NULL) free(rowcheck);
480 <        if (colcheck != NULL) free(colcheck);
479 >        if (rowcheck) free(rowcheck);
480 >        if (colcheck) free(colcheck);
481          return(cmr);
482   }
483  
# Line 419 | Line 486 | int
486   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
487   {
488          static const char       tabEOL[2] = {'\t','\n'};
489 <        const COLORV            *mp = cm->cmem;
489 >        const COLORV            *mp;
490          int                     r, c;
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 435 | Line 505 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
505                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
506                          r = cm->ncols*cm->nrows;
507                          while (r > 0) {
508 <                                c = fwrite(mp, sizeof(COLOR), r, fp);
508 >                                c = putbinary(mp, sizeof(COLOR), r, fp);
509                                  if (c <= 0)
510                                          return(0);
511                                  mp += 3*c;
# Line 446 | Line 516 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
516                          r = cm->ncols*cm->nrows;
517                          while (r--) {
518                                  copycolor(dc, mp);
519 <                                if (fwrite(dc, sizeof(double), 3, fp) != 3)
519 >                                if (putbinary(dc, sizeof(double), 3, fp) != 3)
520                                          return(0);
521                                  mp += 3;
522                          }
# Line 455 | Line 525 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
525                          r = cm->ncols*cm->nrows;
526                          while (r--) {
527                                  copycolor(fc, mp);
528 <                                if (fwrite(fc, sizeof(float), 3, fp) != 3)
528 >                                if (putbinary(fc, sizeof(float), 3, fp) != 3)
529                                          return(0);
530                                  mp += 3;
531                          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines