| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: rmatrix.c,v 2.79 2024/05/19 15:32:24 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* General matrix operations.
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <stdlib.h>
|
| 9 |
#include <errno.h>
|
| 10 |
#include "rtio.h"
|
| 11 |
#include "platform.h"
|
| 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 const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
|
| 20 |
|
| 21 |
/* Initialize a RMATRIX struct but don't allocate array space */
|
| 22 |
RMATRIX *
|
| 23 |
rmx_new(int nr, int nc, int n)
|
| 24 |
{
|
| 25 |
RMATRIX *dnew;
|
| 26 |
|
| 27 |
if (n <= 0)
|
| 28 |
return(NULL);
|
| 29 |
|
| 30 |
dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
|
| 31 |
if (!dnew)
|
| 32 |
return(NULL);
|
| 33 |
|
| 34 |
dnew->dtype = DTrmx_native;
|
| 35 |
dnew->nrows = nr;
|
| 36 |
dnew->ncols = nc;
|
| 37 |
dnew->ncomp = n;
|
| 38 |
setcolor(dnew->cexp, 1.f, 1.f, 1.f);
|
| 39 |
memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
|
| 40 |
|
| 41 |
return(dnew);
|
| 42 |
}
|
| 43 |
|
| 44 |
/* Prepare a RMATRIX for writing (allocate array if needed) */
|
| 45 |
int
|
| 46 |
rmx_prepare(RMATRIX *rm)
|
| 47 |
{
|
| 48 |
if (!rm) return(0);
|
| 49 |
if (rm->mtx)
|
| 50 |
return(1);
|
| 51 |
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0))
|
| 52 |
return(0);
|
| 53 |
rm->mtx = (double *)malloc(rmx_array_size(rm));
|
| 54 |
rm->pflags |= RMF_OURMEM;
|
| 55 |
return(rm->mtx != NULL);
|
| 56 |
}
|
| 57 |
|
| 58 |
/* Call rmx_new() and rmx_prepare() */
|
| 59 |
RMATRIX *
|
| 60 |
rmx_alloc(int nr, int nc, int n)
|
| 61 |
{
|
| 62 |
RMATRIX *dnew = rmx_new(nr, nc, n);
|
| 63 |
|
| 64 |
if (dnew && !rmx_prepare(dnew)) {
|
| 65 |
rmx_free(dnew);
|
| 66 |
dnew = NULL;
|
| 67 |
}
|
| 68 |
return(dnew);
|
| 69 |
}
|
| 70 |
|
| 71 |
/* Clear state by freeing info and matrix data */
|
| 72 |
void
|
| 73 |
rmx_reset(RMATRIX *rm)
|
| 74 |
{
|
| 75 |
if (!rm) return;
|
| 76 |
if (rm->info) {
|
| 77 |
free(rm->info);
|
| 78 |
rm->info = NULL;
|
| 79 |
}
|
| 80 |
if (rm->mtx && rm->pflags & RMF_OURMEM) {
|
| 81 |
#ifdef MAP_FILE
|
| 82 |
if (rm->mapped) {
|
| 83 |
munmap(rm->mapped, rmx_mapped_size(rm));
|
| 84 |
rm->mapped = NULL;
|
| 85 |
} else
|
| 86 |
#endif
|
| 87 |
free(rm->mtx);
|
| 88 |
rm->pflags &= ~RMF_OURMEM;
|
| 89 |
}
|
| 90 |
rm->mtx = NULL;
|
| 91 |
}
|
| 92 |
|
| 93 |
/* Free an RMATRIX struct and data */
|
| 94 |
void
|
| 95 |
rmx_free(RMATRIX *rm)
|
| 96 |
{
|
| 97 |
if (!rm) return;
|
| 98 |
rmx_reset(rm);
|
| 99 |
free(rm);
|
| 100 |
}
|
| 101 |
|
| 102 |
/* Resolve data type based on two input types (returns 0 for mismatch) */
|
| 103 |
int
|
| 104 |
rmx_newtype(int dtyp1, int dtyp2)
|
| 105 |
{
|
| 106 |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | (dtyp1==DTspec) |
|
| 107 |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) | (dtyp2==DTspec)
|
| 108 |
&& dtyp1 != dtyp2)
|
| 109 |
return(0);
|
| 110 |
if (dtyp1 < dtyp2)
|
| 111 |
return(dtyp1);
|
| 112 |
return(dtyp2);
|
| 113 |
}
|
| 114 |
|
| 115 |
/* Append header information associated with matrix data */
|
| 116 |
int
|
| 117 |
rmx_addinfo(RMATRIX *rm, const char *info)
|
| 118 |
{
|
| 119 |
int oldlen = 0;
|
| 120 |
|
| 121 |
if (!rm || !info || !*info)
|
| 122 |
return(0);
|
| 123 |
if (!rm->info) {
|
| 124 |
rm->info = (char *)malloc(strlen(info)+1);
|
| 125 |
if (rm->info) rm->info[0] = '\0';
|
| 126 |
} else {
|
| 127 |
oldlen = strlen(rm->info);
|
| 128 |
rm->info = (char *)realloc(rm->info,
|
| 129 |
oldlen+strlen(info)+1);
|
| 130 |
}
|
| 131 |
if (!rm->info)
|
| 132 |
return(0);
|
| 133 |
strcpy(rm->info+oldlen, info);
|
| 134 |
return(1);
|
| 135 |
}
|
| 136 |
|
| 137 |
static int
|
| 138 |
get_dminfo(char *s, void *p)
|
| 139 |
{
|
| 140 |
RMATRIX *ip = (RMATRIX *)p;
|
| 141 |
char fmt[MAXFMTLEN];
|
| 142 |
int i;
|
| 143 |
|
| 144 |
if (headidval(NULL, s))
|
| 145 |
return(0);
|
| 146 |
if (isncomp(s)) {
|
| 147 |
ip->ncomp = ncompval(s);
|
| 148 |
return(0);
|
| 149 |
}
|
| 150 |
if (!strncmp(s, "NROWS=", 6)) {
|
| 151 |
ip->nrows = atoi(s+6);
|
| 152 |
return(0);
|
| 153 |
}
|
| 154 |
if (!strncmp(s, "NCOLS=", 6)) {
|
| 155 |
ip->ncols = atoi(s+6);
|
| 156 |
return(0);
|
| 157 |
}
|
| 158 |
if ((i = isbigendian(s)) >= 0) {
|
| 159 |
if (nativebigendian() != i)
|
| 160 |
ip->pflags |= RMF_SWAPIN;
|
| 161 |
else
|
| 162 |
ip->pflags &= ~RMF_SWAPIN;
|
| 163 |
return(0);
|
| 164 |
}
|
| 165 |
if (isexpos(s)) {
|
| 166 |
float f = exposval(s);
|
| 167 |
scalecolor(ip->cexp, f);
|
| 168 |
return(0);
|
| 169 |
}
|
| 170 |
if (iscolcor(s)) {
|
| 171 |
COLOR ctmp;
|
| 172 |
colcorval(ctmp, s);
|
| 173 |
multcolor(ip->cexp, ctmp);
|
| 174 |
return(0);
|
| 175 |
}
|
| 176 |
if (iswlsplit(s)) {
|
| 177 |
wlsplitval(ip->wlpart, s);
|
| 178 |
return(0);
|
| 179 |
}
|
| 180 |
if (!formatval(fmt, s)) {
|
| 181 |
rmx_addinfo(ip, s);
|
| 182 |
return(0);
|
| 183 |
} /* else check format */
|
| 184 |
for (i = 1; i < DTend; i++)
|
| 185 |
if (!strcmp(fmt, cm_fmt_id[i])) {
|
| 186 |
ip->dtype = i;
|
| 187 |
return(0);
|
| 188 |
}
|
| 189 |
return(-1);
|
| 190 |
}
|
| 191 |
|
| 192 |
static int
|
| 193 |
rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp)
|
| 194 |
{
|
| 195 |
int j, k;
|
| 196 |
|
| 197 |
for (j = 0; j < rm->ncols; j++)
|
| 198 |
for (k = rm->ncomp; k-- > 0; )
|
| 199 |
if (fscanf(fp, "%lf", drp++) != 1)
|
| 200 |
return(0);
|
| 201 |
return(1);
|
| 202 |
}
|
| 203 |
|
| 204 |
static int
|
| 205 |
rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp)
|
| 206 |
{
|
| 207 |
int j, k;
|
| 208 |
float val[100];
|
| 209 |
|
| 210 |
if (rm->ncomp > 100) {
|
| 211 |
fputs("Unsupported # components in rmx_load_float()\n", stderr);
|
| 212 |
exit(1);
|
| 213 |
}
|
| 214 |
for (j = 0; j < rm->ncols; j++) {
|
| 215 |
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
|
| 216 |
return(0);
|
| 217 |
if (rm->pflags & RMF_SWAPIN)
|
| 218 |
swap32((char *)val, rm->ncomp);
|
| 219 |
for (k = 0; k < rm->ncomp; k++)
|
| 220 |
*drp++ = val[k];
|
| 221 |
}
|
| 222 |
return(1);
|
| 223 |
}
|
| 224 |
|
| 225 |
static int
|
| 226 |
rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp)
|
| 227 |
{
|
| 228 |
if (DTrmx_native != DTdouble)
|
| 229 |
return(0);
|
| 230 |
if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
|
| 231 |
return(0);
|
| 232 |
if (rm->pflags & RMF_SWAPIN)
|
| 233 |
swap64((char *)drp, rm->ncols*rm->ncomp);
|
| 234 |
return(1);
|
| 235 |
}
|
| 236 |
|
| 237 |
static int
|
| 238 |
rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp)
|
| 239 |
{
|
| 240 |
COLR *scan;
|
| 241 |
COLOR col;
|
| 242 |
int j;
|
| 243 |
|
| 244 |
if (rm->ncomp != 3)
|
| 245 |
return(0);
|
| 246 |
scan = (COLR *)tempbuffer(sizeof(COLR)*rm->ncols);
|
| 247 |
if (!scan)
|
| 248 |
return(0);
|
| 249 |
if (freadcolrs(scan, rm->ncols, fp) < 0)
|
| 250 |
return(0);
|
| 251 |
for (j = 0; j < rm->ncols; j++) {
|
| 252 |
colr_color(col, scan[j]);
|
| 253 |
*drp++ = colval(col,RED);
|
| 254 |
*drp++ = colval(col,GRN);
|
| 255 |
*drp++ = colval(col,BLU);
|
| 256 |
}
|
| 257 |
return(1);
|
| 258 |
}
|
| 259 |
|
| 260 |
static int
|
| 261 |
rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp)
|
| 262 |
{
|
| 263 |
uby8 *scan;
|
| 264 |
SCOLOR scol;
|
| 265 |
int j, k;
|
| 266 |
|
| 267 |
if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP))
|
| 268 |
return(0);
|
| 269 |
scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols);
|
| 270 |
if (!scan)
|
| 271 |
return(0);
|
| 272 |
if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0)
|
| 273 |
return(0);
|
| 274 |
for (j = 0; j < rm->ncols; j++) {
|
| 275 |
scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
|
| 276 |
for (k = 0; k < rm->ncomp; k++)
|
| 277 |
*drp++ = scol[k];
|
| 278 |
}
|
| 279 |
return(1);
|
| 280 |
}
|
| 281 |
|
| 282 |
/* Read matrix header from input stream (cannot be XML) */
|
| 283 |
int
|
| 284 |
rmx_load_header(RMATRIX *rm, FILE *fp)
|
| 285 |
{
|
| 286 |
if (!rm | !fp)
|
| 287 |
return(0);
|
| 288 |
rmx_reset(rm); /* clear state */
|
| 289 |
if (rm->nrows | rm->ncols | !rm->dtype) {
|
| 290 |
rm->nrows = rm->ncols = 0;
|
| 291 |
rm->ncomp = 3;
|
| 292 |
setcolor(rm->cexp, 1.f, 1.f, 1.f);
|
| 293 |
memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
|
| 294 |
rm->pflags = 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);
|
| 299 |
return(0);
|
| 300 |
}
|
| 301 |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
|
| 302 |
rm->ncomp != 3)
|
| 303 |
return(0);
|
| 304 |
if (rm->ncols <= 0 && /* resolution string? */
|
| 305 |
!fscnresolu(&rm->ncols, &rm->nrows, fp))
|
| 306 |
return(0);
|
| 307 |
if (rm->dtype == DTascii) /* set file type (WINDOWS) */
|
| 308 |
SET_FILE_TEXT(fp);
|
| 309 |
else
|
| 310 |
SET_FILE_BINARY(fp);
|
| 311 |
return(1);
|
| 312 |
}
|
| 313 |
|
| 314 |
/* Load next row as double (cannot be XML) */
|
| 315 |
int
|
| 316 |
rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp)
|
| 317 |
{
|
| 318 |
switch (rm->dtype) {
|
| 319 |
case DTascii:
|
| 320 |
return(rmx_load_ascii(drp, rm, fp));
|
| 321 |
case DTfloat:
|
| 322 |
return(rmx_load_float(drp, rm, fp));
|
| 323 |
case DTdouble:
|
| 324 |
return(rmx_load_double(drp, rm, fp));
|
| 325 |
case DTrgbe:
|
| 326 |
case DTxyze:
|
| 327 |
return(rmx_load_rgbe(drp, rm, fp));
|
| 328 |
case DTspec:
|
| 329 |
return(rmx_load_spec(drp, rm, fp));
|
| 330 |
default:
|
| 331 |
fputs("Unsupported data type in rmx_load_row()\n", stderr);
|
| 332 |
}
|
| 333 |
return(0);
|
| 334 |
}
|
| 335 |
|
| 336 |
/* Allocate & load post-header data from stream given type set in rm->dtype */
|
| 337 |
int
|
| 338 |
rmx_load_data(RMATRIX *rm, FILE *fp)
|
| 339 |
{
|
| 340 |
int i;
|
| 341 |
#ifdef MAP_FILE
|
| 342 |
long pos; /* map memory for file > 1MB if possible */
|
| 343 |
if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) &&
|
| 344 |
rmx_array_size(rm) >= 1L<<20 &&
|
| 345 |
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
|
| 346 |
rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE,
|
| 347 |
MAP_PRIVATE, fileno(fp), 0);
|
| 348 |
if (rm->mapped != MAP_FAILED) {
|
| 349 |
rm->mtx = (double *)rm->mapped + pos/sizeof(double);
|
| 350 |
rm->pflags |= RMF_OURMEM;
|
| 351 |
return(1);
|
| 352 |
} /* else fall back on reading into memory */
|
| 353 |
rm->mapped = NULL;
|
| 354 |
}
|
| 355 |
#endif
|
| 356 |
if (!rmx_prepare(rm)) { /* need in-core matrix array */
|
| 357 |
fprintf(stderr, "Cannot allocate %g MByte matrix array\n",
|
| 358 |
(1./(1L<<20))*(double)rmx_array_size(rm));
|
| 359 |
return(0);
|
| 360 |
}
|
| 361 |
for (i = 0; i < rm->nrows; i++)
|
| 362 |
if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
|
| 363 |
return(0);
|
| 364 |
return(1);
|
| 365 |
}
|
| 366 |
|
| 367 |
/* Load matrix from supported file type */
|
| 368 |
RMATRIX *
|
| 369 |
rmx_load(const char *inspec, RMPref rmp)
|
| 370 |
{
|
| 371 |
FILE *fp;
|
| 372 |
RMATRIX *dnew;
|
| 373 |
int ok;
|
| 374 |
|
| 375 |
if (!inspec)
|
| 376 |
inspec = stdin_name;
|
| 377 |
else if (!*inspec)
|
| 378 |
return(NULL);
|
| 379 |
if (inspec == stdin_name) /* reading from stdin? */
|
| 380 |
fp = stdin;
|
| 381 |
else if (inspec[0] == '!')
|
| 382 |
fp = popen(inspec+1, "r");
|
| 383 |
else {
|
| 384 |
const char *sp = inspec; /* check suffix */
|
| 385 |
while (*sp)
|
| 386 |
++sp;
|
| 387 |
while (sp > inspec && sp[-1] != '.')
|
| 388 |
--sp;
|
| 389 |
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */
|
| 390 |
CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL :
|
| 391 |
rmp==RMPtrans ? cm_loadBTDF(inspec) :
|
| 392 |
cm_loadBRDF(inspec, rmp==RMPreflB) ;
|
| 393 |
if (!cm)
|
| 394 |
return(NULL);
|
| 395 |
dnew = rmx_from_cmatrix(cm);
|
| 396 |
cm_free(cm);
|
| 397 |
dnew->dtype = DTascii;
|
| 398 |
return(dnew); /* return here */
|
| 399 |
} /* else open it ourselves */
|
| 400 |
fp = fopen(inspec, "r");
|
| 401 |
}
|
| 402 |
if (!fp) {
|
| 403 |
fprintf(stderr, "Cannot open for reading: %s\n", inspec);
|
| 404 |
return(NULL);
|
| 405 |
}
|
| 406 |
#ifdef getc_unlocked
|
| 407 |
flockfile(fp);
|
| 408 |
#endif
|
| 409 |
SET_FILE_BINARY(fp); /* load header info */
|
| 410 |
if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
|
| 411 |
fprintf(stderr, "Bad header in: %s\n", inspec);
|
| 412 |
if (inspec[0] == '!') pclose(fp);
|
| 413 |
else fclose(fp);
|
| 414 |
rmx_free(dnew);
|
| 415 |
return(NULL);
|
| 416 |
}
|
| 417 |
ok = rmx_load_data(dnew, fp); /* allocate & load data */
|
| 418 |
|
| 419 |
if (fp != stdin) { /* close input stream */
|
| 420 |
if (inspec[0] == '!')
|
| 421 |
pclose(fp);
|
| 422 |
else
|
| 423 |
fclose(fp);
|
| 424 |
}
|
| 425 |
#ifdef getc_unlocked
|
| 426 |
else
|
| 427 |
funlockfile(fp);
|
| 428 |
#endif
|
| 429 |
if (!ok) { /* load failure? */
|
| 430 |
fprintf(stderr, "Error loading data from: %s\n", inspec);
|
| 431 |
rmx_free(dnew);
|
| 432 |
return(NULL);
|
| 433 |
}
|
| 434 |
/* undo exposure? */
|
| 435 |
if ((dnew->cexp[0] != 1.f) |
|
| 436 |
(dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) {
|
| 437 |
double cmlt[MAXCSAMP];
|
| 438 |
int i;
|
| 439 |
cmlt[0] = 1./dnew->cexp[0];
|
| 440 |
cmlt[1] = 1./dnew->cexp[1];
|
| 441 |
cmlt[2] = 1./dnew->cexp[2];
|
| 442 |
if (dnew->ncomp > MAXCSAMP) {
|
| 443 |
fprintf(stderr, "Excess spectral components in: %s\n",
|
| 444 |
inspec);
|
| 445 |
rmx_free(dnew);
|
| 446 |
return(NULL);
|
| 447 |
}
|
| 448 |
for (i = dnew->ncomp; i-- > 3; )
|
| 449 |
cmlt[i] = cmlt[1];
|
| 450 |
rmx_scale(dnew, cmlt);
|
| 451 |
setcolor(dnew->cexp, 1.f, 1.f, 1.f);
|
| 452 |
}
|
| 453 |
return(dnew);
|
| 454 |
}
|
| 455 |
|
| 456 |
static int
|
| 457 |
rmx_write_ascii(const double *dp, int nc, int len, FILE *fp)
|
| 458 |
{
|
| 459 |
while (len-- > 0) {
|
| 460 |
int k = nc;
|
| 461 |
while (k-- > 0)
|
| 462 |
fprintf(fp, " %.7e", *dp++);
|
| 463 |
fputc('\t', fp);
|
| 464 |
}
|
| 465 |
return(fputc('\n', fp) != EOF);
|
| 466 |
}
|
| 467 |
|
| 468 |
static int
|
| 469 |
rmx_write_float(const double *dp, int len, FILE *fp)
|
| 470 |
{
|
| 471 |
float val;
|
| 472 |
|
| 473 |
while (len--) {
|
| 474 |
val = *dp++;
|
| 475 |
if (putbinary(&val, sizeof(float), 1, fp) != 1)
|
| 476 |
return(0);
|
| 477 |
}
|
| 478 |
return(1);
|
| 479 |
}
|
| 480 |
|
| 481 |
static int
|
| 482 |
rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp)
|
| 483 |
{
|
| 484 |
COLR *scan;
|
| 485 |
int j;
|
| 486 |
|
| 487 |
if ((nc != 1) & (nc != 3)) return(0);
|
| 488 |
scan = (COLR *)tempbuffer(sizeof(COLR)*len);
|
| 489 |
if (!scan) return(0);
|
| 490 |
|
| 491 |
for (j = 0; j < len; j++, dp += nc)
|
| 492 |
if (nc == 1)
|
| 493 |
setcolr(scan[j], dp[0], dp[0], dp[0]);
|
| 494 |
else
|
| 495 |
setcolr(scan[j], dp[0], dp[1], dp[2]);
|
| 496 |
|
| 497 |
return(fwritecolrs(scan, len, fp) >= 0);
|
| 498 |
}
|
| 499 |
|
| 500 |
static int
|
| 501 |
rmx_write_spec(const double *dp, int nc, int len, FILE *fp)
|
| 502 |
{
|
| 503 |
uby8 *scan;
|
| 504 |
SCOLOR scol;
|
| 505 |
int j, k;
|
| 506 |
|
| 507 |
if (nc < 3) return(0);
|
| 508 |
scan = (uby8 *)tempbuffer((nc+1)*len);
|
| 509 |
if (!scan) return(0);
|
| 510 |
for (j = 0; j < len; j++, dp += nc) {
|
| 511 |
for (k = nc; k--; )
|
| 512 |
scol[k] = dp[k];
|
| 513 |
scolor2scolr(scan+j*(nc+1), scol, nc);
|
| 514 |
}
|
| 515 |
return(fwritescolrs(scan, nc, len, fp) >= 0);
|
| 516 |
}
|
| 517 |
|
| 518 |
/* Check if CIE XYZ primaries were specified */
|
| 519 |
static int
|
| 520 |
findCIEprims(const char *info)
|
| 521 |
{
|
| 522 |
RGBPRIMS prims;
|
| 523 |
|
| 524 |
if (!info)
|
| 525 |
return(0);
|
| 526 |
info = strstr(info, PRIMARYSTR);
|
| 527 |
if (!info || !primsval(prims, info))
|
| 528 |
return(0);
|
| 529 |
|
| 530 |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) &&
|
| 531 |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) &&
|
| 532 |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
|
| 533 |
}
|
| 534 |
|
| 535 |
/* Finish writing header data with resolution and format, returning type used */
|
| 536 |
int
|
| 537 |
rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
|
| 538 |
{
|
| 539 |
if (!rm | !fp || rm->ncols <= 0)
|
| 540 |
return(0);
|
| 541 |
if (rm->info)
|
| 542 |
fputs(rm->info, fp);
|
| 543 |
if (dtype == DTfromHeader)
|
| 544 |
dtype = rm->dtype;
|
| 545 |
else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
|
| 546 |
findCIEprims(rm->info)))
|
| 547 |
dtype = DTxyze;
|
| 548 |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
|
| 549 |
dtype = DTrgbe;
|
| 550 |
if ((dtype < DTspec) & (rm->ncomp > 3))
|
| 551 |
dtype = DTspec;
|
| 552 |
else if ((dtype == DTspec) & (rm->ncomp <= 3))
|
| 553 |
return(0);
|
| 554 |
|
| 555 |
if (dtype == DTascii) /* set file type (WINDOWS) */
|
| 556 |
SET_FILE_TEXT(fp);
|
| 557 |
else
|
| 558 |
SET_FILE_BINARY(fp);
|
| 559 |
/* write exposure? */
|
| 560 |
if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
|
| 561 |
(rm->cexp[GRN] != rm->cexp[BLU]))
|
| 562 |
fputcolcor(rm->cexp, fp);
|
| 563 |
else if (rm->cexp[GRN] != 1.f)
|
| 564 |
fputexpos(rm->cexp[GRN], fp);
|
| 565 |
/* matrix size? */
|
| 566 |
if ((dtype > DTspec) | (rm->nrows <= 0)) {
|
| 567 |
if (rm->nrows > 0)
|
| 568 |
fprintf(fp, "NROWS=%d\n", rm->nrows);
|
| 569 |
fprintf(fp, "NCOLS=%d\n", rm->ncols);
|
| 570 |
}
|
| 571 |
if (dtype >= DTspec) { /* # components & split? */
|
| 572 |
fputncomp(rm->ncomp, fp);
|
| 573 |
if (rm->ncomp > 3 &&
|
| 574 |
memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
|
| 575 |
fputwlsplit(rm->wlpart, fp);
|
| 576 |
} else if ((rm->ncomp != 3) & (rm->ncomp != 1))
|
| 577 |
return(0); /* wrong # components */
|
| 578 |
if ((dtype == DTfloat) | (dtype == DTdouble))
|
| 579 |
fputendian(fp); /* important to record */
|
| 580 |
fputformat(cm_fmt_id[dtype], fp);
|
| 581 |
fputc('\n', fp); /* end of header */
|
| 582 |
if ((dtype <= DTspec) & (rm->nrows > 0))
|
| 583 |
fprtresolu(rm->ncols, rm->nrows, fp);
|
| 584 |
return(dtype);
|
| 585 |
}
|
| 586 |
|
| 587 |
/* Write out matrix data (usually by row) */
|
| 588 |
int
|
| 589 |
rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp)
|
| 590 |
{
|
| 591 |
switch (dtype) {
|
| 592 |
case DTascii:
|
| 593 |
return(rmx_write_ascii(dp, nc, len, fp));
|
| 594 |
case DTfloat:
|
| 595 |
return(rmx_write_float(dp, nc*len, fp));
|
| 596 |
case DTrmx_native:
|
| 597 |
return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
|
| 598 |
case DTrgbe:
|
| 599 |
case DTxyze:
|
| 600 |
return(rmx_write_rgbe(dp, nc, len, fp));
|
| 601 |
case DTspec:
|
| 602 |
return(rmx_write_spec(dp, nc, len, fp));
|
| 603 |
}
|
| 604 |
return(0);
|
| 605 |
}
|
| 606 |
|
| 607 |
/* Write matrix using file format indicated by dtype */
|
| 608 |
int
|
| 609 |
rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
|
| 610 |
{
|
| 611 |
int ok = 0;
|
| 612 |
int i;
|
| 613 |
/* complete header */
|
| 614 |
dtype = rmx_write_header(rm, dtype, fp);
|
| 615 |
if (dtype <= 0)
|
| 616 |
return(0);
|
| 617 |
#ifdef getc_unlocked
|
| 618 |
flockfile(fp);
|
| 619 |
#endif
|
| 620 |
if (dtype == DTrmx_native) /* write all at once? */
|
| 621 |
ok = rmx_write_data(rm->mtx, rm->ncomp,
|
| 622 |
rm->nrows*rm->ncols, dtype, fp);
|
| 623 |
else /* else row by row */
|
| 624 |
for (i = 0; i < rm->nrows; i++) {
|
| 625 |
ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
|
| 626 |
rm->ncols, dtype, fp);
|
| 627 |
if (!ok) break;
|
| 628 |
}
|
| 629 |
|
| 630 |
if (ok) ok = (fflush(fp) == 0);
|
| 631 |
#ifdef getc_unlocked
|
| 632 |
funlockfile(fp);
|
| 633 |
#endif
|
| 634 |
if (!ok) fputs("Error writing matrix\n", stderr);
|
| 635 |
return(ok);
|
| 636 |
}
|
| 637 |
|
| 638 |
/* Allocate and assign square identity matrix with n components */
|
| 639 |
RMATRIX *
|
| 640 |
rmx_identity(const int dim, const int n)
|
| 641 |
{
|
| 642 |
RMATRIX *rid = rmx_alloc(dim, dim, n);
|
| 643 |
int i, k;
|
| 644 |
|
| 645 |
if (!rid)
|
| 646 |
return(NULL);
|
| 647 |
memset(rid->mtx, 0, rmx_array_size(rid));
|
| 648 |
for (i = dim; i--; ) {
|
| 649 |
double *dp = rmx_lval(rid,i,i);
|
| 650 |
for (k = n; k--; )
|
| 651 |
dp[k] = 1.;
|
| 652 |
}
|
| 653 |
return(rid);
|
| 654 |
}
|
| 655 |
|
| 656 |
/* Duplicate the given matrix (may be unallocated) */
|
| 657 |
RMATRIX *
|
| 658 |
rmx_copy(const RMATRIX *rm)
|
| 659 |
{
|
| 660 |
RMATRIX *dnew;
|
| 661 |
|
| 662 |
if (!rm)
|
| 663 |
return(NULL);
|
| 664 |
dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
|
| 665 |
if (!dnew)
|
| 666 |
return(NULL);
|
| 667 |
if (rm->mtx) {
|
| 668 |
if (!rmx_prepare(dnew)) {
|
| 669 |
rmx_free(dnew);
|
| 670 |
return(NULL);
|
| 671 |
}
|
| 672 |
memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew));
|
| 673 |
}
|
| 674 |
rmx_addinfo(dnew, rm->info);
|
| 675 |
dnew->dtype = rm->dtype;
|
| 676 |
copycolor(dnew->cexp, rm->cexp);
|
| 677 |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
|
| 678 |
return(dnew);
|
| 679 |
}
|
| 680 |
|
| 681 |
/* Replace data in first matrix with data from second */
|
| 682 |
int
|
| 683 |
rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
|
| 684 |
{
|
| 685 |
if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) |
|
| 686 |
(rdst->ncols != rsrc->ncols) |
|
| 687 |
(rdst->ncomp != rsrc->ncomp))
|
| 688 |
return(0);
|
| 689 |
|
| 690 |
if (dometa) { /* transfer everything? */
|
| 691 |
rmx_reset(rdst);
|
| 692 |
*rdst = *rsrc;
|
| 693 |
rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
|
| 694 |
return(1);
|
| 695 |
}
|
| 696 |
if (rdst->pflags & RMF_OURMEM) {
|
| 697 |
#ifdef MAP_FILE /* just matrix data -- leave metadata */
|
| 698 |
if (rdst->mapped)
|
| 699 |
munmap(rdst->mapped, rmx_mapped_size(rdst));
|
| 700 |
else
|
| 701 |
#endif
|
| 702 |
if (rdst->mtx)
|
| 703 |
free(rdst->mtx);
|
| 704 |
}
|
| 705 |
rdst->mapped = rsrc->mapped;
|
| 706 |
rdst->mtx = rsrc->mtx;
|
| 707 |
if (rsrc->pflags & RMF_OURMEM)
|
| 708 |
rdst->pflags |= RMF_OURMEM;
|
| 709 |
else
|
| 710 |
rdst->pflags &= ~RMF_OURMEM;
|
| 711 |
rsrc->mapped = NULL; rsrc->mtx = NULL;
|
| 712 |
return(1);
|
| 713 |
}
|
| 714 |
|
| 715 |
/* Allocate and assign transposed matrix */
|
| 716 |
RMATRIX *
|
| 717 |
rmx_transpose(const RMATRIX *rm)
|
| 718 |
{
|
| 719 |
RMATRIX *dnew;
|
| 720 |
int i, j;
|
| 721 |
|
| 722 |
if (!rm || !rm->mtx)
|
| 723 |
return(0);
|
| 724 |
if ((rm->nrows == 1) | (rm->ncols == 1)) {
|
| 725 |
dnew = rmx_copy(rm);
|
| 726 |
if (!dnew)
|
| 727 |
return(NULL);
|
| 728 |
dnew->nrows = rm->ncols;
|
| 729 |
dnew->ncols = rm->nrows;
|
| 730 |
return(dnew);
|
| 731 |
}
|
| 732 |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
|
| 733 |
if (!dnew)
|
| 734 |
return(NULL);
|
| 735 |
if (rm->info) {
|
| 736 |
rmx_addinfo(dnew, rm->info);
|
| 737 |
rmx_addinfo(dnew, "Transposed rows and columns\n");
|
| 738 |
}
|
| 739 |
dnew->dtype = rm->dtype;
|
| 740 |
copycolor(dnew->cexp, rm->cexp);
|
| 741 |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
|
| 742 |
for (j = dnew->ncols; j--; )
|
| 743 |
for (i = dnew->nrows; i--; )
|
| 744 |
memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i),
|
| 745 |
sizeof(double)*dnew->ncomp);
|
| 746 |
return(dnew);
|
| 747 |
}
|
| 748 |
|
| 749 |
/* Multiply (concatenate) two matrices and allocate the result */
|
| 750 |
RMATRIX *
|
| 751 |
rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
|
| 752 |
{
|
| 753 |
RMATRIX *mres;
|
| 754 |
int i, j, k, h;
|
| 755 |
|
| 756 |
if (!m1 | !m2 || !m1->mtx | !m2->mtx |
|
| 757 |
(m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
|
| 758 |
return(NULL);
|
| 759 |
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
|
| 760 |
if (!mres)
|
| 761 |
return(NULL);
|
| 762 |
i = rmx_newtype(m1->dtype, m2->dtype);
|
| 763 |
if (i)
|
| 764 |
mres->dtype = i;
|
| 765 |
else
|
| 766 |
rmx_addinfo(mres, rmx_mismatch_warn);
|
| 767 |
for (i = mres->nrows; i--; )
|
| 768 |
for (j = mres->ncols; j--; )
|
| 769 |
for (k = mres->ncomp; k--; ) {
|
| 770 |
double d = 0;
|
| 771 |
for (h = m1->ncols; h--; )
|
| 772 |
d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k];
|
| 773 |
rmx_lval(mres,i,j)[k] = d;
|
| 774 |
}
|
| 775 |
return(mres);
|
| 776 |
}
|
| 777 |
|
| 778 |
/* Element-wise multiplication (or division) of m2 into m1 */
|
| 779 |
int
|
| 780 |
rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide)
|
| 781 |
{
|
| 782 |
int zeroDivides = 0;
|
| 783 |
int i, j, k;
|
| 784 |
|
| 785 |
if (!m1 | !m2 || !m1->mtx | !m2->mtx |
|
| 786 |
(m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
|
| 787 |
return(0);
|
| 788 |
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
|
| 789 |
return(0);
|
| 790 |
i = rmx_newtype(m1->dtype, m2->dtype);
|
| 791 |
if (i)
|
| 792 |
m1->dtype = i;
|
| 793 |
else
|
| 794 |
rmx_addinfo(m1, rmx_mismatch_warn);
|
| 795 |
for (i = m1->nrows; i--; )
|
| 796 |
for (j = m1->ncols; j--; )
|
| 797 |
if (divide) {
|
| 798 |
double d;
|
| 799 |
if (m2->ncomp == 1) {
|
| 800 |
d = rmx_val(m2,i,j)[0];
|
| 801 |
if (d == 0) {
|
| 802 |
++zeroDivides;
|
| 803 |
for (k = m1->ncomp; k--; )
|
| 804 |
rmx_lval(m1,i,j)[k] = 0;
|
| 805 |
} else {
|
| 806 |
d = 1./d;
|
| 807 |
for (k = m1->ncomp; k--; )
|
| 808 |
rmx_lval(m1,i,j)[k] *= d;
|
| 809 |
}
|
| 810 |
} else
|
| 811 |
for (k = m1->ncomp; k--; ) {
|
| 812 |
d = rmx_val(m2,i,j)[k];
|
| 813 |
if (d == 0) {
|
| 814 |
++zeroDivides;
|
| 815 |
rmx_lval(m1,i,j)[k] = 0;
|
| 816 |
} else
|
| 817 |
rmx_lval(m1,i,j)[k] /= d;
|
| 818 |
}
|
| 819 |
} else {
|
| 820 |
if (m2->ncomp == 1) {
|
| 821 |
const double d = rmx_val(m2,i,j)[0];
|
| 822 |
for (k = m1->ncomp; k--; )
|
| 823 |
rmx_lval(m1,i,j)[k] *= d;
|
| 824 |
} else
|
| 825 |
for (k = m1->ncomp; k--; )
|
| 826 |
rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k];
|
| 827 |
}
|
| 828 |
if (zeroDivides) {
|
| 829 |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n");
|
| 830 |
errno = ERANGE;
|
| 831 |
}
|
| 832 |
return(1);
|
| 833 |
}
|
| 834 |
|
| 835 |
/* Sum second matrix into first, applying scale factor beforehand */
|
| 836 |
int
|
| 837 |
rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[])
|
| 838 |
{
|
| 839 |
double *mysf = NULL;
|
| 840 |
int i, j, k;
|
| 841 |
|
| 842 |
if (!msum | !madd || !msum->mtx | !madd->mtx |
|
| 843 |
(msum->nrows != madd->nrows) |
|
| 844 |
(msum->ncols != madd->ncols) |
|
| 845 |
(msum->ncomp != madd->ncomp))
|
| 846 |
return(0);
|
| 847 |
if (!sf) {
|
| 848 |
mysf = (double *)malloc(sizeof(double)*msum->ncomp);
|
| 849 |
if (!mysf)
|
| 850 |
return(0);
|
| 851 |
for (k = msum->ncomp; k--; )
|
| 852 |
mysf[k] = 1;
|
| 853 |
sf = mysf;
|
| 854 |
}
|
| 855 |
i = rmx_newtype(msum->dtype, madd->dtype);
|
| 856 |
if (i)
|
| 857 |
msum->dtype = i;
|
| 858 |
else
|
| 859 |
rmx_addinfo(msum, rmx_mismatch_warn);
|
| 860 |
for (i = msum->nrows; i--; )
|
| 861 |
for (j = msum->ncols; j--; ) {
|
| 862 |
const double *da = rmx_val(madd,i,j);
|
| 863 |
double *ds = rmx_lval(msum,i,j);
|
| 864 |
for (k = msum->ncomp; k--; )
|
| 865 |
ds[k] += sf[k] * da[k];
|
| 866 |
}
|
| 867 |
if (mysf)
|
| 868 |
free(mysf);
|
| 869 |
return(1);
|
| 870 |
}
|
| 871 |
|
| 872 |
/* Scale the given matrix by the indicated scalar component vector */
|
| 873 |
int
|
| 874 |
rmx_scale(RMATRIX *rm, const double sf[])
|
| 875 |
{
|
| 876 |
int i, j, k;
|
| 877 |
|
| 878 |
if (!rm | !sf || !rm->mtx)
|
| 879 |
return(0);
|
| 880 |
for (i = rm->nrows; i--; )
|
| 881 |
for (j = rm->ncols; j--; ) {
|
| 882 |
double *dp = rmx_lval(rm,i,j);
|
| 883 |
for (k = rm->ncomp; k--; )
|
| 884 |
dp[k] *= sf[k];
|
| 885 |
}
|
| 886 |
if (rm->info)
|
| 887 |
rmx_addinfo(rm, "Applied scalar\n");
|
| 888 |
/* XXX: should record as exposure for COLR and SCOLR types? */
|
| 889 |
return(1);
|
| 890 |
}
|
| 891 |
|
| 892 |
/* Allocate new matrix and apply component transformation */
|
| 893 |
RMATRIX *
|
| 894 |
rmx_transform(const RMATRIX *msrc, int n, const double cmat[])
|
| 895 |
{
|
| 896 |
int i, j, ks, kd;
|
| 897 |
RMATRIX *dnew;
|
| 898 |
|
| 899 |
if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
|
| 900 |
return(NULL);
|
| 901 |
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
|
| 902 |
if (!dnew)
|
| 903 |
return(NULL);
|
| 904 |
if (msrc->info) {
|
| 905 |
char buf[128];
|
| 906 |
sprintf(buf, "Applied %dx%d component transform\n",
|
| 907 |
dnew->ncomp, msrc->ncomp);
|
| 908 |
rmx_addinfo(dnew, msrc->info);
|
| 909 |
rmx_addinfo(dnew, buf);
|
| 910 |
}
|
| 911 |
dnew->dtype = msrc->dtype;
|
| 912 |
for (i = dnew->nrows; i--; )
|
| 913 |
for (j = dnew->ncols; j--; ) {
|
| 914 |
const double *ds = rmx_val(msrc,i,j);
|
| 915 |
for (kd = dnew->ncomp; kd--; ) {
|
| 916 |
double d = 0;
|
| 917 |
for (ks = msrc->ncomp; ks--; )
|
| 918 |
d += cmat[kd*msrc->ncomp + ks] * ds[ks];
|
| 919 |
rmx_lval(dnew,i,j)[kd] = d;
|
| 920 |
}
|
| 921 |
}
|
| 922 |
return(dnew);
|
| 923 |
}
|
| 924 |
|
| 925 |
/* Convert a color matrix to newly allocated RMATRIX buffer */
|
| 926 |
RMATRIX *
|
| 927 |
rmx_from_cmatrix(const CMATRIX *cm)
|
| 928 |
{
|
| 929 |
int i, j;
|
| 930 |
RMATRIX *dnew;
|
| 931 |
|
| 932 |
if (!cm)
|
| 933 |
return(NULL);
|
| 934 |
dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
|
| 935 |
if (!dnew)
|
| 936 |
return(NULL);
|
| 937 |
dnew->dtype = DTfloat;
|
| 938 |
for (i = dnew->nrows; i--; )
|
| 939 |
for (j = dnew->ncols; j--; ) {
|
| 940 |
const COLORV *cv = cm_lval(cm,i,j);
|
| 941 |
double *dp = rmx_lval(dnew,i,j);
|
| 942 |
dp[0] = cv[0];
|
| 943 |
dp[1] = cv[1];
|
| 944 |
dp[2] = cv[2];
|
| 945 |
}
|
| 946 |
return(dnew);
|
| 947 |
}
|
| 948 |
|
| 949 |
/* Convert general matrix to newly allocated CMATRIX buffer */
|
| 950 |
CMATRIX *
|
| 951 |
cm_from_rmatrix(const RMATRIX *rm)
|
| 952 |
{
|
| 953 |
int i, j;
|
| 954 |
CMATRIX *cnew;
|
| 955 |
|
| 956 |
if (!rm || !rm->mtx | (rm->ncomp == 2))
|
| 957 |
return(NULL);
|
| 958 |
cnew = cm_alloc(rm->nrows, rm->ncols);
|
| 959 |
if (!cnew)
|
| 960 |
return(NULL);
|
| 961 |
for (i = cnew->nrows; i--; )
|
| 962 |
for (j = cnew->ncols; j--; ) {
|
| 963 |
const double *dp = rmx_val(rm,i,j);
|
| 964 |
COLORV *cv = cm_lval(cnew,i,j);
|
| 965 |
switch (rm->ncomp) {
|
| 966 |
case 3:
|
| 967 |
setcolor(cv, dp[0], dp[1], dp[2]);
|
| 968 |
break;
|
| 969 |
case 1:
|
| 970 |
setcolor(cv, dp[0], dp[0], dp[0]);
|
| 971 |
break;
|
| 972 |
default: {
|
| 973 |
SCOLOR scol;
|
| 974 |
int k;
|
| 975 |
for (k = rm->ncomp; k--; )
|
| 976 |
scol[k] = dp[k];
|
| 977 |
scolor2color(cv, scol, rm->ncomp, rm->wlpart);
|
| 978 |
} break;
|
| 979 |
}
|
| 980 |
}
|
| 981 |
return(cnew);
|
| 982 |
}
|