ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
Revision: 2.76
Committed: Tue Dec 12 18:45:53 2023 UTC (4 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.75: +9 -5 lines
Log Message:
fix: made rmx_transfer_data() work when destination is memory-mapped

File Contents

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