34 |
|
error(USER, "attempt to create empty matrix"); |
35 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
36 |
|
sizeof(COLOR)*(nrows*ncols - 1)); |
37 |
< |
if (cm == NULL) |
37 |
> |
if (!cm) |
38 |
|
error(SYSTEM, "out of memory in cm_alloc()"); |
39 |
|
cm->nrows = nrows; |
40 |
|
cm->ncols = ncols; |
57 |
|
{ |
58 |
|
size_t old_size, new_size, ra_bounds[2]; |
59 |
|
|
60 |
+ |
if (!cm) |
61 |
+ |
return(NULL); |
62 |
|
if (nrows == cm->nrows) |
63 |
|
return(cm); |
64 |
|
if (nrows <= 0) { |
72 |
|
new_size > ra_bounds[1]) { |
73 |
|
adjacent_ra_sizes(ra_bounds, new_size); |
74 |
|
cm = (CMATRIX *)realloc(cm, ra_bounds[1]); |
75 |
< |
if (cm == NULL) |
75 |
> |
if (!cm) |
76 |
|
error(SYSTEM, "out of memory in cm_resize()"); |
77 |
|
} |
78 |
|
cm->nrows = nrows; |
81 |
|
|
82 |
|
typedef struct { |
83 |
|
int dtype; /* data type */ |
84 |
+ |
int need2swap; /* need byte swap? */ |
85 |
|
int nrows, ncols; /* matrix size */ |
86 |
|
char *err; /* error message */ |
87 |
|
} CMINFO; /* header info record */ |
90 |
|
get_cminfo(char *s, void *p) |
91 |
|
{ |
92 |
|
CMINFO *ip = (CMINFO *)p; |
93 |
< |
char fmt[32]; |
93 |
> |
char fmt[MAXFMTLEN]; |
94 |
|
int i; |
95 |
|
|
96 |
|
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
105 |
|
ip->ncols = atoi(s+6); |
106 |
|
return(0); |
107 |
|
} |
108 |
+ |
if ((i = isbigendian(s)) >= 0) { |
109 |
+ |
ip->need2swap = (nativebigendian() != i); |
110 |
+ |
return(0); |
111 |
+ |
} |
112 |
|
if (!formatval(fmt, s)) |
113 |
|
return(0); |
114 |
|
for (i = 1; i < DTend; i++) |
119 |
|
|
120 |
|
/* Load header to obtain/check data type and number of columns */ |
121 |
|
char * |
122 |
< |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
122 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp) |
123 |
|
{ |
124 |
|
CMINFO cmi; |
125 |
|
/* read header */ |
126 |
|
cmi.dtype = DTfromHeader; |
127 |
+ |
cmi.need2swap = 0; |
128 |
|
cmi.nrows = cmi.ncols = 0; |
129 |
|
cmi.err = "unexpected EOF in header"; |
130 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
131 |
|
return(cmi.err); |
132 |
< |
if (dt != NULL) { /* get/check data type? */ |
132 |
> |
if (dt) { /* get/check data type? */ |
133 |
|
if (cmi.dtype == DTfromHeader) { |
134 |
|
if (*dt == DTfromHeader) |
135 |
|
return("missing/unknown data format in header"); |
138 |
|
else if (*dt != cmi.dtype) |
139 |
|
return("unexpected data format in header"); |
140 |
|
} |
141 |
< |
if (nr != NULL) { /* get/check #rows? */ |
141 |
> |
if (nr) { /* get/check #rows? */ |
142 |
|
if (*nr <= 0) |
143 |
|
*nr = cmi.nrows; |
144 |
|
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
145 |
|
return("unexpected row count in header"); |
146 |
|
} |
147 |
< |
if (nc != NULL) { /* get/check #columns? */ |
147 |
> |
if (nc) { /* get/check #columns? */ |
148 |
|
if (*nc <= 0) |
149 |
|
*nc = cmi.ncols; |
150 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
151 |
|
return("unexpected column count in header"); |
152 |
|
} |
153 |
+ |
if (swp) /* get/check swap? */ |
154 |
+ |
*swp = cmi.need2swap; |
155 |
|
return(NULL); |
156 |
|
} |
157 |
|
|
161 |
|
{ |
162 |
|
const int ROWINC = 2048; |
163 |
|
FILE *fp = stdin; |
164 |
+ |
int swap = 0; |
165 |
|
CMATRIX *cm; |
166 |
|
|
167 |
< |
if (inspec == NULL) |
167 |
> |
if (!inspec) |
168 |
|
inspec = "<stdin>"; |
169 |
|
else if (inspec[0] == '!') { |
170 |
|
fp = popen(inspec+1, "r"); |
171 |
< |
if (fp == NULL) { |
171 |
> |
if (!fp) { |
172 |
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
173 |
|
error(SYSTEM, errmsg); |
174 |
|
} |
175 |
< |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
175 |
> |
} else if (!(fp = fopen(inspec, "r"))) { |
176 |
|
sprintf(errmsg, "cannot open file '%s'", inspec); |
177 |
|
error(SYSTEM, errmsg); |
178 |
|
} |
182 |
|
if (dtype != DTascii) |
183 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
184 |
|
if (!dtype | !ncols) { /* expecting header? */ |
185 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
186 |
< |
if (err != NULL) |
185 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp); |
186 |
> |
if (err) |
187 |
|
error(USER, err); |
188 |
|
if (ncols <= 0) |
189 |
|
error(USER, "unspecified number of columns"); |
224 |
|
cm = cm_alloc(guessrows, ncols); |
225 |
|
} else |
226 |
|
cm = cm_alloc(nrows, ncols); |
227 |
< |
if (cm == NULL) /* XXX never happens */ |
227 |
> |
if (!cm) /* XXX never happens */ |
228 |
|
return(NULL); |
229 |
|
if (dtype == DTascii) { /* read text file */ |
230 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
306 |
|
error(WARNING, errmsg); |
307 |
|
} |
308 |
|
} |
309 |
+ |
if (swap) { |
310 |
+ |
if (dtype == DTfloat) |
311 |
+ |
swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
312 |
+ |
else if (dtype == DTdouble) |
313 |
+ |
swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
314 |
+ |
} |
315 |
|
if (fp != stdin) { |
316 |
|
if (inspec[0] != '!') |
317 |
|
fclose(fp); |
340 |
|
CMATRIX *cvr; |
341 |
|
int dr; |
342 |
|
|
343 |
+ |
if (!cm) |
344 |
+ |
return(NULL); |
345 |
|
if ((c < 0) | (c >= cm->ncols)) |
346 |
|
error(INTERNAL, "column requested outside matrix"); |
347 |
|
cvr = cm_alloc(cm->nrows, 1); |
348 |
< |
if (cvr == NULL) |
348 |
> |
if (!cvr) |
349 |
|
return(NULL); |
350 |
|
for (dr = 0; dr < cm->nrows; dr++) { |
351 |
|
const COLORV *sp = cm_lval(cm,dr,c); |
357 |
|
return(cvr); |
358 |
|
} |
359 |
|
|
341 |
– |
/* Scale a matrix by a single value */ |
342 |
– |
CMATRIX * |
343 |
– |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
344 |
– |
{ |
345 |
– |
CMATRIX *cmr; |
346 |
– |
int dr, dc; |
347 |
– |
|
348 |
– |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
349 |
– |
if (cmr == NULL) |
350 |
– |
return(NULL); |
351 |
– |
for (dr = 0; dr < cmr->nrows; dr++) |
352 |
– |
for (dc = 0; dc < cmr->ncols; dc++) { |
353 |
– |
const COLORV *sp = cm_lval(cm1,dr,dc); |
354 |
– |
COLORV *dp = cm_lval(cmr,dr,dc); |
355 |
– |
dp[0] = sp[0] * sca[0]; |
356 |
– |
dp[1] = sp[1] * sca[1]; |
357 |
– |
dp[2] = sp[2] * sca[2]; |
358 |
– |
} |
359 |
– |
return(cmr); |
360 |
– |
} |
361 |
– |
|
360 |
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
361 |
|
CMATRIX * |
362 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
365 |
|
CMATRIX *cmr; |
366 |
|
int dr, dc, i; |
367 |
|
|
368 |
+ |
if (!cm1 | !cm2) |
369 |
+ |
return(NULL); |
370 |
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
371 |
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
372 |
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
373 |
< |
if (cmr == NULL) |
373 |
> |
if (!cmr) |
374 |
|
return(NULL); |
375 |
|
/* optimization: check for zero rows & cols */ |
376 |
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
395 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
396 |
|
double res[3]; |
397 |
|
dp[0] = dp[1] = dp[2] = 0; |
398 |
< |
if (rowcheck != NULL && !rowcheck[dr]) |
398 |
> |
if (rowcheck && !rowcheck[dr]) |
399 |
|
continue; |
400 |
< |
if (colcheck != NULL && !colcheck[dc]) |
400 |
> |
if (colcheck && !colcheck[dc]) |
401 |
|
continue; |
402 |
|
res[0] = res[1] = res[2] = 0; |
403 |
|
for (i = 0; i < cm1->ncols; i++) { |
409 |
|
} |
410 |
|
copycolor(dp, res); |
411 |
|
} |
412 |
< |
if (rowcheck != NULL) free(rowcheck); |
413 |
< |
if (colcheck != NULL) free(colcheck); |
412 |
> |
if (rowcheck) free(rowcheck); |
413 |
> |
if (colcheck) free(colcheck); |
414 |
|
return(cmr); |
415 |
|
} |
416 |
|
|
419 |
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
420 |
|
{ |
421 |
|
static const char tabEOL[2] = {'\t','\n'}; |
422 |
< |
const COLORV *mp = cm->cmem; |
422 |
> |
const COLORV *mp; |
423 |
|
int r, c; |
424 |
|
|
425 |
+ |
if (!cm) |
426 |
+ |
return(0); |
427 |
+ |
mp = cm->cmem; |
428 |
|
switch (dtype) { |
429 |
|
case DTascii: |
430 |
|
for (r = 0; r < cm->nrows; r++) |