ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
(Generate patch)

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.35 by greg, Wed Aug 14 18:20:02 2019 UTC vs.
Revision 2.48 by greg, Thu Mar 18 00:34:31 2021 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * General matrix operations.
6   */
7  
8 #include <stdio.h>
8   #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
9   #include <errno.h>
10   #include "rtio.h"
11   #include "platform.h"
# Line 27 | Line 24 | rmx_alloc(int nr, int nc, int n)
24          if ((nr <= 0) | (nc <= 0) | (n <= 0))
25                  return(NULL);
26          dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) +
27 <                                        sizeof(dnew->mtx[0])*(n*nr*nc));
27 >                                        sizeof(dnew->mtx[0])*n*nr*nc);
28          if (!dnew)
29                  return(NULL);
30          dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
31          dnew->dtype = DTdouble;
32 +        dnew->swapin = 0;
33          dnew->info = NULL;
34          return(dnew);
35   }
# Line 102 | Line 100 | get_dminfo(char *s, void *p)
100                  ip->swapin = (nativebigendian() != i);
101                  return(0);
102          }
103 +        if (isexpos(s)) {
104 +                double  d = exposval(s);
105 +                scalecolor(ip->mtx, d);
106 +                return(0);
107 +        }
108 +        if (iscolcor(s)) {
109 +                COLOR   ctmp;
110 +                colcorval(ctmp, s);
111 +                multcolor(ip->mtx, ctmp);
112 +                return(0);
113 +        }
114          if (!formatval(fmt, s)) {
115                  rmx_addinfo(ip, s);
116                  return(0);
# Line 152 | Line 161 | rmx_load_float(RMATRIX *rm, FILE *fp)
161   static int
162   rmx_load_double(RMATRIX *rm, FILE *fp)
163   {
164 <        int     i, j;
164 >        int     i;
165  
166 <        for (i = 0; i < rm->nrows; i++)
167 <            for (j = 0; j < rm->ncols; j++) {
168 <                if (getbinary(&rmx_lval(rm,i,j,0), sizeof(double), rm->ncomp, fp) != rm->ncomp)
169 <                    return(0);
166 >        if ((char *)&rmx_lval(rm,1,0,0) - (char *)&rmx_lval(rm,0,0,0) !=
167 >                                        sizeof(double)*rm->ncols*rm->ncomp) {
168 >                fputs("Code error in rmx_load_double()\n", stderr);
169 >                exit(1);
170 >        }
171 >        for (i = 0; i < rm->nrows; i++) {
172 >                if (getbinary(&rmx_lval(rm,i,0,0), sizeof(double)*rm->ncomp,
173 >                                        rm->ncols, fp) != rm->ncols)
174 >                        return(0);
175                  if (rm->swapin)
176 <                    swap64((char *)&rmx_lval(rm,i,j,0), rm->ncomp);
177 <            }
176 >                        swap64((char *)&rmx_lval(rm,i,0,0), rm->ncols*rm->ncomp);
177 >        }
178          return(1);
179   }
180  
# Line 189 | Line 203 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
203  
204   /* Load matrix from supported file type */
205   RMATRIX *
206 < rmx_load(const char *inspec)
206 > rmx_load(const char *inspec, RMPref rmp)
207   {
208 <        FILE            *fp = stdin;
208 >        FILE            *fp;
209          RMATRIX         dinfo;
210          RMATRIX         *dnew;
211  
212 <        if (!inspec) {                          /* reading from stdin? */
213 <                inspec = "<stdin>";
214 <                SET_FILE_BINARY(stdin);
212 >        if (!inspec)
213 >                inspec = stdin_name;
214 >        else if (!*inspec)
215 >                return(NULL);
216 >        if (inspec == stdin_name) {             /* reading from stdin? */
217 >                fp = stdin;
218          } else if (inspec[0] == '!') {
219                  if (!(fp = popen(inspec+1, "r")))
220                          return(NULL);
204                SET_FILE_BINARY(stdin);
221          } else {
222                  const char      *sp = inspec;   /* check suffix */
223                  while (*sp)
# Line 209 | Line 225 | rmx_load(const char *inspec)
225                  while (sp > inspec && sp[-1] != '.')
226                          --sp;
227                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
228 <                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
228 >                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
229 >                                        cm_loadBRDF(inspec, rmp==RMPreflB) ;
230                          if (!cm)
231                                  return(NULL);
232                          dnew = rmx_from_cmatrix(cm);
# Line 218 | Line 235 | rmx_load(const char *inspec)
235                          return(dnew);
236                  }
237                                                  /* else open it ourselves */
238 <                if (!(fp = fopen(inspec, "rb")))
238 >                if (!(fp = fopen(inspec, "r")))
239                          return(NULL);
240          }
241 +        SET_FILE_BINARY(fp);
242   #ifdef getc_unlocked
243          flockfile(fp);
244   #endif
# Line 228 | Line 246 | rmx_load(const char *inspec)
246          dinfo.dtype = DTascii;                  /* assumed w/o FORMAT */
247          dinfo.swapin = 0;
248          dinfo.info = NULL;
249 +        dinfo.mtx[0] = dinfo.mtx[1] = dinfo.mtx[2] = 1.;
250          if (getheader(fp, get_dminfo, &dinfo) < 0) {
251                  fclose(fp);
252                  return(NULL);
# Line 253 | Line 272 | rmx_load(const char *inspec)
272          dnew->info = dinfo.info;
273          switch (dinfo.dtype) {
274          case DTascii:
275 <                SET_FILE_TEXT(stdin);
275 >                SET_FILE_TEXT(fp);
276                  if (!rmx_load_ascii(dnew, fp))
277                          goto loaderr;
278                  dnew->dtype = DTascii;          /* should leave double? */
# Line 274 | Line 293 | rmx_load(const char *inspec)
293          case DTxyze:
294                  if (!rmx_load_rgbe(dnew, fp))
295                          goto loaderr;
296 <                dnew->dtype = dinfo.dtype;
296 >                dnew->dtype = dinfo.dtype;      /* undo exposure? */
297 >                if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) |
298 >                                (dinfo.mtx[2] != 1.)) {
299 >                        dinfo.mtx[0] = 1./dinfo.mtx[0];
300 >                        dinfo.mtx[1] = 1./dinfo.mtx[1];
301 >                        dinfo.mtx[2] = 1./dinfo.mtx[2];
302 >                        rmx_scale(dnew, dinfo.mtx);
303 >                }
304                  break;
305          default:
306                  goto loaderr;
# Line 372 | Line 398 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
398          return(1);
399   }
400  
401 + /* Check if CIE XYZ primaries were specified */
402 + static int
403 + findCIEprims(const char *info)
404 + {
405 +        RGBPRIMS        prims;
406 +
407 +        if (!info)
408 +                return(0);
409 +        info = strstr(info, PRIMARYSTR);
410 +        if (!info || !primsval(prims, info))
411 +                return(0);
412 +
413 +        return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) &&
414 +                        (prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) &&
415 +                        (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
416 + }
417 +
418   /* Write matrix to file type indicated by dtype */
419   int
420   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
# Line 389 | Line 432 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
432                  fputs(rm->info, fp);
433          if (dtype == DTfromHeader)
434                  dtype = rm->dtype;
435 <        else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
435 >        else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
436 >                                        findCIEprims(rm->info)))
437                  dtype = DTxyze;
438          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
439                  dtype = DTrgbe;
# Line 654 | Line 698 | rmx_transform(const RMATRIX *msrc, int n, const double
698                  return(NULL);
699          if (msrc->info) {
700                  char    buf[128];
701 <                sprintf(buf, "Applied %dx%d matrix transform\n",
701 >                sprintf(buf, "Applied %dx%d component transform\n",
702                                  dnew->ncomp, msrc->ncomp);
703                  rmx_addinfo(dnew, msrc->info);
704                  rmx_addinfo(dnew, buf);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines