| 16 | 
  | 
#include <sys/mman.h> | 
| 17 | 
  | 
#endif | 
| 18 | 
  | 
 | 
| 19 | 
+ | 
#ifndef MAXCOMP | 
| 20 | 
+ | 
#define MAXCOMP         MAXCSAMP        /* #components we support */ | 
| 21 | 
+ | 
#endif | 
| 22 | 
+ | 
 | 
| 23 | 
  | 
static const char       rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; | 
| 24 | 
  | 
 | 
| 21 | 
– | 
#define array_size(rm)  (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp) | 
| 22 | 
– | 
#define mapped_size(rm) ((char *)(rm)->mtx + array_size(rm) - (char *)(rm)->mapped) | 
| 23 | 
– | 
 | 
| 25 | 
  | 
/* Initialize a RMATRIX struct but don't allocate array space */ | 
| 26 | 
  | 
RMATRIX * | 
| 27 | 
  | 
rmx_new(int nr, int nc, int n) | 
| 35 | 
  | 
        if (!dnew) | 
| 36 | 
  | 
                return(NULL); | 
| 37 | 
  | 
 | 
| 38 | 
< | 
        dnew->dtype = DTdouble; | 
| 38 | 
> | 
        dnew->dtype = DTrmx_native; | 
| 39 | 
  | 
        dnew->nrows = nr; | 
| 40 | 
  | 
        dnew->ncols = nc; | 
| 41 | 
  | 
        dnew->ncomp = n; | 
| 50 | 
  | 
rmx_prepare(RMATRIX *rm) | 
| 51 | 
  | 
{ | 
| 52 | 
  | 
        if (!rm) return(0); | 
| 53 | 
< | 
        if (rm->mtx) | 
| 53 | 
> | 
        if (rm->mtx)                    /* assume it's right size */ | 
| 54 | 
  | 
                return(1); | 
| 55 | 
  | 
        if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) | 
| 56 | 
  | 
                return(0); | 
| 57 | 
< | 
        rm->mtx = (double *)malloc(array_size(rm)); | 
| 57 | 
> | 
        rm->mtx = (rmx_dtype *)malloc(rmx_array_size(rm)); | 
| 58 | 
> | 
        rm->pflags |= RMF_FREEMEM; | 
| 59 | 
  | 
        return(rm->mtx != NULL); | 
| 60 | 
  | 
} | 
| 61 | 
  | 
 | 
| 65 | 
  | 
{ | 
| 66 | 
  | 
        RMATRIX *dnew = rmx_new(nr, nc, n); | 
| 67 | 
  | 
 | 
| 68 | 
< | 
        if (dnew && !rmx_prepare(dnew)) { | 
| 68 | 
> | 
        if (!rmx_prepare(dnew)) { | 
| 69 | 
  | 
                rmx_free(dnew); | 
| 70 | 
< | 
                dnew = NULL; | 
| 70 | 
> | 
                return(NULL); | 
| 71 | 
  | 
        } | 
| 72 | 
  | 
        return(dnew); | 
| 73 | 
  | 
} | 
| 81 | 
  | 
                free(rm->info); | 
| 82 | 
  | 
                rm->info = NULL; | 
| 83 | 
  | 
        } | 
| 82 | 
– | 
        if (rm->mtx) { | 
| 84 | 
  | 
#ifdef MAP_FILE | 
| 85 | 
< | 
                if (rm->mapped) { | 
| 86 | 
< | 
                        munmap(rm->mapped, mapped_size(rm)); | 
| 87 | 
< | 
                        rm->mapped = NULL; | 
| 88 | 
< | 
                } else | 
| 85 | 
> | 
        if (rm->mapped) { | 
| 86 | 
> | 
                munmap(rm->mapped, rmx_mapped_size(rm)); | 
| 87 | 
> | 
                rm->mapped = NULL; | 
| 88 | 
> | 
        } else | 
| 89 | 
  | 
#endif | 
| 90 | 
< | 
                        free(rm->mtx); | 
| 91 | 
< | 
                rm->mtx = NULL; | 
| 90 | 
> | 
        if (rm->pflags & RMF_FREEMEM) { | 
| 91 | 
> | 
                free(rm->mtx); | 
| 92 | 
> | 
                rm->pflags &= ~RMF_FREEMEM; | 
| 93 | 
  | 
        } | 
| 94 | 
+ | 
        rm->mtx = NULL; | 
| 95 | 
  | 
} | 
| 96 | 
  | 
 | 
| 97 | 
  | 
/* Free an RMATRIX struct and data */ | 
| 120 | 
  | 
int | 
| 121 | 
  | 
rmx_addinfo(RMATRIX *rm, const char *info) | 
| 122 | 
  | 
{ | 
| 123 | 
< | 
        int     oldlen = 0; | 
| 123 | 
> | 
        size_t  oldlen = 0; | 
| 124 | 
  | 
 | 
| 125 | 
  | 
        if (!rm || !info || !*info) | 
| 126 | 
  | 
                return(0); | 
| 127 | 
  | 
        if (!rm->info) { | 
| 128 | 
  | 
                rm->info = (char *)malloc(strlen(info)+1); | 
| 126 | 
– | 
                if (rm->info) rm->info[0] = '\0'; | 
| 129 | 
  | 
        } else { | 
| 130 | 
  | 
                oldlen = strlen(rm->info); | 
| 131 | 
  | 
                rm->info = (char *)realloc(rm->info, | 
| 144 | 
  | 
        char    fmt[MAXFMTLEN]; | 
| 145 | 
  | 
        int     i; | 
| 146 | 
  | 
 | 
| 147 | 
< | 
        if (headidval(NULL, s)) | 
| 147 | 
> | 
        if (isheadid(s)) | 
| 148 | 
  | 
                return(0); | 
| 149 | 
  | 
        if (isncomp(s)) { | 
| 150 | 
  | 
                ip->ncomp = ncompval(s); | 
| 151 | 
< | 
                return(0); | 
| 151 | 
> | 
                return(ip->ncomp - 1); | 
| 152 | 
  | 
        } | 
| 153 | 
  | 
        if (!strncmp(s, "NROWS=", 6)) { | 
| 154 | 
  | 
                ip->nrows = atoi(s+6); | 
| 155 | 
< | 
                return(0); | 
| 155 | 
> | 
                return(ip->nrows - 1); | 
| 156 | 
  | 
        } | 
| 157 | 
  | 
        if (!strncmp(s, "NCOLS=", 6)) { | 
| 158 | 
  | 
                ip->ncols = atoi(s+6); | 
| 159 | 
< | 
                return(0); | 
| 159 | 
> | 
                return(ip->ncols - 1); | 
| 160 | 
  | 
        } | 
| 161 | 
  | 
        if ((i = isbigendian(s)) >= 0) { | 
| 162 | 
< | 
                ip->swapin = (nativebigendian() != i); | 
| 162 | 
> | 
                if (nativebigendian() != i) | 
| 163 | 
> | 
                        ip->pflags |= RMF_SWAPIN; | 
| 164 | 
> | 
                else | 
| 165 | 
> | 
                        ip->pflags &= ~RMF_SWAPIN; | 
| 166 | 
  | 
                return(0); | 
| 167 | 
  | 
        } | 
| 168 | 
  | 
        if (isexpos(s)) { | 
| 169 | 
  | 
                float   f = exposval(s); | 
| 170 | 
  | 
                scalecolor(ip->cexp, f); | 
| 171 | 
< | 
                return(0); | 
| 171 | 
> | 
                return(f > .0 ? 0 : -1); | 
| 172 | 
  | 
        } | 
| 173 | 
  | 
        if (iscolcor(s)) { | 
| 174 | 
  | 
                COLOR   ctmp; | 
| 175 | 
< | 
                colcorval(ctmp, s); | 
| 175 | 
> | 
                if (!colcorval(ctmp, s)) return(-1); | 
| 176 | 
  | 
                multcolor(ip->cexp, ctmp); | 
| 177 | 
  | 
                return(0); | 
| 178 | 
  | 
        } | 
| 179 | 
< | 
        if (iswlsplit(s)) { | 
| 180 | 
< | 
                wlsplitval(ip->wlpart, s); | 
| 181 | 
< | 
                return(0); | 
| 177 | 
< | 
        } | 
| 179 | 
> | 
        if (iswlsplit(s)) | 
| 180 | 
> | 
                return(wlsplitval(ip->wlpart, s) - 1); | 
| 181 | 
> | 
 | 
| 182 | 
  | 
        if (!formatval(fmt, s)) { | 
| 183 | 
  | 
                rmx_addinfo(ip, s); | 
| 184 | 
  | 
                return(0); | 
| 188 | 
  | 
                        ip->dtype = i; | 
| 189 | 
  | 
                        return(0); | 
| 190 | 
  | 
                } | 
| 191 | 
< | 
        return(-1); | 
| 191 | 
> | 
        return(-1);             /* bad format */ | 
| 192 | 
  | 
} | 
| 193 | 
  | 
 | 
| 194 | 
  | 
static int | 
| 195 | 
< | 
rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp) | 
| 195 | 
> | 
rmx_load_ascii(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 196 | 
  | 
{ | 
| 197 | 
  | 
        int     j, k; | 
| 198 | 
  | 
 | 
| 204 | 
  | 
} | 
| 205 | 
  | 
 | 
| 206 | 
  | 
static int | 
| 207 | 
< | 
rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp) | 
| 207 | 
> | 
rmx_load_float(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 208 | 
  | 
{ | 
| 209 | 
  | 
        int     j, k; | 
| 210 | 
< | 
        float   val[100]; | 
| 210 | 
> | 
        float   val[MAXCOMP]; | 
| 211 | 
  | 
 | 
| 212 | 
< | 
        if (rm->ncomp > 100) { | 
| 212 | 
> | 
        if (rm->ncomp > MAXCOMP) { | 
| 213 | 
  | 
                fputs("Unsupported # components in rmx_load_float()\n", stderr); | 
| 214 | 
  | 
                exit(1); | 
| 215 | 
  | 
        } | 
| 216 | 
  | 
        for (j = 0; j < rm->ncols; j++) { | 
| 217 | 
  | 
                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) | 
| 218 | 
  | 
                        return(0); | 
| 219 | 
< | 
                if (rm->swapin) | 
| 219 | 
> | 
                if (rm->pflags & RMF_SWAPIN) | 
| 220 | 
  | 
                        swap32((char *)val, rm->ncomp); | 
| 221 | 
  | 
                for (k = 0; k < rm->ncomp; k++) | 
| 222 | 
  | 
                        *drp++ = val[k]; | 
| 225 | 
  | 
} | 
| 226 | 
  | 
 | 
| 227 | 
  | 
static int | 
| 228 | 
< | 
rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp) | 
| 228 | 
> | 
rmx_load_double(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 229 | 
  | 
{ | 
| 230 | 
  | 
        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) | 
| 231 | 
  | 
                return(0); | 
| 232 | 
< | 
        if (rm->swapin) | 
| 232 | 
> | 
        if (rm->pflags & RMF_SWAPIN) | 
| 233 | 
  | 
                swap64((char *)drp, rm->ncols*rm->ncomp); | 
| 234 | 
  | 
        return(1); | 
| 235 | 
  | 
} | 
| 236 | 
  | 
 | 
| 237 | 
  | 
static int | 
| 238 | 
< | 
rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp) | 
| 238 | 
> | 
rmx_load_rgbe(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 239 | 
  | 
{ | 
| 240 | 
  | 
        COLR    *scan; | 
| 241 | 
  | 
        COLOR   col; | 
| 258 | 
  | 
} | 
| 259 | 
  | 
 | 
| 260 | 
  | 
static int | 
| 261 | 
< | 
rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp) | 
| 261 | 
> | 
rmx_load_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 262 | 
  | 
{ | 
| 263 | 
< | 
        uby8    *scan; | 
| 264 | 
< | 
        SCOLOR  scol; | 
| 263 | 
> | 
        COLRV   *scan; | 
| 264 | 
> | 
        COLORV  scol[MAXCOMP]; | 
| 265 | 
  | 
        int     j, k; | 
| 266 | 
  | 
 | 
| 267 | 
< | 
        if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP)) | 
| 267 | 
> | 
        if ((rm->ncomp < 3) | (rm->ncomp > MAXCOMP)) | 
| 268 | 
  | 
                return(0); | 
| 269 | 
< | 
        scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols); | 
| 269 | 
> | 
        scan = (COLRV *)tempbuffer((rm->ncomp+1)*rm->ncols); | 
| 270 | 
  | 
        if (!scan) | 
| 271 | 
  | 
                return(0); | 
| 272 | 
  | 
        if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) | 
| 291 | 
  | 
                rm->ncomp = 3; | 
| 292 | 
  | 
                setcolor(rm->cexp, 1.f, 1.f, 1.f); | 
| 293 | 
  | 
                memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart)); | 
| 294 | 
< | 
                rm->swapin = 0; | 
| 294 | 
> | 
                rm->pflags = 0; | 
| 295 | 
  | 
        } | 
| 296 | 
  | 
        rm->dtype = DTascii;                    /* assumed w/o FORMAT */ | 
| 297 | 
  | 
        if (getheader(fp, get_dminfo, rm) < 0) { | 
| 298 | 
< | 
                fputs("Unrecognized matrix format\n", stderr); | 
| 298 | 
> | 
                fputs("Bad matrix header\n", stderr); | 
| 299 | 
  | 
                return(0); | 
| 300 | 
  | 
        } | 
| 301 | 
  | 
        if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && | 
| 311 | 
  | 
        return(1); | 
| 312 | 
  | 
} | 
| 313 | 
  | 
 | 
| 314 | 
< | 
/* Load next row as double (cannot be XML) */ | 
| 314 | 
> | 
/* Load next row as rmx_dtype (cannot be XML) */ | 
| 315 | 
  | 
int | 
| 316 | 
< | 
rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp) | 
| 316 | 
> | 
rmx_load_row(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) | 
| 317 | 
  | 
{ | 
| 318 | 
  | 
        switch (rm->dtype) { | 
| 319 | 
  | 
        case DTascii: | 
| 340 | 
  | 
        int     i; | 
| 341 | 
  | 
#ifdef MAP_FILE | 
| 342 | 
  | 
        long    pos;            /* map memory for file > 1MB if possible */ | 
| 343 | 
< | 
        if ((rm->dtype == DTdouble) & !rm->swapin && array_size(rm) >= 1L<<20 && | 
| 344 | 
< | 
                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { | 
| 345 | 
< | 
                rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, | 
| 343 | 
> | 
        if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) & | 
| 344 | 
> | 
                        (rmx_array_size(rm) >= 1L<<20) && | 
| 345 | 
> | 
                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(rmx_dtype))) { | 
| 346 | 
> | 
                rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE, | 
| 347 | 
  | 
                                        MAP_PRIVATE, fileno(fp), 0); | 
| 348 | 
  | 
                if (rm->mapped != MAP_FAILED) { | 
| 349 | 
< | 
                        rm->mtx = (double *)rm->mapped + pos/sizeof(double); | 
| 349 | 
> | 
                        if (rm->pflags & RMF_FREEMEM) | 
| 350 | 
> | 
                                free(rm->mtx); | 
| 351 | 
> | 
                        rm->mtx = (rmx_dtype *)rm->mapped + pos/sizeof(rmx_dtype); | 
| 352 | 
> | 
                        rm->pflags &= ~RMF_FREEMEM; | 
| 353 | 
  | 
                        return(1); | 
| 354 | 
  | 
                }               /* else fall back on reading into memory */ | 
| 355 | 
  | 
                rm->mapped = NULL; | 
| 357 | 
  | 
#endif | 
| 358 | 
  | 
        if (!rmx_prepare(rm)) { /* need in-core matrix array */ | 
| 359 | 
  | 
                fprintf(stderr, "Cannot allocate %g MByte matrix array\n", | 
| 360 | 
< | 
                                (1./(1L<<20))*(double)array_size(rm)); | 
| 360 | 
> | 
                                (1./(1L<<20))*(double)rmx_array_size(rm)); | 
| 361 | 
  | 
                return(0); | 
| 362 | 
  | 
        } | 
| 363 | 
  | 
        for (i = 0; i < rm->nrows; i++) | 
| 382 | 
  | 
                fp = stdin; | 
| 383 | 
  | 
        else if (inspec[0] == '!') | 
| 384 | 
  | 
                fp = popen(inspec+1, "r"); | 
| 385 | 
< | 
        else { | 
| 386 | 
< | 
                const char      *sp = inspec;   /* check suffix */ | 
| 387 | 
< | 
                while (*sp) | 
| 380 | 
< | 
                        ++sp; | 
| 381 | 
< | 
                while (sp > inspec && sp[-1] != '.') | 
| 382 | 
< | 
                        --sp; | 
| 383 | 
< | 
                if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */ | 
| 385 | 
> | 
        else {                                  /* check suffix */ | 
| 386 | 
> | 
                const char      *sp = strrchr(inspec, '.'); | 
| 387 | 
> | 
                if (sp > inspec && !strcasecmp(sp+1, "XML")) {  /* BSDF? */ | 
| 388 | 
  | 
                        CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL : | 
| 389 | 
  | 
                                        rmp==RMPtrans ? cm_loadBTDF(inspec) : | 
| 390 | 
  | 
                                        cm_loadBRDF(inspec, rmp==RMPreflB) ; | 
| 397 | 
  | 
                }                               /* else open it ourselves */ | 
| 398 | 
  | 
                fp = fopen(inspec, "r"); | 
| 399 | 
  | 
        } | 
| 400 | 
< | 
        if (!fp) | 
| 400 | 
> | 
        if (!fp) { | 
| 401 | 
> | 
                fprintf(stderr, "Cannot open for reading: %s\n", inspec); | 
| 402 | 
  | 
                return(NULL); | 
| 403 | 
+ | 
        } | 
| 404 | 
  | 
#ifdef getc_unlocked | 
| 405 | 
  | 
        flockfile(fp); | 
| 406 | 
  | 
#endif | 
| 432 | 
  | 
                                                /* undo exposure? */ | 
| 433 | 
  | 
        if ((dnew->cexp[0] != 1.f) | | 
| 434 | 
  | 
                        (dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { | 
| 435 | 
< | 
                double  cmlt[MAXCSAMP]; | 
| 435 | 
> | 
                double  cmlt[MAXCOMP]; | 
| 436 | 
  | 
                int     i; | 
| 437 | 
< | 
                cmlt[0] = 1./dnew->cexp[0]; | 
| 432 | 
< | 
                cmlt[1] = 1./dnew->cexp[1]; | 
| 433 | 
< | 
                cmlt[2] = 1./dnew->cexp[2]; | 
| 434 | 
< | 
                if (dnew->ncomp > MAXCSAMP) { | 
| 437 | 
> | 
                if (dnew->ncomp > MAXCOMP) { | 
| 438 | 
  | 
                        fprintf(stderr, "Excess spectral components in: %s\n", | 
| 439 | 
  | 
                                        inspec); | 
| 440 | 
  | 
                        rmx_free(dnew); | 
| 441 | 
  | 
                        return(NULL); | 
| 442 | 
  | 
                } | 
| 443 | 
+ | 
                cmlt[0] = 1./dnew->cexp[0]; | 
| 444 | 
+ | 
                cmlt[1] = 1./dnew->cexp[1]; | 
| 445 | 
+ | 
                cmlt[2] = 1./dnew->cexp[2]; | 
| 446 | 
  | 
                for (i = dnew->ncomp; i-- > 3; ) | 
| 447 | 
< | 
                        cmlt[i] = cmlt[1]; | 
| 447 | 
> | 
                        cmlt[i] = cmlt[1];      /* XXX hack! */ | 
| 448 | 
  | 
                rmx_scale(dnew, cmlt); | 
| 449 | 
  | 
                setcolor(dnew->cexp, 1.f, 1.f, 1.f); | 
| 450 | 
  | 
        } | 
| 452 | 
  | 
} | 
| 453 | 
  | 
 | 
| 454 | 
  | 
static int | 
| 455 | 
< | 
rmx_write_ascii(const double *dp, int nc, int len, FILE *fp) | 
| 455 | 
> | 
rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp) | 
| 456 | 
  | 
{ | 
| 457 | 
  | 
        while (len-- > 0) { | 
| 458 | 
  | 
                int     k = nc; | 
| 459 | 
< | 
                while (nc-- > 0) | 
| 459 | 
> | 
                while (k-- > 0) | 
| 460 | 
  | 
                        fprintf(fp, " %.7e", *dp++); | 
| 461 | 
  | 
                fputc('\t', fp); | 
| 462 | 
  | 
        } | 
| 464 | 
  | 
} | 
| 465 | 
  | 
 | 
| 466 | 
  | 
static int | 
| 467 | 
< | 
rmx_write_float(const double *dp, int len, FILE *fp) | 
| 467 | 
> | 
rmx_write_float(const rmx_dtype *dp, int len, FILE *fp) | 
| 468 | 
  | 
{ | 
| 469 | 
  | 
        float   val; | 
| 470 | 
  | 
 | 
| 477 | 
  | 
} | 
| 478 | 
  | 
 | 
| 479 | 
  | 
static int | 
| 480 | 
< | 
rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp) | 
| 480 | 
> | 
rmx_write_rgbe(const rmx_dtype *dp, int nc, int len, FILE *fp) | 
| 481 | 
  | 
{ | 
| 482 | 
  | 
        COLR    *scan; | 
| 483 | 
  | 
        int     j; | 
| 496 | 
  | 
} | 
| 497 | 
  | 
 | 
| 498 | 
  | 
static int | 
| 499 | 
< | 
rmx_write_spec(const double *dp, int nc, int len, FILE *fp) | 
| 499 | 
> | 
rmx_write_spec(const rmx_dtype *dp, int nc, int len, FILE *fp) | 
| 500 | 
  | 
{ | 
| 501 | 
< | 
        uby8    *scan; | 
| 502 | 
< | 
        SCOLOR  scol; | 
| 501 | 
> | 
        COLRV   *scan; | 
| 502 | 
> | 
        COLORV  scol[MAXCOMP]; | 
| 503 | 
  | 
        int     j, k; | 
| 504 | 
  | 
 | 
| 505 | 
< | 
        if (nc < 3) return(0); | 
| 506 | 
< | 
        scan = (uby8 *)tempbuffer((nc+1)*len); | 
| 505 | 
> | 
        if ((nc < 3) | (nc > MAXCOMP)) return(0); | 
| 506 | 
> | 
        scan = (COLRV *)tempbuffer((nc+1)*len); | 
| 507 | 
  | 
        if (!scan) return(0); | 
| 508 | 
< | 
        for (j = len; j--; dp += nc) { | 
| 508 | 
> | 
        for (j = 0; j < len; j++, dp += nc) { | 
| 509 | 
  | 
                for (k = nc; k--; ) | 
| 510 | 
  | 
                        scol[k] = dp[k]; | 
| 511 | 
  | 
                scolor2scolr(scan+j*(nc+1), scol, nc); | 
| 545 | 
  | 
                dtype = DTxyze; | 
| 546 | 
  | 
        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) | 
| 547 | 
  | 
                dtype = DTrgbe; | 
| 548 | 
< | 
        if ((dtype == DTspec) & (rm->ncomp < 3)) | 
| 548 | 
> | 
        if ((dtype < DTspec) & (rm->ncomp > 3)) | 
| 549 | 
> | 
                dtype = DTspec; | 
| 550 | 
> | 
        else if ((dtype == DTspec) & (rm->ncomp <= 3)) | 
| 551 | 
  | 
                return(0); | 
| 552 | 
  | 
 | 
| 553 | 
  | 
        if (dtype == DTascii)                   /* set file type (WINDOWS) */ | 
| 568 | 
  | 
        } | 
| 569 | 
  | 
        if (dtype >= DTspec) {                  /* # components & split? */ | 
| 570 | 
  | 
                fputncomp(rm->ncomp, fp); | 
| 571 | 
< | 
                if (dtype == DTspec || (rm->ncomp > 3 && | 
| 572 | 
< | 
                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))) | 
| 571 | 
> | 
                if (rm->ncomp > 3 && | 
| 572 | 
> | 
                                memcmp(rm->wlpart, WLPART, sizeof(WLPART))) | 
| 573 | 
  | 
                        fputwlsplit(rm->wlpart, fp); | 
| 574 | 
  | 
        } else if ((rm->ncomp != 3) & (rm->ncomp != 1)) | 
| 575 | 
  | 
                return(0);                      /* wrong # components */ | 
| 584 | 
  | 
 | 
| 585 | 
  | 
/* Write out matrix data (usually by row) */ | 
| 586 | 
  | 
int | 
| 587 | 
< | 
rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp) | 
| 587 | 
> | 
rmx_write_data(const rmx_dtype *dp, int nc, int len, int dtype, FILE *fp) | 
| 588 | 
  | 
{ | 
| 589 | 
  | 
        switch (dtype) { | 
| 590 | 
  | 
        case DTascii: | 
| 591 | 
  | 
                return(rmx_write_ascii(dp, nc, len, fp)); | 
| 592 | 
  | 
        case DTfloat: | 
| 593 | 
  | 
                return(rmx_write_float(dp, nc*len, fp)); | 
| 594 | 
< | 
        case DTdouble: | 
| 594 | 
> | 
        case DTrmx_native: | 
| 595 | 
  | 
                return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len); | 
| 596 | 
  | 
        case DTrgbe: | 
| 597 | 
  | 
        case DTxyze: | 
| 615 | 
  | 
#ifdef getc_unlocked | 
| 616 | 
  | 
        flockfile(fp); | 
| 617 | 
  | 
#endif | 
| 618 | 
< | 
        if (dtype == DTdouble)                  /* write all at once? */ | 
| 618 | 
> | 
        if (dtype == DTrmx_native)              /* write all at once? */ | 
| 619 | 
  | 
                ok = rmx_write_data(rm->mtx, rm->ncomp, | 
| 620 | 
  | 
                                rm->nrows*rm->ncols, dtype, fp); | 
| 621 | 
  | 
        else                                    /* else row by row */ | 
| 642 | 
  | 
 | 
| 643 | 
  | 
        if (!rid) | 
| 644 | 
  | 
                return(NULL); | 
| 645 | 
< | 
        memset(rid->mtx, 0, array_size(rid)); | 
| 645 | 
> | 
        memset(rid->mtx, 0, rmx_array_size(rid)); | 
| 646 | 
  | 
        for (i = dim; i--; ) { | 
| 647 | 
< | 
            double      *dp = rmx_lval(rid,i,i); | 
| 647 | 
> | 
            rmx_dtype   *dp = rmx_lval(rid,i,i); | 
| 648 | 
  | 
            for (k = n; k--; ) | 
| 649 | 
  | 
                dp[k] = 1.; | 
| 650 | 
  | 
        } | 
| 667 | 
  | 
                        rmx_free(dnew); | 
| 668 | 
  | 
                        return(NULL); | 
| 669 | 
  | 
                } | 
| 670 | 
< | 
                memcpy(dnew->mtx, rm->mtx, array_size(dnew)); | 
| 670 | 
> | 
                memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew)); | 
| 671 | 
  | 
        } | 
| 672 | 
  | 
        rmx_addinfo(dnew, rm->info); | 
| 673 | 
  | 
        dnew->dtype = rm->dtype; | 
| 676 | 
  | 
        return(dnew); | 
| 677 | 
  | 
} | 
| 678 | 
  | 
 | 
| 679 | 
< | 
/* Allocate and assign transposed matrix */ | 
| 680 | 
< | 
RMATRIX * | 
| 681 | 
< | 
rmx_transpose(const RMATRIX *rm) | 
| 679 | 
> | 
/* Replace data in first matrix with data from second */ | 
| 680 | 
> | 
int | 
| 681 | 
> | 
rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) | 
| 682 | 
  | 
{ | 
| 683 | 
< | 
        RMATRIX *dnew; | 
| 683 | 
> | 
        if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | | 
| 684 | 
> | 
                        (rdst->ncols != rsrc->ncols) | | 
| 685 | 
> | 
                        (rdst->ncomp != rsrc->ncomp)) | 
| 686 | 
> | 
                return(0); | 
| 687 | 
> | 
 | 
| 688 | 
> | 
        if (dometa) {           /* transfer everything? */ | 
| 689 | 
> | 
                rmx_reset(rdst); | 
| 690 | 
> | 
                *rdst = *rsrc; | 
| 691 | 
> | 
                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; | 
| 692 | 
> | 
                return(1); | 
| 693 | 
> | 
        } | 
| 694 | 
> | 
#ifdef MAP_FILE                 /* just matrix data -- leave metadata */ | 
| 695 | 
> | 
        if (rdst->mapped) | 
| 696 | 
> | 
                munmap(rdst->mapped, rmx_mapped_size(rdst)); | 
| 697 | 
> | 
        else | 
| 698 | 
> | 
#endif | 
| 699 | 
> | 
        if (rdst->pflags & RMF_FREEMEM) { | 
| 700 | 
> | 
                free(rdst->mtx); | 
| 701 | 
> | 
                rdst->pflags &= ~RMF_FREEMEM; | 
| 702 | 
> | 
        } | 
| 703 | 
> | 
        rdst->mapped = rsrc->mapped; | 
| 704 | 
> | 
        rdst->mtx = rsrc->mtx; | 
| 705 | 
> | 
        rdst->pflags |= rsrc->pflags & RMF_FREEMEM; | 
| 706 | 
> | 
        rsrc->mapped = NULL; rsrc->mtx = NULL; | 
| 707 | 
> | 
        return(1); | 
| 708 | 
> | 
} | 
| 709 | 
> | 
 | 
| 710 | 
> | 
/* Transpose the given matrix */ | 
| 711 | 
> | 
int | 
| 712 | 
> | 
rmx_transpose(RMATRIX *rm) | 
| 713 | 
> | 
{ | 
| 714 | 
> | 
        RMATRIX dnew; | 
| 715 | 
  | 
        int     i, j; | 
| 716 | 
  | 
 | 
| 717 | 
< | 
        if (!rm || !rm->mtx) | 
| 717 | 
> | 
        if (!rm || !rm->mtx | (rm->ncomp > MAXCOMP)) | 
| 718 | 
  | 
                return(0); | 
| 719 | 
< | 
        if ((rm->nrows == 1) | (rm->ncols == 1)) { | 
| 720 | 
< | 
                dnew = rmx_copy(rm); | 
| 721 | 
< | 
                if (!dnew) | 
| 722 | 
< | 
                        return(NULL); | 
| 723 | 
< | 
                dnew->nrows = rm->ncols; | 
| 724 | 
< | 
                dnew->ncols = rm->nrows; | 
| 725 | 
< | 
                return(dnew); | 
| 719 | 
> | 
        if (rm->info) | 
| 720 | 
> | 
                rmx_addinfo(rm, "Transposed rows and columns\n"); | 
| 721 | 
> | 
        if ((rm->nrows == 1) | (rm->ncols == 1)) { /* vector? */ | 
| 722 | 
> | 
                j = rm->ncols; | 
| 723 | 
> | 
                rm->ncols = rm->nrows; | 
| 724 | 
> | 
                rm->nrows = j; | 
| 725 | 
> | 
                return(1); | 
| 726 | 
  | 
        } | 
| 727 | 
< | 
        dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); | 
| 728 | 
< | 
        if (!dnew) | 
| 729 | 
< | 
                return(NULL); | 
| 730 | 
< | 
        if (rm->info) { | 
| 731 | 
< | 
                rmx_addinfo(dnew, rm->info); | 
| 732 | 
< | 
                rmx_addinfo(dnew, "Transposed rows and columns\n"); | 
| 727 | 
> | 
        if (rm->nrows == rm->ncols) {   /* square matrix case */ | 
| 728 | 
> | 
                rmx_dtype       val[MAXCOMP]; | 
| 729 | 
> | 
                for (j = rm->ncols; j--; ) | 
| 730 | 
> | 
                    for (i = rm->nrows; i--; ) { | 
| 731 | 
> | 
                        if (i == j) continue; | 
| 732 | 
> | 
                        memcpy(val, rmx_val(rm,i,j), | 
| 733 | 
> | 
                                sizeof(rmx_dtype)*rm->ncomp); | 
| 734 | 
> | 
                        memcpy(rmx_lval(rm,i,j), rmx_val(rm,j,i), | 
| 735 | 
> | 
                                sizeof(rmx_dtype)*rm->ncomp); | 
| 736 | 
> | 
                        memcpy(rmx_val(rm,j,i), val, | 
| 737 | 
> | 
                                sizeof(rmx_dtype)*rm->ncomp); | 
| 738 | 
> | 
                    } | 
| 739 | 
> | 
                return(1); | 
| 740 | 
  | 
        } | 
| 741 | 
< | 
        dnew->dtype = rm->dtype; | 
| 742 | 
< | 
        copycolor(dnew->cexp, rm->cexp); | 
| 743 | 
< | 
        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); | 
| 744 | 
< | 
        for (j = dnew->ncols; j--; ) | 
| 745 | 
< | 
            for (i = dnew->nrows; i--; ) | 
| 746 | 
< | 
                memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), | 
| 747 | 
< | 
                                sizeof(double)*dnew->ncomp); | 
| 748 | 
< | 
        return(dnew); | 
| 741 | 
> | 
        memset(&dnew, 0, sizeof(dnew)); | 
| 742 | 
> | 
        dnew.ncols = rm->nrows; dnew.nrows = rm->ncols; | 
| 743 | 
> | 
        dnew.ncomp = rm->ncomp; | 
| 744 | 
> | 
        if (!rmx_prepare(&dnew)) | 
| 745 | 
> | 
                return(0); | 
| 746 | 
> | 
        rmx_addinfo(&dnew, rm->info); | 
| 747 | 
> | 
        dnew.dtype = rm->dtype; | 
| 748 | 
> | 
        copycolor(dnew.cexp, rm->cexp); | 
| 749 | 
> | 
        memcpy(dnew.wlpart, rm->wlpart, sizeof(dnew.wlpart)); | 
| 750 | 
> | 
        for (j = dnew.ncols; j--; ) | 
| 751 | 
> | 
            for (i = dnew.nrows; i--; ) | 
| 752 | 
> | 
                memcpy(rmx_lval(&dnew,i,j), rmx_val(rm,j,i), | 
| 753 | 
> | 
                                sizeof(rmx_dtype)*dnew.ncomp); | 
| 754 | 
> | 
        rmx_reset(rm);                  /* frees memory */ | 
| 755 | 
> | 
        *rm = dnew;                     /* replace w/ transpose */ | 
| 756 | 
> | 
        return(1); | 
| 757 | 
  | 
} | 
| 758 | 
  | 
 | 
| 759 | 
  | 
/* Multiply (concatenate) two matrices and allocate the result */ | 
| 777 | 
  | 
        for (i = mres->nrows; i--; ) | 
| 778 | 
  | 
            for (j = mres->ncols; j--; ) | 
