#ifndef RTS_BINARY_H #define RTS_BINARY_H #include "../envi/envi.h" #include "../math/vector.h" #include #include namespace rts{ //This class contains a bunch of functions useful for multidimensional binary file access template< typename T, unsigned int D = 3 > class binary{ protected: std::fstream file; //file stream used for reading and writing std::string name; //file name unsigned int R[D]; //resolution unsigned int header; //header size (in bytes) //basic initialization void init(){ memset(R, 0, sizeof(unsigned int) * D); //initialize the resolution to zero header = 0; //initialize the header size to zero } //returns the file size // reads the file size from disk and returns it (in bytes) unsigned int get_file_size(){ struct stat results; if(stat(name.c_str(), &results) == 0) return results.st_size; else return 0; } //make sure that the specified file size matches the file size on disk // returns true/false bool test_file_size(){ unsigned int npts = 1; //initialize the number of data points to 1 for(unsigned int i = 0; i r, unsigned int h = 0){ for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object R[i] = r[i]; header = h; //save the header size if(!open_file(filename)) return false; //open the binary file return test_file_size(); } T * saveZ(unsigned int page){ T * p; if (page<1||page>R[2]){ //make sure the bank number is right cout<<"wrong page"; getchar(); return NULL; } p=(T *)malloc(R[0]*R[1]*sizeof(T)); //memory allocation if (p==NULL) cout<<"memory allocation failure"; file.seekg(R[1]*R[0]*(page-1)*sizeof(T),ios::beg); //write into memory from the binary file file.read((char *)p,R[0]*R[1]*sizeof(T)); return p; } T * saveXY(unsigned x, unsigned y){ T * px; unsigned int i; if (x<1||x>R[0]||y<1||y>R[1]){ //make sure the sample and line number is right cout<<"wrong page"; getchar(); return NULL; } px=(T *)malloc(R[2]*sizeof(T)); //memory allocation if (px==NULL) cout<<"memory allocation failure"; x=x-1; y=y-1; file.seekg((x+y*R[0])*sizeof(T),ios::beg); //point to the certain sample and line for (i=0;i(X), h); } bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int h = 0){ return open(filename, vec(X, Y), h); } bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int Z, unsigned int h = 0){ return open(filename, vec(X, Y, Z), h); }*/ }; } #endif