9 |
|
#include <stdlib.h> |
10 |
|
#include <string.h> |
11 |
|
#include <fcntl.h> |
12 |
+ |
#include "rtio.h" |
13 |
+ |
#include "platform.h" |
14 |
|
#include "resolu.h" |
15 |
+ |
#include "paths.h" |
16 |
|
#include "rmatrix.h" |
17 |
|
|
18 |
|
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
49 |
|
int |
50 |
|
rmx_newtype(int dtyp1, int dtyp2) |
51 |
|
{ |
52 |
< |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2) |
52 |
> |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
53 |
> |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
54 |
> |
&& dtyp1 != dtyp2) |
55 |
|
return(0); |
56 |
|
if (dtyp1 < dtyp2) |
57 |
|
return(dtyp1); |
64 |
|
{ |
65 |
|
if (!info || !*info) |
66 |
|
return(0); |
67 |
< |
if (!rm->info) |
67 |
> |
if (!rm->info) { |
68 |
|
rm->info = (char *)malloc(strlen(info)+1); |
69 |
< |
else |
69 |
> |
if (rm->info) rm->info[0] = '\0'; |
70 |
> |
} else |
71 |
|
rm->info = (char *)realloc(rm->info, |
72 |
|
strlen(rm->info)+strlen(info)+1); |
73 |
|
if (!rm->info) |
113 |
|
rmx_load_ascii(RMATRIX *rm, FILE *fp) |
114 |
|
{ |
115 |
|
int i, j, k; |
116 |
< |
#ifdef _WIN32 |
111 |
< |
_setmode(fileno(fp), _O_TEXT); |
112 |
< |
#endif |
116 |
> |
|
117 |
|
for (i = 0; i < rm->nrows; i++) |
118 |
|
for (j = 0; j < rm->ncols; j++) |
119 |
|
for (k = 0; k < rm->ncomp; k++) |
134 |
|
} |
135 |
|
for (i = 0; i < rm->nrows; i++) |
136 |
|
for (j = 0; j < rm->ncols; j++) { |
137 |
< |
if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
137 |
> |
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
138 |
|
return(0); |
139 |
|
for (k = rm->ncomp; k--; ) |
140 |
|
rmx_lval(rm,i,j,k) = val[k]; |
154 |
|
} |
155 |
|
for (i = 0; i < rm->nrows; i++) |
156 |
|
for (j = 0; j < rm->ncols; j++) { |
157 |
< |
if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
157 |
> |
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
158 |
|
return(0); |
159 |
|
for (k = rm->ncomp; k--; ) |
160 |
|
rmx_lval(rm,i,j,k) = val[k]; |
187 |
|
|
188 |
|
/* Load matrix from supported file type */ |
189 |
|
RMATRIX * |
190 |
< |
rmx_load(const char *fname) |
190 |
> |
rmx_load(const char *inspec) |
191 |
|
{ |
192 |
|
FILE *fp = stdin; |
193 |
|
RMATRIX dinfo; |
194 |
|
RMATRIX *dnew; |
195 |
|
|
196 |
< |
if (fname == NULL) { /* reading from stdin? */ |
197 |
< |
fname = "<stdin>"; |
196 |
> |
if (inspec == NULL) { /* reading from stdin? */ |
197 |
> |
inspec = "<stdin>"; |
198 |
> |
SET_FILE_BINARY(stdin); |
199 |
> |
} else if (inspec[0] == '!') { |
200 |
> |
if ((fp = popen(inspec+1, "r")) == NULL) |
201 |
> |
return(NULL); |
202 |
> |
SET_FILE_BINARY(stdin); |
203 |
|
} else { |
204 |
< |
const char *sp = fname; /* check suffix */ |
204 |
> |
const char *sp = inspec; /* check suffix */ |
205 |
|
while (*sp) |
206 |
|
++sp; |
207 |
< |
while (sp > fname && sp[-1] != '.') |
207 |
> |
while (sp > inspec && sp[-1] != '.') |
208 |
|
--sp; |
209 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
210 |
< |
CMATRIX *cm = cm_loadBTDF((char *)fname); |
210 |
> |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
211 |
|
if (cm == NULL) |
212 |
|
return(NULL); |
213 |
|
dnew = rmx_from_cmatrix(cm); |
214 |
|
cm_free(cm); |
215 |
+ |
dnew->dtype = DTascii; |
216 |
|
return(dnew); |
217 |
|
} |
218 |
|
/* else open it ourselves */ |
219 |
< |
if ((fp = fopen(fname, "rb")) == NULL) |
219 |
> |
if ((fp = fopen(inspec, "rb")) == NULL) |
220 |
|
return(NULL); |
221 |
|
} |
222 |
|
#ifdef getc_unlocked |
250 |
|
dnew->info = dinfo.info; |
251 |
|
switch (dinfo.dtype) { |
252 |
|
case DTascii: |
253 |
+ |
SET_FILE_TEXT(stdin); |
254 |
|
if (!rmx_load_ascii(dnew, fp)) |
255 |
|
goto loaderr; |
256 |
+ |
dnew->dtype = DTascii; /* should leave double? */ |
257 |
|
break; |
258 |
|
case DTfloat: |
259 |
|
if (!rmx_load_float(dnew, fp)) |
274 |
|
default: |
275 |
|
goto loaderr; |
276 |
|
} |
277 |
< |
if (fp != stdin) |
278 |
< |
fclose(fp); |
277 |
> |
if (fp != stdin) { |
278 |
> |
if (inspec[0] == '!') |
279 |
> |
pclose(fp); |
280 |
> |
else |
281 |
> |
fclose(fp); |
282 |
> |
} |
283 |
> |
#ifdef getc_unlocked |
284 |
> |
else |
285 |
> |
funlockfile(fp); |
286 |
> |
#endif |
287 |
|
return(dnew); |
288 |
|
loaderr: /* should report error? */ |
289 |
< |
fclose(fp); |
289 |
> |
if (inspec[0] == '!') |
290 |
> |
pclose(fp); |
291 |
> |
else |
292 |
> |
fclose(fp); |
293 |
|
rmx_free(dnew); |
294 |
|
return(NULL); |
295 |
|
} |
298 |
|
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
299 |
|
{ |
300 |
|
int i, j, k; |
301 |
< |
#ifdef _WIN32 |
279 |
< |
_setmode(fileno(fp), _O_TEXT); |
280 |
< |
#endif |
301 |
> |
|
302 |
|
for (i = 0; i < rm->nrows; i++) { |
303 |
|
for (j = 0; j < rm->ncols; j++) { |
304 |
|
for (k = 0; k < rm->ncomp; k++) |
324 |
|
for (j = 0; j < rm->ncols; j++) { |
325 |
|
for (k = rm->ncomp; k--; ) |
326 |
|
val[k] = (float)rmx_lval(rm,i,j,k); |
327 |
< |
if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
327 |
> |
if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
328 |
|
return(0); |
329 |
|
} |
330 |
|
return(1); |
344 |
|
for (j = 0; j < rm->ncols; j++) { |
345 |
|
for (k = rm->ncomp; k--; ) |
346 |
|
val[k] = rmx_lval(rm,i,j,k); |
347 |
< |
if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
347 |
> |
if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
348 |
|
return(0); |
349 |
|
} |
350 |
|
return(1); |
373 |
|
} |
374 |
|
|
375 |
|
/* Write matrix to file type indicated by dtype */ |
376 |
< |
long |
376 |
> |
int |
377 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
378 |
|
{ |
379 |
|
RMATRIX *mydm = NULL; |
381 |
|
|
382 |
|
if ((rm == NULL) | (fp == NULL)) |
383 |
|
return(0); |
384 |
+ |
#ifdef getc_unlocked |
385 |
+ |
flockfile(fp); |
386 |
+ |
#endif |
387 |
|
/* complete header */ |
388 |
|
if (rm->info) |
389 |
|
fputs(rm->info, fp); |
391 |
|
dtype = rm->dtype; |
392 |
|
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
393 |
|
dtype = DTxyze; |
394 |
< |
else if ((dtype = DTxyze) & (rm->dtype == DTrgbe)) |
394 |
> |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
395 |
|
dtype = DTrgbe; |
396 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
397 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
428 |
|
return(0); |
429 |
|
} |
430 |
|
ok &= (fflush(fp) == 0); |
431 |
+ |
#ifdef getc_unlocked |
432 |
+ |
funlockfile(fp); |
433 |
+ |
#endif |
434 |
|
rmx_free(mydm); |
435 |
< |
return(ftell(fp) * ok); /* return # bytes written */ |
435 |
> |
return(ok); |
436 |
|
} |
437 |
|
|
438 |
|
/* Allocate and assign square identity matrix with n components */ |
440 |
|
rmx_identity(const int dim, const int n) |
441 |
|
{ |
442 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
443 |
< |
int i; |
443 |
> |
int i, k; |
444 |
|
|
445 |
|
if (rid == NULL) |
446 |
|
return(NULL); |
447 |
< |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim); |
447 |
> |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
448 |
|
for (i = dim; i--; ) |
449 |
< |
rmx_lval(rid,i,i,0) = 1; |
450 |
< |
for (i = n; --i; ) |
424 |
< |
memcpy(rid->mtx+i*(dim*dim), rid->mtx, |
425 |
< |
sizeof(rid->mtx[0])*dim*dim); |
449 |
> |
for (k = n; k--; ) |
450 |
> |
rmx_lval(rid,i,i,k) = 1; |
451 |
|
return(rid); |
452 |
|
} |
453 |
|
|
513 |
|
rmx_addinfo(mres, rmx_mismatch_warn); |
514 |
|
for (i = mres->nrows; i--; ) |
515 |
|
for (j = mres->ncols; j--; ) |
516 |
< |
for (h = m1->ncols; h--; ) { |
516 |
> |
for (k = mres->ncomp; k--; ) { |
517 |
|
long double d = 0; |
518 |
< |
for (k = mres->ncomp; k--; ) |
518 |
> |
for (h = m1->ncols; h--; ) |
519 |
|
d += (long double)rmx_lval(m1,i,h,k) * |
520 |
|
(long double)rmx_lval(m2,h,j,k); |
521 |
|
rmx_lval(mres,i,j,k) = (double)d; |