From 7d01bb9056f462a37f4fafd7628c94ee643ad1d5 Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Thu, 23 Feb 2017 11:34:55 -0600 Subject: [PATCH] matlab matrix saving using a static function (started) --- stim/image/bmp.h | 13 ++++++++----- stim/image/image.h | 4 +++- stim/math/matrix.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/stim/image/bmp.h b/stim/image/bmp.h index 855d438..b80a88b 100644 --- a/stim/image/bmp.h +++ b/stim/image/bmp.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef STIM_BMP_H +#define STIM_BMP_H namespace stim { #pragma pack(1) @@ -115,8 +116,8 @@ namespace stim { //compression methods - const unsigned int BI_RGB = 0; - const unsigned int BI_BITFIELDS = 3; + const unsigned int STIM_BI_RGB = 0; + const unsigned int STIM_BI_BITFIELDS = 3; class bmp { std::ifstream file; @@ -194,7 +195,7 @@ namespace stim { } read_bmpFileHeader(); //read the file header read_dib(); - if (compression != BI_RGB) { //check for compression + if (compression != STIM_BI_RGB) { //check for compression std::cout << "stim::bmp ERROR: this file is compressed, and compression is not supported" << std::endl; return false; } @@ -259,4 +260,6 @@ namespace stim { free(pad); return true; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/stim/image/image.h b/stim/image/image.h index a479eb5..31e7edb 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -5,6 +5,8 @@ //#include //#include #include +#else + #include #endif #include #include @@ -13,7 +15,7 @@ #include #include -#include + #include namespace stim{ diff --git a/stim/math/matrix.h b/stim/math/matrix.h index d7fd2ef..9ef0c48 100644 --- a/stim/math/matrix.h +++ b/stim/math/matrix.h @@ -10,6 +10,27 @@ namespace stim{ + enum mat4Format { + mat4_float64, + mat4_float32, + mat4_int32, + mat4_int16, + mat4_uint16, + mat4_uint8, + mat4_float //floating point type, determined automatically + }; + + size_t mat4Format_size(mat4Format f){ + switch(f){ + case mat4_float64: return 8; + case mat4_float32: + case mat4_int32: return 4; + case mat4_int16: + case mat4_uint16: return 2; + case mat4_uint8: return 1; + default: return 0; + } + } template class matrix { //the matrix will be stored in column-major order (compatible with OpenGL) @@ -17,6 +38,9 @@ class matrix { size_t R; //number of rows size_t C; //number of colums + size_t bytes() { + return R * C * sizeof(T); //return the number of bytes of matrix data + } /*void init(size_t rows, size_t cols){ R = rows; C = cols; @@ -268,7 +292,7 @@ public: } /// Sort columns of the matrix by the specified indices - matrix sort_cols(size_t* idx) const { + matrix sort_cols(size_t* idx, size_t data_type = mat4_float) const { matrix result(C, R); size_t c; for (c = 0; c < C; c++) { //for each column @@ -355,8 +379,44 @@ public: } } + // saves the matrix as a Level-4 MATLAB file + void mat4(std::string filename, std::string name = std::string("unknown"), mat4Format = mat4_float) { + int MOPT = 0; //initialize the MOPT type value to zero + int m = 0; //little endian + int o = 0; //reserved, always 0 + int p = mat4_float; + if (mat4_float) { + if (sizeof(T) == 4) p = mat4_float32; + else if (sizeof(T) == 8) p = mat4_float64; + else { + std::cout << "stim::matrix ERROR - incorrect format specified" << std::endl; + exit(1); + } + } + int t = 0; + MOPT = m * 1000 + o * 100 + p * 10 + t; //calculate the type value + int mrows = rows(); + int ncols = cols(); + int imagf = 0; //assume real (for now) + name += "\0"; //add a null to the string + int namlen = name.size(); //calculate the name size + + std::ofstream outfile(filename, std::ios::binary); + outfile.write((char*)&MOPT, 4); + outfile.write((char*)&mrows, 4); + outfile.write((char*)&ncols, 4); + outfile.write((char*)&imagf, 4); + outfile.write((char*)&namlen, 4); + outfile.write((char*)&name[0], namlen); + outfile.write((char*)M, bytes()); //write the matrix data + outfile.close(); + } }; +void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){ + //save the matrix file here (use the mat4 function above) + //data format: https://maxwell.ict.griffith.edu.au/spl/matlab-page/matfile_format.pdf (page 32) +} } //end namespace rts -- libgit2 0.21.4