5 |
|
* General matrix operations. |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <stdio.h> |
8 |
|
#include <stdlib.h> |
10 |
– |
#include <string.h> |
11 |
– |
#include <fcntl.h> |
9 |
|
#include <errno.h> |
10 |
|
#include "rtio.h" |
11 |
|
#include "platform.h" |
24 |
|
if ((nr <= 0) | (nc <= 0) | (n <= 0)) |
25 |
|
return(NULL); |
26 |
|
dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) + |
27 |
< |
sizeof(dnew->mtx[0])*(n*nr*nc)); |
28 |
< |
if (dnew == NULL) |
27 |
> |
sizeof(dnew->mtx[0])*n*nr*nc); |
28 |
> |
if (!dnew) |
29 |
|
return(NULL); |
30 |
|
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
31 |
|
dnew->dtype = DTdouble; |
32 |
+ |
dnew->swapin = 0; |
33 |
|
dnew->info = NULL; |
34 |
|
return(dnew); |
35 |
|
} |
61 |
|
int |
62 |
|
rmx_addinfo(RMATRIX *rm, const char *info) |
63 |
|
{ |
64 |
< |
if (!info || !*info) |
64 |
> |
if (!rm || !info || !*info) |
65 |
|
return(0); |
66 |
|
if (!rm->info) { |
67 |
|
rm->info = (char *)malloc(strlen(info)+1); |
96 |
|
ip->ncols = atoi(s+6); |
97 |
|
return(0); |
98 |
|
} |
99 |
+ |
if ((i = isbigendian(s)) >= 0) { |
100 |
+ |
ip->swapin = (nativebigendian() != i); |
101 |
+ |
return(0); |
102 |
+ |
} |
103 |
+ |
if (isexpos(s)) { |
104 |
+ |
double d = exposval(s); |
105 |
+ |
scalecolor(ip->mtx, d); |
106 |
+ |
return(0); |
107 |
+ |
} |
108 |
+ |
if (iscolcor(s)) { |
109 |
+ |
COLOR ctmp; |
110 |
+ |
colcorval(ctmp, s); |
111 |
+ |
multcolor(ip->mtx, ctmp); |
112 |
+ |
return(0); |
113 |
+ |
} |
114 |
|
if (!formatval(fmt, s)) { |
115 |
|
rmx_addinfo(ip, s); |
116 |
|
return(0); |
150 |
|
for (j = 0; j < rm->ncols; j++) { |
151 |
|
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
152 |
|
return(0); |
153 |
+ |
if (rm->swapin) |
154 |
+ |
swap32((char *)val, rm->ncomp); |
155 |
|
for (k = rm->ncomp; k--; ) |
156 |
|
rmx_lval(rm,i,j,k) = val[k]; |
157 |
|
} |
161 |
|
static int |
162 |
|
rmx_load_double(RMATRIX *rm, FILE *fp) |
163 |
|
{ |
164 |
< |
int i, j; |
164 |
> |
int i; |
165 |
|
|
166 |
< |
for (i = 0; i < rm->nrows; i++) |
167 |
< |
for (j = 0; j < rm->ncols; j++) |
168 |
< |
if (getbinary(&rmx_lval(rm,i,j,0), sizeof(double), rm->ncomp, fp) != rm->ncomp) |
169 |
< |
return(0); |
166 |
> |
if ((char *)&rmx_lval(rm,1,0,0) - (char *)&rmx_lval(rm,0,0,0) != |
167 |
> |
sizeof(double)*rm->ncols*rm->ncomp) { |
168 |
> |
fputs("Code error in rmx_load_double()\n", stderr); |
169 |
> |
exit(1); |
170 |
> |
} |
171 |
> |
for (i = 0; i < rm->nrows; i++) { |
172 |
> |
if (getbinary(&rmx_lval(rm,i,0,0), sizeof(double)*rm->ncomp, |
173 |
> |
rm->ncols, fp) != rm->ncols) |
174 |
> |
return(0); |
175 |
> |
if (rm->swapin) |
176 |
> |
swap64((char *)&rmx_lval(rm,i,0,0), rm->ncols*rm->ncomp); |
177 |
> |
} |
178 |
|
return(1); |
179 |
|
} |
180 |
|
|
184 |
|
COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols); |
185 |
|
int i, j; |
186 |
|
|
187 |
< |
if (scan == NULL) |
187 |
> |
if (!scan) |
188 |
|
return(0); |
189 |
|
for (i = 0; i < rm->nrows; i++) { |
190 |
|
if (freadscan(scan, rm->ncols, fp) < 0) { |
209 |
|
RMATRIX dinfo; |
210 |
|
RMATRIX *dnew; |
211 |
|
|
212 |
< |
if (inspec == NULL) { /* reading from stdin? */ |
212 |
> |
if (!inspec) { /* reading from stdin? */ |
213 |
|
inspec = "<stdin>"; |
214 |
|
SET_FILE_BINARY(stdin); |
215 |
|
} else if (inspec[0] == '!') { |
216 |
< |
if ((fp = popen(inspec+1, "r")) == NULL) |
216 |
> |
if (!(fp = popen(inspec+1, "r"))) |
217 |
|
return(NULL); |
218 |
< |
SET_FILE_BINARY(stdin); |
218 |
> |
SET_FILE_BINARY(fp); |
219 |
|
} else { |
220 |
|
const char *sp = inspec; /* check suffix */ |
221 |
|
while (*sp) |
224 |
|
--sp; |
225 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
226 |
|
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
227 |
< |
if (cm == NULL) |
227 |
> |
if (!cm) |
228 |
|
return(NULL); |
229 |
|
dnew = rmx_from_cmatrix(cm); |
230 |
|
cm_free(cm); |
232 |
|
return(dnew); |
233 |
|
} |
234 |
|
/* else open it ourselves */ |
235 |
< |
if ((fp = fopen(inspec, "rb")) == NULL) |
235 |
> |
if (!(fp = fopen(inspec, "rb"))) |
236 |
|
return(NULL); |
237 |
|
} |
238 |
|
#ifdef getc_unlocked |
240 |
|
#endif |
241 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
242 |
|
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
243 |
+ |
dinfo.swapin = 0; |
244 |
|
dinfo.info = NULL; |
245 |
+ |
dinfo.mtx[0] = dinfo.mtx[1] = dinfo.mtx[2] = 1.; |
246 |
|
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
247 |
|
fclose(fp); |
248 |
|
return(NULL); |
261 |
|
} |
262 |
|
} |
263 |
|
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
264 |
< |
if (dnew == NULL) { |
264 |
> |
if (!dnew) { |
265 |
|
fclose(fp); |
266 |
|
return(NULL); |
267 |
|
} |
268 |
|
dnew->info = dinfo.info; |
269 |
|
switch (dinfo.dtype) { |
270 |
|
case DTascii: |
271 |
< |
SET_FILE_TEXT(stdin); |
271 |
> |
SET_FILE_TEXT(fp); |
272 |
|
if (!rmx_load_ascii(dnew, fp)) |
273 |
|
goto loaderr; |
274 |
|
dnew->dtype = DTascii; /* should leave double? */ |
275 |
|
break; |
276 |
|
case DTfloat: |
277 |
+ |
dnew->swapin = dinfo.swapin; |
278 |
|
if (!rmx_load_float(dnew, fp)) |
279 |
|
goto loaderr; |
280 |
|
dnew->dtype = DTfloat; |
281 |
|
break; |
282 |
|
case DTdouble: |
283 |
+ |
dnew->swapin = dinfo.swapin; |
284 |
|
if (!rmx_load_double(dnew, fp)) |
285 |
|
goto loaderr; |
286 |
|
dnew->dtype = DTdouble; |
289 |
|
case DTxyze: |
290 |
|
if (!rmx_load_rgbe(dnew, fp)) |
291 |
|
goto loaderr; |
292 |
< |
dnew->dtype = dinfo.dtype; |
292 |
> |
dnew->dtype = dinfo.dtype; /* undo exposure? */ |
293 |
> |
if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) | |
294 |
> |
(dinfo.mtx[2] != 1.)) { |
295 |
> |
dinfo.mtx[0] = 1./dinfo.mtx[0]; |
296 |
> |
dinfo.mtx[1] = 1./dinfo.mtx[1]; |
297 |
> |
dinfo.mtx[2] = 1./dinfo.mtx[2]; |
298 |
> |
rmx_scale(dnew, dinfo.mtx); |
299 |
> |
} |
300 |
|
break; |
301 |
|
default: |
302 |
|
goto loaderr; |
378 |
|
COLR *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols); |
379 |
|
int i, j; |
380 |
|
|
381 |
< |
if (scan == NULL) |
381 |
> |
if (!scan) |
382 |
|
return(0); |
383 |
|
for (i = 0; i < rm->nrows; i++) { |
384 |
|
for (j = rm->ncols; j--; ) |
401 |
|
RMATRIX *mydm = NULL; |
402 |
|
int ok = 1; |
403 |
|
|
404 |
< |
if ((rm == NULL) | (fp == NULL)) |
404 |
> |
if (!rm | !fp) |
405 |
|
return(0); |
406 |
|
#ifdef getc_unlocked |
407 |
|
flockfile(fp); |
425 |
|
return(0); |
426 |
|
cmtx[0] = cmtx[1] = cmtx[2] = 1; |
427 |
|
mydm = rmx_transform(rm, 3, cmtx); |
428 |
< |
if (mydm == NULL) |
428 |
> |
if (!mydm) |
429 |
|
return(0); |
430 |
|
rm = mydm; |
431 |
|
} |
432 |
+ |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
433 |
+ |
fputendian(fp); /* important to record */ |
434 |
|
fputformat((char *)cm_fmt_id[dtype], fp); |
435 |
|
fputc('\n', fp); |
436 |
|
switch (dtype) { /* write data */ |
455 |
|
#ifdef getc_unlocked |
456 |
|
funlockfile(fp); |
457 |
|
#endif |
458 |
< |
rmx_free(mydm); |
458 |
> |
if (mydm) |
459 |
> |
rmx_free(mydm); |
460 |
|
return(ok); |
461 |
|
} |
462 |
|
|
467 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
468 |
|
int i, k; |
469 |
|
|
470 |
< |
if (rid == NULL) |
470 |
> |
if (!rid) |
471 |
|
return(NULL); |
472 |
|
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
473 |
|
for (i = dim; i--; ) |
482 |
|
{ |
483 |
|
RMATRIX *dnew; |
484 |
|
|
485 |
< |
if (rm == NULL) |
485 |
> |
if (!rm) |
486 |
|
return(NULL); |
487 |
|
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
488 |
< |
if (dnew == NULL) |
488 |
> |
if (!dnew) |
489 |
|
return(NULL); |
490 |
|
rmx_addinfo(dnew, rm->info); |
491 |
|
dnew->dtype = rm->dtype; |
501 |
|
RMATRIX *dnew; |
502 |
|
int i, j, k; |
503 |
|
|
504 |
< |
if (rm == NULL) |
504 |
> |
if (!rm) |
505 |
|
return(0); |
506 |
|
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
507 |
|
dnew = rmx_copy(rm); |
508 |
< |
if (dnew == NULL) |
508 |
> |
if (!dnew) |
509 |
|
return(NULL); |
510 |
|
dnew->nrows = rm->ncols; |
511 |
|
dnew->ncols = rm->nrows; |
512 |
|
return(dnew); |
513 |
|
} |
514 |
|
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
515 |
< |
if (dnew == NULL) |
515 |
> |
if (!dnew) |
516 |
|
return(NULL); |
517 |
|
if (rm->info) { |
518 |
|
rmx_addinfo(dnew, rm->info); |
533 |
|
RMATRIX *mres; |
534 |
|
int i, j, k, h; |
535 |
|
|
536 |
< |
if ((m1 == NULL) | (m2 == NULL) || |
500 |
< |
(m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
536 |
> |
if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
537 |
|
return(NULL); |
538 |
|
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
539 |
< |
if (mres == NULL) |
539 |
> |
if (!mres) |
540 |
|
return(NULL); |
541 |
|
i = rmx_newtype(m1->dtype, m2->dtype); |
542 |
|
if (i) |
561 |
|
int zeroDivides = 0; |
562 |
|
int i, j, k; |
563 |
|
|
564 |
< |
if ((m1 == NULL) | (m2 == NULL) || |
529 |
< |
(m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
564 |
> |
if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
565 |
|
return(0); |
566 |
|
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp)) |
567 |
|
return(0); |
604 |
|
rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k); |
605 |
|
} |
606 |
|
if (zeroDivides) { |
607 |
< |
fputs("Divide by zero in rmx_elemult()\n", stderr); |
607 |
> |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
608 |
|
errno = ERANGE; |
609 |
|
} |
610 |
|
return(1); |
617 |
|
double *mysf = NULL; |
618 |
|
int i, j, k; |
619 |
|
|
620 |
< |
if ((msum == NULL) | (madd == NULL) || |
620 |
> |
if (!msum | !madd || |
621 |
|
(msum->nrows != madd->nrows) | |
622 |
|
(msum->ncols != madd->ncols) | |
623 |
|
(msum->ncomp != madd->ncomp)) |
624 |
|
return(0); |
625 |
< |
if (sf == NULL) { |
625 |
> |
if (!sf) { |
626 |
|
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
627 |
< |
if (mysf == NULL) |
627 |
> |
if (!mysf) |
628 |
|
return(0); |
629 |
|
for (k = msum->ncomp; k--; ) |
630 |
|
mysf[k] = 1; |
639 |
|
for (j = msum->ncols; j--; ) |
640 |
|
for (k = msum->ncomp; k--; ) |
641 |
|
rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k); |
642 |
< |
|
643 |
< |
free(mysf); |
642 |
> |
if (mysf) |
643 |
> |
free(mysf); |
644 |
|
return(1); |
645 |
|
} |
646 |
|
|
650 |
|
{ |
651 |
|
int i, j, k; |
652 |
|
|
653 |
< |
if ((rm == NULL) | (sf == NULL)) |
653 |
> |
if (!rm | !sf) |
654 |
|
return(0); |
655 |
|
for (i = rm->nrows; i--; ) |
656 |
|
for (j = rm->ncols; j--; ) |
669 |
|
int i, j, ks, kd; |
670 |
|
RMATRIX *dnew; |
671 |
|
|
672 |
< |
if ((msrc == NULL) | (n <= 0) | (cmat == NULL)) |
672 |
> |
if (!msrc | (n <= 0) | !cmat) |
673 |
|
return(NULL); |
674 |
|
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
675 |
< |
if (dnew == NULL) |
675 |
> |
if (!dnew) |
676 |
|
return(NULL); |
677 |
|
if (msrc->info) { |
678 |
|
char buf[128]; |
679 |
< |
sprintf(buf, "Applied %dx%d matrix transform\n", |
679 |
> |
sprintf(buf, "Applied %dx%d component transform\n", |
680 |
|
dnew->ncomp, msrc->ncomp); |
681 |
|
rmx_addinfo(dnew, msrc->info); |
682 |
|
rmx_addinfo(dnew, buf); |
700 |
|
int i, j; |
701 |
|
RMATRIX *dnew; |
702 |
|
|
703 |
< |
if (cm == NULL) |
703 |
> |
if (!cm) |
704 |
|
return(NULL); |
705 |
|
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
706 |
< |
if (dnew == NULL) |
706 |
> |
if (!dnew) |
707 |
|
return(NULL); |
708 |
|
dnew->dtype = DTfloat; |
709 |
|
for (i = dnew->nrows; i--; ) |
723 |
|
int i, j; |
724 |
|
CMATRIX *cnew; |
725 |
|
|
726 |
< |
if (rm == NULL || rm->ncomp != 3) |
726 |
> |
if (!rm || rm->ncomp != 3) |
727 |
|
return(NULL); |
728 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
729 |
< |
if (cnew == NULL) |
729 |
> |
if (!cnew) |
730 |
|
return(NULL); |
731 |
|
for (i = cnew->nrows; i--; ) |
732 |
|
for (j = cnew->ncols; j--; ) { |