18 |
|
|
19 |
|
static const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
20 |
|
|
21 |
– |
#define array_size(rm) (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp) |
22 |
– |
#define mapped_size(rm) ((char *)(rm)->mtx + array_size(rm) - (char *)(rm)->mapped) |
23 |
– |
|
21 |
|
/* Initialize a RMATRIX struct but don't allocate array space */ |
22 |
|
RMATRIX * |
23 |
|
rmx_new(int nr, int nc, int n) |
28 |
|
return(NULL); |
29 |
|
|
30 |
|
dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
31 |
< |
if (dnew) { |
32 |
< |
dnew->dtype = DTdouble; |
33 |
< |
dnew->nrows = nr; |
34 |
< |
dnew->ncols = nc; |
35 |
< |
dnew->ncomp = n; |
36 |
< |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
37 |
< |
memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart)); |
38 |
< |
} |
31 |
> |
if (!dnew) |
32 |
> |
return(NULL); |
33 |
> |
|
34 |
> |
dnew->dtype = DTdouble; |
35 |
> |
dnew->nrows = nr; |
36 |
> |
dnew->ncols = nc; |
37 |
> |
dnew->ncomp = n; |
38 |
> |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
39 |
> |
memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart)); |
40 |
> |
|
41 |
|
return(dnew); |
42 |
|
} |
43 |
|
|
50 |
|
return(1); |
51 |
|
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) |
52 |
|
return(0); |
53 |
< |
rm->mtx = (double *)malloc(array_size(rm)); |
53 |
> |
rm->mtx = (double *)malloc(rmx_array_size(rm)); |
54 |
> |
rm->pflags |= RMF_OURMEM; |
55 |
|
return(rm->mtx != NULL); |
56 |
|
} |
57 |
|
|
77 |
|
free(rm->info); |
78 |
|
rm->info = NULL; |
79 |
|
} |
80 |
< |
if (rm->mtx) { |
80 |
> |
if (rm->mtx && rm->pflags & RMF_OURMEM) { |
81 |
|
#ifdef MAP_FILE |
82 |
|
if (rm->mapped) { |
83 |
< |
munmap(rm->mapped, mapped_size(rm)); |
83 |
> |
munmap(rm->mapped, rmx_mapped_size(rm)); |
84 |
|
rm->mapped = NULL; |
85 |
|
} else |
86 |
|
#endif |
87 |
|
free(rm->mtx); |
88 |
< |
rm->mtx = NULL; |
88 |
> |
rm->pflags &= ~RMF_OURMEM; |
89 |
|
} |
90 |
+ |
rm->mtx = NULL; |
91 |
|
} |
92 |
|
|
93 |
|
/* Free an RMATRIX struct and data */ |
156 |
|
return(0); |
157 |
|
} |
158 |
|
if ((i = isbigendian(s)) >= 0) { |
159 |
< |
ip->swapin = (nativebigendian() != i); |
159 |
> |
if (nativebigendian() != i) |
160 |
> |
ip->pflags |= RMF_SWAPIN; |
161 |
> |
else |
162 |
> |
ip->pflags &= ~RMF_SWAPIN; |
163 |
|
return(0); |
164 |
|
} |
165 |
|
if (isexpos(s)) { |
214 |
|
for (j = 0; j < rm->ncols; j++) { |
215 |
|
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
216 |
|
return(0); |
217 |
< |
if (rm->swapin) |
217 |
> |
if (rm->pflags & RMF_SWAPIN) |
218 |
|
swap32((char *)val, rm->ncomp); |
219 |
|
for (k = 0; k < rm->ncomp; k++) |
220 |
|
*drp++ = val[k]; |
227 |
|
{ |
228 |
|
if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) |
229 |
|
return(0); |
230 |
< |
if (rm->swapin) |
230 |
> |
if (rm->pflags & RMF_SWAPIN) |
231 |
|
swap64((char *)drp, rm->ncols*rm->ncomp); |
232 |
|
return(1); |
233 |
|
} |
289 |
|
rm->ncomp = 3; |
290 |
|
setcolor(rm->cexp, 1.f, 1.f, 1.f); |
291 |
|
memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart)); |
292 |
< |
rm->swapin = 0; |
292 |
> |
rm->pflags = 0; |
293 |
|
} |
294 |
|
rm->dtype = DTascii; /* assumed w/o FORMAT */ |
295 |
|
if (getheader(fp, get_dminfo, rm) < 0) { |
296 |
|
fputs("Unrecognized matrix format\n", stderr); |
297 |
|
return(0); |
298 |
|
} |
299 |
< |
/* resolution string? */ |
300 |
< |
if ((rm->nrows <= 0) | (rm->ncols <= 0)) { |
301 |
< |
if (!fscnresolu(&rm->ncols, &rm->nrows, fp)) |
302 |
< |
return(0); |
303 |
< |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
304 |
< |
rm->ncomp != 3) |
305 |
< |
return(0); |
302 |
< |
} |
303 |
< |
if (rm->dtype == DTascii) /* set input type (WINDOWS) */ |
299 |
> |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
300 |
> |
rm->ncomp != 3) |
301 |
> |
return(0); |
302 |
> |
if (rm->ncols <= 0 && /* resolution string? */ |
303 |
> |
!fscnresolu(&rm->ncols, &rm->nrows, fp)) |
304 |
> |
return(0); |
305 |
> |
if (rm->dtype == DTascii) /* set file type (WINDOWS) */ |
306 |
|
SET_FILE_TEXT(fp); |
307 |
|
else |
308 |
|
SET_FILE_BINARY(fp); |
338 |
|
int i; |
339 |
|
#ifdef MAP_FILE |
340 |
|
long pos; /* map memory for file > 1MB if possible */ |
341 |
< |
if ((rm->dtype == DTdouble) & !rm->swapin && array_size(rm) >= 1L<<20 && |
341 |
> |
if ((rm->dtype == DTdouble) & !(rm->pflags & RMF_SWAPIN) && |
342 |
> |
rmx_array_size(rm) >= 1L<<20 && |
343 |
|
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
344 |
< |
rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, |
344 |
> |
rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE, |
345 |
|
MAP_PRIVATE, fileno(fp), 0); |
346 |
|
if (rm->mapped != MAP_FAILED) { |
347 |
|
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
348 |
+ |
rm->pflags |= RMF_OURMEM; |
349 |
|
return(1); |
350 |
|
} /* else fall back on reading into memory */ |
351 |
|
rm->mapped = NULL; |
353 |
|
#endif |
354 |
|
if (!rmx_prepare(rm)) { /* need in-core matrix array */ |
355 |
|
fprintf(stderr, "Cannot allocate %g MByte matrix array\n", |
356 |
< |
(1./(1L<<20))*(double)array_size(rm)); |
356 |
> |
(1./(1L<<20))*(double)rmx_array_size(rm)); |
357 |
|
return(0); |
358 |
|
} |
359 |
|
for (i = 0; i < rm->nrows; i++) |
378 |
|
fp = stdin; |
379 |
|
else if (inspec[0] == '!') |
380 |
|
fp = popen(inspec+1, "r"); |
381 |
< |
else if (rmp != RMPnone) { |
381 |
> |
else { |
382 |
|
const char *sp = inspec; /* check suffix */ |
383 |
|
while (*sp) |
384 |
|
++sp; |
385 |
|
while (sp > inspec && sp[-1] != '.') |
386 |
|
--sp; |
387 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
388 |
< |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
388 |
> |
CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL : |
389 |
> |
rmp==RMPtrans ? cm_loadBTDF(inspec) : |
390 |
|
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
391 |
|
if (!cm) |
392 |
|
return(NULL); |
397 |
|
} /* else open it ourselves */ |
398 |
|
fp = fopen(inspec, "r"); |
399 |
|
} |
400 |
< |
if (!fp) |
400 |
> |
if (!fp) { |
401 |
> |
fprintf(stderr, "Cannot open for reading: %s\n", inspec); |
402 |
|
return(NULL); |
403 |
+ |
} |
404 |
|
#ifdef getc_unlocked |
405 |
|
flockfile(fp); |
406 |
|
#endif |
456 |
|
{ |
457 |
|
while (len-- > 0) { |
458 |
|
int k = nc; |
459 |
< |
while (nc-- > 0) |
459 |
> |
while (k-- > 0) |
460 |
|
fprintf(fp, " %.7e", *dp++); |
461 |
|
fputc('\t', fp); |
462 |
|
} |
505 |
|
if (nc < 3) return(0); |
506 |
|
scan = (uby8 *)tempbuffer((nc+1)*len); |
507 |
|
if (!scan) return(0); |
508 |
< |
for (j = len; j--; dp += nc) { |
508 |
> |
for (j = 0; j < len; j++, dp += nc) { |
509 |
|
for (k = nc; k--; ) |
510 |
|
scol[k] = dp[k]; |
511 |
|
scolor2scolr(scan+j*(nc+1), scol, nc); |
534 |
|
int |
535 |
|
rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp) |
536 |
|
{ |
537 |
< |
if (!rm | !fp || !rm->mtx) |
537 |
> |
if (!rm | !fp || rm->ncols <= 0) |
538 |
|
return(0); |
539 |
|
if (rm->info) |
540 |
|
fputs(rm->info, fp); |
545 |
|
dtype = DTxyze; |
546 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
547 |
|
dtype = DTrgbe; |
548 |
+ |
if ((dtype < DTspec) & (rm->ncomp > 3)) |
549 |
+ |
dtype = DTspec; |
550 |
+ |
else if ((dtype == DTspec) & (rm->ncomp <= 3)) |
551 |
+ |
return(0); |
552 |
|
|
553 |
< |
if (dtype == DTascii) /* set output type (WINDOWS) */ |
553 |
> |
if (dtype == DTascii) /* set file type (WINDOWS) */ |
554 |
|
SET_FILE_TEXT(fp); |
555 |
|
else |
556 |
|
SET_FILE_BINARY(fp); |
560 |
|
fputcolcor(rm->cexp, fp); |
561 |
|
else if (rm->cexp[GRN] != 1.f) |
562 |
|
fputexpos(rm->cexp[GRN], fp); |
563 |
< |
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
564 |
< |
if (dtype != DTspec) { |
563 |
> |
/* matrix size? */ |
564 |
> |
if ((dtype > DTspec) | (rm->nrows <= 0)) { |
565 |
> |
if (rm->nrows > 0) |
566 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
567 |
< |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
568 |
< |
} else if (rm->ncomp < 3) |
569 |
< |
return(0); /* bad # components */ |
567 |
> |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
568 |
> |
} |
569 |
> |
if (dtype >= DTspec) { /* # components & split? */ |
570 |
|
fputncomp(rm->ncomp, fp); |
571 |
< |
if (dtype == DTspec || (rm->ncomp > 3 && |
572 |
< |
memcmp(rm->wlpart, WLPART, sizeof(WLPART)))) |
571 |
> |
if (rm->ncomp > 3 && |
572 |
> |
memcmp(rm->wlpart, WLPART, sizeof(WLPART))) |
573 |
|
fputwlsplit(rm->wlpart, fp); |
574 |
|
} else if ((rm->ncomp != 3) & (rm->ncomp != 1)) |
575 |
|
return(0); /* wrong # components */ |
577 |
|
fputendian(fp); /* important to record */ |
578 |
|
fputformat(cm_fmt_id[dtype], fp); |
579 |
|
fputc('\n', fp); /* end of header */ |
580 |
< |
if (dtype <= DTspec) |
580 |
> |
if ((dtype <= DTspec) & (rm->nrows > 0)) |
581 |
|
fprtresolu(rm->ncols, rm->nrows, fp); |
582 |
|
return(dtype); |
583 |
|
} |
642 |
|
|
643 |
|
if (!rid) |
644 |
|
return(NULL); |
645 |
< |
memset(rid->mtx, 0, array_size(rid)); |
645 |
> |
memset(rid->mtx, 0, rmx_array_size(rid)); |
646 |
|
for (i = dim; i--; ) { |
647 |
|
double *dp = rmx_lval(rid,i,i); |
648 |
|
for (k = n; k--; ) |
651 |
|
return(rid); |
652 |
|
} |
653 |
|
|
654 |
< |
/* Duplicate the given matrix */ |
654 |
> |
/* Duplicate the given matrix (may be unallocated) */ |
655 |
|
RMATRIX * |
656 |
|
rmx_copy(const RMATRIX *rm) |
657 |
|
{ |
658 |
|
RMATRIX *dnew; |
659 |
|
|
660 |
< |
if (!rm || !rm->mtx) |
660 |
> |
if (!rm) |
661 |
|
return(NULL); |
662 |
< |
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
662 |
> |
dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp); |
663 |
|
if (!dnew) |
664 |
|
return(NULL); |
665 |
+ |
if (rm->mtx) { |
666 |
+ |
if (!rmx_prepare(dnew)) { |
667 |
+ |
rmx_free(dnew); |
668 |
+ |
return(NULL); |
669 |
+ |
} |
670 |
+ |
memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew)); |
671 |
+ |
} |
672 |
|
rmx_addinfo(dnew, rm->info); |
673 |
|
dnew->dtype = rm->dtype; |
674 |
|
copycolor(dnew->cexp, rm->cexp); |
675 |
|
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
657 |
– |
memcpy(dnew->mtx, rm->mtx, array_size(dnew)); |
676 |
|
return(dnew); |
677 |
+ |
} |
678 |
+ |
|
679 |
+ |
/* Replace data in first matrix with data from second */ |
680 |
+ |
int |
681 |
+ |
rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) |
682 |
+ |
{ |
683 |
+ |
if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | |
684 |
+ |
(rdst->ncols != rsrc->ncols) | |
685 |
+ |
(rdst->ncomp != rsrc->ncomp)) |
686 |
+ |
return(0); |
687 |
+ |
|
688 |
+ |
if (dometa) { /* transfer everything? */ |
689 |
+ |
rmx_reset(rdst); |
690 |
+ |
*rdst = *rsrc; |
691 |
+ |
rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; |
692 |
+ |
return(1); |
693 |
+ |
} |
694 |
+ |
if (rdst->pflags & RMF_OURMEM) { |
695 |
+ |
#ifdef MAP_FILE /* just matrix data -- leave metadata */ |
696 |
+ |
if (rdst->mapped) |
697 |
+ |
munmap(rdst->mapped, rmx_mapped_size(rdst)); |
698 |
+ |
else |
699 |
+ |
#endif |
700 |
+ |
if (rdst->mtx) |
701 |
+ |
free(rdst->mtx); |
702 |
+ |
} |
703 |
+ |
rdst->mapped = rsrc->mapped; |
704 |
+ |
rdst->mtx = rsrc->mtx; |
705 |
+ |
if (rsrc->pflags & RMF_OURMEM) |
706 |
+ |
rdst->pflags |= RMF_OURMEM; |
707 |
+ |
else |
708 |
+ |
rdst->pflags &= ~RMF_OURMEM; |
709 |
+ |
rsrc->mapped = NULL; rsrc->mtx = NULL; |
710 |
+ |
return(1); |
711 |
|
} |
712 |
|
|
713 |
|
/* Allocate and assign transposed matrix */ |