| 12 |
|
#include "resolu.h" |
| 13 |
|
#include "paths.h" |
| 14 |
|
#include "rmatrix.h" |
| 15 |
+ |
#if !defined(_WIN32) && !defined(_WIN64) |
| 16 |
+ |
#include <sys/mman.h> |
| 17 |
+ |
#endif |
| 18 |
|
|
| 19 |
|
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
| 20 |
|
|
| 21 |
< |
/* Allocate a nr x nc matrix with n components */ |
| 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 |
> |
|
| 24 |
> |
/* Initialize a RMATRIX struct but don't allocate array space */ |
| 25 |
|
RMATRIX * |
| 26 |
+ |
rmx_new(int nr, int nc, int n) |
| 27 |
+ |
{ |
| 28 |
+ |
RMATRIX *dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
| 29 |
+ |
|
| 30 |
+ |
if (dnew) { |
| 31 |
+ |
dnew->dtype = DTdouble; |
| 32 |
+ |
dnew->nrows = nr; |
| 33 |
+ |
dnew->ncols = nc; |
| 34 |
+ |
dnew->ncomp = n; |
| 35 |
+ |
} |
| 36 |
+ |
return(dnew); |
| 37 |
+ |
} |
| 38 |
+ |
|
| 39 |
+ |
/* Prepare a RMATRIX for writing (allocate array if needed) */ |
| 40 |
+ |
int |
| 41 |
+ |
rmx_prepare(RMATRIX *rm) |
| 42 |
+ |
{ |
| 43 |
+ |
if (!rm) return(0); |
| 44 |
+ |
if (rm->mtx) |
| 45 |
+ |
return(1); |
| 46 |
+ |
rm->mtx = (double *)malloc(array_size(rm)); |
| 47 |
+ |
return(rm->mtx != NULL); |
| 48 |
+ |
} |
| 49 |
+ |
|
| 50 |
+ |
/* Call rmx_new() and rmx_prepare() */ |
| 51 |
+ |
RMATRIX * |
| 52 |
|
rmx_alloc(int nr, int nc, int n) |
| 53 |
|
{ |
| 54 |
< |
RMATRIX *dnew; |
| 54 |
> |
RMATRIX *dnew = rmx_new(nr, nc, n); |
| 55 |
|
|
| 56 |
< |
if ((nr <= 0) | (nc <= 0) | (n <= 0)) |
| 57 |
< |
return(NULL); |
| 58 |
< |
dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) + |
| 59 |
< |
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; |
| 56 |
> |
if (dnew && !rmx_prepare(dnew)) { |
| 57 |
> |
rmx_free(dnew); |
| 58 |
> |
dnew = NULL; |
| 59 |
> |
} |
| 60 |
|
return(dnew); |
| 61 |
|
} |
| 62 |
|
|
| 67 |
|
if (!rm) return; |
| 68 |
|
if (rm->info) |
| 69 |
|
free(rm->info); |
| 70 |
+ |
#ifdef MAP_FILE |
| 71 |
+ |
if (rm->mapped) |
| 72 |
+ |
munmap(rm->mapped, mapped_size(rm)); |
| 73 |
+ |
else |
| 74 |
+ |
#endif |
| 75 |
+ |
free(rm->mtx); |
| 76 |
|
free(rm); |
| 77 |
|
} |
| 78 |
|
|
| 93 |
|
int |
| 94 |
|
rmx_addinfo(RMATRIX *rm, const char *info) |
| 95 |
|
{ |
| 96 |
+ |
int oldlen = 0; |
| 97 |
+ |
|
| 98 |
|
if (!rm || !info || !*info) |
| 99 |
|
return(0); |
| 100 |
|
if (!rm->info) { |
| 101 |
|
rm->info = (char *)malloc(strlen(info)+1); |
| 102 |
|
if (rm->info) rm->info[0] = '\0'; |
| 103 |
< |
} else |
| 103 |
> |
} else { |
| 104 |
> |
oldlen = strlen(rm->info); |
| 105 |
|
rm->info = (char *)realloc(rm->info, |
| 106 |
< |
strlen(rm->info)+strlen(info)+1); |
| 106 |
> |
oldlen+strlen(info)+1); |
| 107 |
> |
} |
| 108 |
|
if (!rm->info) |
| 109 |
|
return(0); |
| 110 |
< |
strcat(rm->info, info); |
| 110 |
> |
strcpy(rm->info+oldlen, info); |
| 111 |
|
return(1); |
| 112 |
|
} |
| 113 |
|
|
| 138 |
|
} |
| 139 |
|
if (isexpos(s)) { |
| 140 |
|
double d = exposval(s); |
| 141 |
< |
scalecolor(ip->mtx, d); |
| 141 |
> |
scalecolor(ip->cexp, d); |
| 142 |
|
return(0); |
| 143 |
|
} |
| 144 |
|
if (iscolcor(s)) { |
| 145 |
|
COLOR ctmp; |
| 146 |
|
colcorval(ctmp, s); |
| 147 |
< |
multcolor(ip->mtx, ctmp); |
| 147 |
> |
multcolor(ip->cexp, ctmp); |
| 148 |
|
return(0); |
| 149 |
|
} |
| 150 |
|
if (!formatval(fmt, s)) { |
| 151 |
|
rmx_addinfo(ip, s); |
| 152 |
|
return(0); |
| 153 |
< |
} |
| 153 |
> |
} /* else check format */ |
| 154 |
|
for (i = 1; i < DTend; i++) |
| 155 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
| 156 |
|
ip->dtype = i; |
| 164 |
|
{ |
| 165 |
|
int i, j, k; |
| 166 |
|
|
| 167 |
+ |
if (!rmx_prepare(rm)) |
| 168 |
+ |
return(0); |
| 169 |
|
for (i = 0; i < rm->nrows; i++) |
| 170 |
|
for (j = 0; j < rm->ncols; j++) |
| 171 |
|
for (k = 0; k < rm->ncomp; k++) |
| 184 |
|
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
| 185 |
|
exit(1); |
| 186 |
|
} |
| 187 |
+ |
if (!rmx_prepare(rm)) |
| 188 |
+ |
return(0); |
| 189 |
|
for (i = 0; i < rm->nrows; i++) |
| 190 |
|
for (j = 0; j < rm->ncols; j++) { |
| 191 |
|
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
| 202 |
|
rmx_load_double(RMATRIX *rm, FILE *fp) |
| 203 |
|
{ |
| 204 |
|
int i; |
| 205 |
< |
|
| 206 |
< |
if ((char *)&rmx_lval(rm,0,0,0) - (char *)&rmx_lval(rm,1,0,0) != |
| 207 |
< |
sizeof(double)*rm->ncols*rm->ncomp) { |
| 208 |
< |
fputs("Code error in rmx_load_double()\n", stderr); |
| 209 |
< |
exit(1); |
| 205 |
> |
#ifdef MAP_FILE |
| 206 |
> |
long pos; /* map memory to file if possible */ |
| 207 |
> |
if (!rm->swapin && array_size(rm) >= 1L<<20 && |
| 208 |
> |
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
| 209 |
> |
rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, |
| 210 |
> |
MAP_PRIVATE, fileno(fp), 0); |
| 211 |
> |
if (rm->mapped != MAP_FAILED) { |
| 212 |
> |
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
| 213 |
> |
return(1); |
| 214 |
> |
} |
| 215 |
> |
rm->mapped = NULL; |
| 216 |
|
} |
| 217 |
+ |
#endif |
| 218 |
+ |
if (!rmx_prepare(rm)) |
| 219 |
+ |
return(0); |
| 220 |
|
for (i = 0; i < rm->nrows; i++) { |
| 221 |
|
if (getbinary(&rmx_lval(rm,i,0,0), sizeof(double)*rm->ncomp, |
| 222 |
|
rm->ncols, fp) != rm->ncols) |
| 235 |
|
|
| 236 |
|
if (!scan) |
| 237 |
|
return(0); |
| 238 |
+ |
if (!rmx_prepare(rm)) |
| 239 |
+ |
return(0); |
| 240 |
|
for (i = 0; i < rm->nrows; i++) { |
| 241 |
|
if (freadscan(scan, rm->ncols, fp) < 0) { |
| 242 |
|
free(scan); |
| 254 |
|
|
| 255 |
|
/* Load matrix from supported file type */ |
| 256 |
|
RMATRIX * |
| 257 |
< |
rmx_load(const char *inspec) |
| 257 |
> |
rmx_load(const char *inspec, RMPref rmp) |
| 258 |
|
{ |
| 259 |
< |
FILE *fp = stdin; |
| 209 |
< |
RMATRIX dinfo; |
| 259 |
> |
FILE *fp; |
| 260 |
|
RMATRIX *dnew; |
| 261 |
|
|
| 262 |
< |
if (!inspec) { /* reading from stdin? */ |
| 263 |
< |
inspec = "<stdin>"; |
| 264 |
< |
SET_FILE_BINARY(stdin); |
| 262 |
> |
if (!inspec) |
| 263 |
> |
inspec = stdin_name; |
| 264 |
> |
else if (!*inspec) |
| 265 |
> |
return(NULL); |
| 266 |
> |
if (inspec == stdin_name) { /* reading from stdin? */ |
| 267 |
> |
fp = stdin; |
| 268 |
|
} else if (inspec[0] == '!') { |
| 269 |
|
if (!(fp = popen(inspec+1, "r"))) |
| 270 |
|
return(NULL); |
| 218 |
– |
SET_FILE_BINARY(fp); |
| 271 |
|
} else { |
| 272 |
|
const char *sp = inspec; /* check suffix */ |
| 273 |
|
while (*sp) |
| 275 |
|
while (sp > inspec && sp[-1] != '.') |
| 276 |
|
--sp; |
| 277 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
| 278 |
< |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
| 278 |
> |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
| 279 |
> |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
| 280 |
|
if (!cm) |
| 281 |
|
return(NULL); |
| 282 |
|
dnew = rmx_from_cmatrix(cm); |
| 285 |
|
return(dnew); |
| 286 |
|
} |
| 287 |
|
/* else open it ourselves */ |
| 288 |
< |
if (!(fp = fopen(inspec, "rb"))) |
| 288 |
> |
if (!(fp = fopen(inspec, "r"))) |
| 289 |
|
return(NULL); |
| 290 |
|
} |
| 291 |
+ |
SET_FILE_BINARY(fp); |
| 292 |
|
#ifdef getc_unlocked |
| 293 |
|
flockfile(fp); |
| 294 |
|
#endif |
| 295 |
< |
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) { |
| 295 |
> |
if (!(dnew = rmx_new(0,0,3))) { |
| 296 |
|
fclose(fp); |
| 297 |
|
return(NULL); |
| 298 |
|
} |
| 299 |
< |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
| 300 |
< |
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
| 299 |
> |
dnew->dtype = DTascii; /* assumed w/o FORMAT */ |
| 300 |
> |
dnew->cexp[0] = dnew->cexp[1] = dnew->cexp[2] = 1.f; |
| 301 |
> |
if (getheader(fp, get_dminfo, dnew) < 0) { |
| 302 |
> |
fclose(fp); |
| 303 |
> |
return(NULL); |
| 304 |
> |
} |
| 305 |
> |
if ((dnew->nrows <= 0) | (dnew->ncols <= 0)) { |
| 306 |
> |
if (!fscnresolu(&dnew->ncols, &dnew->nrows, fp)) { |
| 307 |
|
fclose(fp); |
| 308 |
|
return(NULL); |
| 309 |
|
} |
| 310 |
< |
if (dinfo.ncomp <= 0) |
| 311 |
< |
dinfo.ncomp = 3; |
| 257 |
< |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
| 258 |
< |
dinfo.ncomp != 3) { |
| 310 |
> |
if ((dnew->dtype == DTrgbe) | (dnew->dtype == DTxyze) && |
| 311 |
> |
dnew->ncomp != 3) { |
| 312 |
|
fclose(fp); |
| 313 |
|
return(NULL); |
| 314 |
|
} |
| 315 |
|
} |
| 316 |
< |
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
| 264 |
< |
if (!dnew) { |
| 265 |
< |
fclose(fp); |
| 266 |
< |
return(NULL); |
| 267 |
< |
} |
| 268 |
< |
dnew->info = dinfo.info; |
| 269 |
< |
switch (dinfo.dtype) { |
| 316 |
> |
switch (dnew->dtype) { |
| 317 |
|
case DTascii: |
| 318 |
|
SET_FILE_TEXT(fp); |
| 319 |
|
if (!rmx_load_ascii(dnew, fp)) |
| 321 |
|
dnew->dtype = DTascii; /* should leave double? */ |
| 322 |
|
break; |
| 323 |
|
case DTfloat: |
| 277 |
– |
dnew->swapin = dinfo.swapin; |
| 324 |
|
if (!rmx_load_float(dnew, fp)) |
| 325 |
|
goto loaderr; |
| 326 |
|
dnew->dtype = DTfloat; |
| 327 |
|
break; |
| 328 |
|
case DTdouble: |
| 283 |
– |
dnew->swapin = dinfo.swapin; |
| 329 |
|
if (!rmx_load_double(dnew, fp)) |
| 330 |
|
goto loaderr; |
| 331 |
|
dnew->dtype = DTdouble; |
| 334 |
|
case DTxyze: |
| 335 |
|
if (!rmx_load_rgbe(dnew, fp)) |
| 336 |
|
goto loaderr; |
| 337 |
< |
dnew->dtype = dinfo.dtype; /* undo exposure? */ |
| 338 |
< |
if ((dinfo.mtx[0] != 1.) | (dinfo.mtx[1] != 1.) | |
| 339 |
< |
(dinfo.mtx[2] != 1.)) { |
| 340 |
< |
dinfo.mtx[0] = 1./dinfo.mtx[0]; |
| 341 |
< |
dinfo.mtx[1] = 1./dinfo.mtx[1]; |
| 342 |
< |
dinfo.mtx[2] = 1./dinfo.mtx[2]; |
| 343 |
< |
rmx_scale(dnew, dinfo.mtx); |
| 337 |
> |
/* undo exposure? */ |
| 338 |
> |
if ((dnew->cexp[0] != 1.f) | (dnew->cexp[1] != 1.f) | |
| 339 |
> |
(dnew->cexp[2] != 1.f)) { |
| 340 |
> |
double cmlt[3]; |
| 341 |
> |
cmlt[0] = 1./dnew->cexp[0]; |
| 342 |
> |
cmlt[1] = 1./dnew->cexp[1]; |
| 343 |
> |
cmlt[2] = 1./dnew->cexp[2]; |
| 344 |
> |
rmx_scale(dnew, cmlt); |
| 345 |
|
} |
| 346 |
+ |
dnew->swapin = 0; |
| 347 |
|
break; |
| 348 |
|
default: |
| 349 |
|
goto loaderr; |
| 441 |
|
return(1); |
| 442 |
|
} |
| 443 |
|
|
| 444 |
+ |
/* Check if CIE XYZ primaries were specified */ |
| 445 |
+ |
static int |
| 446 |
+ |
findCIEprims(const char *info) |
| 447 |
+ |
{ |
| 448 |
+ |
RGBPRIMS prims; |
| 449 |
+ |
|
| 450 |
+ |
if (!info) |
| 451 |
+ |
return(0); |
| 452 |
+ |
info = strstr(info, PRIMARYSTR); |
| 453 |
+ |
if (!info || !primsval(prims, info)) |
| 454 |
+ |
return(0); |
| 455 |
+ |
|
| 456 |
+ |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
| 457 |
+ |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
| 458 |
+ |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
| 459 |
+ |
} |
| 460 |
+ |
|
| 461 |
|
/* Write matrix to file type indicated by dtype */ |
| 462 |
|
int |
| 463 |
|
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
| 475 |
|
fputs(rm->info, fp); |
| 476 |
|
if (dtype == DTfromHeader) |
| 477 |
|
dtype = rm->dtype; |
| 478 |
< |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
| 478 |
> |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
| 479 |
> |
findCIEprims(rm->info))) |
| 480 |
|
dtype = DTxyze; |
| 481 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
| 482 |
|
dtype = DTrgbe; |
| 496 |
|
} |
| 497 |
|
if ((dtype == DTfloat) | (dtype == DTdouble)) |
| 498 |
|
fputendian(fp); /* important to record */ |
| 499 |
< |
fputformat((char *)cm_fmt_id[dtype], fp); |
| 499 |
> |
fputformat(cm_fmt_id[dtype], fp); |
| 500 |
|
fputc('\n', fp); |
| 501 |
|
switch (dtype) { /* write data */ |
| 502 |
|
case DTascii: |