convert_matvar_pdl.c

filter: Statementstmt 79.4% 54 / 68 Branchbran 46.8% 15 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.0% 69 / 100 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Matlab.xs ↑ Summary blib/lib/PDL/IO/Matlab.pm »
1 #include <pdl.h>
2  
3 static char *matvar_class_type_desc[16] =
4 {"Undefined","Cell Array","Structure",
5 "Object","Character Array","Sparse Array","Double Precision Array",
6 "Single Precision Array", "8-bit, signed integer array",
7 "8-bit, unsigned integer array","16-bit, signed integer array",
8 "16-bit, unsigned integer array","32-bit, signed integer array",
9 "32-bit, unsigned integer array","64-bit, signed integer array",
10 "64-bit, unsigned integer array"
11 };
12  
13  
14 /* enum matio_classes { */
15 /* MAT_C_EMPTY = 0, /\**< @brief Empty array *\/ */
16 /* MAT_C_CELL = 1, /\**< @brief Matlab cell array class *\/ */
17 /* MAT_C_STRUCT = 2, /\**< @brief Matlab structure class *\/ */
18 /* MAT_C_OBJECT = 3, /\**< @brief Matlab object class *\/ */
19 /* MAT_C_CHAR = 4, /\**< @brief Matlab character array class *\/ */
20 /* MAT_C_SPARSE = 5, /\**< @brief Matlab sparse array class *\/ */
21 /* MAT_C_DOUBLE = 6, /\**< @brief Matlab double-precision class *\/ */
22 /* MAT_C_SINGLE = 7, /\**< @brief Matlab single-precision class *\/ */
23 /* MAT_C_INT8 = 8, /\**< @brief Matlab signed 8-bit integer class *\/ */
24 /* MAT_C_UINT8 = 9, /\**< @brief Matlab unsigned 8-bit integer class *\/ */
25 /* MAT_C_INT16 = 10, /\**< @brief Matlab signed 16-bit integer class *\/ */
26 /* MAT_C_UINT16 = 11, /\**< @brief Matlab unsigned 16-bit integer class *\/ */
27 /* MAT_C_INT32 = 12, /\**< @brief Matlab signed 32-bit integer class *\/ */
28 /* MAT_C_UINT32 = 13, /\**< @brief Matlab unsigned 32-bit integer class *\/ */
29 /* MAT_C_INT64 = 14, /\**< @brief Matlab unsigned 32-bit integer class *\/ */
30 /* MAT_C_UINT64 = 15, /\**< @brief Matlab unsigned 32-bit integer class *\/ */
31 /* MAT_C_FUNCTION = 16 /\**< @brief Matlab unsigned 32-bit integer class *\/ */
32 /* }; */
33  
34  
35 static int matvar_class_to_pdl_type[16] =
36 /* 0 1 2 3 */
37 { -1, -1, -1, -1,
38  
39 /* 4 5 6 7 */
40 -1, -1, PDL_D, PDL_F,
41  
42 /* 8 9 10 11 */
43 PDL_B, PDL_B, PDL_S, PDL_US,
44  
45 /* 12 13 14 15 */
46 PDL_L, -1, -1, -1
47 };
48  
49  
50 static int pdl_type_to_matvar_type[] =
51 {
52 [PDL_B] = MAT_T_INT8,
53 [PDL_S] = MAT_T_INT16,
54 [PDL_US] = MAT_T_UINT16,
55 [PDL_L] = MAT_T_INT32,
56 [PDL_LL] = MAT_T_INT32,
57 [PDL_F] = MAT_T_SINGLE,
58 [PDL_D] = MAT_T_DOUBLE,
59 };
60  
61 static int pdl_type_to_matvar_class[] =
62 {
63 [PDL_B] = MAT_C_INT8,
64 [PDL_S] = MAT_C_INT16,
65 [PDL_US] = MAT_C_UINT16,
66 [PDL_L] = MAT_C_INT32,
67 [PDL_LL] = MAT_C_INT32,
68 [PDL_F] = MAT_C_SINGLE,
69 [PDL_D] = MAT_C_DOUBLE,
70 };
71  
72  
73 21 static void delete_matvar_to_pdl_data(pdl* p, size_t param)
74 {
75 21 ▶ if (p->data)
Branch: 1 branch
BranchTrueFalseTotal
if (p->data) 21 0 21
76 21 free(p->data);
77 21 p->data = 0;
78 21 }
79  
80 typedef void (*DelMagic)(pdl *, size_t param);
81 0 static void default_magic(pdl *p, size_t pa) { p->data = 0; }
82 21 static pdl* my_pdl_wrap(void *data, int datatype, PDL_Indx dims[],
83 int ndims, DelMagic delete_magic, int delparam)
84 {
85 21 pdl* npdl = PDL->pdlnew();
86 21 PDL->setdims(npdl,dims,ndims);
87 21 npdl->datatype = datatype;
88 21 npdl->data = data;
89 21 npdl->state |= PDL_DONTTOUCHDATA | PDL_ALLOCATED;
90 21 ▶ if (delete_magic != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (delete_magic != NULL) 21 0 21
91 21 PDL->add_deletedata_magic(npdl, (void *) delete_magic, delparam);
92 else
93 0 PDL->add_deletedata_magic(npdl, default_magic, 0);
94 21 return npdl;
95 }
96  
97 21 static pdl* matvar_to_pdl (matvar_t * matvar, int onedr) {
98 21 int ndims = matvar->rank;
99 pdl * ndarray;
100 int i, pdl_data_type;
101 PDL_Indx * dims;
102 21 ▶ if ( matvar->isComplex )
Branch: 1 branch
BranchTrueFalseTotal
if ( matvar->isComplex ) 0 21 21
103 0 barf("matvar_to_pdl: Complex matlab variables not supported.");
104 21 dims = (PDL_Indx *)malloc(sizeof(PDL_Indx) * ndims);
105 // fprintf(stderr, "ONEDR %d\n", onedr);
106 21 ▶ if (ndims == 2 && onedr != 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ndims == 2 && onedr != 0 ) { 21 0 21
if (ndims == 2 && onedr != 0 ) { 21 0 21
107 21 ▶ if (matvar->dims[0] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (matvar->dims[0] == 1) { 0 21 21
108 0 ndims = 1;
109 0 dims[0] = matvar->dims[1];
110 }
111 21 ▶ else if (matvar->dims[1] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (matvar->dims[1] == 1) { 21 0 21
112 21 ndims = 1;
113 21 dims[0] = matvar->dims[0];
114 }
115 0 ▶ else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i];
Branch: 1 branch
BranchTrueFalseTotal
else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i]; 0 0 0
116 }
117 0 ▶ else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i];
Branch: 1 branch
BranchTrueFalseTotal
else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i]; 0 0 0
118 21 ▶ if ( 0 > (pdl_data_type = matvar_class_to_pdl_type[matvar->class_type] )) {
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 > (pdl_data_type = matvar_class_to_pdl_type[matvar->class_type] )) { 0 21 21
119 0 fprintf(stderr, "matvar_to_pdl: matlab data class is '%s'\n",matvar_class_type_desc[matvar->class_type]);
120 0 barf("matvar_to_pdl: No pdl data type corresponding to this class type.");}
121 21 ndarray = my_pdl_wrap(matvar->data, pdl_data_type, dims, ndims,
122 delete_matvar_to_pdl_data, 0);
123 21 matvar->mem_conserve = 1; // prevent matio freeing memory for data
124 21 free(dims);
125 21 return ndarray;
126 }
127  
128 25 pdl * convert_next_matvar_to_pdl (mat_t * matfp, matvar_t ** matvar, int onedr) {
129 25 *matvar = Mat_VarReadNext(matfp);
130 25 ▶ if (*matvar == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if (*matvar == NULL ) 4 21 25
131 4 return NULL;
132 21 return matvar_to_pdl(*matvar,onedr); // calling code must call Mat_VarFree(matvar)
133 }
134  
135 /*******************************************************
136 * pdl to matvar
137 *******************************************************/
138  
139 9 matvar_t * pdl_to_matvar (pdl * ndarray, char *varname, int onedw) {
140 9 int ndims = ndarray->ndims;
141 matvar_t *matvar;
142 int i, matvar_class_type, matvar_data_type;
143 9 int opt = MAT_F_DONT_COPY_DATA;
144 size_t * dims;
145 int tmp;
146 9 dims = (size_t *)malloc(sizeof(size_t) * (ndims+1));
147 18 ▶ for(i=0;i<ndims;i++) dims[i] = ndarray->dims[i];
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<ndims;i++) dims[i] = ndarray->dims[i]; 9 9 18
148 9 ▶ if (ndims == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ndims == 1 ) { 9 0 9
149 9 ▶ if ( onedw == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ( onedw == 1) { 9 0 9
150 9 ndims = 2;
151 9 dims[1] = 1;
152 }
153 0 ▶ else if ( onedw == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( onedw == 2) { 0 0 0
154 0 ndims = 2;
155 0 tmp = dims[0];
156 0 dims[0] = 1;
157 0 dims[1] = tmp;
158 }
159 }
160 9 matvar_class_type = pdl_type_to_matvar_class[ndarray->datatype];
161 9 matvar_data_type = pdl_type_to_matvar_type[ndarray->datatype];
162 9 matvar = Mat_VarCreate(varname,matvar_class_type, matvar_data_type,
163 9 ndims, dims, ndarray->data, opt);
164 9 free(dims);
165 9 return matvar;
166 }
167  
168 9 int write_pdl_to_matlab_file (mat_t *mat, pdl *ndarray, char *varname, int onedw,
169 int compress) {
170 matvar_t * matvar;
171 9 matvar = pdl_to_matvar(ndarray,varname,onedw);
172 int retval;
173 9 ▶ if ( compress == 1 ) retval = Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
Branch: 1 branch
BranchTrueFalseTotal
if ( compress == 1 ) retval = Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB); 0 9 9
174 9 else retval = Mat_VarWrite(mat, matvar, MAT_COMPRESSION_NONE);
175 9 Mat_VarFree(matvar);
176 9 return retval;
177 }
« Matlab.xs ↑ Summary blib/lib/PDL/IO/Matlab.pm »