| 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 */ | 
| 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 |  | } | 
| 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 |  |  | 
| 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 |  |  | 
| 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; | 
| 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)) | 
| 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)); | 
| 725 |  | int | 
| 726 |  | rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) | 
| 727 |  | { | 
| 728 | < | if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | | 
| 684 | < | (rdst->ncols != rsrc->ncols) | | 
| 685 | < | (rdst->ncomp != rsrc->ncomp)) | 
| 728 | > | if (!rdst | !rsrc) | 
| 729 |  | return(0); | 
| 687 | – |  | 
| 730 |  | if (dometa) {           /* transfer everything? */ | 
| 731 |  | rmx_reset(rdst); | 
| 732 |  | *rdst = *rsrc; | 
| 733 |  | rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; | 
| 734 |  | return(1); | 
| 735 |  | } | 
| 736 | < | #ifdef MAP_FILE                 /* just matrix data -- leave metadata */ | 
| 736 | > | /* just matrix data -- leave metadata */ | 
| 737 | > | if ((rdst->nrows != rsrc->nrows) | | 
| 738 | > | (rdst->ncols != rsrc->ncols) | | 
| 739 | > | (rdst->ncomp != rsrc->ncomp)) | 
| 740 | > | return(0); | 
| 741 | > | #ifdef MAP_FILE | 
| 742 |  | if (rdst->mapped) | 
| 743 |  | munmap(rdst->mapped, rmx_mapped_size(rdst)); | 
| 744 |  | else | 
| 758 |  | int | 
| 759 |  | rmx_transpose(RMATRIX *rm) | 
| 760 |  | { | 
| 761 | < | RMATRIX dnew; | 
| 762 | < | int     i, j; | 
| 761 | > | uby8            *bmap; | 
| 762 | > | rmx_dtype       val[MAXCOMP]; | 
| 763 | > | RMATRIX         dold; | 
| 764 | > | int             i, j; | 
| 765 |  |  | 
| 766 |  | if (!rm || !rm->mtx | (rm->ncomp > MAXCOMP)) | 
| 767 |  | return(0); | 
| 774 |  | return(1); | 
| 775 |  | } | 
| 776 |  | if (rm->nrows == rm->ncols) {   /* square matrix case */ | 
| 777 | < | rmx_dtype       val[MAXCOMP]; | 
| 778 | < | for (j = rm->ncols; j--; ) | 
| 779 | < | for (i = rm->nrows; i--; ) { | 
| 780 | < | if (i == j) continue; | 
| 732 | < | memcpy(val, rmx_val(rm,i,j), | 
| 777 | > | for (i = rm->nrows; i--; ) | 
| 778 | > | for (j = rm->ncols; j--; ) { | 
| 779 | > | if (i == j) continue; | 
| 780 | > | memcpy(val, rmx_val(rm,i,j), | 
| 781 |  | sizeof(rmx_dtype)*rm->ncomp); | 
| 782 | < | memcpy(rmx_lval(rm,i,j), rmx_val(rm,j,i), | 
| 782 | > | memcpy(rmx_lval(rm,i,j), rmx_val(rm,j,i), | 
| 783 |  | sizeof(rmx_dtype)*rm->ncomp); | 
| 784 | < | memcpy(rmx_val(rm,j,i), val, | 
| 784 | > | memcpy(rmx_val(rm,j,i), val, | 
| 785 |  | sizeof(rmx_dtype)*rm->ncomp); | 
| 786 | < | } | 
| 786 | > | } | 
| 787 |  | return(1); | 
| 788 |  | } | 
| 789 | < | memset(&dnew, 0, sizeof(dnew)); | 
| 790 | < | dnew.ncols = rm->nrows; dnew.nrows = rm->ncols; | 
| 791 | < | dnew.ncomp = rm->ncomp; | 
| 792 | < | if (!rmx_prepare(&dnew)) | 
| 789 | > | #define bmbyte(r,c)     bmap[((r)*rm->ncols+(c))>>3] | 
| 790 | > | #define bmbit(r,c)      (1 << ((r)*rm->ncols+(c) & 7)) | 
| 791 | > | #define bmop(r,c, op)   (bmbyte(r,c) op bmbit(r,c)) | 
| 792 | > | #define bmtest(r,c)     bmop(r,c,&) | 
| 793 | > | #define bmset(r,c)      bmop(r,c,|=) | 
| 794 | > | /* loop completion bitmap */ | 
| 795 | > | bmap = (uby8 *)calloc(((size_t)rm->nrows*rm->ncols+7)>>3, 1); | 
| 796 | > | if (!bmap) | 
| 797 |  | return(0); | 
| 798 | < | rmx_addinfo(&dnew, rm->info); | 
| 799 | < | dnew.dtype = rm->dtype; | 
| 800 | < | copycolor(dnew.cexp, rm->cexp); | 
| 801 | < | memcpy(dnew.wlpart, rm->wlpart, sizeof(dnew.wlpart)); | 
| 802 | < | for (j = dnew.ncols; j--; ) | 
| 803 | < | for (i = dnew.nrows; i--; ) | 
| 804 | < | memcpy(rmx_lval(&dnew,i,j), rmx_val(rm,j,i), | 
| 805 | < | sizeof(rmx_dtype)*dnew.ncomp); | 
| 806 | < | rmx_reset(rm);                  /* frees memory */ | 
| 807 | < | *rm = dnew;                     /* replace w/ transpose */ | 
| 798 | > | dold = *rm; | 
| 799 | > | rm->ncols = dold.nrows; rm->nrows = dold.ncols; | 
| 800 | > | for (i = rm->nrows; i--; )      /* try every starting point */ | 
| 801 | > | for (j = rm->ncols; j--; ) { | 
| 802 | > | int     i0, j0; | 
| 803 | > | int     i1 = i; | 
| 804 | > | size_t  j1 = j; | 
| 805 | > | if (bmtest(i, j)) | 
| 806 | > | continue;       /* traversed loop earlier */ | 
| 807 | > | memcpy(val, rmx_val(rm,i,j), | 
| 808 | > | sizeof(rmx_dtype)*rm->ncomp); | 
| 809 | > | for ( ; ; ) {           /* new transpose loop */ | 
| 810 | > | const rmx_dtype     *ds; | 
| 811 | > | i0 = i1; j0 = j1; | 
| 812 | > | ds = rmx_val(&dold, j0, i0); | 
| 813 | > | j1 = (ds - dold.mtx)/dold.ncomp; | 
| 814 | > | i1 = j1 / rm->ncols; | 
| 815 | > | j1 -= (size_t)i1*rm->ncols; | 
| 816 | > | bmset(i1, j1);      /* mark as done */ | 
| 817 | > | if ((i1 == i) & (j1 == j)) | 
| 818 | > | break;          /* back at start */ | 
| 819 | > | memcpy(rmx_lval(rm,i0,j0), ds, | 
| 820 | > | sizeof(rmx_dtype)*rm->ncomp); | 
| 821 | > | }                       /* complete the loop */ | 
| 822 | > | memcpy(rmx_lval(rm,i0,j0), val, | 
| 823 | > | sizeof(rmx_dtype)*rm->ncomp); | 
| 824 | > | } | 
| 825 | > | free(bmap);                     /* all done! */ | 
| 826 |  | return(1); | 
| 827 | + | #undef  bmbyte | 
| 828 | + | #undef  bmbit | 
| 829 | + | #undef  bmop | 
| 830 | + | #undef  bmtest | 
| 831 | + | #undef  bmset | 
| 832 |  | } | 
| 833 |  |  | 
| 834 |  | /* Multiply (concatenate) two matrices and allocate the result */ | 
| 852 |  | for (i = mres->nrows; i--; ) | 
| 853 |  | for (j = mres->ncols; j--; ) | 
| 854 |  | for (k = mres->ncomp; k--; ) { | 
| 855 | < | rmx_dtype   d = 0; | 
| 855 | > | double      d = 0; | 
| 856 |  | for (h = m1->ncols; h--; ) | 
| 857 | < | d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; | 
| 858 | < | rmx_lval(mres,i,j)[k] = d; | 
| 857 | > | d += (double)rmx_val(m1,i,h)[k] * | 
| 858 | > | rmx_val(m2,h,j)[k]; | 
| 859 | > | rmx_lval(mres,i,j)[k] = (rmx_dtype)d; | 
| 860 |  | } | 
| 861 |  | return(mres); | 
| 862 |  | } | 
| 948 |  | const rmx_dtype *da = rmx_val(madd,i,j); | 
| 949 |  | rmx_dtype       *ds = rmx_lval(msum,i,j); | 
| 950 |  | for (k = msum->ncomp; k--; ) | 
| 951 | < | ds[k] += sf[k] * da[k]; | 
| 951 | > | ds[k] += (rmx_dtype)sf[k] * da[k]; | 
| 952 |  | } | 
| 953 |  | if (mysf) | 
| 954 |  | free(mysf); | 
| 967 |  | for (j = rm->ncols; j--; ) { | 
| 968 |  | rmx_dtype       *dp = rmx_lval(rm,i,j); | 
| 969 |  | for (k = rm->ncomp; k--; ) | 
| 970 | < | dp[k] *= sf[k]; | 
| 970 | > | dp[k] *= (rmx_dtype)sf[k]; | 
| 971 |  | } | 
| 972 |  | if (rm->info) | 
| 973 |  | rmx_addinfo(rm, "Applied scalar\n"); | 
| 999 |  | for (j = dnew->ncols; j--; ) { | 
| 1000 |  | const rmx_dtype *ds = rmx_val(msrc,i,j); | 
| 1001 |  | for (kd = dnew->ncomp; kd--; ) { | 
| 1002 | < | rmx_dtype   d = 0; | 
| 1002 | > | double      d = 0; | 
| 1003 |  | for (ks = msrc->ncomp; ks--; ) | 
| 1004 |  | d += cmat[kd*msrc->ncomp + ks] * ds[ks]; | 
| 1005 | < | rmx_lval(dnew,i,j)[kd] = d; | 
| 1005 | > | rmx_lval(dnew,i,j)[kd] = (rmx_dtype)d; | 
| 1006 |  | } | 
| 1007 |  | } | 
| 1008 |  | return(dnew); | 
| 1012 |  | RMATRIX * | 
| 1013 |  | rmx_from_cmatrix(const CMATRIX *cm) | 
| 1014 |  | { | 
| 939 | – | int     i, j; | 
| 1015 |  | RMATRIX *dnew; | 
| 1016 |  |  | 
| 1017 |  | if (!cm) | 
| 1019 |  | dnew = rmx_alloc(cm->nrows, cm->ncols, 3); | 
| 1020 |  | if (!dnew) | 
| 1021 |  | return(NULL); | 
| 1022 | < | dnew->dtype = DTfloat; | 
| 1023 | < | for (i = dnew->nrows; i--; ) | 
| 1024 | < | for (j = dnew->ncols; j--; ) { | 
| 1025 | < | const COLORV    *cv = cm_lval(cm,i,j); | 
| 1026 | < | rmx_dtype       *dp = rmx_lval(dnew,i,j); | 
| 1027 | < | dp[0] = cv[0]; | 
| 1028 | < | dp[1] = cv[1]; | 
| 1029 | < | dp[2] = cv[2]; | 
| 1030 | < | } | 
| 1022 | > |  | 
| 1023 | > | dnew->dtype = sizeof(COLORV)==sizeof(float) ? | 
| 1024 | > | DTfloat : DTdouble; | 
| 1025 | > |  | 
| 1026 | > | if (sizeof(COLORV) == sizeof(rmx_dtype)) { | 
| 1027 | > | memcpy(dnew->mtx, cm->cmem, rmx_array_size(dnew)); | 
| 1028 | > | } else { | 
| 1029 | > | int     i, j; | 
| 1030 | > | for (i = dnew->nrows; i--; ) | 
| 1031 | > | for (j = dnew->ncols; j--; ) { | 
| 1032 | > | const COLORV    *cv = cm_lval(cm,i,j); | 
| 1033 | > | rmx_dtype       *dp = rmx_lval(dnew,i,j); | 
| 1034 | > | dp[0] = cv[0]; | 
| 1035 | > | dp[1] = cv[1]; | 
| 1036 | > | dp[2] = cv[2]; | 
| 1037 | > | } | 
| 1038 | > | } | 
| 1039 |  | return(dnew); | 
| 1040 |  | } | 
| 1041 |  |  | 
| 1043 |  | CMATRIX * | 
| 1044 |  | cm_from_rmatrix(const RMATRIX *rm) | 
| 1045 |  | { | 
| 963 | – | int     i, j; | 
| 1046 |  | CMATRIX *cnew; | 
| 1047 |  |  | 
| 1048 |  | if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP)) | 
| 1050 |  | cnew = cm_alloc(rm->nrows, rm->ncols); | 
| 1051 |  | if (!cnew) | 
| 1052 |  | return(NULL); | 
| 1053 | < | for (i = cnew->nrows; i--; ) | 
| 1054 | < | for (j = cnew->ncols; j--; ) { | 
| 1055 | < | const rmx_dtype *dp = rmx_val(rm,i,j); | 
| 1056 | < | COLORV          *cv = cm_lval(cnew,i,j); | 
| 1057 | < | switch (rm->ncomp) { | 
| 1058 | < | case 3: | 
| 1059 | < | setcolor(cv, dp[0], dp[1], dp[2]); | 
| 1060 | < | break; | 
| 1061 | < | case 1: | 
| 1062 | < | setcolor(cv, dp[0], dp[0], dp[0]); | 
| 1063 | < | break; | 
| 1064 | < | default: { | 
| 1065 | < | COLORV  scol[MAXCOMP]; | 
| 1066 | < | int     k; | 
| 1067 | < | for (k = rm->ncomp; k--; ) | 
| 1068 | < | scol[k] = dp[k]; | 
| 1069 | < | scolor2color(cv, scol, rm->ncomp, rm->wlpart); | 
| 1070 | < | } break; | 
| 1071 | < | } | 
| 1072 | < | } | 
| 1053 | > | if ((sizeof(COLORV) == sizeof(rmx_dtype)) & (rm->ncomp == 3)) { | 
| 1054 | > | memcpy(cnew->cmem, rm->mtx, rmx_array_size(rm)); | 
| 1055 | > | } else { | 
| 1056 | > | int     i, j; | 
| 1057 | > | for (i = cnew->nrows; i--; ) | 
| 1058 | > | for (j = cnew->ncols; j--; ) { | 
| 1059 | > | const rmx_dtype *dp = rmx_val(rm,i,j); | 
| 1060 | > | COLORV          *cv = cm_lval(cnew,i,j); | 
| 1061 | > | switch (rm->ncomp) { | 
| 1062 | > | case 3: | 
| 1063 | > | setcolor(cv, dp[0], dp[1], dp[2]); | 
| 1064 | > | break; | 
| 1065 | > | case 1: | 
| 1066 | > | setcolor(cv, dp[0], dp[0], dp[0]); | 
| 1067 | > | break; | 
| 1068 | > | default: | 
| 1069 | > | if (sizeof(COLORV) == sizeof(rmx_dtype)) { | 
| 1070 | > | scolor2color(cv, (const COLORV *)dp, | 
| 1071 | > | rm->ncomp, rm->wlpart); | 
| 1072 | > | } else { | 
| 1073 | > | COLORV  scol[MAXCOMP]; | 
| 1074 | > | int     k = rm->ncomp; | 
| 1075 | > | while (k--) scol[k] = dp[k]; | 
| 1076 | > | scolor2color(cv, scol, rm->ncomp, rm->wlpart); | 
| 1077 | > | } | 
| 1078 | > | break; | 
| 1079 | > | } | 
| 1080 | > | } | 
| 1081 | > | } | 
| 1082 |  | return(cnew); | 
| 1083 |  | } |