Commit 7d01bb9056f462a37f4fafd7628c94ee643ad1d5

Authored by David Mayerich
1 parent a8ad9192

matlab matrix saving using a static function (started)

stim/image/bmp.h
1   -#pragma once
  1 +#ifndef STIM_BMP_H
  2 +#define STIM_BMP_H
2 3  
3 4 namespace stim {
4 5 #pragma pack(1)
... ... @@ -115,8 +116,8 @@ namespace stim {
115 116  
116 117  
117 118 //compression methods
118   - const unsigned int BI_RGB = 0;
119   - const unsigned int BI_BITFIELDS = 3;
  119 + const unsigned int STIM_BI_RGB = 0;
  120 + const unsigned int STIM_BI_BITFIELDS = 3;
120 121  
121 122 class bmp {
122 123 std::ifstream file;
... ... @@ -194,7 +195,7 @@ namespace stim {
194 195 }
195 196 read_bmpFileHeader(); //read the file header
196 197 read_dib();
197   - if (compression != BI_RGB) { //check for compression
  198 + if (compression != STIM_BI_RGB) { //check for compression
198 199 std::cout << "stim::bmp ERROR: this file is compressed, and compression is not supported" << std::endl;
199 200 return false;
200 201 }
... ... @@ -259,4 +260,6 @@ namespace stim {
259 260 free(pad);
260 261 return true;
261 262 }
262   -}
263 263 \ No newline at end of file
  264 +}
  265 +
  266 +#endif
264 267 \ No newline at end of file
... ...
stim/image/image.h
... ... @@ -5,6 +5,8 @@
5 5 //#include <opencv2/core/core.hpp>
6 6 //#include <opencv2/highgui/highgui.hpp>
7 7 #include <opencv2/opencv.hpp>
  8 +#else
  9 + #include <stim/image/bmp.h>
8 10 #endif
9 11 #include <vector>
10 12 #include <iostream>
... ... @@ -13,7 +15,7 @@
13 15 #include <fstream>
14 16 #include <cstring>
15 17  
16   -#include <stim/image/bmp.h>
  18 +
17 19 #include <stim/parser/filename.h>
18 20  
19 21 namespace stim{
... ...
stim/math/matrix.h
... ... @@ -10,6 +10,27 @@
10 10  
11 11 namespace stim{
12 12  
  13 + enum mat4Format {
  14 + mat4_float64,
  15 + mat4_float32,
  16 + mat4_int32,
  17 + mat4_int16,
  18 + mat4_uint16,
  19 + mat4_uint8,
  20 + mat4_float //floating point type, determined automatically
  21 + };
  22 +
  23 + size_t mat4Format_size(mat4Format f){
  24 + switch(f){
  25 + case mat4_float64: return 8;
  26 + case mat4_float32:
  27 + case mat4_int32: return 4;
  28 + case mat4_int16:
  29 + case mat4_uint16: return 2;
  30 + case mat4_uint8: return 1;
  31 + default: return 0;
  32 + }
  33 + }
13 34 template <class T>
14 35 class matrix {
15 36 //the matrix will be stored in column-major order (compatible with OpenGL)
... ... @@ -17,6 +38,9 @@ class matrix {
17 38 size_t R; //number of rows
18 39 size_t C; //number of colums
19 40  
  41 + size_t bytes() {
  42 + return R * C * sizeof(T); //return the number of bytes of matrix data
  43 + }
20 44 /*void init(size_t rows, size_t cols){
21 45 R = rows;
22 46 C = cols;
... ... @@ -268,7 +292,7 @@ public:
268 292 }
269 293  
270 294 /// Sort columns of the matrix by the specified indices
271   - matrix<T> sort_cols(size_t* idx) const {
  295 + matrix<T> sort_cols(size_t* idx, size_t data_type = mat4_float) const {
272 296 matrix<T> result(C, R);
273 297 size_t c;
274 298 for (c = 0; c < C; c++) { //for each column
... ... @@ -355,8 +379,44 @@ public:
355 379 }
356 380 }
357 381  
  382 + // saves the matrix as a Level-4 MATLAB file
  383 + void mat4(std::string filename, std::string name = std::string("unknown"), mat4Format = mat4_float) {
  384 + int MOPT = 0; //initialize the MOPT type value to zero
  385 + int m = 0; //little endian
  386 + int o = 0; //reserved, always 0
  387 + int p = mat4_float;
  388 + if (mat4_float) {
  389 + if (sizeof(T) == 4) p = mat4_float32;
  390 + else if (sizeof(T) == 8) p = mat4_float64;
  391 + else {
  392 + std::cout << "stim::matrix ERROR - incorrect format specified" << std::endl;
  393 + exit(1);
  394 + }
  395 + }
  396 + int t = 0;
  397 + MOPT = m * 1000 + o * 100 + p * 10 + t; //calculate the type value
  398 + int mrows = rows();
  399 + int ncols = cols();
  400 + int imagf = 0; //assume real (for now)
  401 + name += "\0"; //add a null to the string
  402 + int namlen = name.size(); //calculate the name size
  403 +
  404 + std::ofstream outfile(filename, std::ios::binary);
  405 + outfile.write((char*)&MOPT, 4);
  406 + outfile.write((char*)&mrows, 4);
  407 + outfile.write((char*)&ncols, 4);
  408 + outfile.write((char*)&imagf, 4);
  409 + outfile.write((char*)&namlen, 4);
  410 + outfile.write((char*)&name[0], namlen);
  411 + outfile.write((char*)M, bytes()); //write the matrix data
  412 + outfile.close();
  413 + }
358 414 };
359 415  
  416 +void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){
  417 + //save the matrix file here (use the mat4 function above)
  418 + //data format: https://maxwell.ict.griffith.edu.au/spl/matlab-page/matfile_format.pdf (page 32)
  419 +}
360 420 } //end namespace rts
361 421  
362 422  
... ...