| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: rmatrix.c,v 2.47 2021/01/22 16:19:15 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 |
|
| 16 |
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
| 17 |
|
| 18 |
/* Allocate a nr x nc matrix with n components */ |
| 19 |
RMATRIX * |
| 20 |
rmx_alloc(int nr, int nc, int n) |
| 21 |
{ |
| 22 |
RMATRIX *dnew; |
| 23 |
|
| 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); |
| 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 |
} |
| 36 |
|
| 37 |
/* Free a RMATRIX array */ |
| 38 |
void |
| 39 |
rmx_free(RMATRIX *rm) |
| 40 |
{ |
| 41 |
if (!rm) return; |
| 42 |
if (rm->info) |
| 43 |
free(rm->info); |
| 44 |
free(rm); |
| 45 |
} |
| 46 |
|
| 47 |
/* Resolve data type based on two input types (returns 0 for mismatch) */ |
| 48 |
int |
| 49 |
rmx_newtype(int dtyp1, int dtyp2) |
| 50 |
{ |
| 51 |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
| 52 |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
| 53 |
&& dtyp1 != dtyp2) |
| 54 |
return(0); |
| 55 |
if (dtyp1 < dtyp2) |
| 56 |
return(dtyp1); |
| 57 |
return(dtyp2); |
| 58 |
} |
| 59 |
|
| 60 |
/* Append header information associated with matrix data */ |
| 61 |
int |
| 62 |
rmx_addinfo(RMATRIX *rm, const char *info) |
| 63 |
{ |
| 64 |
if (!rm || !info || !*info) |
| 65 |
return(0); |
| 66 |
if (!rm->info) { |
| 67 |
rm->info = (char *)malloc(strlen(info)+1); |
| 68 |
if (rm->info) rm->info[0] = '\0'; |
| 69 |
} else |
| 70 |
rm->info = (char *)realloc(rm->info, |
| 71 |
strlen(rm->info)+strlen(info)+1); |
| 72 |
if (!rm->info) |
| 73 |
return(0); |
| 74 |
strcat(rm->info, info); |
| 75 |
return(1); |
| 76 |
} |
| 77 |
|
| 78 |
static int |
| 79 |
get_dminfo(char *s, void *p) |
| 80 |
{ |
| 81 |
RMATRIX *ip = (RMATRIX *)p; |
| 82 |
char fmt[MAXFMTLEN]; |
| 83 |
int i; |
| 84 |
|
| 85 |
if (headidval(fmt, s)) |
| 86 |
return(0); |
| 87 |
if (!strncmp(s, "NCOMP=", 6)) { |
| 88 |
ip->ncomp = atoi(s+6); |
| 89 |
return(0); |
| 90 |
} |
| 91 |
if (!strncmp(s, "NROWS=", 6)) { |
| 92 |
ip->nrows = atoi(s+6); |
| 93 |
return(0); |
| 94 |
} |
| 95 |
if (!strncmp(s, "NCOLS=", 6)) { |
| 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); |
| 117 |
} |
| 118 |
for (i = 1; i < DTend; i++) |
| 119 |
if (!strcmp(fmt, cm_fmt_id[i])) { |
| 120 |
ip->dtype = i; |
| 121 |
return(0); |
| 122 |
} |
| 123 |
return(-1); |
| 124 |
} |
| 125 |
|
| 126 |
static int |
| 127 |
rmx_load_ascii(RMATRIX *rm, FILE *fp) |
| 128 |
{ |
| 129 |
int i, j, k; |
| 130 |
|
| 131 |
for (i = 0; i < rm->nrows; i++) |
| 132 |
for (j = 0; j < rm->ncols; j++) |
| 133 |
for (k = 0; k < rm->ncomp; k++) |
| 134 |
if (fscanf(fp, "%lf", &rmx_lval(rm,i,j,k)) != 1) |
| 135 |
return(0); |
| 136 |
return(1); |
| 137 |
} |
| 138 |
|
| 139 |
static int |
| 140 |
rmx_load_float(RMATRIX *rm, FILE *fp) |
| 141 |
{ |
| 142 |
int i, j, k; |
| 143 |
float val[100]; |
| 144 |
|
| 145 |
if (rm->ncomp > 100) { |
| 146 |
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
| 147 |
exit(1); |
| 148 |
} |
| 149 |
for (i = 0; i < rm->nrows; i++) |
| 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 |
} |
| 158 |
return(1); |
| 159 |
} |
| 160 |
|
| 161 |
static int |
| 162 |
rmx_load_double(RMATRIX *rm, FILE *fp) |
| 163 |
{ |
| 164 |
int i; |
| 165 |
|
| 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 |
|
| 181 |
static int |
| 182 |
rmx_load_rgbe(RMATRIX *rm, FILE *fp) |
| 183 |
{ |
| 184 |
COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols); |
| 185 |
int i, j; |
| 186 |
|
| 187 |
if (!scan) |
| 188 |
return(0); |
| 189 |
for (i = 0; i < rm->nrows; i++) { |
| 190 |
if (freadscan(scan, rm->ncols, fp) < 0) { |
| 191 |
free(scan); |
| 192 |
return(0); |
| 193 |
} |
| 194 |
for (j = rm->ncols; j--; ) { |
| 195 |
rmx_lval(rm,i,j,0) = colval(scan[j],RED); |
| 196 |
rmx_lval(rm,i,j,1) = colval(scan[j],GRN); |
| 197 |
rmx_lval(rm,i,j,2) = colval(scan[j],BLU); |
| 198 |
} |
| 199 |
} |
| 200 |
free(scan); |
| 201 |
return(1); |
| 202 |
} |
| 203 |
|
| 204 |
/* Load matrix from supported file type */ |
| 205 |
RMATRIX * |
| 206 |
rmx_load(const char *inspec, RMPref rmp) |
| 207 |
{ |
| 208 |
FILE *fp; |
| 209 |
RMATRIX dinfo; |
| 210 |
RMATRIX *dnew; |
| 211 |
|
| 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); |
| 221 |
} else { |
| 222 |
const char *sp = inspec; /* check suffix */ |
| 223 |
while (*sp) |
| 224 |
++sp; |
| 225 |
while (sp > inspec && sp[-1] != '.') |
| 226 |
--sp; |
| 227 |
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
| 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); |
| 233 |
cm_free(cm); |
| 234 |
dnew->dtype = DTascii; |
| 235 |
return(dnew); |
| 236 |
} |
| 237 |
/* else open it ourselves */ |
| 238 |
if (!(fp = fopen(inspec, "r"))) |
| 239 |
return(NULL); |
| 240 |
} |
| 241 |
SET_FILE_BINARY(fp); |
| 242 |
#ifdef getc_unlocked |
| 243 |
flockfile(fp); |
| 244 |
#endif |
| 245 |
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
| 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); |
| 253 |
} |
| 254 |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
| 255 |
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
| 256 |
fclose(fp); |
| 257 |
return(NULL); |
| 258 |
} |
| 259 |
if (dinfo.ncomp <= 0) |
| 260 |
dinfo.ncomp = 3; |
| 261 |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
| 262 |
dinfo.ncomp != 3) { |
| 263 |
fclose(fp); |
| 264 |
return(NULL); |
| 265 |
} |
| 266 |
} |
| 267 |
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
| 268 |
if (!dnew) { |
| 269 |
fclose(fp); |
| 270 |
return(NULL); |
| 271 |
} |
| 272 |
dnew->info = dinfo.info; |
| 273 |
switch (dinfo.dtype) { |
| 274 |
case DTascii: |
| 275 |
SET_FILE_TEXT(fp); |
| 276 |
if (!rmx_load_ascii(dnew, fp)) |
| 277 |
goto loaderr; |
| 278 |
dnew->dtype = DTascii; /* should leave double? */ |
| 279 |
break; |
| 280 |
case DTfloat: |
| 281 |
dnew->swapin = dinfo.swapin; |
| 282 |
if (!rmx_load_float(dnew, fp)) |
| 283 |
goto loaderr; |
| 284 |
dnew->dtype = DTfloat; |
| 285 |
break; |
| 286 |
case DTdouble: |
| 287 |
dnew->swapin = dinfo.swapin; |
| 288 |
if (!rmx_load_double(dnew, fp)) |
| 289 |
goto loaderr; |
| 290 |
dnew->dtype = DTdouble; |
| 291 |
break; |
| 292 |
case DTrgbe: |
| 293 |
case DTxyze: |
| 294 |
if (!rmx_load_rgbe(dnew, fp)) |
| 295 |
goto loaderr; |
| 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; |
| 307 |
} |
| 308 |
if (fp != stdin) { |
| 309 |
if (inspec[0] == '!') |
| 310 |
pclose(fp); |
| 311 |
else |
| 312 |
fclose(fp); |
| 313 |
} |
| 314 |
#ifdef getc_unlocked |
| 315 |
else |
| 316 |
funlockfile(fp); |
| 317 |
#endif |
| 318 |
return(dnew); |
| 319 |
loaderr: /* should report error? */ |
| 320 |
if (inspec[0] == '!') |
| 321 |
pclose(fp); |
| 322 |
else |
| 323 |
fclose(fp); |
| 324 |
rmx_free(dnew); |
| 325 |
return(NULL); |
| 326 |
} |
| 327 |
|
| 328 |
static int |
| 329 |
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
| 330 |
{ |
| 331 |
const char *fmt = (rm->dtype == DTfloat) ? " %.7e" : |
| 332 |
(rm->dtype == DTrgbe) | (rm->dtype == DTxyze) ? " %.3e" : |
| 333 |
" %.15e" ; |
| 334 |
int i, j, k; |
| 335 |
|
| 336 |
for (i = 0; i < rm->nrows; i++) { |
| 337 |
for (j = 0; j < rm->ncols; j++) { |
| 338 |
for (k = 0; k < rm->ncomp; k++) |
| 339 |
fprintf(fp, fmt, rmx_lval(rm,i,j,k)); |
| 340 |
fputc('\t', fp); |
| 341 |
} |
| 342 |
fputc('\n', fp); |
| 343 |
} |
| 344 |
return(1); |
| 345 |
} |
| 346 |
|
| 347 |
static int |
| 348 |
rmx_write_float(const RMATRIX *rm, FILE *fp) |
| 349 |
{ |
| 350 |
int i, j, k; |
| 351 |
float val[100]; |
| 352 |
|
| 353 |
if (rm->ncomp > 100) { |
| 354 |
fputs("Unsupported # components in rmx_write_float()\n", stderr); |
| 355 |
exit(1); |
| 356 |
} |
| 357 |
for (i = 0; i < rm->nrows; i++) |
| 358 |
for (j = 0; j < rm->ncols; j++) { |
| 359 |
for (k = rm->ncomp; k--; ) |
| 360 |
val[k] = (float)rmx_lval(rm,i,j,k); |
| 361 |
if (putbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
| 362 |
return(0); |
| 363 |
} |
| 364 |
return(1); |
| 365 |
} |
| 366 |
|
| 367 |
static int |
| 368 |
rmx_write_double(const RMATRIX *rm, FILE *fp) |
| 369 |
{ |
| 370 |
int i, j; |
| 371 |
|
| 372 |
for (i = 0; i < rm->nrows; i++) |
| 373 |
for (j = 0; j < rm->ncols; j++) |
| 374 |
if (putbinary(&rmx_lval(rm,i,j,0), sizeof(double), rm->ncomp, fp) != rm->ncomp) |
| 375 |
return(0); |
| 376 |
return(1); |
| 377 |
} |
| 378 |
|
| 379 |
static int |
| 380 |
rmx_write_rgbe(const RMATRIX *rm, FILE *fp) |
| 381 |
{ |
| 382 |
COLR *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols); |
| 383 |
int i, j; |
| 384 |
|
| 385 |
if (!scan) |
| 386 |
return(0); |
| 387 |
for (i = 0; i < rm->nrows; i++) { |
| 388 |
for (j = rm->ncols; j--; ) |
| 389 |
setcolr(scan[j], rmx_lval(rm,i,j,0), |
| 390 |
rmx_lval(rm,i,j,1), |
| 391 |
rmx_lval(rm,i,j,2) ); |
| 392 |
if (fwritecolrs(scan, rm->ncols, fp) < 0) { |
| 393 |
free(scan); |
| 394 |
return(0); |
| 395 |
} |
| 396 |
} |
| 397 |
free(scan); |
| 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) |
| 421 |
{ |
| 422 |
RMATRIX *mydm = NULL; |
| 423 |
int ok = 1; |
| 424 |
|
| 425 |
if (!rm | !fp) |
| 426 |
return(0); |
| 427 |
#ifdef getc_unlocked |
| 428 |
flockfile(fp); |
| 429 |
#endif |
| 430 |
/* complete header */ |
| 431 |
if (rm->info) |
| 432 |
fputs(rm->info, fp); |
| 433 |
if (dtype == DTfromHeader) |
| 434 |
dtype = rm->dtype; |
| 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; |
| 440 |
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
| 441 |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
| 442 |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
| 443 |
fprintf(fp, "NCOMP=%d\n", rm->ncomp); |
| 444 |
} else if (rm->ncomp != 3) { /* wrong # components? */ |
| 445 |
double cmtx[3]; |
| 446 |
if (rm->ncomp != 1) /* only convert grayscale */ |
| 447 |
return(0); |
| 448 |
cmtx[0] = cmtx[1] = cmtx[2] = 1; |
| 449 |
mydm = rmx_transform(rm, 3, cmtx); |
| 450 |
if (!mydm) |
| 451 |
return(0); |
| 452 |
rm = mydm; |
| 453 |
} |
| 454 |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
| 455 |
fputendian(fp); /* important to record */ |
| 456 |
fputformat((char *)cm_fmt_id[dtype], fp); |
| 457 |
fputc('\n', fp); |
| 458 |
switch (dtype) { /* write data */ |
| 459 |
case DTascii: |
| 460 |
ok = rmx_write_ascii(rm, fp); |
| 461 |
break; |
| 462 |
case DTfloat: |
| 463 |
ok = rmx_write_float(rm, fp); |
| 464 |
break; |
| 465 |
case DTdouble: |
| 466 |
ok = rmx_write_double(rm, fp); |
| 467 |
break; |
| 468 |
case DTrgbe: |
| 469 |
case DTxyze: |
| 470 |
fprtresolu(rm->ncols, rm->nrows, fp); |
| 471 |
ok = rmx_write_rgbe(rm, fp); |
| 472 |
break; |
| 473 |
default: |
| 474 |
return(0); |
| 475 |
} |
| 476 |
ok &= (fflush(fp) == 0); |
| 477 |
#ifdef getc_unlocked |
| 478 |
funlockfile(fp); |
| 479 |
#endif |
| 480 |
if (mydm) |
| 481 |
rmx_free(mydm); |
| 482 |
return(ok); |
| 483 |
} |
| 484 |
|
| 485 |
/* Allocate and assign square identity matrix with n components */ |
| 486 |
RMATRIX * |
| 487 |
rmx_identity(const int dim, const int n) |
| 488 |
{ |
| 489 |
RMATRIX *rid = rmx_alloc(dim, dim, n); |
| 490 |
int i, k; |
| 491 |
|
| 492 |
if (!rid) |
| 493 |
return(NULL); |
| 494 |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
| 495 |
for (i = dim; i--; ) |
| 496 |
for (k = n; k--; ) |
| 497 |
rmx_lval(rid,i,i,k) = 1; |
| 498 |
return(rid); |
| 499 |
} |
| 500 |
|
| 501 |
/* Duplicate the given matrix */ |
| 502 |
RMATRIX * |
| 503 |
rmx_copy(const RMATRIX *rm) |
| 504 |
{ |
| 505 |
RMATRIX *dnew; |
| 506 |
|
| 507 |
if (!rm) |
| 508 |
return(NULL); |
| 509 |
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
| 510 |
if (!dnew) |
| 511 |
return(NULL); |
| 512 |
rmx_addinfo(dnew, rm->info); |
| 513 |
dnew->dtype = rm->dtype; |
| 514 |
memcpy(dnew->mtx, rm->mtx, |
| 515 |
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
| 516 |
return(dnew); |
| 517 |
} |
| 518 |
|
| 519 |
/* Allocate and assign transposed matrix */ |
| 520 |
RMATRIX * |
| 521 |
rmx_transpose(const RMATRIX *rm) |
| 522 |
{ |
| 523 |
RMATRIX *dnew; |
| 524 |
int i, j, k; |
| 525 |
|
| 526 |
if (!rm) |
| 527 |
return(0); |
| 528 |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
| 529 |
dnew = rmx_copy(rm); |
| 530 |
if (!dnew) |
| 531 |
return(NULL); |
| 532 |
dnew->nrows = rm->ncols; |
| 533 |
dnew->ncols = rm->nrows; |
| 534 |
return(dnew); |
| 535 |
} |
| 536 |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
| 537 |
if (!dnew) |
| 538 |
return(NULL); |
| 539 |
if (rm->info) { |
| 540 |
rmx_addinfo(dnew, rm->info); |
| 541 |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
| 542 |
} |
| 543 |
dnew->dtype = rm->dtype; |
| 544 |
for (i = dnew->nrows; i--; ) |
| 545 |
for (j = dnew->ncols; j--; ) |
| 546 |
for (k = dnew->ncomp; k--; ) |
| 547 |
rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k); |
| 548 |
return(dnew); |
| 549 |
} |
| 550 |
|
| 551 |
/* Multiply (concatenate) two matrices and allocate the result */ |
| 552 |
RMATRIX * |
| 553 |
rmx_multiply(const RMATRIX *m1, const RMATRIX *m2) |
| 554 |
{ |
| 555 |
RMATRIX *mres; |
| 556 |
int i, j, k, h; |
| 557 |
|
| 558 |
if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
| 559 |
return(NULL); |
| 560 |
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
| 561 |
if (!mres) |
| 562 |
return(NULL); |
| 563 |
i = rmx_newtype(m1->dtype, m2->dtype); |
| 564 |
if (i) |
| 565 |
mres->dtype = i; |
| 566 |
else |
| 567 |
rmx_addinfo(mres, rmx_mismatch_warn); |
| 568 |
for (i = mres->nrows; i--; ) |
| 569 |
for (j = mres->ncols; j--; ) |
| 570 |
for (k = mres->ncomp; k--; ) { |
| 571 |
long double d = 0; |
| 572 |
for (h = m1->ncols; h--; ) |
| 573 |
d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k); |
| 574 |
rmx_lval(mres,i,j,k) = (double)d; |
| 575 |
} |
| 576 |
return(mres); |
| 577 |
} |
| 578 |
|
| 579 |
/* Element-wise multiplication (or division) of m2 into m1 */ |
| 580 |
int |
| 581 |
rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide) |
| 582 |
{ |
| 583 |
int zeroDivides = 0; |
| 584 |
int i, j, k; |
| 585 |
|
| 586 |
if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
| 587 |
return(0); |
| 588 |
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp)) |
| 589 |
return(0); |
| 590 |
i = rmx_newtype(m1->dtype, m2->dtype); |
| 591 |
if (i) |
| 592 |
m1->dtype = i; |
| 593 |
else |
| 594 |
rmx_addinfo(m1, rmx_mismatch_warn); |
| 595 |
for (i = m1->nrows; i--; ) |
| 596 |
for (j = m1->ncols; j--; ) |
| 597 |
if (divide) { |
| 598 |
double d; |
| 599 |
if (m2->ncomp == 1) { |
| 600 |
d = rmx_lval(m2,i,j,0); |
| 601 |
if (d == 0) { |
| 602 |
++zeroDivides; |
| 603 |
for (k = m1->ncomp; k--; ) |
| 604 |
rmx_lval(m1,i,j,k) = 0; |
| 605 |
} else { |
| 606 |
d = 1./d; |
| 607 |
for (k = m1->ncomp; k--; ) |
| 608 |
rmx_lval(m1,i,j,k) *= d; |
| 609 |
} |
| 610 |
} else |
| 611 |
for (k = m1->ncomp; k--; ) { |
| 612 |
d = rmx_lval(m2,i,j,k); |
| 613 |
if (d == 0) { |
| 614 |
++zeroDivides; |
| 615 |
rmx_lval(m1,i,j,k) = 0; |
| 616 |
} else |
| 617 |
rmx_lval(m1,i,j,k) /= d; |
| 618 |
} |
| 619 |
} else { |
| 620 |
if (m2->ncomp == 1) { |
| 621 |
const double d = rmx_lval(m2,i,j,0); |
| 622 |
for (k = m1->ncomp; k--; ) |
| 623 |
rmx_lval(m1,i,j,k) *= d; |
| 624 |
} else |
| 625 |
for (k = m1->ncomp; k--; ) |
| 626 |
rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k); |
| 627 |
} |
| 628 |
if (zeroDivides) { |
| 629 |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
| 630 |
errno = ERANGE; |
| 631 |
} |
| 632 |
return(1); |
| 633 |
} |
| 634 |
|
| 635 |
/* Sum second matrix into first, applying scale factor beforehand */ |
| 636 |
int |
| 637 |
rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]) |
| 638 |
{ |
| 639 |
double *mysf = NULL; |
| 640 |
int i, j, k; |
| 641 |
|
| 642 |
if (!msum | !madd || |
| 643 |
(msum->nrows != madd->nrows) | |
| 644 |
(msum->ncols != madd->ncols) | |
| 645 |
(msum->ncomp != madd->ncomp)) |
| 646 |
return(0); |
| 647 |
if (!sf) { |
| 648 |
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
| 649 |
if (!mysf) |
| 650 |
return(0); |
| 651 |
for (k = msum->ncomp; k--; ) |
| 652 |
mysf[k] = 1; |
| 653 |
sf = mysf; |
| 654 |
} |
| 655 |
i = rmx_newtype(msum->dtype, madd->dtype); |
| 656 |
if (i) |
| 657 |
msum->dtype = i; |
| 658 |
else |
| 659 |
rmx_addinfo(msum, rmx_mismatch_warn); |
| 660 |
for (i = msum->nrows; i--; ) |
| 661 |
for (j = msum->ncols; j--; ) |
| 662 |
for (k = msum->ncomp; k--; ) |
| 663 |
rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k); |
| 664 |
if (mysf) |
| 665 |
free(mysf); |
| 666 |
return(1); |
| 667 |
} |
| 668 |
|
| 669 |
/* Scale the given matrix by the indicated scalar component vector */ |
| 670 |
int |
| 671 |
rmx_scale(RMATRIX *rm, const double sf[]) |
| 672 |
{ |
| 673 |
int i, j, k; |
| 674 |
|
| 675 |
if (!rm | !sf) |
| 676 |
return(0); |
| 677 |
for (i = rm->nrows; i--; ) |
| 678 |
for (j = rm->ncols; j--; ) |
| 679 |
for (k = rm->ncomp; k--; ) |
| 680 |
rmx_lval(rm,i,j,k) *= sf[k]; |
| 681 |
|
| 682 |
if (rm->info) |
| 683 |
rmx_addinfo(rm, "Applied scalar\n"); |
| 684 |
return(1); |
| 685 |
} |
| 686 |
|
| 687 |
/* Allocate new matrix and apply component transformation */ |
| 688 |
RMATRIX * |
| 689 |
rmx_transform(const RMATRIX *msrc, int n, const double cmat[]) |
| 690 |
{ |
| 691 |
int i, j, ks, kd; |
| 692 |
RMATRIX *dnew; |
| 693 |
|
| 694 |
if (!msrc | (n <= 0) | !cmat) |
| 695 |
return(NULL); |
| 696 |
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
| 697 |
if (!dnew) |
| 698 |
return(NULL); |
| 699 |
if (msrc->info) { |
| 700 |
char buf[128]; |
| 701 |
sprintf(buf, "Applied %dx%d component transform\n", |
| 702 |
dnew->ncomp, msrc->ncomp); |
| 703 |
rmx_addinfo(dnew, msrc->info); |
| 704 |
rmx_addinfo(dnew, buf); |
| 705 |
} |
| 706 |
dnew->dtype = msrc->dtype; |
| 707 |
for (i = dnew->nrows; i--; ) |
| 708 |
for (j = dnew->ncols; j--; ) |
| 709 |
for (kd = dnew->ncomp; kd--; ) { |
| 710 |
double d = 0; |
| 711 |
for (ks = msrc->ncomp; ks--; ) |
| 712 |
d += cmat[kd*msrc->ncomp + ks] * rmx_lval(msrc,i,j,ks); |
| 713 |
rmx_lval(dnew,i,j,kd) = d; |
| 714 |
} |
| 715 |
return(dnew); |
| 716 |
} |
| 717 |
|
| 718 |
/* Convert a color matrix to newly allocated RMATRIX buffer */ |
| 719 |
RMATRIX * |
| 720 |
rmx_from_cmatrix(const CMATRIX *cm) |
| 721 |
{ |
| 722 |
int i, j; |
| 723 |
RMATRIX *dnew; |
| 724 |
|
| 725 |
if (!cm) |
| 726 |
return(NULL); |
| 727 |
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
| 728 |
if (!dnew) |
| 729 |
return(NULL); |
| 730 |
dnew->dtype = DTfloat; |
| 731 |
for (i = dnew->nrows; i--; ) |
| 732 |
for (j = dnew->ncols; j--; ) { |
| 733 |
const COLORV *cv = cm_lval(cm,i,j); |
| 734 |
rmx_lval(dnew,i,j,0) = cv[0]; |
| 735 |
rmx_lval(dnew,i,j,1) = cv[1]; |
| 736 |
rmx_lval(dnew,i,j,2) = cv[2]; |
| 737 |
} |
| 738 |
return(dnew); |
| 739 |
} |
| 740 |
|
| 741 |
/* Convert general matrix to newly allocated CMATRIX buffer */ |
| 742 |
CMATRIX * |
| 743 |
cm_from_rmatrix(const RMATRIX *rm) |
| 744 |
{ |
| 745 |
int i, j; |
| 746 |
CMATRIX *cnew; |
| 747 |
|
| 748 |
if (!rm || rm->ncomp != 3) |
| 749 |
return(NULL); |
| 750 |
cnew = cm_alloc(rm->nrows, rm->ncols); |
| 751 |
if (!cnew) |
| 752 |
return(NULL); |
| 753 |
for (i = cnew->nrows; i--; ) |
| 754 |
for (j = cnew->ncols; j--; ) { |
| 755 |
COLORV *cv = cm_lval(cnew,i,j); |
| 756 |
cv[0] = (COLORV)rmx_lval(rm,i,j,0); |
| 757 |
cv[1] = (COLORV)rmx_lval(rm,i,j,1); |
| 758 |
cv[2] = (COLORV)rmx_lval(rm,i,j,2); |
| 759 |
} |
| 760 |
return(cnew); |
| 761 |
} |