8 |
|
*/ |
9 |
|
|
10 |
|
#include <ctype.h> |
11 |
+ |
#include "platform.h" |
12 |
|
#include "standard.h" |
13 |
|
#include "cmatrix.h" |
14 |
|
#include "platform.h" |
15 |
< |
#include "rtprocess.h" |
15 |
> |
#include "paths.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
|
const char *cm_fmt_id[] = { |
21 |
|
}; |
22 |
|
|
23 |
|
const int cm_elem_size[] = { |
24 |
< |
0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4 |
24 |
> |
0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double) |
25 |
|
}; |
26 |
|
|
27 |
|
/* Allocate a color coefficient matrix */ |
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; |
89 |
|
get_cminfo(char *s, void *p) |
90 |
|
{ |
91 |
|
CMINFO *ip = (CMINFO *)p; |
92 |
< |
char fmt[32]; |
92 |
> |
char fmt[MAXFMTLEN]; |
93 |
|
int i; |
94 |
|
|
95 |
|
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
123 |
|
cmi.err = "unexpected EOF in header"; |
124 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
125 |
|
return(cmi.err); |
126 |
< |
if (dt != NULL) { /* get/check data type? */ |
126 |
> |
if (dt) { /* get/check data type? */ |
127 |
|
if (cmi.dtype == DTfromHeader) { |
128 |
|
if (*dt == DTfromHeader) |
129 |
|
return("missing/unknown data format in header"); |
132 |
|
else if (*dt != cmi.dtype) |
133 |
|
return("unexpected data format in header"); |
134 |
|
} |
135 |
< |
if (nr != NULL) { /* get/check #rows? */ |
135 |
> |
if (nr) { /* get/check #rows? */ |
136 |
|
if (*nr <= 0) |
137 |
|
*nr = cmi.nrows; |
138 |
|
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
139 |
|
return("unexpected row count in header"); |
140 |
|
} |
141 |
< |
if (nc != NULL) { /* get/check #columns? */ |
141 |
> |
if (nc) { /* get/check #columns? */ |
142 |
|
if (*nc <= 0) |
143 |
|
*nc = cmi.ncols; |
144 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
151 |
|
CMATRIX * |
152 |
|
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
153 |
|
{ |
154 |
< |
FILE *fp = stdin; |
155 |
< |
CMATRIX *cm; |
154 |
> |
const int ROWINC = 2048; |
155 |
> |
FILE *fp = stdin; |
156 |
> |
CMATRIX *cm; |
157 |
|
|
158 |
< |
if (inspec == NULL) |
158 |
> |
if (!inspec) |
159 |
|
inspec = "<stdin>"; |
160 |
|
else if (inspec[0] == '!') { |
161 |
|
fp = popen(inspec+1, "r"); |
162 |
< |
if (fp == NULL) { |
162 |
> |
if (!fp) { |
163 |
|
sprintf(errmsg, "cannot start command '%s'", inspec); |
164 |
|
error(SYSTEM, errmsg); |
165 |
|
} |
166 |
< |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
166 |
> |
} else if (!(fp = fopen(inspec, "r"))) { |
167 |
|
sprintf(errmsg, "cannot open file '%s'", inspec); |
168 |
|
error(SYSTEM, errmsg); |
169 |
|
} |
174 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
175 |
|
if (!dtype | !ncols) { /* expecting header? */ |
176 |
|
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
177 |
< |
if (err != NULL) |
177 |
> |
if (err) |
178 |
|
error(USER, err); |
179 |
|
if (ncols <= 0) |
180 |
|
error(USER, "unspecified number of columns"); |
215 |
|
cm = cm_alloc(guessrows, ncols); |
216 |
|
} else |
217 |
|
cm = cm_alloc(nrows, ncols); |
218 |
< |
if (cm == NULL) /* XXX never happens */ |
218 |
> |
if (!cm) /* XXX never happens */ |
219 |
|
return(NULL); |
220 |
|
if (dtype == DTascii) { /* read text file */ |
221 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
222 |
|
int r, c; |
223 |
|
for (r = 0; r < maxrow; r++) { |
224 |
|
if (r >= cm->nrows) /* need more space? */ |
225 |
< |
cm = cm_resize(cm, 2*cm->nrows); |
225 |
> |
cm = cm_resize(cm, cm->nrows+ROWINC); |
226 |
|
for (c = 0; c < ncols; c++) { |
227 |
|
COLORV *cv = cm_lval(cm,r,c); |
228 |
|
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { |
246 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
247 |
|
int nread = 0; |
248 |
|
do { /* read all we can */ |
249 |
< |
nread += fread(cm->cmem + 3*nread, |
249 |
> |
nread += getbinary(cm->cmem + 3*nread, |
250 |
|
sizeof(COLOR), |
251 |
|
cm->nrows*cm->ncols - nread, |
252 |
|
fp); |
253 |
|
if (nrows <= 0) { /* unknown length */ |
254 |
|
if (nread == cm->nrows*cm->ncols) |
255 |
|
/* need more space? */ |
256 |
< |
cm = cm_resize(cm, 2*cm->nrows); |
256 |
> |
cm = cm_resize(cm, cm->nrows+ROWINC); |
257 |
|
else if (nread && !(nread % cm->ncols)) |
258 |
|
/* seem to be done */ |
259 |
|
cm = cm_resize(cm, nread/cm->ncols); |
271 |
|
if (n <= 0) |
272 |
|
goto not_handled; |
273 |
|
while (n--) { |
274 |
< |
if (fread(dc, sizeof(double), 3, fp) != 3) |
274 |
> |
if (getbinary(dc, sizeof(double), 3, fp) != 3) |
275 |
|
goto EOFerror; |
276 |
|
copycolor(cvp, dc); |
277 |
|
cvp += 3; |
284 |
|
if (n <= 0) |
285 |
|
goto not_handled; |
286 |
|
while (n--) { |
287 |
< |
if (fread(fc, sizeof(float), 3, fp) != 3) |
287 |
> |
if (getbinary(fc, sizeof(float), 3, fp) != 3) |
288 |
|
goto EOFerror; |
289 |
|
copycolor(cvp, fc); |
290 |
|
cvp += 3; |
325 |
|
CMATRIX *cvr; |
326 |
|
int dr; |
327 |
|
|
328 |
+ |
if (!cm) |
329 |
+ |
return(NULL); |
330 |
|
if ((c < 0) | (c >= cm->ncols)) |
331 |
|
error(INTERNAL, "column requested outside matrix"); |
332 |
|
cvr = cm_alloc(cm->nrows, 1); |
333 |
< |
if (cvr == NULL) |
333 |
> |
if (!cvr) |
334 |
|
return(NULL); |
335 |
|
for (dr = 0; dr < cm->nrows; dr++) { |
336 |
|
const COLORV *sp = cm_lval(cm,dr,c); |
342 |
|
return(cvr); |
343 |
|
} |
344 |
|
|
339 |
– |
/* Scale a matrix by a single value */ |
340 |
– |
CMATRIX * |
341 |
– |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
342 |
– |
{ |
343 |
– |
CMATRIX *cmr; |
344 |
– |
int dr, dc; |
345 |
– |
|
346 |
– |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
347 |
– |
if (cmr == NULL) |
348 |
– |
return(NULL); |
349 |
– |
for (dr = 0; dr < cmr->nrows; dr++) |
350 |
– |
for (dc = 0; dc < cmr->ncols; dc++) { |
351 |
– |
const COLORV *sp = cm_lval(cm1,dr,dc); |
352 |
– |
COLORV *dp = cm_lval(cmr,dr,dc); |
353 |
– |
dp[0] = sp[0] * sca[0]; |
354 |
– |
dp[1] = sp[1] * sca[1]; |
355 |
– |
dp[2] = sp[2] * sca[2]; |
356 |
– |
} |
357 |
– |
return(cmr); |
358 |
– |
} |
359 |
– |
|
345 |
|
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
346 |
|
CMATRIX * |
347 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
350 |
|
CMATRIX *cmr; |
351 |
|
int dr, dc, i; |
352 |
|
|
353 |
+ |
if (!cm1 | !cm2) |
354 |
+ |
return(NULL); |
355 |
|
if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows)) |
356 |
|
error(INTERNAL, "matrix dimension mismatch in cm_multiply()"); |
357 |
|
cmr = cm_alloc(cm1->nrows, cm2->ncols); |
358 |
< |
if (cmr == NULL) |
358 |
> |
if (!cmr) |
359 |
|
return(NULL); |
360 |
|
/* optimization: check for zero rows & cols */ |
361 |
|
if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) { |
380 |
|
COLORV *dp = cm_lval(cmr,dr,dc); |
381 |
|
double res[3]; |
382 |
|
dp[0] = dp[1] = dp[2] = 0; |
383 |
< |
if (rowcheck != NULL && !rowcheck[dr]) |
383 |
> |
if (rowcheck && !rowcheck[dr]) |
384 |
|
continue; |
385 |
< |
if (colcheck != NULL && !colcheck[dc]) |
385 |
> |
if (colcheck && !colcheck[dc]) |
386 |
|
continue; |
387 |
|
res[0] = res[1] = res[2] = 0; |
388 |
|
for (i = 0; i < cm1->ncols; i++) { |
389 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
390 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
391 |
< |
res[0] += cp1[0] * cp2[0]; |
392 |
< |
res[1] += cp1[1] * cp2[1]; |
393 |
< |
res[2] += cp1[2] * cp2[2]; |
391 |
> |
res[0] += (double)cp1[0] * cp2[0]; |
392 |
> |
res[1] += (double)cp1[1] * cp2[1]; |
393 |
> |
res[2] += (double)cp1[2] * cp2[2]; |
394 |
|
} |
395 |
|
copycolor(dp, res); |
396 |
|
} |
397 |
< |
if (rowcheck != NULL) free(rowcheck); |
398 |
< |
if (colcheck != NULL) free(colcheck); |
397 |
> |
if (rowcheck) free(rowcheck); |
398 |
> |
if (colcheck) free(colcheck); |
399 |
|
return(cmr); |
400 |
|
} |
401 |
|
|
404 |
|
cm_write(const CMATRIX *cm, int dtype, FILE *fp) |
405 |
|
{ |
406 |
|
static const char tabEOL[2] = {'\t','\n'}; |
407 |
< |
const COLORV *mp = cm->cmem; |
407 |
> |
const COLORV *mp; |
408 |
|
int r, c; |
409 |
|
|
410 |
+ |
if (!cm) |
411 |
+ |
return(0); |
412 |
+ |
mp = cm->cmem; |
413 |
|
switch (dtype) { |
414 |
|
case DTascii: |
415 |
|
for (r = 0; r < cm->nrows; r++) |
423 |
|
if (sizeof(COLOR) == cm_elem_size[dtype]) { |
424 |
|
r = cm->ncols*cm->nrows; |
425 |
|
while (r > 0) { |
426 |
< |
c = fwrite(mp, sizeof(COLOR), r, fp); |
426 |
> |
c = putbinary(mp, sizeof(COLOR), r, fp); |
427 |
|
if (c <= 0) |
428 |
|
return(0); |
429 |
|
mp += 3*c; |
434 |
|
r = cm->ncols*cm->nrows; |
435 |
|
while (r--) { |
436 |
|
copycolor(dc, mp); |
437 |
< |
if (fwrite(dc, sizeof(double), 3, fp) != 3) |
437 |
> |
if (putbinary(dc, sizeof(double), 3, fp) != 3) |
438 |
|
return(0); |
439 |
|
mp += 3; |
440 |
|
} |
443 |
|
r = cm->ncols*cm->nrows; |
444 |
|
while (r--) { |
445 |
|
copycolor(fc, mp); |
446 |
< |
if (fwrite(fc, sizeof(float), 3, fp) != 3) |
446 |
> |
if (putbinary(fc, sizeof(float), 3, fp) != 3) |
447 |
|
return(0); |
448 |
|
mp += 3; |
449 |
|
} |