| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static int matvar_class_to_pdl_type[16] = |
| 36 |
|
| 37 |
{ -1, -1, -1, -1, |
| 38 |
|
| 39 |
|
| 40 |
-1, -1, PDL_D, PDL_F, |
| 41 |
|
| 42 |
|
| 43 |
PDL_B, PDL_B, PDL_S, PDL_US, |
| 44 |
|
| 45 |
|
| 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 |
|
| 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; |
| 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); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
} |