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.8 by greg, Wed Aug 27 13:33:47 2014 UTC vs.
Revision 2.21 by greg, Thu Aug 18 00:52:48 2016 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   #include <stdlib.h>
10   #include <string.h>
11   #include <fcntl.h>
12 + #include "rtio.h"
13 + #include "platform.h"
14   #include "resolu.h"
15 + #include "paths.h"
16   #include "rmatrix.h"
17  
18   static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
# Line 46 | Line 49 | rmx_free(RMATRIX *rm)
49   int
50   rmx_newtype(int dtyp1, int dtyp2)
51   {
52 <        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2)
52 >        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
53 >                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
54 >                        && dtyp1 != dtyp2)
55                  return(0);
56          if (dtyp1 < dtyp2)
57                  return(dtyp1);
# Line 108 | Line 113 | static int
113   rmx_load_ascii(RMATRIX *rm, FILE *fp)
114   {
115          int     i, j, k;
116 < #ifdef _WIN32
112 <        _setmode(fileno(fp), _O_TEXT);
113 < #endif
116 >
117          for (i = 0; i < rm->nrows; i++)
118              for (j = 0; j < rm->ncols; j++)
119                  for (k = 0; k < rm->ncomp; k++)
# Line 131 | Line 134 | rmx_load_float(RMATRIX *rm, FILE *fp)
134          }
135          for (i = 0; i < rm->nrows; i++)
136              for (j = 0; j < rm->ncols; j++) {
137 <                if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
137 >                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
138                      return(0);
139                  for (k = rm->ncomp; k--; )
140                       rmx_lval(rm,i,j,k) = val[k];
# Line 151 | Line 154 | rmx_load_double(RMATRIX *rm, FILE *fp)
154          }
155          for (i = 0; i < rm->nrows; i++)
156              for (j = 0; j < rm->ncols; j++) {
157 <                if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
157 >                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
158                      return(0);
159                  for (k = rm->ncomp; k--; )
160                       rmx_lval(rm,i,j,k) = val[k];
# Line 184 | Line 187 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
187  
188   /* Load matrix from supported file type */
189   RMATRIX *
190 < rmx_load(const char *fname)
190 > rmx_load(const char *inspec)
191   {
192          FILE            *fp = stdin;
193          RMATRIX         dinfo;
194          RMATRIX         *dnew;
195  
196 <        if (fname == NULL) {                    /* reading from stdin? */
197 <                fname = "<stdin>";
198 < #ifdef _WIN32
196 >        if (inspec == NULL) {                   /* reading from stdin? */
197 >                inspec = "<stdin>";
198 > #if defined(_WIN32) || defined(_WIN64)
199                  _setmode(fileno(stdin), _O_BINARY);
200   #endif
201 +        } else if (inspec[0] == '!') {
202 +                if ((fp = popen(inspec+1, "r")) == NULL)
203 +                        return(NULL);
204 + #if defined(_WIN32) || defined(_WIN64)
205 +                _setmode(fileno(fp), _O_BINARY);
206 + #endif
207          } else {
208 <                const char      *sp = fname;    /* check suffix */
208 >                const char      *sp = inspec;   /* check suffix */
209                  while (*sp)
210                          ++sp;
211 <                while (sp > fname && sp[-1] != '.')
211 >                while (sp > inspec && sp[-1] != '.')
212                          --sp;
213                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
214 <                        CMATRIX *cm = cm_loadBTDF((char *)fname);
214 >                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
215                          if (cm == NULL)
216                                  return(NULL);
217                          dnew = rmx_from_cmatrix(cm);
218                          cm_free(cm);
219 +                        dnew->dtype = DTascii;
220                          return(dnew);
221                  }
222                                                  /* else open it ourselves */
223 <                if ((fp = fopen(fname, "rb")) == NULL)
223 >                if ((fp = fopen(inspec, "rb")) == NULL)
224                          return(NULL);
225          }
226   #ifdef getc_unlocked
# Line 244 | Line 254 | rmx_load(const char *fname)
254          dnew->info = dinfo.info;
255          switch (dinfo.dtype) {
256          case DTascii:
257 + #if defined(_WIN32) || defined(_WIN64)
258 +                _setmode(fileno(fp), _O_TEXT);
259 + #endif
260                  if (!rmx_load_ascii(dnew, fp))
261                          goto loaderr;
262 +                dnew->dtype = DTascii;          /* should leave double? */
263                  break;
264          case DTfloat:
265                  if (!rmx_load_float(dnew, fp))
# Line 266 | Line 280 | rmx_load(const char *fname)
280          default:
281                  goto loaderr;
282          }
283 <        if (fp != stdin)
284 <                fclose(fp);
283 >        if (fp != stdin) {
284 >                if (inspec[0] == '!')
285 >                        pclose(fp);
286 >                else
287 >                        fclose(fp);
288 >        }
289 > #ifdef getc_unlocked
290 >        else
291 >                funlockfile(fp);
292 > #endif
293          return(dnew);
294   loaderr:                                        /* should report error? */
295 <        fclose(fp);
295 >        if (inspec[0] == '!')
296 >                pclose(fp);
297 >        else
298 >                fclose(fp);
299          rmx_free(dnew);
300          return(NULL);
301   }
# Line 279 | Line 304 | static int
304   rmx_write_ascii(const RMATRIX *rm, FILE *fp)
305   {
306          int     i, j, k;
307 < #ifdef _WIN32
283 <        _setmode(fileno(fp), _O_TEXT);
284 < #endif
307 >
308          for (i = 0; i < rm->nrows; i++) {
309              for (j = 0; j < rm->ncols; j++) {
310                  for (k = 0; k < rm->ncomp; k++)
# Line 307 | Line 330 | rmx_write_float(const RMATRIX *rm, FILE *fp)
330              for (j = 0; j < rm->ncols; j++) {
331                  for (k = rm->ncomp; k--; )
332                      val[k] = (float)rmx_lval(rm,i,j,k);
333 <                if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
333 >                if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
334                          return(0);
335              }
336          return(1);
# Line 327 | Line 350 | rmx_write_double(const RMATRIX *rm, FILE *fp)
350              for (j = 0; j < rm->ncols; j++) {
351                  for (k = rm->ncomp; k--; )
352                      val[k] = rmx_lval(rm,i,j,k);
353 <                if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
353 >                if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
354                          return(0);
355              }
356          return(1);
# Line 356 | Line 379 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
379   }
380  
381   /* Write matrix to file type indicated by dtype */
382 < long
382 > int
383   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
384   {
385          RMATRIX *mydm = NULL;
# Line 364 | Line 387 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
387  
388          if ((rm == NULL) | (fp == NULL))
389                  return(0);
390 + #ifdef getc_unlocked
391 +        flockfile(fp);
392 + #endif
393                                                  /* complete header */
394          if (rm->info)
395                  fputs(rm->info, fp);
# Line 371 | Line 397 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
397                  dtype = rm->dtype;
398          else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
399                  dtype = DTxyze;
400 <        else if ((dtype = DTxyze) & (rm->dtype == DTrgbe))
400 >        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
401                  dtype = DTrgbe;
402          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
403                  fprintf(fp, "NROWS=%d\n", rm->nrows);
# Line 408 | Line 434 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
434                  return(0);
435          }
436          ok &= (fflush(fp) == 0);
437 + #ifdef getc_unlocked
438 +        funlockfile(fp);
439 + #endif
440          rmx_free(mydm);
441 <        return(ftell(fp) * ok);         /* return # bytes written */
441 >        return(ok);
442   }
443  
444   /* Allocate and assign square identity matrix with n components */
# Line 417 | Line 446 | RMATRIX *
446   rmx_identity(const int dim, const int n)
447   {
448          RMATRIX *rid = rmx_alloc(dim, dim, n);
449 <        int     i;
449 >        int     i, k;
450  
451          if (rid == NULL)
452                  return(NULL);
453 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
453 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
454          for (i = dim; i--; )
455 <                rmx_lval(rid,i,i,0) = 1;
456 <        for (i = n; --i; )
428 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
429 <                                sizeof(rid->mtx[0])*dim*dim);
455 >            for (k = n; k--; )
456 >                rmx_lval(rid,i,i,k) = 1;
457          return(rid);
458   }
459  
# Line 495 | Line 522 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
522                  for (k = mres->ncomp; k--; ) {
523                      long double d = 0;
524                      for (h = m1->ncols; h--; )
525 <                        d += (long double)rmx_lval(m1,i,h,k) *
499 <                                (long double)rmx_lval(m2,h,j,k);
525 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
526                      rmx_lval(mres,i,j,k) = (double)d;
527                  }
528          return(mres);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines