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.1 by greg, Sat May 31 05:02:37 2014 UTC vs.
Revision 2.5 by greg, Fri Aug 1 23:37:24 2014 UTC

# Line 12 | Line 12 | static const char RCSid[] = "$Id$";
12   #include "resolu.h"
13   #include "rmatrix.h"
14  
15 + #define MAX_INFO        16000
16 +
17   typedef struct {
18          int     nrows, ncols, ncomp;
19          int     dtype;
20 +        int     info_len;
21 +        char    info[MAX_INFO];
22   } DMINFO;
23  
24   /* Allocate a nr x nc matrix with n components */
# Line 30 | Line 34 | rmx_alloc(int nr, int nc, int n)
34          if (dnew == NULL)
35                  return(NULL);
36          dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n;
37 +        dnew->info = NULL;
38          return(dnew);
39   }
40  
41 + /* Append header information associated with matrix data */
42 + int
43 + rmx_addinfo(RMATRIX *rm, const char *info)
44 + {
45 +        if (!info || !*info)
46 +                return(0);
47 +        if (!rm->info)
48 +                rm->info = (char *)malloc(strlen(info)+1);
49 +        else
50 +                rm->info = (char *)realloc(rm->info,
51 +                                strlen(rm->info)+strlen(info)+1);
52 +        if (!rm->info)
53 +                return(0);
54 +        strcat(rm->info, info);
55 +        return(1);
56 + }
57 +
58   static int
59   get_dminfo(char *s, void *p)
60   {
61          DMINFO  *ip = (DMINFO *)p;
62 <        char    fmt[32];
62 >        char    fmt[64];
63          int     i;
64  
65          if (!strncmp(s, "NCOMP=", 6)) {
# Line 52 | Line 74 | get_dminfo(char *s, void *p)
74                  ip->ncols = atoi(s+6);
75                  return(0);
76          }
77 <        if (!formatval(fmt, s))
77 >        if (!formatval(fmt, s)) {
78 >                if (headidval(fmt, s))
79 >                        return(0);
80 >                while (*s) {
81 >                        if (ip->info_len == MAX_INFO-2 &&
82 >                                        ip->info[MAX_INFO-3] != '\n')
83 >                                ip->info[ip->info_len++] = '\n';
84 >                        if (ip->info_len >= MAX_INFO-1)
85 >                                break;
86 >                        ip->info[ip->info_len++] = *s++;
87 >                }
88 >                ip->info[ip->info_len] = '\0';
89                  return(0);
90 +        }
91          for (i = 1; i < DTend; i++)
92                  if (!strcmp(fmt, cm_fmt_id[i])) {
93                          ip->dtype = i;
# Line 173 | Line 207 | rmx_load(const char *fname)
207   #endif
208          dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0;
209          dinfo.dtype = DTascii;
210 <        if (getheader(fp, &get_dminfo, &dinfo) < 0) {
210 >        dinfo.info_len = 0;
211 >        if (getheader(fp, get_dminfo, &dinfo) < 0) {
212                  fclose(fp);
213                  return(NULL);
214          }
215 <        if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze)) {
215 >        if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) {
216                  if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) {
217                          fclose(fp);
218                          return(NULL);
219                  }
220 <                dinfo.ncomp = 3;
220 >                if (dinfo.ncomp <= 0)
221 >                        dinfo.ncomp = 3;
222 >                else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) &&
223 >                                dinfo.ncomp != 3) {
224 >                        fclose(fp);
225 >                        return(NULL);
226 >                }
227          }
228          dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp);
229          if (dnew == NULL) {
230                  fclose(fp);
231                  return(NULL);
232          }
233 +        if (dinfo.info_len)
234 +                rmx_addinfo(dnew, dinfo.info);
235          switch (dinfo.dtype) {
236          case DTascii:
237                  if (!rmx_load_ascii(dnew, fp))
# Line 309 | Line 352 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
352          if ((rm == NULL) | (fp == NULL))
353                  return(0);
354                                                  /* complete header */
355 +        if (rm->info)
356 +                fputs(rm->info, fp);
357          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
358                  fprintf(fp, "NROWS=%d\n", rm->nrows);
359                  fprintf(fp, "NCOLS=%d\n", rm->ncols);
# Line 377 | Line 422 | rmx_copy(const RMATRIX *rm)
422          dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
423          if (dnew == NULL)
424                  return(NULL);
425 +        rmx_addinfo(dnew, rm->info);
426          memcpy(dnew->mtx, rm->mtx,
427                  sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
428          return(dnew);
429   }
430  
431 < /* Swap rows and columns in the given matrix in situ */
432 < int
433 < rmx_transpose(RMATRIX *rm)
431 > /* Allocate and assign transposed matrix */
432 > RMATRIX *
433 > rmx_transpose(const RMATRIX *rm)
434   {
435 <        RMATRIX dswap;
435 >        RMATRIX *dnew;
436          int     i, j, k;
437  
438          if (rm == NULL)
439                  return(0);
440 <        dswap.nrows = rm->ncols;
441 <        dswap.ncols = rm->nrows;
442 <        dswap.ncomp = rm->ncomp;
443 <        for (i = 1; i < rm->nrows; i++)
444 <            for (j = 0; j < i; j++)
445 <                for (k = rm->ncomp; k--; ) {
446 <                        double  *opp = rm->mtx + rmx_indx(&dswap,j,i,k);
447 <                        double  d = *opp;
448 <                        *opp = rmx_lval(rm,i,j,k);
449 <                        rmx_lval(rm,i,j,k) = d;
450 <                }
451 <        rm->nrows = dswap.nrows;
406 <        rm->ncols = dswap.ncols;
407 <        return(1);
440 >        dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
441 >        if (dnew == NULL)
442 >                return(NULL);
443 >        if (rm->info) {
444 >                rmx_addinfo(dnew, rm->info);
445 >                rmx_addinfo(dnew, "Transposed rows and columns\n");
446 >        }
447 >        for (i = dnew->nrows; i--; )
448 >            for (j = dnew->ncols; j--; )
449 >                for (k = dnew->ncomp; k--; )
450 >                        rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k);
451 >        return(dnew);
452   }
453  
454   /* Multiply (concatenate) two matrices and allocate the result */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines