| 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 |
|
|
| 25 |
|
/* Initialize a RMATRIX struct but don't allocate array space */ |
| 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 |
|
} |
| 260 |
|
static int |
| 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) |
| 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) |
| 384 |
< |
++sp; |
| 385 |
< |
while (sp > inspec && sp[-1] != '.') |
| 386 |
< |
--sp; |
| 387 |
< |
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) ; |
| 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 |
< |
if (dnew->ncomp > MAXCSAMP) { |
| 437 |
> |
if (dnew->ncomp > MAXCOMP) { |
| 438 |
|
fprintf(stderr, "Excess spectral components in: %s\n", |
| 439 |
|
inspec); |
| 440 |
|
rmx_free(dnew); |
| 498 |
|
static int |
| 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 = 0; j < len; j++, dp += nc) { |
| 509 |
|
for (k = nc; k--; ) |
| 680 |
|
int |
| 681 |
|
rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) |
| 682 |
|
{ |
| 683 |
< |
if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | |
| 684 |
< |
(rdst->ncols != rsrc->ncols) | |
| 685 |
< |
(rdst->ncomp != rsrc->ncomp)) |
| 683 |
> |
if (!rdst | !rsrc) |
| 684 |
|
return(0); |
| 687 |
– |
|
| 685 |
|
if (dometa) { /* transfer everything? */ |
| 686 |
|
rmx_reset(rdst); |
| 687 |
|
*rdst = *rsrc; |
| 688 |
|
rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; |
| 689 |
|
return(1); |
| 690 |
|
} |
| 691 |
< |
#ifdef MAP_FILE /* just matrix data -- leave metadata */ |
| 691 |
> |
/* just matrix data -- leave metadata */ |
| 692 |
> |
if ((rdst->nrows != rsrc->nrows) | |
| 693 |
> |
(rdst->ncols != rsrc->ncols) | |
| 694 |
> |
(rdst->ncomp != rsrc->ncomp)) |
| 695 |
> |
return(0); |
| 696 |
> |
#ifdef MAP_FILE |
| 697 |
|
if (rdst->mapped) |
| 698 |
|
munmap(rdst->mapped, rmx_mapped_size(rdst)); |
| 699 |
|
else |
| 709 |
|
return(1); |
| 710 |
|
} |
| 711 |
|
|
| 712 |
< |
/* Allocate and assign transposed matrix */ |
| 713 |
< |
RMATRIX * |
| 714 |
< |
rmx_transpose(const RMATRIX *rm) |
| 712 |
> |
/* Transpose the given matrix */ |
| 713 |
> |
int |
| 714 |
> |
rmx_transpose(RMATRIX *rm) |
| 715 |
|
{ |
| 716 |
< |
RMATRIX *dnew; |
| 716 |
> |
RMATRIX dnew; |
| 717 |
|
int i, j; |
| 718 |
|
|
| 719 |
< |
if (!rm || !rm->mtx) |
| 719 |
> |
if (!rm || !rm->mtx | (rm->ncomp > MAXCOMP)) |
| 720 |
|
return(0); |
| 721 |
< |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
| 722 |
< |
dnew = rmx_copy(rm); |
| 723 |
< |
if (!dnew) |
| 724 |
< |
return(NULL); |
| 725 |
< |
dnew->nrows = rm->ncols; |
| 726 |
< |
dnew->ncols = rm->nrows; |
| 727 |
< |
return(dnew); |
| 721 |
> |
if (rm->info) |
| 722 |
> |
rmx_addinfo(rm, "Transposed rows and columns\n"); |
| 723 |
> |
if ((rm->nrows == 1) | (rm->ncols == 1)) { /* vector? */ |
| 724 |
> |
j = rm->ncols; |
| 725 |
> |
rm->ncols = rm->nrows; |
| 726 |
> |
rm->nrows = j; |
| 727 |
> |
return(1); |
| 728 |
|
} |
| 729 |
< |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
| 730 |
< |
if (!dnew) |
| 731 |
< |
return(NULL); |
| 732 |
< |
if (rm->info) { |
| 733 |
< |
rmx_addinfo(dnew, rm->info); |
| 734 |
< |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
| 729 |
> |
if (rm->nrows == rm->ncols) { /* square matrix case */ |
| 730 |
> |
rmx_dtype val[MAXCOMP]; |
| 731 |
> |
for (j = rm->ncols; j--; ) |
| 732 |
> |
for (i = rm->nrows; i--; ) { |
| 733 |
> |
if (i == j) continue; |
| 734 |
> |
memcpy(val, rmx_val(rm,i,j), |
| 735 |
> |
sizeof(rmx_dtype)*rm->ncomp); |
| 736 |
> |
memcpy(rmx_lval(rm,i,j), rmx_val(rm,j,i), |
| 737 |
> |
sizeof(rmx_dtype)*rm->ncomp); |
| 738 |
> |
memcpy(rmx_val(rm,j,i), val, |
| 739 |
> |
sizeof(rmx_dtype)*rm->ncomp); |
| 740 |
> |
} |
| 741 |
> |
return(1); |
| 742 |
|
} |
| 743 |
< |
dnew->dtype = rm->dtype; |
| 744 |
< |
copycolor(dnew->cexp, rm->cexp); |
| 745 |
< |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
| 746 |
< |
for (j = dnew->ncols; j--; ) |
| 747 |
< |
for (i = dnew->nrows; i--; ) |
| 748 |
< |
memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), |
| 749 |
< |
sizeof(rmx_dtype)*dnew->ncomp); |
| 750 |
< |
return(dnew); |
| 743 |
> |
memset(&dnew, 0, sizeof(dnew)); |
| 744 |
> |
dnew.ncols = rm->nrows; dnew.nrows = rm->ncols; |
| 745 |
> |
dnew.ncomp = rm->ncomp; |
| 746 |
> |
if (!rmx_prepare(&dnew)) |
| 747 |
> |
return(0); |
| 748 |
> |
rmx_addinfo(&dnew, rm->info); |
| 749 |
> |
dnew.dtype = rm->dtype; |
| 750 |
> |
copycolor(dnew.cexp, rm->cexp); |
| 751 |
> |
memcpy(dnew.wlpart, rm->wlpart, sizeof(dnew.wlpart)); |
| 752 |
> |
for (j = dnew.ncols; j--; ) |
| 753 |
> |
for (i = dnew.nrows; i--; ) |
| 754 |
> |
memcpy(rmx_lval(&dnew,i,j), rmx_val(rm,j,i), |
| 755 |
> |
sizeof(rmx_dtype)*dnew.ncomp); |
| 756 |
> |
/* and reassign result */ |
| 757 |
> |
return(rmx_transfer_data(rm, &dnew, 1)); |
| 758 |
|
} |
| 759 |
|
|
| 760 |
|
/* Multiply (concatenate) two matrices and allocate the result */ |
| 964 |
|
int i, j; |
| 965 |
|
CMATRIX *cnew; |
| 966 |
|
|
| 967 |
< |
if (!rm || !rm->mtx | (rm->ncomp == 2)) |
| 967 |
> |
if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP)) |
| 968 |
|
return(NULL); |
| 969 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
| 970 |
|
if (!cnew) |
| 981 |
|
setcolor(cv, dp[0], dp[0], dp[0]); |
| 982 |
|
break; |
| 983 |
|
default: { |
| 984 |
< |
SCOLOR scol; |
| 984 |
> |
COLORV scol[MAXCOMP]; |
| 985 |
|
int k; |
| 986 |
|
for (k = rm->ncomp; k--; ) |
| 987 |
|
scol[k] = dp[k]; |