| 10 |
|
#include <string.h> |
| 11 |
|
#include <fcntl.h> |
| 12 |
|
#include "resolu.h" |
| 13 |
+ |
#include "rtprocess.h" |
| 14 |
|
#include "rmatrix.h" |
| 15 |
|
|
| 16 |
|
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
| 47 |
|
int |
| 48 |
|
rmx_newtype(int dtyp1, int dtyp2) |
| 49 |
|
{ |
| 50 |
< |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2) |
| 50 |
> |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
| 51 |
> |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
| 52 |
> |
&& dtyp1 != dtyp2) |
| 53 |
|
return(0); |
| 54 |
|
if (dtyp1 < dtyp2) |
| 55 |
|
return(dtyp1); |
| 62 |
|
{ |
| 63 |
|
if (!info || !*info) |
| 64 |
|
return(0); |
| 65 |
< |
if (!rm->info) |
| 65 |
> |
if (!rm->info) { |
| 66 |
|
rm->info = (char *)malloc(strlen(info)+1); |
| 67 |
< |
else |
| 67 |
> |
if (rm->info) rm->info[0] = '\0'; |
| 68 |
> |
} else |
| 69 |
|
rm->info = (char *)realloc(rm->info, |
| 70 |
|
strlen(rm->info)+strlen(info)+1); |
| 71 |
|
if (!rm->info) |
| 111 |
|
rmx_load_ascii(RMATRIX *rm, FILE *fp) |
| 112 |
|
{ |
| 113 |
|
int i, j, k; |
| 114 |
< |
#ifdef _WIN32 |
| 111 |
< |
_setmode(fileno(fp), _O_TEXT); |
| 112 |
< |
#endif |
| 114 |
> |
|
| 115 |
|
for (i = 0; i < rm->nrows; i++) |
| 116 |
|
for (j = 0; j < rm->ncols; j++) |
| 117 |
|
for (k = 0; k < rm->ncomp; k++) |
| 185 |
|
|
| 186 |
|
/* Load matrix from supported file type */ |
| 187 |
|
RMATRIX * |
| 188 |
< |
rmx_load(const char *fname) |
| 188 |
> |
rmx_load(const char *inspec) |
| 189 |
|
{ |
| 190 |
|
FILE *fp = stdin; |
| 191 |
|
RMATRIX dinfo; |
| 192 |
|
RMATRIX *dnew; |
| 193 |
|
|
| 194 |
< |
if (fname == NULL) { /* reading from stdin? */ |
| 195 |
< |
fname = "<stdin>"; |
| 194 |
> |
if (inspec == NULL) { /* reading from stdin? */ |
| 195 |
> |
inspec = "<stdin>"; |
| 196 |
|
#ifdef _WIN32 |
| 197 |
|
_setmode(fileno(stdin), _O_BINARY); |
| 198 |
|
#endif |
| 199 |
+ |
} else if (inspec[0] == '!') { |
| 200 |
+ |
if ((fp = popen(inspec+1, "r")) == NULL) |
| 201 |
+ |
return(NULL); |
| 202 |
+ |
#ifdef _WIN32 |
| 203 |
+ |
_setmode(fileno(fp), _O_BINARY); |
| 204 |
+ |
#endif |
| 205 |
|
} else { |
| 206 |
< |
const char *sp = fname; /* check suffix */ |
| 206 |
> |
const char *sp = inspec; /* check suffix */ |
| 207 |
|
while (*sp) |
| 208 |
|
++sp; |
| 209 |
< |
while (sp > fname && sp[-1] != '.') |
| 209 |
> |
while (sp > inspec && sp[-1] != '.') |
| 210 |
|
--sp; |
| 211 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
| 212 |
< |
CMATRIX *cm = cm_loadBTDF((char *)fname); |
| 212 |
> |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
| 213 |
|
if (cm == NULL) |
| 214 |
|
return(NULL); |
| 215 |
|
dnew = rmx_from_cmatrix(cm); |
| 217 |
|
return(dnew); |
| 218 |
|
} |
| 219 |
|
/* else open it ourselves */ |
| 220 |
< |
if ((fp = fopen(fname, "rb")) == NULL) |
| 220 |
> |
if ((fp = fopen(inspec, "rb")) == NULL) |
| 221 |
|
return(NULL); |
| 222 |
|
} |
| 223 |
|
#ifdef getc_unlocked |
| 251 |
|
dnew->info = dinfo.info; |
| 252 |
|
switch (dinfo.dtype) { |
| 253 |
|
case DTascii: |
| 254 |
+ |
#ifdef _WIN32 |
| 255 |
+ |
_setmode(fileno(fp), _O_TEXT); |
| 256 |
+ |
#endif |
| 257 |
|
if (!rmx_load_ascii(dnew, fp)) |
| 258 |
|
goto loaderr; |
| 259 |
+ |
dnew->dtype = DTascii; /* should leave double? */ |
| 260 |
|
break; |
| 261 |
|
case DTfloat: |
| 262 |
|
if (!rmx_load_float(dnew, fp)) |
| 277 |
|
default: |
| 278 |
|
goto loaderr; |
| 279 |
|
} |
| 280 |
< |
if (fp != stdin) |
| 281 |
< |
fclose(fp); |
| 280 |
> |
if (fp != stdin) { |
| 281 |
> |
if (inspec[0] == '!') |
| 282 |
> |
pclose(fp); |
| 283 |
> |
else |
| 284 |
> |
fclose(fp); |
| 285 |
> |
} |
| 286 |
> |
#ifdef getc_unlocked |
| 287 |
> |
else |
| 288 |
> |
funlockfile(fp); |
| 289 |
> |
#endif |
| 290 |
|
return(dnew); |
| 291 |
|
loaderr: /* should report error? */ |
| 292 |
< |
fclose(fp); |
| 292 |
> |
if (inspec[0] == '!') |
| 293 |
> |
pclose(fp); |
| 294 |
> |
else |
| 295 |
> |
fclose(fp); |
| 296 |
|
rmx_free(dnew); |
| 297 |
|
return(NULL); |
| 298 |
|
} |
| 301 |
|
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
| 302 |
|
{ |
| 303 |
|
int i, j, k; |
| 304 |
< |
#ifdef _WIN32 |
| 282 |
< |
_setmode(fileno(fp), _O_TEXT); |
| 283 |
< |
#endif |
| 304 |
> |
|
| 305 |
|
for (i = 0; i < rm->nrows; i++) { |
| 306 |
|
for (j = 0; j < rm->ncols; j++) { |
| 307 |
|
for (k = 0; k < rm->ncomp; k++) |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
/* Write matrix to file type indicated by dtype */ |
| 379 |
< |
long |
| 379 |
> |
int |
| 380 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
| 381 |
|
{ |
| 382 |
|
RMATRIX *mydm = NULL; |
| 384 |
|
|
| 385 |
|
if ((rm == NULL) | (fp == NULL)) |
| 386 |
|
return(0); |
| 387 |
+ |
#ifdef getc_unlocked |
| 388 |
+ |
flockfile(fp); |
| 389 |
+ |
#endif |
| 390 |
|
/* complete header */ |
| 391 |
|
if (rm->info) |
| 392 |
|
fputs(rm->info, fp); |
| 394 |
|
dtype = rm->dtype; |
| 395 |
|
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
| 396 |
|
dtype = DTxyze; |
| 397 |
< |
else if ((dtype = DTxyze) & (rm->dtype == DTrgbe)) |
| 397 |
> |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
| 398 |
|
dtype = DTrgbe; |
| 399 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
| 400 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
| 431 |
|
return(0); |
| 432 |
|
} |
| 433 |
|
ok &= (fflush(fp) == 0); |
| 434 |
+ |
#ifdef getc_unlocked |
| 435 |
+ |
funlockfile(fp); |
| 436 |
+ |
#endif |
| 437 |
|
rmx_free(mydm); |
| 438 |
< |
return(ftell(fp) * ok); /* return # bytes written */ |
| 438 |
> |
return(ok); |
| 439 |
|
} |
| 440 |
|
|
| 441 |
|
/* Allocate and assign square identity matrix with n components */ |
| 443 |
|
rmx_identity(const int dim, const int n) |
| 444 |
|
{ |
| 445 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
| 446 |
< |
int i; |
| 446 |
> |
int i, k; |
| 447 |
|
|
| 448 |
|
if (rid == NULL) |
| 449 |
|
return(NULL); |
| 450 |
< |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim); |
| 450 |
> |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
| 451 |
|
for (i = dim; i--; ) |
| 452 |
< |
rmx_lval(rid,i,i,0) = 1; |
| 453 |
< |
for (i = n; --i; ) |
| 427 |
< |
memcpy(rid->mtx+i*(dim*dim), rid->mtx, |
| 428 |
< |
sizeof(rid->mtx[0])*dim*dim); |
| 452 |
> |
for (k = n; k--; ) |
| 453 |
> |
rmx_lval(rid,i,i,k) = 1; |
| 454 |
|
return(rid); |
| 455 |
|
} |
| 456 |
|
|
| 516 |
|
rmx_addinfo(mres, rmx_mismatch_warn); |
| 517 |
|
for (i = mres->nrows; i--; ) |
| 518 |
|
for (j = mres->ncols; j--; ) |
| 519 |
< |
for (h = m1->ncols; h--; ) { |
| 519 |
> |
for (k = mres->ncomp; k--; ) { |
| 520 |
|
long double d = 0; |
| 521 |
< |
for (k = mres->ncomp; k--; ) |
| 522 |
< |
d += (long double)rmx_lval(m1,i,h,k) * |
| 498 |
< |
(long double)rmx_lval(m2,h,j,k); |
| 521 |
> |
for (h = m1->ncols; h--; ) |
| 522 |
> |
d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k); |
| 523 |
|
rmx_lval(mres,i,j,k) = (double)d; |
| 524 |
|
} |
| 525 |
|
return(mres); |