Blame view

envi/binary.h 2.46 KB
180d7f3a   David Mayerich   added binary file...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #ifndef RTS_BINARY_H
  #define RTS_BINARY_H
  
  #include "../envi/envi.h"
  
  #include <fstream>
  #include <sys/stat.h>
  
  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
3725ee1d   David Mayerich   simplify modifica...
31
  	//	reads the file size from disk and returns it (in bytes)
180d7f3a   David Mayerich   added binary file...
32
33
34
35
36
37
38
39
  	unsigned int get_file_size(){
  
  		struct stat results;
  		if(stat(name.c_str(), &results) == 0)
  			return results.st_size;
  		else return 0;
  	}
  
3725ee1d   David Mayerich   simplify modifica...
40
41
  	//make sure that the specified file size matches the file size on disk
  	//	returns true/false
180d7f3a   David Mayerich   added binary file...
42
43
44
45
46
47
48
49
50
  	bool test_file_size(){
  		unsigned int sum = header;			//initialize the sum (in bytes) to the header size
  		for(unsigned int i = 0; i<D; i++)	//iterate over each dimension, summing the byte size
  			sum += R[i] * sizeof(T);
  		if(sum == get_file_size()) return true;	//if the byte size matches the file size, we're golden
  		else return false;					//otherwise return an error
  
  	}
  
3725ee1d   David Mayerich   simplify modifica...
51
  	//open the file specified in "name" for binary reading and writing
180d7f3a   David Mayerich   added binary file...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  	bool open_file(std::string filename){
  		//open the file as binary for reading and writing
  		file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::binary);
  
  		//if the file is successful
  		if(file){
  			name = filename;		//set the name
  			if(test_file_size())	//test the file size
  				return true;
  		}
  		
  		return false;
  	}
  
  public:
  
3725ee1d   David Mayerich   simplify modifica...
68
  	//open a file, given the file name, resolution (as a vector) and header size
180d7f3a   David Mayerich   added binary file...
69
70
71
72
73
74
75
76
77
78
79
  	bool open(std::string filename, vec<unsigned int, D> r, unsigned int h = 0){
  		if(!open_file(filename)) return false;
  
  		for(unsigned int i = 0; i < D; i++)
  			R[i] = r[i];
  
  		header = h;
  
  		return test_file_size();
  	}
  
3725ee1d   David Mayerich   simplify modifica...
80
  	/*bool open(std::string filename, unsigned int X, unsigned int h = 0){
180d7f3a   David Mayerich   added binary file...
81
82
83
84
85
  		return open(filename, vec<unsigned int, 1>(X), h);
  	}
  
  	bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int h = 0){
  		return open(filename, vec<unsigned int, 2>(X, Y), h);
180d7f3a   David Mayerich   added binary file...
86
87
88
89
  	}
  
  	bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int Z, unsigned int h = 0){
  		return open(filename, vec<unsigned int, 3>(X, Y, Z), h);
3725ee1d   David Mayerich   simplify modifica...
90
  	}*/
180d7f3a   David Mayerich   added binary file...
91
92
93
94
95
96
  
  };
  
  }
  
  #endif