matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
ConversionUtilities.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
6  */
7 
9 
10 bool matioCpp::get_matio_types(const matioCpp::VariableType &inputVariableType, const matioCpp::ValueType &inputValueType, matio_classes &outputMatioClasses, matio_types &outputMatioType)
11 {
12  if (inputVariableType == VariableType::Element ||
13  inputVariableType == VariableType::Vector ||
14  inputVariableType == VariableType::MultiDimensionalArray)
15  {
16  switch (inputValueType)
17  {
19  outputMatioClasses = matio_classes::MAT_C_INT8;
20  outputMatioType = matio_types::MAT_T_INT8;
21  break;
24  outputMatioClasses = matio_classes::MAT_C_UINT8;
25  outputMatioType = matio_types::MAT_T_UINT8;
26  break;
28  outputMatioClasses = matio_classes::MAT_C_INT16;
29  outputMatioType = matio_types::MAT_T_INT16;
30  break;
32  outputMatioClasses = matio_classes::MAT_C_UINT16;
33  outputMatioType = matio_types::MAT_T_UINT16;
34  break;
36  outputMatioClasses = matio_classes::MAT_C_INT32;
37  outputMatioType = matio_types::MAT_T_INT32;
38  break;
40  outputMatioClasses = matio_classes::MAT_C_UINT32;
41  outputMatioType = matio_types::MAT_T_UINT32;
42  break;
44  outputMatioClasses = matio_classes::MAT_C_SINGLE;
45  outputMatioType = matio_types::MAT_T_SINGLE;
46  break;
48  outputMatioClasses = matio_classes::MAT_C_DOUBLE;
49  outputMatioType = matio_types::MAT_T_DOUBLE;
50  break;
52  outputMatioClasses = matio_classes::MAT_C_INT64;
53  outputMatioType = matio_types::MAT_T_INT64;
54  break;
56  outputMatioClasses = matio_classes::MAT_C_UINT64;
57  outputMatioType = matio_types::MAT_T_UINT64;
58  break;
60  outputMatioClasses = matio_classes::MAT_C_CHAR;
61  outputMatioType = matio_types::MAT_T_UTF8;
62  break;
64  outputMatioClasses = matio_classes::MAT_C_CHAR;
65  outputMatioType = matio_types::MAT_T_UTF16;
66  break;
68  outputMatioClasses = matio_classes::MAT_C_CHAR;
69  outputMatioType = matio_types::MAT_T_UTF32;
70  break;
72  outputMatioClasses = matio_classes::MAT_C_CHAR;
73  outputMatioType = matio_types::MAT_T_STRING;
74  break;
75  default:
76  return false;
77  }
78  }
79  else if (inputVariableType == VariableType::Struct || inputVariableType == VariableType::StructArray)
80  {
81  outputMatioClasses = matio_classes::MAT_C_STRUCT;
82  outputMatioType = matio_types::MAT_T_STRUCT;
83  }
84  else if (inputVariableType == VariableType::CellArray)
85  {
86  outputMatioClasses = matio_classes::MAT_C_CELL;
87  outputMatioType = MAT_T_CELL;
88  }
89  else
90  {
91  return false;
92  }
93 
94  return true;
95 }
96 
97 bool matioCpp::get_types_from_matvart(const matvar_t *input, matioCpp::VariableType &outputVariableType, matioCpp::ValueType &outputValueType)
98 {
99  if (!input)
100  {
101  return false;
102  }
103 
104  switch (input->data_type)
105  {
106  case matio_types::MAT_T_INT8:
107  outputValueType = matioCpp::ValueType::INT8;
108  break;
109  case matio_types::MAT_T_UINT8:
110  outputValueType = matioCpp::ValueType::UINT8;
111  break;
112  case matio_types::MAT_T_INT16:
113  outputValueType = matioCpp::ValueType::INT16;
114  break;
115  case matio_types::MAT_T_UINT16:
116  outputValueType = matioCpp::ValueType::UINT16;
117  break;
118  case matio_types::MAT_T_INT32:
119  outputValueType = matioCpp::ValueType::INT32;
120  break;
121  case matio_types::MAT_T_UINT32:
122  outputValueType = matioCpp::ValueType::UINT32;
123  break;
124  case matio_types::MAT_T_SINGLE:
125  outputValueType = matioCpp::ValueType::SINGLE;
126  break;
127  case matio_types::MAT_T_DOUBLE:
128  outputValueType = matioCpp::ValueType::DOUBLE;
129  break;
130  case matio_types::MAT_T_INT64:
131  outputValueType = matioCpp::ValueType::INT64;
132  break;
133  case matio_types::MAT_T_UINT64:
134  outputValueType = matioCpp::ValueType::UINT64;
135  break;
136  case matio_types::MAT_T_UTF8:
137  outputValueType = matioCpp::ValueType::UTF8;
138  break;
139  case matio_types::MAT_T_UTF16:
140  outputValueType = matioCpp::ValueType::UTF16;
141  break;
142  case matio_types::MAT_T_UTF32:
143  outputValueType = matioCpp::ValueType::UTF32;
144  break;
145  case matio_types::MAT_T_STRING:
146  outputValueType = matioCpp::ValueType::STRING;
147  break;
148  case matio_types::MAT_T_CELL:
149  case matio_types::MAT_T_STRUCT:
150  case matio_types::MAT_T_ARRAY:
151  case matio_types::MAT_T_MATRIX:
152  outputValueType = matioCpp::ValueType::VARIABLE;
153  break;
154  case matio_types::MAT_T_COMPRESSED:
155  case matio_types::MAT_T_FUNCTION:
156  case matio_types::MAT_T_UNKNOWN:
157  outputValueType = matioCpp::ValueType::UNSUPPORTED;
158  break;
159  }
160 
161  if (input->isLogical)
162  {
163  if (outputValueType == matioCpp::ValueType::UINT8)
164  {
165  outputValueType = matioCpp::ValueType::LOGICAL;
166  }
167  }
168 
169  if ((input->class_type == matio_classes::MAT_C_OBJECT) ||
170  (input->class_type == matio_classes::MAT_C_SPARSE) ||
171  (input->class_type == matio_classes::MAT_C_FUNCTION) ||
172  (input->class_type == matio_classes::MAT_C_OPAQUE) ||
173  (outputValueType == matioCpp::ValueType::UNSUPPORTED) ||
174  (input->rank < 2)) //Matio requires the rank to be at least 2
175  {
176  outputVariableType = matioCpp::VariableType::Unsupported;
177  return true;
178  }
179 
180  size_t dimensionsProduct = 1;
181  for (int i = 0; i < input->rank; ++i)
182  {
183  dimensionsProduct *= input->dims[i];
184  }
185 
186  if (input->class_type == matio_classes::MAT_C_CELL || input->data_type == matio_types::MAT_T_CELL)
187  {
188  outputVariableType = matioCpp::VariableType::CellArray;
189  }
190  else if (dimensionsProduct == 1)
191  {
192  if (input->data_type == matio_types::MAT_T_STRUCT)
193  {
194  outputVariableType = matioCpp::VariableType::Struct;
195  }
196  else if ((input->data_type == MAT_T_ARRAY) || (input->data_type == MAT_T_MATRIX)) //This would be a weird case where the variable has dimension 1x1, it is not a cell nor a struct, yet it contains an array
197  {
198  outputVariableType = matioCpp::VariableType::Unsupported;
199  }
200  else
201  {
202  outputVariableType = matioCpp::VariableType::Element;
203  }
204  }
205  else
206  {
207  if (input->data_type == matio_types::MAT_T_STRUCT)
208  {
209  outputVariableType = matioCpp::VariableType::StructArray;
210  }
211  else if ((input->data_type == MAT_T_ARRAY) || (input->data_type == MAT_T_MATRIX))
212  {
213  outputVariableType = matioCpp::VariableType::Unsupported;
214  }
215  else if ((input->rank == 2) && ((input->dims[0] == 1) || (input->dims[1] == 1)))
216  {
217  outputVariableType = matioCpp::VariableType::Vector;
218  }
219  else
220  {
222  }
223  }
224 
225  return true;
226 }
227 
228 bool matioCpp::get_types_names_from_matvart(const matvar_t *input, std::string &classType, std::string &dataType)
229 {
230  if (!input)
231  {
232  return false;
233  }
234 
235  switch (input->data_type)
236  {
237  case matio_types::MAT_T_INT8:
238  dataType = "INT8";
239  break;
240  case matio_types::MAT_T_UINT8:
241  dataType = "UINT8";
242  break;
243  case matio_types::MAT_T_INT16:
244  dataType = "INT16";
245  break;
246  case matio_types::MAT_T_UINT16:
247  dataType = "UINT16";
248  break;
249  case matio_types::MAT_T_INT32:
250  dataType = "INT32";
251  break;
252  case matio_types::MAT_T_UINT32:
253  dataType = "UINT32";
254  break;
255  case matio_types::MAT_T_SINGLE:
256  dataType = "SINGLE";
257  break;
258  case matio_types::MAT_T_DOUBLE:
259  dataType = "DOUBLE";
260  break;
261  case matio_types::MAT_T_INT64:
262  dataType = "INT64";
263  break;
264  case matio_types::MAT_T_UINT64:
265  dataType = "UINT64";
266  break;
267  case matio_types::MAT_T_UTF8:
268  dataType = "UTF8";
269  break;
270  case matio_types::MAT_T_UTF16:
271  dataType = "UTF16";
272  break;
273  case matio_types::MAT_T_UTF32:
274  dataType = "UTF32";
275  break;
276  case matio_types::MAT_T_STRING:
277  dataType = "STRING";
278  break;
279  case matio_types::MAT_T_CELL:
280  dataType = "CELL";
281  break;
282  case matio_types::MAT_T_STRUCT:
283  dataType = "STRUCT";
284  break;
285  case matio_types::MAT_T_ARRAY:
286  dataType = "ARRAY";
287  break;
288  case matio_types::MAT_T_MATRIX:
289  dataType = "MATRIX";
290  break;
291  case matio_types::MAT_T_COMPRESSED:
292  dataType = "COMPRESSED";
293  break;
294  case matio_types::MAT_T_FUNCTION:
295  dataType = "FUNCTION";
296  break;
297  case matio_types::MAT_T_UNKNOWN:
298  dataType = "UNKNOWN";
299  break;
300  }
301 
302  if (input->isLogical)
303  {
304  dataType = dataType + " (Logical)";
305  }
306 
307  switch (input->class_type)
308  {
309  case MAT_C_EMPTY:
310  classType = "EMPTY";
311  break;
312  case MAT_C_CELL:
313  classType = "CELL";
314  break;
315  case MAT_C_STRUCT:
316  classType = "STRUCT";
317  break;
318  case MAT_C_OBJECT:
319  classType = "OBJECT";
320  break;
321  case MAT_C_CHAR:
322  classType = "CHAR";
323  break;
324  case MAT_C_SPARSE:
325  classType = "SPARSE";
326  break;
327  case MAT_C_DOUBLE:
328  classType = "DOUBLE";
329  break;
330  case MAT_C_SINGLE:
331  classType = "SINGLE";
332  break;
333  case MAT_C_INT8:
334  classType = "INT8";
335  break;
336  case MAT_C_UINT8:
337  classType = "UINT8";
338  break;
339  case MAT_C_INT16:
340  classType = "INT16";
341  break;
342  case MAT_C_UINT16:
343  classType = "UINT16";
344  break;
345  case MAT_C_INT32:
346  classType = "INT32";
347  break;
348  case MAT_C_UINT32:
349  classType = "UINT32";
350  break;
351  case MAT_C_INT64:
352  classType = "INT64";
353  break;
354  case MAT_C_UINT64:
355  classType = "UINT64";
356  break;
357  case MAT_C_FUNCTION:
358  classType = "FUNCTION";
359  break;
360  case MAT_C_OPAQUE:
361  classType = "OPAQUE";
362  break;
363  }
364 
365  return true;
366 }
bool get_matio_types(const VariableType &inputVariableType, const ValueType &inputValueType, matio_classes &outputMatioClasses, matio_types &outputMatioType)
Get both the matio type and class from the input VariableType and ValueType.
VariableType
Define the type of variable.
bool get_types_from_matvart(const matvar_t *input, VariableType &outputVariableType, ValueType &outputValueType)
Get the VariableType and the ValueType from a matvar_t pointer.
ValueType
The list of types for an element of a certain variable type.
bool get_types_names_from_matvart(const matvar_t *input, std::string &classType, std::string &dataType)
Get the type names from the input pointer.