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