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.7 by greg, Tue Jul 8 16:39:41 2014 UTC vs.
Revision 2.25 by greg, Wed Mar 25 01:51:09 2020 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 "paths.h"
16   #include "resolu.h"
17  
18   const char      *cm_fmt_id[] = {
19 <                        "unknown", "ascii", "float", "double",
20 <                        COLRFMT, CIEFMT
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, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
25                  };
26  
27   /* Allocate a color coefficient matrix */
# Line 32 | 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 55 | 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 68 | 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 77 | 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 85 | 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 100 | 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 110 | 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 128 | 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 a matrix from the given file (or stdin if NULL) */
177 < CMATRIX *
178 < cm_load(const char *fname, int nrows, int ncols, int dtype)
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 <        FILE    *fp = stdin;
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 <        if (fname == NULL)
215 <                fname = "<stdin>";
216 <        else if ((fp = fopen(fname, "r")) == NULL) {
217 <                sprintf(errmsg, "cannot open file '%s'", fname);
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)
225 >                inspec = "<stdin>";
226 >        else if (inspec[0] == '!') {
227 >                fp = popen(inspec+1, "r");
228 >                if (!fp) {
229 >                        sprintf(errmsg, "cannot start command '%s'", inspec);
230 >                        error(SYSTEM, errmsg);
231 >                }
232 >        } else if (!(fp = fopen(inspec, "r"))) {
233 >                sprintf(errmsg, "cannot open file '%s'", inspec);
234                  error(SYSTEM, errmsg);
235          }
236   #ifdef getc_unlocked
# Line 162 | Line 239 | cm_load(const char *fname, int nrows, int ncols, int d
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 173 | Line 250 | cm_load(const char *fname, int nrows, int ncols, int d
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)) {
262 >                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
263                          long    startpos = ftell(fp);
264                          if (fseek(fp, 0L, SEEK_END) == 0) {
265                                  long    endpos = ftell(fp);
# Line 188 | Line 269 | cm_load(const char *fname, int nrows, int ncols, int d
269                                  if ((endpos - startpos) % (ncols*elemsiz)) {
270                                          sprintf(errmsg,
271                                          "improper length for binary file '%s'",
272 <                                                        fname);
272 >                                                        inspec);
273                                          error(USER, errmsg);
274                                  }
275                                  guessrows = (endpos - startpos)/(ncols*elemsiz);
276                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
277                                          sprintf(errmsg,
278                                                  "fseek() error on file '%s'",
279 <                                                        fname);
279 >                                                        inspec);
280                                          error(SYSTEM, errmsg);
281                                  }
282                                  nrows = guessrows;      /* we're confident */
# Line 204 | Line 285 | cm_load(const char *fname, int nrows, int ncols, int d
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);
292                  int     r, c;
293                  for (r = 0; r < maxrow; r++) {
294                      if (r >= cm->nrows)                 /* need more space? */
295 <                        cm = cm_resize(cm, 2*cm->nrows);
295 >                        cm = cm_resize(cm, cm->nrows+ROWINC);
296                      for (c = 0; c < ncols; c++) {
297                          COLORV  *cv = cm_lval(cm,r,c);
298 <                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
298 >                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) {
299                                  if ((nrows <= 0) & (r > 0) & !c) {
300                                          cm = cm_resize(cm, maxrow=r);
301                                          break;
302                                  } else
303                                          goto EOFerror;
304 +                        }
305                      }
306                  }
307                  while ((c = getc(fp)) != EOF)
308                          if (!isspace(c)) {
309                                  sprintf(errmsg,
310 <                                "unexpected data at end of ascii file %s",
311 <                                                fname);
310 >                                "unexpected data at end of ascii input '%s'",
311 >                                                inspec);
312                                  error(WARNING, errmsg);
313                                  break;
314                          }
# Line 234 | Line 316 | cm_load(const char *fname, int nrows, int ncols, int d
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);
323                                  if (nrows <= 0) {       /* unknown length */
324                                          if (nread == cm->nrows*cm->ncols)
325                                                          /* need more space? */
326 <                                                cm = cm_resize(cm, 2*cm->nrows);
326 >                                                cm = cm_resize(cm, cm->nrows+ROWINC);
327                                          else if (nread && !(nread % cm->ncols))
328                                                          /* seem to be  done */
329                                                  cm = cm_resize(cm, nread/cm->ncols);
# Line 259 | Line 341 | cm_load(const char *fname, int nrows, int ncols, int d
341                          if (n <= 0)
342                                  goto not_handled;
343                          while (n--) {
344 <                                if (fread(dc, sizeof(double), 3, fp) != 3)
344 >                                if (getbinary(dc, sizeof(double), 3, fp) != 3)
345                                          goto EOFerror;
346                                  copycolor(cvp, dc);
347                                  cvp += 3;
# Line 272 | Line 354 | cm_load(const char *fname, int nrows, int ncols, int d
354                          if (n <= 0)
355                                  goto not_handled;
356                          while (n--) {
357 <                                if (fread(fc, sizeof(float), 3, fp) != 3)
357 >                                if (getbinary(fc, sizeof(float), 3, fp) != 3)
358                                          goto EOFerror;
359                                  copycolor(cvp, fc);
360                                  cvp += 3;
# Line 280 | Line 362 | cm_load(const char *fname, int nrows, int ncols, int d
362                  }
363                  if (fgetc(fp) != EOF) {
364                                  sprintf(errmsg,
365 <                                "unexpected data at end of binary file %s",
366 <                                                fname);
365 >                                "unexpected data at end of binary input '%s'",
366 >                                                inspec);
367                                  error(WARNING, errmsg);
368                  }
369          }
370 <        if (fp != stdin)
371 <                fclose(fp);
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);
375 >        }
376 > cleanup:
377 >        if (fp != stdin) {
378 >                if (inspec[0] != '!')
379 >                        fclose(fp);
380 >                else if (pclose(fp)) {
381 >                        sprintf(errmsg, "error running command '%s'", inspec);
382 >                        error(WARNING, errmsg);
383 >                }
384 >        }
385   #ifdef getc_unlocked
386          else
387                  funlockfile(fp);
388   #endif
389          return(cm);
390   EOFerror:
391 <        sprintf(errmsg, "unexpected EOF reading %s", fname);
391 >        sprintf(errmsg, "unexpected EOF reading %s", inspec);
392          error(USER, errmsg);
393 +        return(NULL);
394   not_handled:
395          error(INTERNAL, "unhandled data size or length in cm_load()");
396          return(NULL);   /* gratis return */
# Line 307 | Line 403 | cm_column(const CMATRIX *cm, int c)
403          CMATRIX *cvr;
404          int     dr;
405  
406 +        if (!cm)
407 +                return(NULL);
408          if ((c < 0) | (c >= cm->ncols))
409                  error(INTERNAL, "column requested outside matrix");
410          cvr = cm_alloc(cm->nrows, 1);
411 <        if (cvr == NULL)
411 >        if (!cvr)
412                  return(NULL);
413          for (dr = 0; dr < cm->nrows; dr++) {
414                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 322 | Line 420 | cm_column(const CMATRIX *cm, int c)
420          return(cvr);
421   }
422  
325 /* Scale a matrix by a single value */
326 CMATRIX *
327 cm_scale(const CMATRIX *cm1, const COLOR sca)
328 {
329        CMATRIX *cmr;
330        int     dr, dc;
331
332        cmr = cm_alloc(cm1->nrows, cm1->ncols);
333        if (cmr == NULL)
334                return(NULL);
335        for (dr = 0; dr < cmr->nrows; dr++)
336            for (dc = 0; dc < cmr->ncols; dc++) {
337                const COLORV    *sp = cm_lval(cm1,dr,dc);
338                COLORV          *dp = cm_lval(cmr,dr,dc);
339                dp[0] = sp[0] * sca[0];
340                dp[1] = sp[1] * sca[1];
341                dp[2] = sp[2] * sca[2];
342            }
343        return(cmr);
344 }
345
423   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
424   CMATRIX *
425   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 351 | Line 428 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
428          CMATRIX *cmr;
429          int     dr, dc, i;
430  
431 +        if (!cm1 | !cm2)
432 +                return(NULL);
433          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
434                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
435          cmr = cm_alloc(cm1->nrows, cm2->ncols);
436 <        if (cmr == NULL)
436 >        if (!cmr)
437                  return(NULL);
438                                  /* optimization: check for zero rows & cols */
439          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 379 | Line 458 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
458                  COLORV  *dp = cm_lval(cmr,dr,dc);
459                  double  res[3];
460                  dp[0] = dp[1] = dp[2] = 0;
461 <                if (rowcheck != NULL && !rowcheck[dr])
461 >                if (rowcheck && !rowcheck[dr])
462                          continue;
463 <                if (colcheck != NULL && !colcheck[dc])
463 >                if (colcheck && !colcheck[dc])
464                          continue;
465                  res[0] = res[1] = res[2] = 0;
466                  for (i = 0; i < cm1->ncols; i++) {
467                      const COLORV        *cp1 = cm_lval(cm1,dr,i);
468                      const COLORV        *cp2 = cm_lval(cm2,i,dc);
469 <                    res[0] += cp1[0] * cp2[0];
470 <                    res[1] += cp1[1] * cp2[1];
471 <                    res[2] += cp1[2] * cp2[2];
469 >                    res[0] += (double)cp1[0] * cp2[0];
470 >                    res[1] += (double)cp1[1] * cp2[1];
471 >                    res[2] += (double)cp1[2] * cp2[2];
472                  }
473                  copycolor(dp, res);
474              }
475 <        if (rowcheck != NULL) free(rowcheck);
476 <        if (colcheck != NULL) free(colcheck);
475 >        if (rowcheck) free(rowcheck);
476 >        if (colcheck) free(colcheck);
477          return(cmr);
478   }
479  
# Line 403 | Line 482 | int
482   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
483   {
484          static const char       tabEOL[2] = {'\t','\n'};
485 <        const COLORV            *mp = cm->cmem;
485 >        const COLORV            *mp;
486          int                     r, c;
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 419 | Line 501 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
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);
504 >                                c = putbinary(mp, sizeof(COLOR), r, fp);
505                                  if (c <= 0)
506                                          return(0);
507                                  mp += 3*c;
# Line 430 | Line 512 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
512                          r = cm->ncols*cm->nrows;
513                          while (r--) {
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                          }
# Line 439 | Line 521 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
521                          r = cm->ncols*cm->nrows;
522                          while (r--) {
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