NeuralEngine A Game Engine with embeded Machine Learning algorithms based on Gaussian Processes. |

NeMatlabIO.h
1
11#pragma once
12
13#include <string>
14#include <vector>
15#include <cstdio>
16#include <fstream>
17#include <iostream>
18#include <zlib.h>
19#include <opencv2/core/core.hpp>
20#include <Core/NeMatlabIOContainer.h>
21#include <Core/NeEFStream.h>
22#include <Core/NeTypetraits.h>
23
24 namespace NeuralEngine
25{
26 enum
27 {
28 VERSION_5 = 5,
29 VERSION_73 = 73
30 };
31
45 class NE_IMPEXP MatlabIO
46 {
47 public:
48 // constructors
49
55 MatlabIO() {}
56
62 virtual ~MatlabIO() { Close(); }
63
71 std::string Filename(void) { return std::string(filename_); }
72
83 bool Open(std::string filename, std::string mode);
84
95 bool Close(void);
96
114 std::vector<MatlabIOContainer> Read(void);
115
128 void Whos(std::vector<MatlabIOContainer> variables) const;
129
130 // templated functions (must be declared and defined in the header file)
131 template<class T>
132 T Find(std::vector<MatlabIOContainer>& variables, std::string name) const
133 {
134 for (unsigned int n = 0; n < variables.size(); ++n) {
135 if (variables[n].Name().compare(name) == 0) {
136 if (isPrimitiveType<T>()) {
137 return variables[n].Data<cv::Mat>().at<T>(0);
138 }
139 else {
140 return variables[n].Data<T>();
141 }
142 }
143 }
144 throw new std::exception();
145 }
146
147 MatlabIOContainer Find(std::vector<MatlabIOContainer>& variables, std::string name) const
148 {
149 for (unsigned int n = 0; n < variables.size(); ++n) {
150 if (variables[n].Name().compare(name) == 0) return variables[n];
151 }
152 throw new std::exception();
153 }
154
155 template<class T>
156 bool TypeEquals(std::vector<MatlabIOContainer>& variables, std::string name) const
157 {
158 for (unsigned int n = 0; n < variables.size(); ++n) {
159 if (variables[n].Name().compare(name) == 0) return variables[n].TypeEquals<T>();
160 }
161 return false;
162 }
163
164 template<typename T>
165 bool IsPrimitiveType(void) const
166 {
167 if (typeid(T) == typeid(uint8_t) || typeid(T) == typeid(int8_t) ||
168 typeid(T) == typeid(uint16_t) || typeid(T) == typeid(int16_t) ||
169 typeid(T) == typeid(uint32_t) || typeid(T) == typeid(int32_t) ||
170 typeid(T) == typeid(float) || typeid(T) == typeid(double) ||
171 typeid(T) == typeid(uchar) || typeid(T) == typeid(char) ||
172 typeid(T) == typeid(bool)) {
173 return true;
174 }
175 else {
176 return false;
177 }
178 }
179
180 private:
181
196 void GetHeader(void);
197 //void SetHeader(void);
198 bool HasVariable(void) { return fid_.peek() != EOF; }
199
215 MatlabIOContainer ConstructString(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real);
216
229 MatlabIOContainer ConstructSparse(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real, std::vector<char>& imag);
230
250 MatlabIOContainer ConstructCell(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real);
251
263 MatlabIOContainer ConstructStruct(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real);
264
284 const char* ReadVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data);
285
306 MatlabIOContainer CollateMatrixFields(uint32_t data_type, uint32_t nbytes, std::vector<char> data);
307
326 std::vector<char> UncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const std::vector<char> &data);
327
343 MatlabIOContainer ReadVariable(uint32_t data_type, uint32_t nbytes, const std::vector<char> &data);
344
360 MatlabIOContainer ReadBlock(void);
361 //MatlabIOContainer UncompressFromBin(std::vector<char> data, uint32_t nbytes);
362
377 void TransposeMat(const cv::Mat& src, cv::Mat& dst);
378
393 template<class T1, class T2> std::vector<T2> ConvertPrimitiveType(const std::vector<char>& in);
394
408 template<typename T> T Product(const std::vector<T>& vec);
409
434 template<class T> MatlabIOContainer ConstructMatrix(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real, std::vector<char>& imag, uint32_t stor_type);
435
436 // member variables
437 static const int HEADER_LENGTH = 116;
438 static const int SUBSYS_LENGTH = 8;
439 static const int ENDIAN_LENGTH = 2;
440 char header_[HEADER_LENGTH + 1];
441 char subsys_[SUBSYS_LENGTH + 1];
442 char endian_[ENDIAN_LENGTH + 1];
443 int16_t version_;
444 bool byte_swap_;
445 int bytes_read_;
446 std::string filename_;
447 EFStream fid_;
448 };
449
450 template<class T1, class T2>
451 inline std::vector<T2> MatlabIO::ConvertPrimitiveType(const std::vector<char>& in)
452 {
453 std::vector<T2> out;
454
455 // firstly reinterpret the input as type T1
456 const unsigned int T1_size = in.size() / sizeof(T1);
457
458 if (T1_size != 0)
459 {
460 const T1* in_ptr = reinterpret_cast<const T1*>(&(in[0]));
461
462 // construct the new vector
463 std::vector<T2> tmp(in_ptr, in_ptr + T1_size);
464 out = tmp;
465 }
466
467 return out;
468
469 }
470
471 template<typename T>
472 inline T MatlabIO::Product(const std::vector<T>& vec)
473 {
474 T acc = 1;
475 for (unsigned int n = 0; n < vec.size(); ++n) acc *= vec[n];
476 return acc;
477 }
478
479 template<class T>
480 inline MatlabIOContainer MatlabIO::ConstructMatrix(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real, std::vector<char>& imag, uint32_t stor_type)
481 {
482 std::vector<T> vec_real;
483 std::vector<T> vec_imag;
484 std::vector<cv::Mat> vec_mat;
485 cv::Mat flat;
486 cv::Mat mat;
487 switch (stor_type) {
488 case MAT_INT8:
489 vec_real = ConvertPrimitiveType<int8_t, T>(real);
490 vec_imag = ConvertPrimitiveType<int8_t, T>(imag);
491 break;
492 case MAT_UINT8:
493 vec_real = ConvertPrimitiveType<uint8_t, T>(real);
494 vec_imag = ConvertPrimitiveType<uint8_t, T>(imag);
495 break;
496 case MAT_INT16:
497 vec_real = ConvertPrimitiveType<int16_t, T>(real);
498 vec_imag = ConvertPrimitiveType<int16_t, T>(imag);
499 break;
500 case MAT_UINT16:
501 vec_real = ConvertPrimitiveType<uint16_t, T>(real);
502 vec_imag = ConvertPrimitiveType<uint16_t, T>(imag);
503 break;
504 case MAT_INT32:
505 vec_real = ConvertPrimitiveType<int32_t, T>(real);
506 vec_imag = ConvertPrimitiveType<int32_t, T>(imag);
507 break;
508 case MAT_UINT32:
509 vec_real = ConvertPrimitiveType<uint32_t, T>(real);
510 vec_imag = ConvertPrimitiveType<uint32_t, T>(imag);
511 break;
512 case MAT_INT64:
513 vec_real = ConvertPrimitiveType<int64_t, T>(real);
514 vec_imag = ConvertPrimitiveType<int64_t, T>(imag);
515 break;
516 case MAT_UINT64:
517 vec_real = ConvertPrimitiveType<uint64_t, T>(real);
518 vec_imag = ConvertPrimitiveType<uint64_t, T>(imag);
519 break;
520 case MAT_FLOAT:
521 vec_real = ConvertPrimitiveType<float, T>(real);
522 vec_imag = ConvertPrimitiveType<float, T>(imag);
523 break;
524 case MAT_DOUBLE:
525 vec_real = ConvertPrimitiveType<double, T>(real);
526 vec_imag = ConvertPrimitiveType<double, T>(imag);
527 break;
528 case MAT_UTF8:
529 vec_real = ConvertPrimitiveType<char, T>(real);
530 vec_imag = ConvertPrimitiveType<char, T>(imag);
531 break;
532 default:
533 return MatlabIOContainer();
534 }
535
536 // assert that the conversion has not modified the number of elements
537 uint32_t numel = 1;
538 for (unsigned int n = 0; n < dims.size(); ++n) numel *= dims[n];
539 assert(vec_real.size() == numel);
540
541 // if the data is a scalar, don't write it to a matrix
542 //if (vec_real.size() == 1 && vec_imag.size() == 0) return MatlabIOContainer(string(&(name[0])), vec_real[0]);
543
544 // get the number of channels
545 const unsigned int channels = dims.size() == 3 ? dims[2] : 1;
546 bool complx = vec_imag.size() != 0;
547
548 // put each plane of the image into a vector
549 std::vector<cv::Mat> sflat;
550 flat = cv::Mat(vec_real, true);
551 for (unsigned int n = 0; n < channels; ++n)
552 sflat.push_back(flat(cv::Range(dims[0] * dims[1] * n, dims[0] * dims[1] * (n + 1)), cv::Range::all()));
553 flat = cv::Mat(vec_imag, true);
554 for (unsigned int n = 0; n < channels*complx; ++n)
555 sflat.push_back(flat(cv::Range(dims[0] * dims[1] * n, dims[0] * dims[1] * (n + 1)), cv::Range::all()));
556
557 // merge the planes into a matrix
558 merge(sflat, flat);
559
560 // reshape to the image dimensions
561 mat = flat.reshape(flat.channels(), dims[1]);
562
563 // transpose the matrix since matlab stores them in column major ordering
564 TransposeMat(mat, mat);
565
566 return MatlabIOContainer(std::string(&(name[0])), mat);
567 }
568}
NeuralEngine::EFStream
Definition: NeEFStream.h:20
NeuralEngine::MatlabIOContainer
A container class for storing type agnostic variables.
Definition: NeMatlabIOContainer.h:36
NeuralEngine::MatlabIO
Matlab Mat file parser for C++ OpenCV.
Definition: NeMatlabIO.h:46
NeuralEngine::MatlabIO::ConstructStruct
MatlabIOContainer ConstructStruct(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a structure.
NeuralEngine::MatlabIO::ConstructSparse
MatlabIOContainer ConstructSparse(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real, std::vector< char > &imag)
Construct a sparse matrix.
NeuralEngine::MatlabIO::ReadVariable
MatlabIOContainer ReadVariable(uint32_t data_type, uint32_t nbytes, const std::vector< char > &data)
Interpret a variable from a binary block of data.
NeuralEngine::MatlabIO::UncompressVariable
std::vector< char > UncompressVariable(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const std::vector< char > &data)
Uncompress a variable.
NeuralEngine::MatlabIO::ReadVariableTag
const char * ReadVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data)
Interpret the variable header information.
NeuralEngine::MatlabIO::ConvertPrimitiveType
std::vector< T2 > ConvertPrimitiveType(const std::vector< char > &in)
Convert the type of a variable.
Definition: NeMatlabIO.h:451
NeuralEngine::MatlabIO::Filename
std::string Filename(void)
Gets the filename.
Definition: NeMatlabIO.h:71
NeuralEngine::MatlabIO::Close
bool Close(void)
Close the filestream and release all resources.
NeuralEngine::MatlabIO::ConstructCell
MatlabIOContainer ConstructCell(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a cell array.
NeuralEngine::MatlabIO::~MatlabIO
virtual ~MatlabIO()
Destructor.
Definition: NeMatlabIO.h:62
NeuralEngine::MatlabIO::Whos
void Whos(std::vector< MatlabIOContainer > variables) const
Print a formatted list of the contents of a file.
NeuralEngine::MatlabIO::Read
std::vector< MatlabIOContainer > Read(void)
Read all variables from a file.
NeuralEngine::MatlabIO::ConstructString
MatlabIOContainer ConstructString(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a string from an extracted set of fields.
NeuralEngine::MatlabIO::CollateMatrixFields
MatlabIOContainer CollateMatrixFields(uint32_t data_type, uint32_t nbytes, std::vector< char > data)
Interpret all fields of a matrix.
NeuralEngine::MatlabIO::TransposeMat
void TransposeMat(const cv::Mat &src, cv::Mat &dst)
Transpose a multi-channel matrix.
NeuralEngine::MatlabIO::MatlabIO
MatlabIO()
Default constructor.
Definition: NeMatlabIO.h:55
NeuralEngine::MatlabIO::GetHeader
void GetHeader(void)
Gets the .Mat file header information.
NeuralEngine::MatlabIO::ConstructMatrix
MatlabIOContainer ConstructMatrix(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real, std::vector< char > &imag, uint32_t stor_type)
Construct a matrix from an extracted set of fields.
Definition: NeMatlabIO.h:480
NeuralEngine::MatlabIO::Open
bool Open(std::string filename, std::string mode)
Open a filestream for reading or writing.
NeuralEngine::MatlabIO::ReadBlock
MatlabIOContainer ReadBlock(void)
Reads a block of data from the file being parsed.
NeuralEngine::MatlabIO::Product
T Product(const std::vector< T > &vec)
Product of the elements of a vector.
Definition: NeMatlabIO.h:472
NeuralEngine
Definition: NeArray2.h:18