| 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; |
| 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); |
| 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,j,0), rm->ncomp); |
| 177 |
< |
} |
| 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) |
| 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"))) |
| 220 |
|
return(NULL); |
| 205 |
– |
SET_FILE_BINARY(fp); |
| 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); |
| 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); |
| 235 |
|
return(dnew); |
| 236 |
|
} |
| 237 |
|
/* else open it ourselves */ |
| 238 |
< |
if (!(fp = fopen(inspec, "rb"))) |
| 238 |
> |
if (!(fp = fopen(inspec, "r"))) |
| 239 |
|
return(NULL); |
| 240 |
|
} |
| 241 |
+ |
SET_FILE_BINARY(fp); |
| 242 |
|
#ifdef getc_unlocked |
| 243 |
|
flockfile(fp); |
| 244 |
|
#endif |
| 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); |
| 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; |
| 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) |
| 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; |
| 453 |
|
} |
| 454 |
|
if ((dtype == DTfloat) | (dtype == DTdouble)) |
| 455 |
|
fputendian(fp); /* important to record */ |
| 456 |
< |
fputformat((char *)cm_fmt_id[dtype], fp); |
| 456 |
> |
fputformat(cm_fmt_id[dtype], fp); |
| 457 |
|
fputc('\n', fp); |
| 458 |
|
switch (dtype) { /* write data */ |
| 459 |
|
case DTascii: |