Skip to content

Commit

Permalink
Merge pull request #1 from headupinclouds/pr.add.istream
Browse files Browse the repository at this point in the history
Pr.add.istream
  • Loading branch information
headupinclouds authored Aug 21, 2016
2 parents d0c851b + 8f67157 commit 046060a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 114 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ cmake_minimum_required(VERSION 3.0)

include("cmake/HunterGate.cmake")
HunterGate(
URL "https://github.com/ruslo/hunter/archive/v0.10.6.tar.gz"
SHA1 "59cae8be6506e65a6c756dd42fc0eab128b05eda"
)
URL "https://github.com/ruslo/hunter/archive/v0.15.16.tar.gz"
SHA1 "ca1b3388940bd2ca4452b62a407e6aca743d0ea2"
)

project(cvmatio VERSION 1.0.18)

Expand Down
78 changes: 0 additions & 78 deletions include/EFStream.hpp

This file was deleted.

34 changes: 31 additions & 3 deletions include/MatlabIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <assert.h>

#include "MatlabIOContainer.hpp"
#include "EFStream.hpp"
#include "typetraits.hpp"

#include <opencv2/core/core.hpp>
Expand Down Expand Up @@ -82,11 +81,15 @@ class MatlabIO {
bool byte_swap_;
int bytes_read_;
std::string filename_;
EFStream fid_;

std::fstream fid_;
std::ostream *os_ = nullptr;
std::istream *is_ = nullptr;

// internal methods
void getHeader(void);
void setHeader(void);
bool hasVariable(void) { return fid_.peek() != EOF; }
bool hasVariable(void) { return is_->peek() != EOF; }
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);
MatlabIOContainer constructString(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real);
MatlabIOContainer constructSparse(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real, std::vector<char>& imag);
Expand All @@ -98,6 +101,28 @@ class MatlabIO {
MatlabIOContainer readVariable(uint32_t data_type, uint32_t nbytes, const std::vector<char> &data);
MatlabIOContainer readBlock(void);
MatlabIOContainer uncompressFromBin(std::vector<char> data, uint32_t nbytes);

// Below routines migrated from EFSstream to support arbitrary istream input

// get and set byte swap methods
bool byteSwap(void) { return byte_swap_; }
void setByteSwap(bool state) { byte_swap_ = state; }

// method to swap the Endianness of a stream
void swapEndian(char *s, std::streamsize N) {
for (int n = 0; n < N; n+=2) std::swap(s[n], s[n+1]);
}

// overloaded fstream read method with
// byte swapping capacity
std::istream& read(char *s, std::streamsize n) {
// call the parent read
std::istream& stream = is_->read(s,n);
// swap the endianness if necessary
if (byte_swap_ && n%2 == 0) swapEndian(s,n);
return stream;
}

public:
// constructors
MatlabIO() {}
Expand All @@ -106,6 +131,9 @@ class MatlabIO {
// get and set methods
std::string filename(void) { return std::string(filename_); }
// read and write routines

bool attach(std::ostream &os); // output
bool attach(std::istream &is); // input
bool open(std::string filename, std::string mode);
bool close(void);
std::vector<MatlabIOContainer> read(void);
Expand Down
5 changes: 2 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# build the project
set(BUILD_EXECUTABLE False)
if(BUILD_EXECUTABLE)
option(CVMATIO_BUILD_EXECUTABLE OFF)
if(CVMATIO_BUILD_EXECUTABLE)
set(SRC_FILES MatlabIO.cpp main.cpp)
add_executable(${PROJECT_NAME} ${SRC_FILES})
else()
Expand Down Expand Up @@ -42,7 +42,6 @@ install(

install(
FILES
../include/EFStream.hpp
../include/MatlabIO.hpp
../include/MatlabIOContainer.hpp
../include/typetraits.hpp
Expand Down
55 changes: 33 additions & 22 deletions src/MatlabIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,30 @@ bool MatlabIO::open(string filename, string mode) {

// open the file
filename_ = filename;
if (mode.compare("r") == 0) fid_.open(filename.c_str(), fstream::in | fstream::binary);
if (mode.compare("w") == 0) fid_.open(filename.c_str(), fstream::out | fstream::binary);
if (mode.compare("r") == 0) {
fid_.open(filename.c_str(), fstream::in | fstream::binary);
is_ = &fid_;
}
if (mode.compare("w") == 0) {
fid_.open(filename.c_str(), fstream::out | fstream::binary);
os_ = &fid_;
}

return !fid_.fail();
}

bool MatlabIO::attach(std::ostream &os)
{
os_ = &os;
return !os.fail();
}

bool MatlabIO::attach(std::istream &is)
{
is_ = &is;
return !is.fail();
}

/*! @brief close the filestream and release all resources
*
* @return true if the filestream was successfully closed,
Expand Down Expand Up @@ -149,10 +168,11 @@ void MatlabIO::getHeader(void) {
for (unsigned int n = 0; n < HEADER_LENGTH+1; ++n) header_[n] = '\0';
for (unsigned int n = 0; n < SUBSYS_LENGTH+1; ++n) subsys_[n] = '\0';
for (unsigned int n = 0; n < ENDIAN_LENGTH+1; ++n) endian_[n] = '\0';
fid_.read(header_, sizeof(char)*HEADER_LENGTH);
fid_.read(subsys_, sizeof(char)*SUBSYS_LENGTH);
fid_.read((char *)&version_, sizeof(int16_t));
fid_.read(endian_, sizeof(char)*ENDIAN_LENGTH);

read(header_, sizeof(char)*HEADER_LENGTH);
read(subsys_, sizeof(char)*SUBSYS_LENGTH);
read((char *)&version_, sizeof(int16_t));
read(endian_, sizeof(char)*ENDIAN_LENGTH);

// get the actual version
if (version_ == 0x0100) version_ = VERSION_5;
Expand All @@ -161,8 +181,6 @@ void MatlabIO::getHeader(void) {
// get the endianess
if (strcmp(endian_, "IM") == 0) byte_swap_ = false;
if (strcmp(endian_, "MI") == 0) byte_swap_ = true;
// turn on byte swapping if necessary
fid_.setByteSwap(byte_swap_);

//printf("Header: %s\nSubsys: %s\nVersion: %d\nEndian: %s\nByte Swap: %d\n", header_, subsys_, version_, endian_, byte_swap_);
bytes_read_ = 128;
Expand Down Expand Up @@ -572,17 +590,12 @@ vector<char> MatlabIO::uncompressVariable(uint32_t& data_type, uint32_t& dbytes,
readVariableTag(data_type, dbytes, wbytes, buf);

// inflate the remainder of the variable, now that we know its size
char *udata_tmp = new char[dbytes];
std::vector<char> udata(dbytes);
infstream.avail_out = dbytes;
infstream.next_out = (unsigned char *)udata_tmp;
infstream.next_out = (unsigned char *)&udata[0];
inflate(&infstream, Z_FINISH);
inflateEnd(&infstream);

// convert to a vector
vector<char> udata(udata_tmp, udata_tmp+dbytes);
delete [] udata_tmp;
return udata;

}

/*! @brief Interpret a variable from a binary block of data
Expand Down Expand Up @@ -658,23 +671,21 @@ MatlabIOContainer MatlabIO::readBlock(void) {
uint32_t dbytes;
uint32_t wbytes;
char buf[8];
fid_.read(buf, sizeof(char)*8);
read(buf, sizeof(char)*8);
readVariableTag(data_type, dbytes, wbytes, buf);

// read the binary data block
//printf("\nReading binary data block...\n"); fflush(stdout);
char *data_tmp = new char[dbytes];
fid_.read(data_tmp, sizeof(char)*dbytes);
vector<char> data(data_tmp, data_tmp+dbytes);
delete [] data_tmp;
vector<char> data(dbytes);
read(&data[0], sizeof(char)*data.size());

// move the seek head position to the next 64-bit boundary
// (but only if the data is uncompressed. Saving yet another 8 tiny bytes...)
if (data_type != MAT_COMPRESSED) {
//printf("Aligning seek head to next 64-bit boundary...\n");
streampos head_pos = fid_.tellg();
streampos head_pos = is_->tellg();
int padding = head_pos % 8;
fid_.seekg(padding, fstream::cur);
is_->seekg(padding, fstream::cur);
}

// now read the variable contained in the block
Expand Down
26 changes: 21 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
using namespace cv;
using namespace std;

#define CVMATIO_TEST_ISTREAM 1

int main(int argc, char **argv) {

// get the Matlab .Mat file from the command line
Expand All @@ -63,8 +65,15 @@ int main(int argc, char **argv) {

// create a new reader
MatlabIO matio;

#if CVMATIO_TEST_ISTREAM
std::ifstream is(filename, std::fstream::in | std::fstream::binary);
if(is.fail()) return -1;
matio.attach(is);
#else
bool ok = matio.open(filename, "r");
if (!ok) return -1;
#endif

// read all of the variables in the file
vector<MatlabIOContainer> variables;
Expand All @@ -75,11 +84,15 @@ int main(int argc, char **argv) {

// display the file info
matio.whos(variables);
waitKey();

cv::Mat d = variables[0].data< cv::Mat >();
if(d.cols < 32 && d.rows < 32)
{
std::cout << d << std::endl;
}

// search for a variable named "im" and "gray"
Mat gray;
Mat im;
Mat gray, im;
for (unsigned int n = 0; n < variables.size(); ++n) {
if (variables[n].name().compare("im") == 0) {
im = variables[n].data<Mat>();
Expand All @@ -90,7 +103,10 @@ int main(int argc, char **argv) {
}
}

imshow("image", im);
waitKey(0);
if(!im.empty())
{
imshow("image", im);
waitKey(0);
}
return 0;
}

0 comments on commit 046060a

Please sign in to comment.