| 779 | 
  | 
                for (k = mres->ncomp; k--; ) { | 
| 780 | 
< | 
                    double      d = 0; | 
| 780 | 
> | 
                    rmx_dtype   d = 0; | 
| 781 | 
  | 
                    for (h = m1->ncols; h--; ) | 
| 782 | 
  | 
                        d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; | 
| 783 | 
  | 
                    rmx_lval(mres,i,j)[k] = d; | 
| 805 | 
  | 
        for (i = m1->nrows; i--; ) | 
| 806 | 
  | 
            for (j = m1->ncols; j--; ) | 
| 807 | 
  | 
                if (divide) { | 
| 808 | 
< | 
                    double      d; | 
| 808 | 
> | 
                    rmx_dtype   d; | 
| 809 | 
  | 
                    if (m2->ncomp == 1) { | 
| 810 | 
  | 
                        d = rmx_val(m2,i,j)[0]; | 
| 811 | 
  | 
                        if (d == 0) { | 
| 828 | 
  | 
                        } | 
| 829 | 
  | 
                } else { | 
| 830 | 
  | 
                    if (m2->ncomp == 1) { | 
| 831 | 
< | 
                        const double    d = rmx_val(m2,i,j)[0]; | 
| 831 | 
> | 
                        const rmx_dtype d = rmx_val(m2,i,j)[0]; | 
| 832 | 
  | 
                        for (k = m1->ncomp; k--; ) | 
| 833 | 
  | 
                            rmx_lval(m1,i,j)[k] *= d; | 
| 834 | 
  | 
                    } else | 
| 869 | 
  | 
                rmx_addinfo(msum, rmx_mismatch_warn); | 
| 870 | 
  | 
        for (i = msum->nrows; i--; ) | 
| 871 | 
  | 
            for (j = msum->ncols; j--; ) { | 
| 872 | 
< | 
                const double    *da = rmx_val(madd,i,j); | 
| 873 | 
< | 
                double          *ds = rmx_lval(msum,i,j); | 
| 872 | 
> | 
                const rmx_dtype *da = rmx_val(madd,i,j); | 
| 873 | 
> | 
                rmx_dtype       *ds = rmx_lval(msum,i,j); | 
| 874 | 
  | 
                for (k = msum->ncomp; k--; ) | 
| 875 | 
  | 
                     ds[k] += sf[k] * da[k]; | 
| 876 | 
  | 
            } | 
| 889 | 
  | 
                return(0); | 
| 890 | 
  | 
        for (i = rm->nrows; i--; ) | 
| 891 | 
  | 
            for (j = rm->ncols; j--; ) { | 
| 892 | 
< | 
                double  *dp = rmx_lval(rm,i,j); | 
| 892 | 
> | 
                rmx_dtype       *dp = rmx_lval(rm,i,j); | 
| 893 | 
  | 
                for (k = rm->ncomp; k--; ) | 
| 894 | 
  | 
                    dp[k] *= sf[k]; | 
| 895 | 
  | 
            } | 
| 921 | 
  | 
        dnew->dtype = msrc->dtype; | 
| 922 | 
  | 
        for (i = dnew->nrows; i--; ) | 
| 923 | 
  | 
            for (j = dnew->ncols; j--; ) { | 
| 924 | 
< | 
                const double    *ds = rmx_val(msrc,i,j); | 
| 924 | 
> | 
                const rmx_dtype *ds = rmx_val(msrc,i,j); | 
| 925 | 
  | 
                for (kd = dnew->ncomp; kd--; ) { | 
| 926 | 
< | 
                    double      d = 0; | 
| 926 | 
> | 
                    rmx_dtype   d = 0; | 
| 927 | 
  | 
                    for (ks = msrc->ncomp; ks--; ) | 
| 928 | 
  | 
                        d += cmat[kd*msrc->ncomp + ks] * ds[ks]; | 
| 929 | 
  | 
                    rmx_lval(dnew,i,j)[kd] = d; | 
| 948 | 
  | 
        for (i = dnew->nrows; i--; ) | 
| 949 | 
  | 
            for (j = dnew->ncols; j--; ) { | 
| 950 | 
  | 
                const COLORV    *cv = cm_lval(cm,i,j); | 
| 951 | 
< | 
                double          *dp = rmx_lval(dnew,i,j); | 
| 951 | 
> | 
                rmx_dtype       *dp = rmx_lval(dnew,i,j); | 
| 952 | 
  | 
                dp[0] = cv[0]; | 
| 953 | 
  | 
                dp[1] = cv[1]; | 
| 954 | 
  | 
                dp[2] = cv[2]; | 
| 963 | 
  | 
        int     i, j; | 
| 964 | 
  | 
        CMATRIX *cnew; | 
| 965 | 
  | 
 | 
| 966 | 
< | 
        if (!rm || !rm->mtx | (rm->ncomp == 2)) | 
| 966 | 
> | 
        if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP)) | 
| 967 | 
  | 
                return(NULL); | 
| 968 | 
  | 
        cnew = cm_alloc(rm->nrows, rm->ncols); | 
| 969 | 
  | 
        if (!cnew) | 
| 970 | 
  | 
                return(NULL); | 
| 971 | 
  | 
        for (i = cnew->nrows; i--; ) | 
| 972 | 
  | 
            for (j = cnew->ncols; j--; ) { | 
| 973 | 
< | 
                const double    *dp = rmx_val(rm,i,j); | 
| 973 | 
> | 
                const rmx_dtype *dp = rmx_val(rm,i,j); | 
| 974 | 
  | 
                COLORV          *cv = cm_lval(cnew,i,j); | 
| 975 | 
  | 
                switch (rm->ncomp) { | 
| 976 | 
  | 
                case 3: | 
| 980 | 
  | 
                    setcolor(cv, dp[0], dp[0], dp[0]); | 
| 981 | 
  | 
                    break; | 
| 982 | 
  | 
                default: { | 
| 983 | 
< | 
                        SCOLOR  scol; | 
| 983 | 
> | 
                        COLORV  scol[MAXCOMP]; | 
| 984 | 
  | 
                        int     k; | 
| 985 | 
  | 
                        for (k = rm->ncomp; k--; ) | 
| 986 | 
  | 
                                scol[k] = dp[k]; |