| 31 |
|
if ((nrows <= 0) | (ncols <= 0)) |
| 32 |
|
error(USER, "attempt to create empty matrix"); |
| 33 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
| 34 |
< |
3*sizeof(COLORV)*(nrows*ncols - 1)); |
| 34 |
> |
sizeof(COLOR)*(nrows*ncols - 1)); |
| 35 |
|
if (cm == NULL) |
| 36 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
| 37 |
|
cm->nrows = nrows; |
| 50 |
|
return(NULL); |
| 51 |
|
} |
| 52 |
|
cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) + |
| 53 |
< |
3*sizeof(COLORV)*(nrows*cm->ncols - 1)); |
| 53 |
> |
sizeof(COLOR)*(nrows*cm->ncols - 1)); |
| 54 |
|
if (cm == NULL) |
| 55 |
|
error(SYSTEM, "out of memory in cm_resize()"); |
| 56 |
|
cm->nrows = nrows; |
| 57 |
|
return(cm); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
+ |
typedef struct { |
| 61 |
+ |
int dtype; /* data type */ |
| 62 |
+ |
int nrows, ncols; /* matrix size */ |
| 63 |
+ |
char *err; /* error message */ |
| 64 |
+ |
} CMINFO; /* header info record */ |
| 65 |
+ |
|
| 66 |
|
static int |
| 67 |
< |
getDT(char *s, void *p) |
| 67 |
> |
get_cminfo(char *s, void *p) |
| 68 |
|
{ |
| 69 |
+ |
CMINFO *ip = (CMINFO *)p; |
| 70 |
|
char fmt[32]; |
| 71 |
|
int i; |
| 72 |
< |
|
| 72 |
> |
|
| 73 |
> |
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
| 74 |
> |
ip->err = "unexpected # components (must be 3)"; |
| 75 |
> |
return(-1); |
| 76 |
> |
} |
| 77 |
> |
if (!strncmp(s, "NROWS=", 6)) { |
| 78 |
> |
ip->nrows = atoi(s+6); |
| 79 |
> |
return(0); |
| 80 |
> |
} |
| 81 |
> |
if (!strncmp(s, "NCOLS=", 6)) { |
| 82 |
> |
ip->ncols = atoi(s+6); |
| 83 |
> |
return(0); |
| 84 |
> |
} |
| 85 |
|
if (!formatval(fmt, s)) |
| 86 |
|
return(0); |
| 87 |
|
for (i = 1; i < DTend; i++) |
| 88 |
|
if (!strcmp(fmt, cm_fmt_id[i])) |
| 89 |
< |
*((int *)p) = i; |
| 89 |
> |
ip->dtype = i; |
| 90 |
|
return(0); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
< |
/* Load header to obtain data type */ |
| 94 |
< |
int |
| 95 |
< |
getDTfromHeader(FILE *fp) |
| 93 |
> |
/* Load header to obtain/check data type and number of columns */ |
| 94 |
> |
char * |
| 95 |
> |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
| 96 |
|
{ |
| 97 |
< |
int dt = DTfromHeader; |
| 98 |
< |
|
| 99 |
< |
if (getheader(fp, getDT, &dt) < 0) |
| 100 |
< |
error(SYSTEM, "header read error"); |
| 101 |
< |
if (dt == DTfromHeader) |
| 102 |
< |
error(USER, "missing data format in header"); |
| 103 |
< |
return(dt); |
| 97 |
> |
CMINFO cmi; |
| 98 |
> |
/* read header */ |
| 99 |
> |
cmi.dtype = DTfromHeader; |
| 100 |
> |
cmi.nrows = cmi.ncols = 0; |
| 101 |
> |
cmi.err = "unexpected EOF in header"; |
| 102 |
> |
if (getheader(fp, &get_cminfo, &cmi) < 0) |
| 103 |
> |
return(cmi.err); |
| 104 |
> |
if (dt != NULL) { /* get/check data type? */ |
| 105 |
> |
if (cmi.dtype == DTfromHeader) { |
| 106 |
> |
if (*dt == DTfromHeader) |
| 107 |
> |
return("missing/unknown data format in header"); |
| 108 |
> |
} else if (*dt == DTfromHeader) |
| 109 |
> |
*dt = cmi.dtype; |
| 110 |
> |
else if (*dt != cmi.dtype) |
| 111 |
> |
return("unexpected data format in header"); |
| 112 |
> |
} |
| 113 |
> |
if (nr != NULL) { /* get/check #rows? */ |
| 114 |
> |
if (*nr <= 0) |
| 115 |
> |
*nr = cmi.nrows; |
| 116 |
> |
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
| 117 |
> |
return("unexpected row count in header"); |
| 118 |
> |
} |
| 119 |
> |
if (nc != NULL) { /* get/check #columns? */ |
| 120 |
> |
if (*nc <= 0) |
| 121 |
> |
*nc = cmi.ncols; |
| 122 |
> |
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
| 123 |
> |
return("unexpected column count in header"); |
| 124 |
> |
} |
| 125 |
> |
return(NULL); |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
| 132 |
|
FILE *fp = stdin; |
| 133 |
|
CMATRIX *cm; |
| 134 |
|
|
| 94 |
– |
if (ncols <= 0) |
| 95 |
– |
error(USER, "Non-positive number of columns"); |
| 135 |
|
if (fname == NULL) |
| 136 |
|
fname = "<stdin>"; |
| 137 |
|
else if ((fp = fopen(fname, "r")) == NULL) { |
| 143 |
|
#endif |
| 144 |
|
if (dtype != DTascii) |
| 145 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
| 146 |
< |
if (dtype == DTfromHeader) |
| 147 |
< |
dtype = getDTfromHeader(fp); |
| 146 |
> |
if (!dtype | !ncols) { /* expecting header? */ |
| 147 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
| 148 |
> |
if (err != NULL) |
| 149 |
> |
error(USER, err); |
| 150 |
> |
if (ncols <= 0) |
| 151 |
> |
error(USER, "unspecified number of columns"); |
| 152 |
> |
} |
| 153 |
|
switch (dtype) { |
| 154 |
|
case DTascii: |
| 155 |
|
case DTfloat: |
| 217 |
|
int nread = 0; |
| 218 |
|
do { /* read all we can */ |
| 219 |
|
nread += fread(cm->cmem + 3*nread, |
| 220 |
< |
3*sizeof(COLORV), |
| 220 |
> |
sizeof(COLOR), |
| 221 |
|
cm->nrows*cm->ncols - nread, |
| 222 |
|
fp); |
| 223 |
|
if (nrows <= 0) { /* unknown length */ |
| 359 |
|
for (dr = 0; dr < cmr->nrows; dr++) |
| 360 |
|
for (dc = 0; dc < cmr->ncols; dc++) { |
| 361 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
| 362 |
+ |
double res[3]; |
| 363 |
|
dp[0] = dp[1] = dp[2] = 0; |
| 364 |
|
if (rowcheck != NULL && !rowcheck[dr]) |
| 365 |
|
continue; |
| 366 |
|
if (colcheck != NULL && !colcheck[dc]) |
| 367 |
|
continue; |
| 368 |
+ |
res[0] = res[1] = res[2] = 0; |
| 369 |
|
for (i = 0; i < cm1->ncols; i++) { |
| 370 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
| 371 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
| 372 |
< |
dp[0] += cp1[0] * cp2[0]; |
| 373 |
< |
dp[1] += cp1[1] * cp2[1]; |
| 374 |
< |
dp[2] += cp1[2] * cp2[2]; |
| 372 |
> |
res[0] += cp1[0] * cp2[0]; |
| 373 |
> |
res[1] += cp1[1] * cp2[1]; |
| 374 |
> |
res[2] += cp1[2] * cp2[2]; |
| 375 |
|
} |
| 376 |
+ |
copycolor(dp, res); |
| 377 |
|
} |
| 378 |
|
if (rowcheck != NULL) free(rowcheck); |
| 379 |
|
if (colcheck != NULL) free(colcheck); |