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.88 by greg, Fri Apr 4 02:53:03 2025 UTC vs.
Revision 2.90 by greg, Fri Apr 4 22:47:56 2025 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include <sys/mman.h>
17   #endif
18  
19 #ifndef MAXCOMP
20 #define MAXCOMP         MAXCSAMP        /* #components we support */
21 #endif
22
19   static const char       rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
20  
21   /* Initialize a RMATRIX struct but don't allocate array space */
# Line 198 | Line 194 | rmx_load_ascii(rmx_dtype *drp, const RMATRIX *rm, FILE
194  
195          for (j = 0; j < rm->ncols; j++)
196                  for (k = rm->ncomp; k-- > 0; )
197 <                        if (fscanf(fp, "%lf", drp++) != 1)
197 >                        if (fscanf(fp, rmx_scanfmt, drp++) != 1)
198                                  return(0);
199          return(1);
200   }
# Line 206 | Line 202 | rmx_load_ascii(rmx_dtype *drp, const RMATRIX *rm, FILE
202   static int
203   rmx_load_float(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
204   {
205 + #if DTrmx_native==DTfloat
206 +        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
207 +                return(0);
208 +        if (rm->pflags & RMF_SWAPIN)
209 +                swap32((char *)drp, rm->ncols*rm->ncomp);
210 + #else
211          int     j, k;
212          float   val[MAXCOMP];
213  
# Line 221 | Line 223 | rmx_load_float(rmx_dtype *drp, const RMATRIX *rm, FILE
223                  for (k = 0; k < rm->ncomp; k++)
224                          *drp++ = val[k];
225          }
226 + #endif
227          return(1);
228   }
229  
230   static int
231   rmx_load_double(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
232   {
233 + #if DTrmx_native==DTdouble
234          if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
235                  return(0);
236          if (rm->pflags & RMF_SWAPIN)
237                  swap64((char *)drp, rm->ncols*rm->ncomp);
238 + #else
239 +        int     j, k;
240 +        double  val[MAXCOMP];
241 +
242 +        if (rm->ncomp > MAXCOMP) {
243 +                fputs("Unsupported # components in rmx_load_double()\n", stderr);
244 +                exit(1);
245 +        }
246 +        for (j = 0; j < rm->ncols; j++) {
247 +                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
248 +                        return(0);
249 +                if (rm->pflags & RMF_SWAPIN)
250 +                        swap64((char *)val, rm->ncomp);
251 +                for (k = 0; k < rm->ncomp; k++)
252 +                        *drp++ = (float)val[k];
253 +        }
254 + #endif
255          return(1);
256   }
257  
# Line 451 | Line 472 | rmx_load(const char *inspec, RMPref rmp)
472          return(dnew);
473   }
474  
475 + #if DTrmx_native==DTdouble
476   static int
477 < rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp)
477 > rmx_write_float(const rmx_dtype *dp, int len, FILE *fp)
478   {
479 <        while (len-- > 0) {
480 <                int     k = nc;
481 <                while (k-- > 0)
482 <                        fprintf(fp, " %.7e", *dp++);
483 <                fputc('\t', fp);
479 >        float   val;
480 >
481 >        while (len--) {
482 >                val = (float)*dp++;
483 >                if (putbinary(&val, sizeof(val), 1, fp) != 1)
484 >                        return(0);
485          }
486 <        return(fputc('\n', fp) != EOF);
486 >        return(1);
487   }
488 <
488 > #else
489   static int
490 < rmx_write_float(const rmx_dtype *dp, int len, FILE *fp)
490 > rmx_write_double(const rmx_dtype *dp, int len, FILE *fp)
491   {
492 <        float   val;
492 >        double  val;
493  
494          while (len--) {
495                  val = *dp++;
496 <                if (putbinary(&val, sizeof(float), 1, fp) != 1)
496 >                if (putbinary(&val, sizeof(val), 1, fp) != 1)
497                          return(0);
498          }
499          return(1);
500   }
501 + #endif
502  
503   static int
504 + rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp)
505 + {
506 +        while (len-- > 0) {
507 +                int     k = nc;
508 +                while (k-- > 0)
509 +                        fprintf(fp, " %.7e", *dp++);
510 +                fputc('\t', fp);
511 +        }
512 +        return(fputc('\n', fp) != EOF);
513 + }
514 +
515 + static int
516   rmx_write_rgbe(const rmx_dtype *dp, int nc, int len, FILE *fp)
517   {
518          COLR    *scan;
# Line 538 | Line 574 | rmx_write_header(const RMATRIX *rm, int dtype, FILE *f
574                  return(0);
575          if (rm->info)
576                  fputs(rm->info, fp);
577 <        if (dtype == DTfromHeader)
577 >        if (dtype == DTfromHeader) {
578                  dtype = rm->dtype;
579 <        else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
579 > #if DTrmx_native==DTfloat
580 >                if (dtype == DTdouble)          /* but stored as float? */
581 >                        dtype = DTfloat;
582 > #endif
583 >        } else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
584                                          findCIEprims(rm->info)))
585                  dtype = DTxyze;
586          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
# Line 587 | Line 627 | int
627   rmx_write_data(const rmx_dtype *dp, int nc, int len, int dtype, FILE *fp)
628   {
629          switch (dtype) {
630 <        case DTascii:
591 <                return(rmx_write_ascii(dp, nc, len, fp));
630 > #if DTrmx_native==DTdouble
631          case DTfloat:
632                  return(rmx_write_float(dp, nc*len, fp));
633 + #else
634 +        case DTdouble:
635 +                return(rmx_write_double(dp, nc*len, fp));
636 + #endif
637          case DTrmx_native:
638                  return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
639 +        case DTascii:
640 +                return(rmx_write_ascii(dp, nc, len, fp));
641          case DTrgbe:
642          case DTxyze:
643                  return(rmx_write_rgbe(dp, nc, len, fp));
# Line 778 | Line 823 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
823          for (i = mres->nrows; i--; )
824              for (j = mres->ncols; j--; )
825                  for (k = mres->ncomp; k--; ) {
826 <                    rmx_dtype   d = 0;
826 >                    double      d = 0;
827                      for (h = m1->ncols; h--; )
828 <                        d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k];
829 <                    rmx_lval(mres,i,j)[k] = d;
828 >                        d += (double)rmx_val(m1,i,h)[k] *
829 >                                        rmx_val(m2,h,j)[k];
830 >                    rmx_lval(mres,i,j)[k] = (rmx_dtype)d;
831                  }
832          return(mres);
833   }
# Line 873 | Line 919 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
919                  const rmx_dtype *da = rmx_val(madd,i,j);
920                  rmx_dtype       *ds = rmx_lval(msum,i,j);
921                  for (k = msum->ncomp; k--; )
922 <                     ds[k] += sf[k] * da[k];
922 >                     ds[k] += (rmx_dtype)sf[k] * da[k];
923              }
924          if (mysf)
925                  free(mysf);
# Line 892 | Line 938 | rmx_scale(RMATRIX *rm, const double sf[])
938              for (j = rm->ncols; j--; ) {
939                  rmx_dtype       *dp = rmx_lval(rm,i,j);
940                  for (k = rm->ncomp; k--; )
941 <                    dp[k] *= sf[k];
941 >                    dp[k] *= (rmx_dtype)sf[k];
942              }
943          if (rm->info)
944                  rmx_addinfo(rm, "Applied scalar\n");
# Line 924 | Line 970 | rmx_transform(const RMATRIX *msrc, int n, const double
970              for (j = dnew->ncols; j--; ) {
971                  const rmx_dtype *ds = rmx_val(msrc,i,j);
972                  for (kd = dnew->ncomp; kd--; ) {
973 <                    rmx_dtype   d = 0;
973 >                    double      d = 0;
974                      for (ks = msrc->ncomp; ks--; )
975                          d += cmat[kd*msrc->ncomp + ks] * ds[ks];
976 <                    rmx_lval(dnew,i,j)[kd] = d;
976 >                    rmx_lval(dnew,i,j)[kd] = (rmx_dtype)d;
977                  }
978              }
979          return(dnew);
# Line 937 | Line 983 | rmx_transform(const RMATRIX *msrc, int n, const double
983   RMATRIX *
984   rmx_from_cmatrix(const CMATRIX *cm)
985   {
940        int     i, j;
986          RMATRIX *dnew;
987  
988          if (!cm)
# Line 945 | Line 990 | rmx_from_cmatrix(const CMATRIX *cm)
990          dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
991          if (!dnew)
992                  return(NULL);
993 <        dnew->dtype = DTfloat;
994 <        for (i = dnew->nrows; i--; )
995 <            for (j = dnew->ncols; j--; ) {
996 <                const COLORV    *cv = cm_lval(cm,i,j);
997 <                rmx_dtype       *dp = rmx_lval(dnew,i,j);
998 <                dp[0] = cv[0];
999 <                dp[1] = cv[1];
1000 <                dp[2] = cv[2];
1001 <            }
993 >
994 >        dnew->dtype = sizeof(COLORV)==sizeof(float) ?
995 >                        DTfloat : DTdouble;
996 >
997 >        if (sizeof(COLORV) == sizeof(rmx_dtype)) {
998 >                memcpy(dnew->mtx, cm->cmem, rmx_array_size(dnew));
999 >        } else {
1000 >                int     i, j;
1001 >                for (i = dnew->nrows; i--; )
1002 >                    for (j = dnew->ncols; j--; ) {
1003 >                        const COLORV    *cv = cm_lval(cm,i,j);
1004 >                        rmx_dtype       *dp = rmx_lval(dnew,i,j);
1005 >                        dp[0] = cv[0];
1006 >                        dp[1] = cv[1];
1007 >                        dp[2] = cv[2];
1008 >                    }
1009 >        }
1010          return(dnew);
1011   }
1012  
# Line 961 | Line 1014 | rmx_from_cmatrix(const CMATRIX *cm)
1014   CMATRIX *
1015   cm_from_rmatrix(const RMATRIX *rm)
1016   {
964        int     i, j;
1017          CMATRIX *cnew;
1018  
1019          if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP))
# Line 969 | Line 1021 | cm_from_rmatrix(const RMATRIX *rm)
1021          cnew = cm_alloc(rm->nrows, rm->ncols);
1022          if (!cnew)
1023                  return(NULL);
1024 <        for (i = cnew->nrows; i--; )
1025 <            for (j = cnew->ncols; j--; ) {
1026 <                const rmx_dtype *dp = rmx_val(rm,i,j);
1027 <                COLORV          *cv = cm_lval(cnew,i,j);
1028 <                switch (rm->ncomp) {
1029 <                case 3:
1030 <                    setcolor(cv, dp[0], dp[1], dp[2]);
1031 <                    break;
1032 <                case 1:
1033 <                    setcolor(cv, dp[0], dp[0], dp[0]);
1034 <                    break;
1035 <                default: {
1036 <                        COLORV  scol[MAXCOMP];
1037 <                        int     k;
1038 <                        for (k = rm->ncomp; k--; )
1039 <                                scol[k] = dp[k];
1040 <                        scolor2color(cv, scol, rm->ncomp, rm->wlpart);
1041 <                    } break;
1042 <                }
1043 <            }
1024 >        if ((sizeof(COLORV) == sizeof(rmx_dtype)) & (rm->ncomp == 3)) {
1025 >                memcpy(cnew->cmem, rm->mtx, rmx_array_size(rm));
1026 >        } else {
1027 >                int     i, j;
1028 >                for (i = cnew->nrows; i--; )
1029 >                    for (j = cnew->ncols; j--; ) {
1030 >                        const rmx_dtype *dp = rmx_val(rm,i,j);
1031 >                        COLORV          *cv = cm_lval(cnew,i,j);
1032 >                        switch (rm->ncomp) {
1033 >                        case 3:
1034 >                            setcolor(cv, dp[0], dp[1], dp[2]);
1035 >                            break;
1036 >                        case 1:
1037 >                            setcolor(cv, dp[0], dp[0], dp[0]);
1038 >                            break;
1039 >                        default: {
1040 >                                COLORV  scol[MAXCOMP];
1041 >                                int     k;
1042 >                                for (k = rm->ncomp; k--; )
1043 >                                        scol[k] = dp[k];
1044 >                                scolor2color(cv, scol, rm->ncomp, rm->wlpart);
1045 >                            } break;
1046 >                        }
1047 >                    }
1048 >        }
1049          return(cnew);
1050   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines