| 16 |
|
#include <sys/mman.h> |
| 17 |
|
#endif |
| 18 |
|
|
| 19 |
+ |
#ifndef MAXCOMP |
| 20 |
+ |
#define MAXCOMP MAXCSAMP /* #components we support */ |
| 21 |
+ |
#endif |
| 22 |
+ |
|
| 23 |
|
static const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
| 24 |
|
|
| 25 |
|
/* Initialize a RMATRIX struct but don't allocate array space */ |
| 50 |
|
rmx_prepare(RMATRIX *rm) |
| 51 |
|
{ |
| 52 |
|
if (!rm) return(0); |
| 53 |
< |
if (rm->mtx) |
| 53 |
> |
if (rm->mtx) /* assume it's right size */ |
| 54 |
|
return(1); |
| 55 |
|
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) |
| 56 |
|
return(0); |
| 65 |
|
{ |
| 66 |
|
RMATRIX *dnew = rmx_new(nr, nc, n); |
| 67 |
|
|
| 68 |
< |
if (dnew && !rmx_prepare(dnew)) { |
| 68 |
> |
if (!rmx_prepare(dnew)) { |
| 69 |
|
rmx_free(dnew); |
| 70 |
< |
dnew = NULL; |
| 70 |
> |
return(NULL); |
| 71 |
|
} |
| 72 |
|
return(dnew); |
| 73 |
|
} |
| 120 |
|
int |
| 121 |
|
rmx_addinfo(RMATRIX *rm, const char *info) |
| 122 |
|
{ |
| 123 |
< |
int oldlen = 0; |
| 123 |
> |
size_t oldlen = 0; |
| 124 |
|
|
| 125 |
|
if (!rm || !info || !*info) |
| 126 |
|
return(0); |
| 127 |
|
if (!rm->info) { |
| 128 |
|
rm->info = (char *)malloc(strlen(info)+1); |
| 125 |
– |
if (rm->info) rm->info[0] = '\0'; |
| 129 |
|
} else { |
| 130 |
|
oldlen = strlen(rm->info); |
| 131 |
|
rm->info = (char *)realloc(rm->info, |
| 144 |
|
char fmt[MAXFMTLEN]; |
| 145 |
|
int i; |
| 146 |
|
|
| 147 |
< |
if (headidval(NULL, s)) |
| 147 |
> |
if (isheadid(s)) |
| 148 |
|
return(0); |
| 149 |
|
if (isncomp(s)) { |
| 150 |
|
ip->ncomp = ncompval(s); |
| 151 |
< |
return(0); |
| 151 |
> |
return(ip->ncomp - 1); |
| 152 |
|
} |
| 153 |
|
if (!strncmp(s, "NROWS=", 6)) { |
| 154 |
|
ip->nrows = atoi(s+6); |
| 155 |
< |
return(0); |
| 155 |
> |
return(ip->nrows - 1); |
| 156 |
|
} |
| 157 |
|
if (!strncmp(s, "NCOLS=", 6)) { |
| 158 |
|
ip->ncols = atoi(s+6); |
| 159 |
< |
return(0); |
| 159 |
> |
return(ip->ncols - 1); |
| 160 |
|
} |
| 161 |
|
if ((i = isbigendian(s)) >= 0) { |
| 162 |
|
if (nativebigendian() != i) |
| 168 |
|
if (isexpos(s)) { |
| 169 |
|
float f = exposval(s); |
| 170 |
|
scalecolor(ip->cexp, f); |
| 171 |
< |
return(0); |
| 171 |
> |
return(f > .0 ? 0 : -1); |
| 172 |
|
} |
| 173 |
|
if (iscolcor(s)) { |
| 174 |
|
COLOR ctmp; |
| 175 |
< |
colcorval(ctmp, s); |
| 175 |
> |
if (!colcorval(ctmp, s)) return(-1); |
| 176 |
|
multcolor(ip->cexp, ctmp); |
| 177 |
|
return(0); |
| 178 |
|
} |
| 179 |
< |
if (iswlsplit(s)) { |
| 180 |
< |
wlsplitval(ip->wlpart, s); |
| 181 |
< |
return(0); |
| 179 |
< |
} |
| 179 |
> |
if (iswlsplit(s)) |
| 180 |
> |
return(wlsplitval(ip->wlpart, s) - 1); |
| 181 |
> |
|
| 182 |
|
if (!formatval(fmt, s)) { |
| 183 |
|
rmx_addinfo(ip, s); |
| 184 |
|
return(0); |
| 188 |
|
ip->dtype = i; |
| 189 |
|
return(0); |
| 190 |
|
} |
| 191 |
< |
return(-1); |
| 191 |
> |
return(-1); /* bad format */ |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
static int |
| 207 |
|
rmx_load_float(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
| 208 |
|
{ |
| 209 |
|
int j, k; |
| 210 |
< |
float val[100]; |
| 210 |
> |
float val[MAXCOMP]; |
| 211 |
|
|
| 212 |
< |
if (rm->ncomp > 100) { |
| 212 |
> |
if (rm->ncomp > MAXCOMP) { |
| 213 |
|
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
| 214 |
|
exit(1); |
| 215 |
|
} |
| 260 |
|
static int |
| 261 |
|
rmx_load_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
| 262 |
|
{ |
| 263 |
< |
uby8 *scan; |
| 264 |
< |
SCOLOR scol; |
| 263 |
> |
COLRV *scan; |
| 264 |
> |
COLORV scol[MAXCOMP]; |
| 265 |
|
int j, k; |
| 266 |
|
|
| 267 |
< |
if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP)) |
| 267 |
> |
if ((rm->ncomp < 3) | (rm->ncomp > MAXCOMP)) |
| 268 |
|
return(0); |
| 269 |
< |
scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols); |
| 269 |
> |
scan = (COLRV *)tempbuffer((rm->ncomp+1)*rm->ncols); |
| 270 |
|
if (!scan) |
| 271 |
|
return(0); |
| 272 |
|
if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) |
| 295 |
|
} |
| 296 |
|
rm->dtype = DTascii; /* assumed w/o FORMAT */ |
| 297 |
|
if (getheader(fp, get_dminfo, rm) < 0) { |
| 298 |
< |
fputs("Unrecognized matrix format\n", stderr); |
| 298 |
> |
fputs("Bad matrix header\n", stderr); |
| 299 |
|
return(0); |
| 300 |
|
} |
| 301 |
|
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
| 382 |
|
fp = stdin; |
| 383 |
|
else if (inspec[0] == '!') |
| 384 |
|
fp = popen(inspec+1, "r"); |
| 385 |
< |
else { |
| 386 |
< |
const char *sp = inspec; /* check suffix */ |
| 387 |
< |
while (*sp) |
| 386 |
< |
++sp; |
| 387 |
< |
while (sp > inspec && sp[-1] != '.') |
| 388 |
< |
--sp; |
| 389 |
< |
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
| 385 |
> |
else { /* check suffix */ |
| 386 |
> |
const char *sp = strrchr(inspec, '.'); |
| 387 |
> |
if (sp > inspec && !strcasecmp(sp+1, "XML")) { /* BSDF? */ |
| 388 |
|
CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL : |
| 389 |
|
rmp==RMPtrans ? cm_loadBTDF(inspec) : |
| 390 |
|
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
| 432 |
|
/* undo exposure? */ |
| 433 |
|
if ((dnew->cexp[0] != 1.f) | |
| 434 |
|
(dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { |
| 435 |
< |
double cmlt[MAXCSAMP]; |
| 435 |
> |
double cmlt[MAXCOMP]; |
| 436 |
|
int i; |
| 437 |
< |
if (dnew->ncomp > MAXCSAMP) { |
| 437 |
> |
if (dnew->ncomp > MAXCOMP) { |
| 438 |
|
fprintf(stderr, "Excess spectral components in: %s\n", |
| 439 |
|
inspec); |
| 440 |
|
rmx_free(dnew); |
| 498 |
|
static int |
| 499 |
|
rmx_write_spec(const rmx_dtype *dp, int nc, int len, FILE *fp) |
| 500 |
|
{ |
| 501 |
< |
uby8 *scan; |
| 502 |
< |
SCOLOR scol; |
| 501 |
> |
COLRV *scan; |
| 502 |
> |
COLORV scol[MAXCOMP]; |
| 503 |
|
int j, k; |
| 504 |
|
|
| 505 |
< |
if (nc < 3) return(0); |
| 506 |
< |
scan = (uby8 *)tempbuffer((nc+1)*len); |
| 505 |
> |
if ((nc < 3) | (nc > MAXCOMP)) return(0); |
| 506 |
> |
scan = (COLRV *)tempbuffer((nc+1)*len); |
| 507 |
|
if (!scan) return(0); |
| 508 |
|
for (j = 0; j < len; j++, dp += nc) { |
| 509 |
|
for (k = nc; k--; ) |
| 696 |
|
munmap(rdst->mapped, rmx_mapped_size(rdst)); |
| 697 |
|
else |
| 698 |
|
#endif |
| 699 |
< |
if (rdst->pflags & RMF_FREEMEM) |
| 699 |
> |
if (rdst->pflags & RMF_FREEMEM) { |
| 700 |
|
free(rdst->mtx); |
| 701 |
+ |
rdst->pflags &= ~RMF_FREEMEM; |
| 702 |
+ |
} |
| 703 |
|
rdst->mapped = rsrc->mapped; |
| 704 |
|
rdst->mtx = rsrc->mtx; |
| 705 |
< |
if (rsrc->pflags & RMF_FREEMEM) |
| 706 |
< |
rdst->pflags |= RMF_FREEMEM; |
| 707 |
< |
else |
| 708 |
< |
rdst->pflags &= ~RMF_FREEMEM; |
| 705 |
> |
rdst->pflags |= rsrc->pflags & RMF_FREEMEM; |
| 706 |
|
rsrc->mapped = NULL; rsrc->mtx = NULL; |
| 707 |
|
return(1); |
| 708 |
|
} |
| 948 |
|
int i, j; |
| 949 |
|
CMATRIX *cnew; |
| 950 |
|
|
| 951 |
< |
if (!rm || !rm->mtx | (rm->ncomp == 2)) |
| 951 |
> |
if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP)) |
| 952 |
|
return(NULL); |
| 953 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
| 954 |
|
if (!cnew) |
| 965 |
|
setcolor(cv, dp[0], dp[0], dp[0]); |
| 966 |
|
break; |
| 967 |
|
default: { |
| 968 |
< |
SCOLOR scol; |
| 968 |
> |
COLORV scol[MAXCOMP]; |
| 969 |
|
int k; |
| 970 |
|
for (k = rm->ncomp; k--; ) |
| 971 |
|
scol[k] = dp[k]; |