11 |
|
#include "standard.h" |
12 |
|
#include "cmatrix.h" |
13 |
|
#include "platform.h" |
14 |
+ |
#include "rtprocess.h" |
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
|
const char *cm_fmt_id[] = { |
144 |
|
return(NULL); |
145 |
|
} |
146 |
|
|
147 |
< |
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
147 |
> |
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
148 |
|
CMATRIX * |
149 |
< |
cm_load(const char *fname, int nrows, int ncols, int dtype) |
149 |
> |
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
150 |
|
{ |
151 |
|
FILE *fp = stdin; |
152 |
|
CMATRIX *cm; |
153 |
|
|
154 |
< |
if (fname == NULL) |
155 |
< |
fname = "<stdin>"; |
156 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
157 |
< |
sprintf(errmsg, "cannot open file '%s'", fname); |
154 |
> |
if (inspec == NULL) |
155 |
> |
inspec = "<stdin>"; |
156 |
> |
else if (inspec[0] == '!') { |
157 |
> |
fp = popen(inspec+1, "r"); |
158 |
> |
if (fp == NULL) { |
159 |
> |
sprintf(errmsg, "cannot start command '%s'", inspec); |
160 |
> |
error(SYSTEM, errmsg); |
161 |
> |
} |
162 |
> |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
163 |
> |
sprintf(errmsg, "cannot open file '%s'", inspec); |
164 |
|
error(SYSTEM, errmsg); |
165 |
|
} |
166 |
|
#ifdef getc_unlocked |
185 |
|
} |
186 |
|
if (nrows <= 0) { /* don't know length? */ |
187 |
|
int guessrows = 147; /* usually big enough */ |
188 |
< |
if ((dtype != DTascii) & (fp != stdin)) { |
188 |
> |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
189 |
|
long startpos = ftell(fp); |
190 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
191 |
|
long endpos = ftell(fp); |
195 |
|
if ((endpos - startpos) % (ncols*elemsiz)) { |
196 |
|
sprintf(errmsg, |
197 |
|
"improper length for binary file '%s'", |
198 |
< |
fname); |
198 |
> |
inspec); |
199 |
|
error(USER, errmsg); |
200 |
|
} |
201 |
|
guessrows = (endpos - startpos)/(ncols*elemsiz); |
202 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
203 |
|
sprintf(errmsg, |
204 |
|
"fseek() error on file '%s'", |
205 |
< |
fname); |
205 |
> |
inspec); |
206 |
|
error(SYSTEM, errmsg); |
207 |
|
} |
208 |
|
nrows = guessrows; /* we're confident */ |
232 |
|
while ((c = getc(fp)) != EOF) |
233 |
|
if (!isspace(c)) { |
234 |
|
sprintf(errmsg, |
235 |
< |
"unexpected data at end of ascii file %s", |
236 |
< |
fname); |
235 |
> |
"unexpected data at end of ascii input '%s'", |
236 |
> |
inspec); |
237 |
|
error(WARNING, errmsg); |
238 |
|
break; |
239 |
|
} |
287 |
|
} |
288 |
|
if (fgetc(fp) != EOF) { |
289 |
|
sprintf(errmsg, |
290 |
< |
"unexpected data at end of binary file %s", |
291 |
< |
fname); |
290 |
> |
"unexpected data at end of binary input '%s'", |
291 |
> |
inspec); |
292 |
|
error(WARNING, errmsg); |
293 |
|
} |
294 |
|
} |
295 |
< |
if (fp != stdin) |
296 |
< |
fclose(fp); |
295 |
> |
if (fp != stdin) { |
296 |
> |
if (inspec[0] != '!') |
297 |
> |
fclose(fp); |
298 |
> |
else if (pclose(fp)) { |
299 |
> |
sprintf(errmsg, "error running command '%s'", inspec); |
300 |
> |
error(WARNING, errmsg); |
301 |
> |
} |
302 |
> |
} |
303 |
|
#ifdef getc_unlocked |
304 |
|
else |
305 |
|
funlockfile(fp); |
306 |
|
#endif |
307 |
|
return(cm); |
308 |
|
EOFerror: |
309 |
< |
sprintf(errmsg, "unexpected EOF reading %s", fname); |
309 |
> |
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
310 |
|
error(USER, errmsg); |
311 |
|
not_handled: |
312 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |
400 |
|
for (i = 0; i < cm1->ncols; i++) { |
401 |
|
const COLORV *cp1 = cm_lval(cm1,dr,i); |
402 |
|
const COLORV *cp2 = cm_lval(cm2,i,dc); |
403 |
< |
res[0] += cp1[0] * cp2[0]; |
404 |
< |
res[1] += cp1[1] * cp2[1]; |
405 |
< |
res[2] += cp1[2] * cp2[2]; |
403 |
> |
res[0] += (double)cp1[0] * (double)cp2[0]; |
404 |
> |
res[1] += (double)cp1[1] * (double)cp2[1]; |
405 |
> |
res[2] += (double)cp1[2] * (double)cp2[2]; |
406 |
|
} |
407 |
|
copycolor(dp, res); |
408 |
|
} |