10 |
|
#include <string.h> |
11 |
|
#include <fcntl.h> |
12 |
|
#include "resolu.h" |
13 |
+ |
#include "rtprocess.h" |
14 |
|
#include "rmatrix.h" |
15 |
|
|
16 |
< |
typedef struct { |
16 |
< |
int nrows, ncols, ncomp; |
17 |
< |
int dtype; |
18 |
< |
} DMINFO; |
16 |
> |
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
17 |
|
|
18 |
|
/* Allocate a nr x nc matrix with n components */ |
19 |
|
RMATRIX * |
28 |
|
if (dnew == NULL) |
29 |
|
return(NULL); |
30 |
|
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
31 |
+ |
dnew->dtype = DTdouble; |
32 |
+ |
dnew->info = NULL; |
33 |
|
return(dnew); |
34 |
|
} |
35 |
|
|
36 |
+ |
/* Free a RMATRIX array */ |
37 |
+ |
void |
38 |
+ |
rmx_free(RMATRIX *rm) |
39 |
+ |
{ |
40 |
+ |
if (!rm) return; |
41 |
+ |
if (rm->info) |
42 |
+ |
free(rm->info); |
43 |
+ |
free(rm); |
44 |
+ |
} |
45 |
+ |
|
46 |
+ |
/* Resolve data type based on two input types (returns 0 for mismatch) */ |
47 |
+ |
int |
48 |
+ |
rmx_newtype(int dtyp1, int dtyp2) |
49 |
+ |
{ |
50 |
+ |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
51 |
+ |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
52 |
+ |
&& dtyp1 != dtyp2) |
53 |
+ |
return(0); |
54 |
+ |
if (dtyp1 < dtyp2) |
55 |
+ |
return(dtyp1); |
56 |
+ |
return(dtyp2); |
57 |
+ |
} |
58 |
+ |
|
59 |
+ |
/* Append header information associated with matrix data */ |
60 |
+ |
int |
61 |
+ |
rmx_addinfo(RMATRIX *rm, const char *info) |
62 |
+ |
{ |
63 |
+ |
if (!info || !*info) |
64 |
+ |
return(0); |
65 |
+ |
if (!rm->info) { |
66 |
+ |
rm->info = (char *)malloc(strlen(info)+1); |
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) |
72 |
+ |
return(0); |
73 |
+ |
strcat(rm->info, info); |
74 |
+ |
return(1); |
75 |
+ |
} |
76 |
+ |
|
77 |
|
static int |
78 |
|
get_dminfo(char *s, void *p) |
79 |
|
{ |
80 |
< |
DMINFO *ip = (DMINFO *)p; |
81 |
< |
char fmt[32]; |
80 |
> |
RMATRIX *ip = (RMATRIX *)p; |
81 |
> |
char fmt[64]; |
82 |
|
int i; |
83 |
|
|
84 |
+ |
if (headidval(fmt, s)) |
85 |
+ |
return(0); |
86 |
|
if (!strncmp(s, "NCOMP=", 6)) { |
87 |
|
ip->ncomp = atoi(s+6); |
88 |
|
return(0); |
95 |
|
ip->ncols = atoi(s+6); |
96 |
|
return(0); |
97 |
|
} |
98 |
< |
if (!formatval(fmt, s)) |
98 |
> |
if (!formatval(fmt, s)) { |
99 |
> |
rmx_addinfo(ip, s); |
100 |
|
return(0); |
101 |
+ |
} |
102 |
|
for (i = 1; i < DTend; i++) |
103 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
104 |
|
ip->dtype = i; |
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 |
< |
DMINFO dinfo; |
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 |
> |
#ifdef _WIN32 |
199 |
> |
_setmode(fileno(stdin), _O_BINARY); |
200 |
> |
#endif |
201 |
> |
} else if (inspec[0] == '!') { |
202 |
> |
if ((fp = popen(inspec+1, "r")) == NULL) |
203 |
> |
return(NULL); |
204 |
> |
#ifdef _WIN32 |
205 |
> |
_setmode(fileno(fp), _O_BINARY); |
206 |
> |
#endif |
207 |
|
} else { |
208 |
< |
const char *sp = fname; /* check suffix */ |
208 |
> |
const char *sp = inspec; /* check suffix */ |
209 |
|
while (*sp) |
210 |
|
++sp; |
211 |
< |
while (sp > fname && sp[-1] != '.') |
211 |
> |
while (sp > inspec && sp[-1] != '.') |
212 |
|
--sp; |
213 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
214 |
< |
CMATRIX *cm = cm_loadBTDF((char *)fname); |
214 |
> |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
215 |
|
if (cm == NULL) |
216 |
|
return(NULL); |
217 |
|
dnew = rmx_from_cmatrix(cm); |
219 |
|
return(dnew); |
220 |
|
} |
221 |
|
/* else open it ourselves */ |
222 |
< |
if ((fp = fopen(fname, "rb")) == NULL) |
222 |
> |
if ((fp = fopen(inspec, "rb")) == NULL) |
223 |
|
return(NULL); |
224 |
|
} |
225 |
|
#ifdef getc_unlocked |
226 |
|
flockfile(fp); |
227 |
|
#endif |
228 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
229 |
< |
dinfo.dtype = DTascii; |
230 |
< |
if (getheader(fp, &get_dminfo, &dinfo) < 0) { |
229 |
> |
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
230 |
> |
dinfo.info = NULL; |
231 |
> |
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
232 |
|
fclose(fp); |
233 |
|
return(NULL); |
234 |
|
} |
235 |
< |
if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze)) { |
235 |
> |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
236 |
|
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
237 |
|
fclose(fp); |
238 |
|
return(NULL); |
239 |
|
} |
240 |
< |
dinfo.ncomp = 3; |
240 |
> |
if (dinfo.ncomp <= 0) |
241 |
> |
dinfo.ncomp = 3; |
242 |
> |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
243 |
> |
dinfo.ncomp != 3) { |
244 |
> |
fclose(fp); |
245 |
> |
return(NULL); |
246 |
> |
} |
247 |
|
} |
248 |
|
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
249 |
|
if (dnew == NULL) { |
250 |
|
fclose(fp); |
251 |
|
return(NULL); |
252 |
|
} |
253 |
+ |
dnew->info = dinfo.info; |
254 |
|
switch (dinfo.dtype) { |
255 |
|
case DTascii: |
256 |
|
if (!rmx_load_ascii(dnew, fp)) |
257 |
|
goto loaderr; |
258 |
+ |
dnew->dtype = DTascii; /* should leave double? */ |
259 |
|
break; |
260 |
|
case DTfloat: |
261 |
|
if (!rmx_load_float(dnew, fp)) |
262 |
|
goto loaderr; |
263 |
+ |
dnew->dtype = DTfloat; |
264 |
|
break; |
265 |
|
case DTdouble: |
266 |
|
if (!rmx_load_double(dnew, fp)) |
267 |
|
goto loaderr; |
268 |
+ |
dnew->dtype = DTdouble; |
269 |
|
break; |
270 |
|
case DTrgbe: |
271 |
|
case DTxyze: |
272 |
|
if (!rmx_load_rgbe(dnew, fp)) |
273 |
|
goto loaderr; |
274 |
+ |
dnew->dtype = dinfo.dtype; |
275 |
|
break; |
276 |
|
default: |
277 |
|
goto loaderr; |
278 |
|
} |
279 |
< |
if (fp != stdin) |
280 |
< |
fclose(fp); |
279 |
> |
if (fp != stdin) { |
280 |
> |
if (inspec[0] == '!') |
281 |
> |
pclose(fp); |
282 |
> |
else |
283 |
> |
fclose(fp); |
284 |
> |
} |
285 |
> |
#ifdef getc_unlocked |
286 |
> |
else |
287 |
> |
funlockfile(fp); |
288 |
> |
#endif |
289 |
|
return(dnew); |
290 |
|
loaderr: /* should report error? */ |
291 |
< |
fclose(fp); |
291 |
> |
if (inspec[0] == '!') |
292 |
> |
pclose(fp); |
293 |
> |
else |
294 |
> |
fclose(fp); |
295 |
|
rmx_free(dnew); |
296 |
|
return(NULL); |
297 |
|
} |
376 |
|
return(1); |
377 |
|
} |
378 |
|
|
379 |
< |
/* Write matrix to file type indicated by dt */ |
380 |
< |
long |
379 |
> |
/* Write matrix to file type indicated by dtype */ |
380 |
> |
int |
381 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
382 |
|
{ |
383 |
|
RMATRIX *mydm = NULL; |
386 |
|
if ((rm == NULL) | (fp == NULL)) |
387 |
|
return(0); |
388 |
|
/* complete header */ |
389 |
+ |
if (rm->info) |
390 |
+ |
fputs(rm->info, fp); |
391 |
+ |
if (dtype == DTfromHeader) |
392 |
+ |
dtype = rm->dtype; |
393 |
+ |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
394 |
+ |
dtype = DTxyze; |
395 |
+ |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
396 |
+ |
dtype = DTrgbe; |
397 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
398 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
399 |
|
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
430 |
|
} |
431 |
|
ok &= (fflush(fp) == 0); |
432 |
|
rmx_free(mydm); |
433 |
< |
return(ftell(fp) * ok); /* return # bytes written */ |
433 |
> |
return(ok); |
434 |
|
} |
435 |
|
|
436 |
|
/* Allocate and assign square identity matrix with n components */ |
438 |
|
rmx_identity(const int dim, const int n) |
439 |
|
{ |
440 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
441 |
< |
int i; |
441 |
> |
int i, k; |
442 |
|
|
443 |
|
if (rid == NULL) |
444 |
|
return(NULL); |
445 |
< |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim); |
445 |
> |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
446 |
|
for (i = dim; i--; ) |
447 |
< |
rmx_lval(rid,i,i,0) = 1; |
448 |
< |
for (i = n; --i; ) |
364 |
< |
memcpy(rid->mtx+i*(dim*dim), rid->mtx, |
365 |
< |
sizeof(rid->mtx[0])*dim*dim); |
447 |
> |
for (k = n; k--; ) |
448 |
> |
rmx_lval(rid,i,i,k) = 1; |
449 |
|
return(rid); |
450 |
|
} |
451 |
|
|
460 |
|
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
461 |
|
if (dnew == NULL) |
462 |
|
return(NULL); |
463 |
+ |
rmx_addinfo(dnew, rm->info); |
464 |
+ |
dnew->dtype = rm->dtype; |
465 |
|
memcpy(dnew->mtx, rm->mtx, |
466 |
|
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
467 |
|
return(dnew); |
468 |
|
} |
469 |
|
|
470 |
< |
/* Swap rows and columns in the given matrix in situ */ |
471 |
< |
int |
472 |
< |
rmx_transpose(RMATRIX *rm) |
470 |
> |
/* Allocate and assign transposed matrix */ |
471 |
> |
RMATRIX * |
472 |
> |
rmx_transpose(const RMATRIX *rm) |
473 |
|
{ |
474 |
< |
RMATRIX dswap; |
474 |
> |
RMATRIX *dnew; |
475 |
|
int i, j, k; |
476 |
|
|
477 |
|
if (rm == NULL) |
478 |
|
return(0); |
479 |
< |
dswap.nrows = rm->ncols; |
480 |
< |
dswap.ncols = rm->nrows; |
481 |
< |
dswap.ncomp = rm->ncomp; |
482 |
< |
for (i = 1; i < rm->nrows; i++) |
483 |
< |
for (j = 0; j < i; j++) |
484 |
< |
for (k = rm->ncomp; k--; ) { |
485 |
< |
double *opp = rm->mtx + rmx_indx(&dswap,j,i,k); |
486 |
< |
double d = *opp; |
487 |
< |
*opp = rmx_lval(rm,i,j,k); |
488 |
< |
rmx_lval(rm,i,j,k) = d; |
489 |
< |
} |
490 |
< |
rm->nrows = dswap.nrows; |
491 |
< |
rm->ncols = dswap.ncols; |
407 |
< |
return(1); |
479 |
> |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
480 |
> |
if (dnew == NULL) |
481 |
> |
return(NULL); |
482 |
> |
if (rm->info) { |
483 |
> |
rmx_addinfo(dnew, rm->info); |
484 |
> |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
485 |
> |
} |
486 |
> |
dnew->dtype = rm->dtype; |
487 |
> |
for (i = dnew->nrows; i--; ) |
488 |
> |
for (j = dnew->ncols; j--; ) |
489 |
> |
for (k = dnew->ncomp; k--; ) |
490 |
> |
rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k); |
491 |
> |
return(dnew); |
492 |
|
} |
493 |
|
|
494 |
|
/* Multiply (concatenate) two matrices and allocate the result */ |
504 |
|
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
505 |
|
if (mres == NULL) |
506 |
|
return(NULL); |
507 |
+ |
i = rmx_newtype(m1->dtype, m2->dtype); |
508 |
+ |
if (i) |
509 |
+ |
mres->dtype = i; |
510 |
+ |
else |
511 |
+ |
rmx_addinfo(mres, rmx_mismatch_warn); |
512 |
|
for (i = mres->nrows; i--; ) |
513 |
|
for (j = mres->ncols; j--; ) |
514 |
< |
for (h = m1->ncols; h--; ) { |
514 |
> |
for (k = mres->ncomp; k--; ) { |
515 |
|
long double d = 0; |
516 |
< |
for (k = mres->ncomp; k--; ) |
517 |
< |
d += (long double)rmx_lval(m1,i,h,k) * |
429 |
< |
(long double)rmx_lval(m2,h,j,k); |
516 |
> |
for (h = m1->ncols; h--; ) |
517 |
> |
d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k); |
518 |
|
rmx_lval(mres,i,j,k) = (double)d; |
519 |
|
} |
520 |
|
return(mres); |
540 |
|
mysf[k] = 1; |
541 |
|
sf = mysf; |
542 |
|
} |
543 |
+ |
i = rmx_newtype(msum->dtype, madd->dtype); |
544 |
+ |
if (i) |
545 |
+ |
msum->dtype = i; |
546 |
+ |
else |
547 |
+ |
rmx_addinfo(msum, rmx_mismatch_warn); |
548 |
|
for (i = msum->nrows; i--; ) |
549 |
|
for (j = msum->ncols; j--; ) |
550 |
|
for (k = msum->ncomp; k--; ) |
582 |
|
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
583 |
|
if (dnew == NULL) |
584 |
|
return(NULL); |
585 |
+ |
dnew->dtype = msrc->dtype; |
586 |
|
for (i = dnew->nrows; i--; ) |
587 |
|
for (j = dnew->ncols; j--; ) |
588 |
|
for (kd = dnew->ncomp; kd--; ) { |
606 |
|
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
607 |
|
if (dnew == NULL) |
608 |
|
return(NULL); |
609 |
+ |
dnew->dtype = DTfloat; |
610 |
|
for (i = dnew->nrows; i--; ) |
611 |
|
for (j = dnew->ncols; j--; ) { |
612 |
|
const COLORV *cv = cm_lval(cm,i,j); |