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) |
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--; ) |
707 |
|
return(1); |
708 |
|
} |
709 |
|
|
710 |
< |
/* Allocate and assign transposed matrix */ |
711 |
< |
RMATRIX * |
712 |
< |
rmx_transpose(const RMATRIX *rm) |
710 |
> |
/* Transpose the given matrix */ |
711 |
> |
int |
712 |
> |
rmx_transpose(RMATRIX *rm) |
713 |
|
{ |
714 |
< |
RMATRIX *dnew; |
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(rmx_dtype)*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 */ |
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) |
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]; |