Debian Codegraph @

convert_matvar_pdl.c

raw symbols SCIP
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 static void delete_matvar_to_pdl_data(pdl* p, size_t param)
74 {
75 if (p->data)
76 free(p->data);
77 p->data = 0;
78 }
79
80 typedef void (*DelMagic)(pdl *, size_t param);
81 static void default_magic(pdl *p, size_t pa) { p->data = 0; }
82 static pdl* my_pdl_wrap(void *data, int datatype, PDL_Indx dims[],
83 int ndims, DelMagic delete_magic, int delparam)
84 {
85 pdl* npdl = PDL->pdlnew();
86 PDL->setdims(npdl,dims,ndims);
87 npdl->datatype = datatype;
88 npdl->data = data;
89 npdl->state |= PDL_DONTTOUCHDATA | PDL_ALLOCATED;
90 if (delete_magic != NULL)
91 PDL->add_deletedata_magic(npdl, (void *) delete_magic, delparam);
92 else
93 PDL->add_deletedata_magic(npdl, default_magic, 0);
94 return npdl;
95 }
96
97 static pdl* matvar_to_pdl (matvar_t * matvar, int onedr) {
98 int ndims = matvar->rank;
99 pdl * ndarray;
100 int i, pdl_data_type;
101 PDL_Indx * dims;
102 if ( matvar->isComplex )
103 barf("matvar_to_pdl: Complex matlab variables not supported.");
104 dims = (PDL_Indx *)malloc(sizeof(PDL_Indx) * ndims);
105 // fprintf(stderr, "ONEDR %d\n", onedr);
106 if (ndims == 2 && onedr != 0 ) {
107 if (matvar->dims[0] == 1) {
108 ndims = 1;
109 dims[0] = matvar->dims[1];
110 }
111 else if (matvar->dims[1] == 1) {
112 ndims = 1;
113 dims[0] = matvar->dims[0];
114 }
115 else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i];
116 }
117 else for(i=0;i<ndims;i++) dims[i] = matvar->dims[i];
118 if ( 0 > (pdl_data_type = matvar_class_to_pdl_type[matvar->class_type] )) {
119 fprintf(stderr, "matvar_to_pdl: matlab data class is '%s'\n",matvar_class_type_desc[matvar->class_type]);
120 barf("matvar_to_pdl: No pdl data type corresponding to this class type.");}
121 ndarray = my_pdl_wrap(matvar->data, pdl_data_type, dims, ndims,
122 delete_matvar_to_pdl_data, 0);
123 matvar->mem_conserve = 1; // prevent matio freeing memory for data
124 free(dims);
125 return ndarray;
126 }
127
128 pdl * convert_next_matvar_to_pdl (mat_t * matfp, matvar_t ** matvar, int onedr) {
129 *matvar = Mat_VarReadNext(matfp);
130 if (*matvar == NULL )
131 return NULL;
132 return matvar_to_pdl(*matvar,onedr); // calling code must call Mat_VarFree(matvar)
133 }
134
135 /*******************************************************
136 * pdl to matvar
137 *******************************************************/
138
139 matvar_t * pdl_to_matvar (pdl * ndarray, char *varname, int onedw) {
140 int ndims = ndarray->ndims;
141 matvar_t *matvar;
142 int i, matvar_class_type, matvar_data_type;
143 int opt = MAT_F_DONT_COPY_DATA;
144 size_t * dims;
145 int tmp;
146 dims = (size_t *)malloc(sizeof(size_t) * (ndims+1));
147 for(i=0;i<ndims;i++) dims[i] = ndarray->dims[i];
148 if (ndims == 1 ) {
149 if ( onedw == 1) {
150 ndims = 2;
151 dims[1] = 1;
152 }
153 else if ( onedw == 2) {
154 ndims = 2;
155 tmp = dims[0];
156 dims[0] = 1;
157 dims[1] = tmp;
158 }
159 }
160 matvar_class_type = pdl_type_to_matvar_class[ndarray->datatype];
161 matvar_data_type = pdl_type_to_matvar_type[ndarray->datatype];
162 matvar = Mat_VarCreate(varname,matvar_class_type, matvar_data_type,
163 ndims, dims, ndarray->data, opt);
164 free(dims);
165 return matvar;
166 }
167
168 int write_pdl_to_matlab_file (mat_t *mat, pdl *ndarray, char *varname, int onedw,
169 int compress) {
170 matvar_t * matvar;
171 matvar = pdl_to_matvar(ndarray,varname,onedw);
172 int retval;
173 if ( compress == 1 ) retval = Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
174 else retval = Mat_VarWrite(mat, matvar, MAT_COMPRESSION_NONE);
175 Mat_VarFree(matvar);
176 return retval;
177 }