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)); |
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 |
|
} |
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 |
|
|
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) { /* reading from stdin? */ |
213 |
< |
inspec = "<stdin>"; |
214 |
< |
SET_FILE_BINARY(stdin); |
212 |
> |
if (!inspec || !*inspec) |
213 |
> |
return(NULL); |
214 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
215 |
> |
fp = stdin; |
216 |
|
} else if (inspec[0] == '!') { |
217 |
|
if (!(fp = popen(inspec+1, "r"))) |
218 |
|
return(NULL); |
195 |
– |
SET_FILE_BINARY(stdin); |
219 |
|
} else { |
220 |
|
const char *sp = inspec; /* check suffix */ |
221 |
|
while (*sp) |
223 |
|
while (sp > inspec && sp[-1] != '.') |
224 |
|
--sp; |
225 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
226 |
< |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
226 |
> |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
227 |
> |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
228 |
|
if (!cm) |
229 |
|
return(NULL); |
230 |
|
dnew = rmx_from_cmatrix(cm); |
233 |
|
return(dnew); |
234 |
|
} |
235 |
|
/* else open it ourselves */ |
236 |
< |
if (!(fp = fopen(inspec, "rb"))) |
236 |
> |
if (!(fp = fopen(inspec, "r"))) |
237 |
|
return(NULL); |
238 |
|
} |
239 |
+ |
SET_FILE_BINARY(fp); |
240 |
|
#ifdef getc_unlocked |
241 |
|
flockfile(fp); |
242 |
|
#endif |
243 |
|
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
244 |
|
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
245 |
+ |
dinfo.swapin = 0; |
246 |
|
dinfo.info = NULL; |
247 |
+ |
dinfo.mtx[0] = dinfo.mtx[1] = dinfo.mtx[2] = 1.; |
248 |
|
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
249 |
|
fclose(fp); |
250 |
|
return(NULL); |
270 |
|
dnew->info = dinfo.info; |
271 |
|
switch (dinfo.dtype) { |
272 |
|
case DTascii: |
273 |
< |
SET_FILE_TEXT(stdin); |
273 |
> |
SET_FILE_TEXT(fp); |
274 |
|
if (!rmx_load_ascii(dnew, fp)) |
275 |
|
goto loaderr; |
276 |
|
dnew->dtype = DTascii; /* should leave double? */ |
277 |
|
break; |
278 |
|
case DTfloat: |
279 |
+ |
dnew->swapin = dinfo.swapin; |
280 |
|
if (!rmx_load_float(dnew, fp)) |
281 |
|
goto loaderr; |
282 |
|
dnew->dtype = DTfloat; |
283 |
|
break; |
284 |
|
case DTdouble: |
285 |
+ |
dnew->swapin = dinfo.swapin; |
286 |
|
if (!rmx_load_double(dnew, fp)) |
287 |
|
goto loaderr; |
288 |
|
dnew->dtype = DTdouble; |
291 |
|
case DTxyze: |
292 |
|
if (!rmx_load_rgbe(dnew, fp)) |
293 |
|
goto loaderr; |
294 |
< |
dnew->dtype = dinfo.dtype; |
294 |
> |
dnew->dtype = dinfo.dtype; /* undo exposure? */ |
295 |
> |
if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) | |
296 |
> |
(dinfo.mtx[2] != 1.)) { |
297 |
> |
dinfo.mtx[0] = 1./dinfo.mtx[0]; |
298 |
> |
dinfo.mtx[1] = 1./dinfo.mtx[1]; |
299 |
> |
dinfo.mtx[2] = 1./dinfo.mtx[2]; |
300 |
> |
rmx_scale(dnew, dinfo.mtx); |
301 |
> |
} |
302 |
|
break; |
303 |
|
default: |
304 |
|
goto loaderr; |
431 |
|
return(0); |
432 |
|
rm = mydm; |
433 |
|
} |
434 |
+ |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
435 |
+ |
fputendian(fp); /* important to record */ |
436 |
|
fputformat((char *)cm_fmt_id[dtype], fp); |
437 |
|
fputc('\n', fp); |
438 |
|
switch (dtype) { /* write data */ |
606 |
|
rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k); |
607 |
|
} |
608 |
|
if (zeroDivides) { |
609 |
< |
fputs("Divide by zero in rmx_elemult()\n", stderr); |
609 |
> |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
610 |
|
errno = ERANGE; |
611 |
|
} |
612 |
|
return(1); |
678 |
|
return(NULL); |
679 |
|
if (msrc->info) { |
680 |
|
char buf[128]; |
681 |
< |
sprintf(buf, "Applied %dx%d matrix transform\n", |
681 |
> |
sprintf(buf, "Applied %dx%d component transform\n", |
682 |
|
dnew->ncomp, msrc->ncomp); |
683 |
|
rmx_addinfo(dnew, msrc->info); |
684 |
|
rmx_addinfo(dnew, buf); |