1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Convert between RMATRIX and CMATRIX types |
6 |
* |
7 |
* Segregated because it depends on both sublibraries |
8 |
*/ |
9 |
|
10 |
#include "rmatrix.h" |
11 |
|
12 |
/* Convert a color matrix to newly allocated RMATRIX buffer */ |
13 |
RMATRIX * |
14 |
rmx_from_cmatrix(const CMATRIX *cm) |
15 |
{ |
16 |
RMATRIX *dnew; |
17 |
|
18 |
if (!cm) |
19 |
return(NULL); |
20 |
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
21 |
if (!dnew) |
22 |
return(NULL); |
23 |
|
24 |
dnew->dtype = sizeof(COLORV)==sizeof(float) ? |
25 |
DTfloat : DTdouble; |
26 |
|
27 |
if (sizeof(COLORV) == sizeof(rmx_dtype)) { |
28 |
memcpy(dnew->mtx, cm->cmem, rmx_array_size(dnew)); |
29 |
} else { |
30 |
int i, j; |
31 |
for (i = dnew->nrows; i--; ) |
32 |
for (j = dnew->ncols; j--; ) { |
33 |
const COLORV *cv = cm_lval(cm,i,j); |
34 |
rmx_dtype *dp = rmx_lval(dnew,i,j); |
35 |
dp[0] = cv[0]; |
36 |
dp[1] = cv[1]; |
37 |
dp[2] = cv[2]; |
38 |
} |
39 |
} |
40 |
return(dnew); |
41 |
} |
42 |
|
43 |
/* Convert general matrix to newly allocated CMATRIX buffer */ |
44 |
CMATRIX * |
45 |
cm_from_rmatrix(const RMATRIX *rm) |
46 |
{ |
47 |
CMATRIX *cnew; |
48 |
|
49 |
if (!rm || !rm->mtx | (rm->ncomp == 2) | (rm->ncomp > MAXCOMP)) |
50 |
return(NULL); |
51 |
cnew = cm_alloc(rm->nrows, rm->ncols); |
52 |
if (!cnew) |
53 |
return(NULL); |
54 |
if ((sizeof(COLORV) == sizeof(rmx_dtype)) & (rm->ncomp == 3)) { |
55 |
memcpy(cnew->cmem, rm->mtx, rmx_array_size(rm)); |
56 |
} else { |
57 |
int i, j; |
58 |
for (i = cnew->nrows; i--; ) |
59 |
for (j = cnew->ncols; j--; ) { |
60 |
const rmx_dtype *dp = rmx_val(rm,i,j); |
61 |
COLORV *cv = cm_lval(cnew,i,j); |
62 |
switch (rm->ncomp) { |
63 |
case 3: |
64 |
setcolor(cv, dp[0], dp[1], dp[2]); |
65 |
break; |
66 |
case 1: |
67 |
setcolor(cv, dp[0], dp[0], dp[0]); |
68 |
break; |
69 |
default: |
70 |
if (sizeof(COLORV) == sizeof(rmx_dtype)) { |
71 |
scolor2color(cv, (const COLORV *)dp, |
72 |
rm->ncomp, rm->wlpart); |
73 |
} else { |
74 |
COLORV scol[MAXCOMP]; |
75 |
int k = rm->ncomp; |
76 |
while (k--) scol[k] = dp[k]; |
77 |
scolor2color(cv, scol, rm->ncomp, rm->wlpart); |
78 |
} |
79 |
break; |
80 |
} |
81 |
} |
82 |
} |
83 |
return(cnew); |
84 |
} |