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); |
79 |
|
get_dminfo(char *s, void *p) |
80 |
|
{ |
81 |
|
RMATRIX *ip = (RMATRIX *)p; |
82 |
< |
char fmt[64]; |
82 |
> |
char fmt[MAXFMTLEN]; |
83 |
|
int i; |
84 |
|
|
85 |
|
if (headidval(fmt, s)) |
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) { |
203 |
|
|
204 |
|
/* Load matrix from supported file type */ |
205 |
|
RMATRIX * |
206 |
< |
rmx_load(const char *inspec) |
206 |
> |
rmx_load(const char *inspec, RMPref rmp) |
207 |
|
{ |
208 |
< |
FILE *fp = stdin; |
208 |
> |
FILE *fp; |
209 |
|
RMATRIX dinfo; |
210 |
|
RMATRIX *dnew; |
211 |
|
|
212 |
< |
if (inspec == NULL) { /* reading from stdin? */ |
213 |
< |
inspec = "<stdin>"; |
214 |
< |
SET_FILE_BINARY(stdin); |
212 |
> |
if (!inspec) |
213 |
> |
inspec = stdin_name; |
214 |
> |
else if (!*inspec) |
215 |
> |
return(NULL); |
216 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
217 |
> |
fp = stdin; |
218 |
|
} else if (inspec[0] == '!') { |
219 |
< |
if ((fp = popen(inspec+1, "r")) == NULL) |
219 |
> |
if (!(fp = popen(inspec+1, "r"))) |
220 |
|
return(NULL); |
195 |
– |
SET_FILE_BINARY(stdin); |
221 |
|
} else { |
222 |
|
const char *sp = inspec; /* check suffix */ |
223 |
|
while (*sp) |
225 |
|
while (sp > inspec && sp[-1] != '.') |
226 |
|
--sp; |
227 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
228 |
< |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
229 |
< |
if (cm == NULL) |
228 |
> |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
229 |
> |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
230 |
> |
if (!cm) |
231 |
|
return(NULL); |
232 |
|
dnew = rmx_from_cmatrix(cm); |
233 |
|
cm_free(cm); |
235 |
|
return(dnew); |
236 |
|
} |
237 |
|
/* else open it ourselves */ |
238 |
< |
if ((fp = fopen(inspec, "rb")) == NULL) |
238 |
> |
if (!(fp = fopen(inspec, "r"))) |
239 |
|
return(NULL); |
240 |
|
} |
241 |
+ |
SET_FILE_BINARY(fp); |
242 |
|
#ifdef getc_unlocked |
243 |
|
flockfile(fp); |
244 |
|
#endif |
245 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
246 |
|
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
247 |
+ |
dinfo.swapin = 0; |
248 |
|
dinfo.info = NULL; |
249 |
+ |
dinfo.mtx[0] = dinfo.mtx[1] = dinfo.mtx[2] = 1.; |
250 |
|
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
251 |
|
fclose(fp); |
252 |
|
return(NULL); |
265 |
|
} |
266 |
|
} |
267 |
|
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
268 |
< |
if (dnew == NULL) { |
268 |
> |
if (!dnew) { |
269 |
|
fclose(fp); |
270 |
|
return(NULL); |
271 |
|
} |
272 |
|
dnew->info = dinfo.info; |
273 |
|
switch (dinfo.dtype) { |
274 |
|
case DTascii: |
275 |
< |
SET_FILE_TEXT(stdin); |
275 |
> |
SET_FILE_TEXT(fp); |
276 |
|
if (!rmx_load_ascii(dnew, fp)) |
277 |
|
goto loaderr; |
278 |
|
dnew->dtype = DTascii; /* should leave double? */ |
279 |
|
break; |
280 |
|
case DTfloat: |
281 |
+ |
dnew->swapin = dinfo.swapin; |
282 |
|
if (!rmx_load_float(dnew, fp)) |
283 |
|
goto loaderr; |
284 |
|
dnew->dtype = DTfloat; |
285 |
|
break; |
286 |
|
case DTdouble: |
287 |
+ |
dnew->swapin = dinfo.swapin; |
288 |
|
if (!rmx_load_double(dnew, fp)) |
289 |
|
goto loaderr; |
290 |
|
dnew->dtype = DTdouble; |
293 |
|
case DTxyze: |
294 |
|
if (!rmx_load_rgbe(dnew, fp)) |
295 |
|
goto loaderr; |
296 |
< |
dnew->dtype = dinfo.dtype; |
296 |
> |
dnew->dtype = dinfo.dtype; /* undo exposure? */ |
297 |
> |
if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) | |
298 |
> |
(dinfo.mtx[2] != 1.)) { |
299 |
> |
dinfo.mtx[0] = 1./dinfo.mtx[0]; |
300 |
> |
dinfo.mtx[1] = 1./dinfo.mtx[1]; |
301 |
> |
dinfo.mtx[2] = 1./dinfo.mtx[2]; |
302 |
> |
rmx_scale(dnew, dinfo.mtx); |
303 |
> |
} |
304 |
|
break; |
305 |
|
default: |
306 |
|
goto loaderr; |
328 |
|
static int |
329 |
|
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
330 |
|
{ |
331 |
+ |
const char *fmt = (rm->dtype == DTfloat) ? " %.7e" : |
332 |
+ |
(rm->dtype == DTrgbe) | (rm->dtype == DTxyze) ? " %.3e" : |
333 |
+ |
" %.15e" ; |
334 |
|
int i, j, k; |
335 |
|
|
336 |
|
for (i = 0; i < rm->nrows; i++) { |
337 |
|
for (j = 0; j < rm->ncols; j++) { |
338 |
|
for (k = 0; k < rm->ncomp; k++) |
339 |
< |
fprintf(fp, " %.15e", rmx_lval(rm,i,j,k)); |
339 |
> |
fprintf(fp, fmt, rmx_lval(rm,i,j,k)); |
340 |
|
fputc('\t', fp); |
341 |
|
} |
342 |
|
fputc('\n', fp); |
382 |
|
COLR *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols); |
383 |
|
int i, j; |
384 |
|
|
385 |
< |
if (scan == NULL) |
385 |
> |
if (!scan) |
386 |
|
return(0); |
387 |
|
for (i = 0; i < rm->nrows; i++) { |
388 |
|
for (j = rm->ncols; j--; ) |
398 |
|
return(1); |
399 |
|
} |
400 |
|
|
401 |
+ |
/* Check if CIE XYZ primaries were specified */ |
402 |
+ |
static int |
403 |
+ |
findCIEprims(const char *info) |
404 |
+ |
{ |
405 |
+ |
RGBPRIMS prims; |
406 |
+ |
|
407 |
+ |
if (!info) |
408 |
+ |
return(0); |
409 |
+ |
info = strstr(info, PRIMARYSTR); |
410 |
+ |
if (!info || !primsval(prims, info)) |
411 |
+ |
return(0); |
412 |
+ |
|
413 |
+ |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
414 |
+ |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
415 |
+ |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
416 |
+ |
} |
417 |
+ |
|
418 |
|
/* Write matrix to file type indicated by dtype */ |
419 |
|
int |
420 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
422 |
|
RMATRIX *mydm = NULL; |
423 |
|
int ok = 1; |
424 |
|
|
425 |
< |
if ((rm == NULL) | (fp == NULL)) |
425 |
> |
if (!rm | !fp) |
426 |
|
return(0); |
427 |
|
#ifdef getc_unlocked |
428 |
|
flockfile(fp); |
432 |
|
fputs(rm->info, fp); |
433 |
|
if (dtype == DTfromHeader) |
434 |
|
dtype = rm->dtype; |
435 |
< |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
435 |
> |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
436 |
> |
findCIEprims(rm->info))) |
437 |
|
dtype = DTxyze; |
438 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
439 |
|
dtype = DTrgbe; |
447 |
|
return(0); |
448 |
|
cmtx[0] = cmtx[1] = cmtx[2] = 1; |
449 |
|
mydm = rmx_transform(rm, 3, cmtx); |
450 |
< |
if (mydm == NULL) |
450 |
> |
if (!mydm) |
451 |
|
return(0); |
452 |
|
rm = mydm; |
453 |
|
} |
454 |
< |
fputformat((char *)cm_fmt_id[dtype], fp); |
454 |
> |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
455 |
> |
fputendian(fp); /* important to record */ |
456 |
> |
fputformat(cm_fmt_id[dtype], fp); |
457 |
|
fputc('\n', fp); |
458 |
|
switch (dtype) { /* write data */ |
459 |
|
case DTascii: |
477 |
|
#ifdef getc_unlocked |
478 |
|
funlockfile(fp); |
479 |
|
#endif |
480 |
< |
rmx_free(mydm); |
480 |
> |
if (mydm) |
481 |
> |
rmx_free(mydm); |
482 |
|
return(ok); |
483 |
|
} |
484 |
|
|
489 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
490 |
|
int i, k; |
491 |
|
|
492 |
< |
if (rid == NULL) |
492 |
> |
if (!rid) |
493 |
|
return(NULL); |
494 |
|
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
495 |
|
for (i = dim; i--; ) |
504 |
|
{ |
505 |
|
RMATRIX *dnew; |
506 |
|
|
507 |
< |
if (rm == NULL) |
507 |
> |
if (!rm) |
508 |
|
return(NULL); |
509 |
|
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
510 |
< |
if (dnew == NULL) |
510 |
> |
if (!dnew) |
511 |
|
return(NULL); |
512 |
|
rmx_addinfo(dnew, rm->info); |
513 |
|
dnew->dtype = rm->dtype; |
523 |
|
RMATRIX *dnew; |
524 |
|
int i, j, k; |
525 |
|
|
526 |
< |
if (rm == NULL) |
526 |
> |
if (!rm) |
527 |
|
return(0); |
528 |
+ |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
529 |
+ |
dnew = rmx_copy(rm); |
530 |
+ |
if (!dnew) |
531 |
+ |
return(NULL); |
532 |
+ |
dnew->nrows = rm->ncols; |
533 |
+ |
dnew->ncols = rm->nrows; |
534 |
+ |
return(dnew); |
535 |
+ |
} |
536 |
|
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
537 |
< |
if (dnew == NULL) |
537 |
> |
if (!dnew) |
538 |
|
return(NULL); |
539 |
|
if (rm->info) { |
540 |
|
rmx_addinfo(dnew, rm->info); |
555 |
|
RMATRIX *mres; |
556 |
|
int i, j, k, h; |
557 |
|
|
558 |
< |
if ((m1 == NULL) | (m2 == NULL) || |
489 |
< |
(m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
558 |
> |
if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
559 |
|
return(NULL); |
560 |
|
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
561 |
< |
if (mres == NULL) |
561 |
> |
if (!mres) |
562 |
|
return(NULL); |
563 |
|
i = rmx_newtype(m1->dtype, m2->dtype); |
564 |
|
if (i) |
583 |
|
int zeroDivides = 0; |
584 |
|
int i, j, k; |
585 |
|
|
586 |
< |
if ((m1 == NULL) | (m2 == NULL) || |
518 |
< |
(m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
586 |
> |
if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
587 |
|
return(0); |
588 |
|
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp)) |
589 |
|
return(0); |
626 |
|
rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k); |
627 |
|
} |
628 |
|
if (zeroDivides) { |
629 |
< |
fputs("Divide by zero in rmx_elemult()\n", stderr); |
629 |
> |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
630 |
|
errno = ERANGE; |
631 |
|
} |
632 |
|
return(1); |
639 |
|
double *mysf = NULL; |
640 |
|
int i, j, k; |
641 |
|
|
642 |
< |
if ((msum == NULL) | (madd == NULL) || |
642 |
> |
if (!msum | !madd || |
643 |
|
(msum->nrows != madd->nrows) | |
644 |
|
(msum->ncols != madd->ncols) | |
645 |
|
(msum->ncomp != madd->ncomp)) |
646 |
|
return(0); |
647 |
< |
if (sf == NULL) { |
647 |
> |
if (!sf) { |
648 |
|
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
649 |
< |
if (mysf == NULL) |
649 |
> |
if (!mysf) |
650 |
|
return(0); |
651 |
|
for (k = msum->ncomp; k--; ) |
652 |
|
mysf[k] = 1; |
661 |
|
for (j = msum->ncols; j--; ) |
662 |
|
for (k = msum->ncomp; k--; ) |
663 |
|
rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k); |
664 |
< |
|
665 |
< |
free(mysf); |
664 |
> |
if (mysf) |
665 |
> |
free(mysf); |
666 |
|
return(1); |
667 |
|
} |
668 |
|
|
672 |
|
{ |
673 |
|
int i, j, k; |
674 |
|
|
675 |
< |
if ((rm == NULL) | (sf == NULL)) |
675 |
> |
if (!rm | !sf) |
676 |
|
return(0); |
677 |
|
for (i = rm->nrows; i--; ) |
678 |
|
for (j = rm->ncols; j--; ) |
691 |
|
int i, j, ks, kd; |
692 |
|
RMATRIX *dnew; |
693 |
|
|
694 |
< |
if ((msrc == NULL) | (n <= 0) | (cmat == NULL)) |
694 |
> |
if (!msrc | (n <= 0) | !cmat) |
695 |
|
return(NULL); |
696 |
|
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
697 |
< |
if (dnew == NULL) |
697 |
> |
if (!dnew) |
698 |
|
return(NULL); |
699 |
|
if (msrc->info) { |
700 |
|
char buf[128]; |
701 |
< |
sprintf(buf, "Applied %dx%d matrix transform\n", |
701 |
> |
sprintf(buf, "Applied %dx%d component transform\n", |
702 |
|
dnew->ncomp, msrc->ncomp); |
703 |
|
rmx_addinfo(dnew, msrc->info); |
704 |
|
rmx_addinfo(dnew, buf); |
722 |
|
int i, j; |
723 |
|
RMATRIX *dnew; |
724 |
|
|
725 |
< |
if (cm == NULL) |
725 |
> |
if (!cm) |
726 |
|
return(NULL); |
727 |
|
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
728 |
< |
if (dnew == NULL) |
728 |
> |
if (!dnew) |
729 |
|
return(NULL); |
730 |
|
dnew->dtype = DTfloat; |
731 |
|
for (i = dnew->nrows; i--; ) |
745 |
|
int i, j; |
746 |
|
CMATRIX *cnew; |
747 |
|
|
748 |
< |
if (rm == NULL || rm->ncomp != 3) |
748 |
> |
if (!rm || rm->ncomp != 3) |
749 |
|
return(NULL); |
750 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
751 |
< |
if (cnew == NULL) |
751 |
> |
if (!cnew) |
752 |
|
return(NULL); |
753 |
|
for (i = cnew->nrows; i--; ) |
754 |
|
for (j = cnew->ncols; j--; ) { |