ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmatrix.c
Revision: 2.47
Committed: Fri Jan 22 16:19:15 2021 UTC (3 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.46: +4 -2 lines
Log Message:
fix: reinstated interpretation of NULL file name as stdin, so dctimestep works

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rmatrix.c,v 2.46 2021/01/19 23:32:00 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 /* Write matrix to file type indicated by dtype */
402 int
403 rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
404 {
405 RMATRIX *mydm = NULL;
406 int ok = 1;
407
408 if (!rm | !fp)
409 return(0);
410 #ifdef getc_unlocked
411 flockfile(fp);
412 #endif
413 /* complete header */
414 if (rm->info)
415 fputs(rm->info, fp);
416 if (dtype == DTfromHeader)
417 dtype = rm->dtype;
418 else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
419 dtype = DTxyze;
420 else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
421 dtype = DTrgbe;
422 if ((dtype != DTrgbe) & (dtype != DTxyze)) {
423 fprintf(fp, "NROWS=%d\n", rm->nrows);
424 fprintf(fp, "NCOLS=%d\n", rm->ncols);
425 fprintf(fp, "NCOMP=%d\n", rm->ncomp);
426 } else if (rm->ncomp != 3) { /* wrong # components? */
427 double cmtx[3];
428 if (rm->ncomp != 1) /* only convert grayscale */
429 return(0);
430 cmtx[0] = cmtx[1] = cmtx[2] = 1;
431 mydm = rmx_transform(rm, 3, cmtx);
432 if (!mydm)
433 return(0);
434 rm = mydm;
435 }
436 if ((dtype == DTfloat) | (dtype == DTdouble))
437 fputendian(fp); /* important to record */
438 fputformat((char *)cm_fmt_id[dtype], fp);
439 fputc('\n', fp);
440 switch (dtype) { /* write data */
441 case DTascii:
442 ok = rmx_write_ascii(rm, fp);
443 break;
444 case DTfloat:
445 ok = rmx_write_float(rm, fp);
446 break;
447 case DTdouble:
448 ok = rmx_write_double(rm, fp);
449 break;
450 case DTrgbe:
451 case DTxyze:
452 fprtresolu(rm->ncols, rm->nrows, fp);
453 ok = rmx_write_rgbe(rm, fp);
454 break;
455 default:
456 return(0);
457 }
458 ok &= (fflush(fp) == 0);
459 #ifdef getc_unlocked
460 funlockfile(fp);
461 #endif
462 if (mydm)
463 rmx_free(mydm);
464 return(ok);
465 }
466
467 /* Allocate and assign square identity matrix with n components */
468 RMATRIX *
469 rmx_identity(const int dim, const int n)
470 {
471 RMATRIX *rid = rmx_alloc(dim, dim, n);
472 int i, k;
473
474 if (!rid)
475 return(NULL);
476 memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
477 for (i = dim; i--; )
478 for (k = n; k--; )
479 rmx_lval(rid,i,i,k) = 1;
480 return(rid);
481 }
482
483 /* Duplicate the given matrix */
484 RMATRIX *
485 rmx_copy(const RMATRIX *rm)
486 {
487 RMATRIX *dnew;
488
489 if (!rm)
490 return(NULL);
491 dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
492 if (!dnew)
493 return(NULL);
494 rmx_addinfo(dnew, rm->info);
495 dnew->dtype = rm->dtype;
496 memcpy(dnew->mtx, rm->mtx,
497 sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols);
498 return(dnew);
499 }
500
501 /* Allocate and assign transposed matrix */
502 RMATRIX *
503 rmx_transpose(const RMATRIX *rm)
504 {
505 RMATRIX *dnew;
506 int i, j, k;
507
508 if (!rm)
509 return(0);
510 if ((rm->nrows == 1) | (rm->ncols == 1)) {
511 dnew = rmx_copy(rm);
512 if (!dnew)
513 return(NULL);
514 dnew->nrows = rm->ncols;
515 dnew->ncols = rm->nrows;
516 return(dnew);
517 }
518 dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
519 if (!dnew)
520 return(NULL);
521 if (rm->info) {
522 rmx_addinfo(dnew, rm->info);
523 rmx_addinfo(dnew, "Transposed rows and columns\n");
524 }
525 dnew->dtype = rm->dtype;
526 for (i = dnew->nrows; i--; )
527 for (j = dnew->ncols; j--; )
528 for (k = dnew->ncomp; k--; )
529 rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k);
530 return(dnew);
531 }
532
533 /* Multiply (concatenate) two matrices and allocate the result */
534 RMATRIX *
535 rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
536 {
537 RMATRIX *mres;
538 int i, j, k, h;
539
540 if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
541 return(NULL);
542 mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
543 if (!mres)
544 return(NULL);
545 i = rmx_newtype(m1->dtype, m2->dtype);
546 if (i)
547 mres->dtype = i;
548 else
549 rmx_addinfo(mres, rmx_mismatch_warn);
550 for (i = mres->nrows; i--; )
551 for (j = mres->ncols; j--; )
552 for (k = mres->ncomp; k--; ) {
553 long double d = 0;
554 for (h = m1->ncols; h--; )
555 d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
556 rmx_lval(mres,i,j,k) = (double)d;
557 }
558 return(mres);
559 }
560
561 /* Element-wise multiplication (or division) of m2 into m1 */
562 int
563 rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide)
564 {
565 int zeroDivides = 0;
566 int i, j, k;
567
568 if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
569 return(0);
570 if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
571 return(0);
572 i = rmx_newtype(m1->dtype, m2->dtype);
573 if (i)
574 m1->dtype = i;
575 else
576 rmx_addinfo(m1, rmx_mismatch_warn);
577 for (i = m1->nrows; i--; )
578 for (j = m1->ncols; j--; )
579 if (divide) {
580 double d;
581 if (m2->ncomp == 1) {
582 d = rmx_lval(m2,i,j,0);
583 if (d == 0) {
584 ++zeroDivides;
585 for (k = m1->ncomp; k--; )
586 rmx_lval(m1,i,j,k) = 0;
587 } else {
588 d = 1./d;
589 for (k = m1->ncomp; k--; )
590 rmx_lval(m1,i,j,k) *= d;
591 }
592 } else
593 for (k = m1->ncomp; k--; ) {
594 d = rmx_lval(m2,i,j,k);
595 if (d == 0) {
596 ++zeroDivides;
597 rmx_lval(m1,i,j,k) = 0;
598 } else
599 rmx_lval(m1,i,j,k) /= d;
600 }
601 } else {
602 if (m2->ncomp == 1) {
603 const double d = rmx_lval(m2,i,j,0);
604 for (k = m1->ncomp; k--; )
605 rmx_lval(m1,i,j,k) *= d;
606 } else
607 for (k = m1->ncomp; k--; )
608 rmx_lval(m1,i,j,k) *= rmx_lval(m2,i,j,k);
609 }
610 if (zeroDivides) {
611 rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n");
612 errno = ERANGE;
613 }
614 return(1);
615 }
616
617 /* Sum second matrix into first, applying scale factor beforehand */
618 int
619 rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[])
620 {
621 double *mysf = NULL;
622 int i, j, k;
623
624 if (!msum | !madd ||
625 (msum->nrows != madd->nrows) |
626 (msum->ncols != madd->ncols) |
627 (msum->ncomp != madd->ncomp))
628 return(0);
629 if (!sf) {
630 mysf = (double *)malloc(sizeof(double)*msum->ncomp);
631 if (!mysf)
632 return(0);
633 for (k = msum->ncomp; k--; )
634 mysf[k] = 1;
635 sf = mysf;
636 }
637 i = rmx_newtype(msum->dtype, madd->dtype);
638 if (i)
639 msum->dtype = i;
640 else
641 rmx_addinfo(msum, rmx_mismatch_warn);
642 for (i = msum->nrows; i--; )
643 for (j = msum->ncols; j--; )
644 for (k = msum->ncomp; k--; )
645 rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k);
646 if (mysf)
647 free(mysf);
648 return(1);
649 }
650
651 /* Scale the given matrix by the indicated scalar component vector */
652 int
653 rmx_scale(RMATRIX *rm, const double sf[])
654 {
655 int i, j, k;
656
657 if (!rm | !sf)
658 return(0);
659 for (i = rm->nrows; i--; )
660 for (j = rm->ncols; j--; )
661 for (k = rm->ncomp; k--; )
662 rmx_lval(rm,i,j,k) *= sf[k];
663
664 if (rm->info)
665 rmx_addinfo(rm, "Applied scalar\n");
666 return(1);
667 }
668
669 /* Allocate new matrix and apply component transformation */
670 RMATRIX *
671 rmx_transform(const RMATRIX *msrc, int n, const double cmat[])
672 {
673 int i, j, ks, kd;
674 RMATRIX *dnew;
675
676 if (!msrc | (n <= 0) | !cmat)
677 return(NULL);
678 dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
679 if (!dnew)
680 return(NULL);
681 if (msrc->info) {
682 char buf[128];
683 sprintf(buf, "Applied %dx%d component transform\n",
684 dnew->ncomp, msrc->ncomp);
685 rmx_addinfo(dnew, msrc->info);
686 rmx_addinfo(dnew, buf);
687 }
688 dnew->dtype = msrc->dtype;
689 for (i = dnew->nrows; i--; )
690 for (j = dnew->ncols; j--; )
691 for (kd = dnew->ncomp; kd--; ) {
692 double d = 0;
693 for (ks = msrc->ncomp; ks--; )
694 d += cmat[kd*msrc->ncomp + ks] * rmx_lval(msrc,i,j,ks);
695 rmx_lval(dnew,i,j,kd) = d;
696 }
697 return(dnew);
698 }
699
700 /* Convert a color matrix to newly allocated RMATRIX buffer */
701 RMATRIX *
702 rmx_from_cmatrix(const CMATRIX *cm)
703 {
704 int i, j;
705 RMATRIX *dnew;
706
707 if (!cm)
708 return(NULL);
709 dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
710 if (!dnew)
711 return(NULL);
712 dnew->dtype = DTfloat;
713 for (i = dnew->nrows; i--; )
714 for (j = dnew->ncols; j--; ) {
715 const COLORV *cv = cm_lval(cm,i,j);
716 rmx_lval(dnew,i,j,0) = cv[0];
717 rmx_lval(dnew,i,j,1) = cv[1];
718 rmx_lval(dnew,i,j,2) = cv[2];
719 }
720 return(dnew);
721 }
722
723 /* Convert general matrix to newly allocated CMATRIX buffer */
724 CMATRIX *
725 cm_from_rmatrix(const RMATRIX *rm)
726 {
727 int i, j;
728 CMATRIX *cnew;
729
730 if (!rm || rm->ncomp != 3)
731 return(NULL);
732 cnew = cm_alloc(rm->nrows, rm->ncols);
733 if (!cnew)
734 return(NULL);
735 for (i = cnew->nrows; i--; )
736 for (j = cnew->ncols; j--; ) {
737 COLORV *cv = cm_lval(cnew,i,j);
738 cv[0] = (COLORV)rmx_lval(rm,i,j,0);
739 cv[1] = (COLORV)rmx_lval(rm,i,j,1);
740 cv[2] = (COLORV)rmx_lval(rm,i,j,2);
741 }
742 return(cnew);
743 }