Blame view

envi/binary.h 5.87 KB
1f2ca8ec   David Mayerich   added files for b...
1
2
  
  //make sure that this header file is only loaded once
180d7f3a   David Mayerich   added binary file...
3
4
5
  #ifndef RTS_BINARY_H
  #define RTS_BINARY_H
  
e8eb202f   David Mayerich   added a new ENVI ...
6
  #include "../envi/envi_header.h"
4dcc9392   heziqi   Ziqi's first chan...
7
  #include "../math/vector.h"
180d7f3a   David Mayerich   added binary file...
8
9
10
  #include <fstream>
  #include <sys/stat.h>
  
8a86bd56   David Mayerich   changed rts names...
11
  namespace stim{
180d7f3a   David Mayerich   added binary file...
12
13
14
15
16
17
  
  //This class contains a bunch of functions useful for multidimensional binary file access
  template< typename T, unsigned int D = 3 >
  class binary{
  
  protected:
180d7f3a   David Mayerich   added binary file...
18
19
20
  	std::fstream file;		//file stream used for reading and writing
  	std::string name;		//file name
  
193ff00e   David Mayerich   dealth with preci...
21
  	unsigned long long int R[D];		//resolution
180d7f3a   David Mayerich   added binary file...
22
  	unsigned int header;	//header size (in bytes)
f4c5d71b   David Mayerich   started working w...
23
  	unsigned char* mask;	//pointer to a character array: 0 = background, 1 = foreground (or valid data)
180d7f3a   David Mayerich   added binary file...
24
25
  
  
ee4dea28   David Mayerich   fixed errors in c...
26
27
  
  
180d7f3a   David Mayerich   added binary file...
28
29
30
31
  	//basic initialization
  	void init(){
  		memset(R, 0, sizeof(unsigned int) * D);		//initialize the resolution to zero
  		header = 0;									//initialize the header size to zero
f4c5d71b   David Mayerich   started working w...
32
  		mask = NULL;
180d7f3a   David Mayerich   added binary file...
33
34
35
  	}
  
  	//returns the file size
3725ee1d   David Mayerich   simplify modifica...
36
  	//	reads the file size from disk and returns it (in bytes)
b0fee590   heziqi   convert long int ...
37
  	long long int get_file_size(){
193ff00e   David Mayerich   dealth with preci...
38
  #ifdef _WIN32
b0fee590   heziqi   convert long int ...
39
40
  		struct _stat64 results;
  		if(_stat64(name.c_str(), &results) == 0)
180d7f3a   David Mayerich   added binary file...
41
  			return results.st_size;
193ff00e   David Mayerich   dealth with preci...
42
43
44
45
46
  #else
  		struct stat results;
  		if(stat(name.c_str(), &results) == 0)
  			return results.st_size;
  #endif
180d7f3a   David Mayerich   added binary file...
47
48
49
  		else return 0;
  	}
  
3725ee1d   David Mayerich   simplify modifica...
50
51
  	//make sure that the specified file size matches the file size on disk
  	//	returns true/false
180d7f3a   David Mayerich   added binary file...
52
  	bool test_file_size(){
b0fee590   heziqi   convert long int ...
53
  		long long int npts = 1;				//initialize the number of data points to 1
87da9adc   David Mayerich   fixed issues with...
54
55
  		for(unsigned int i = 0; i<D; i++)	//iterate over each dimension
  			npts *= R[i];					//compute the total number of data points in the file
b0fee590   heziqi   convert long int ...
56
  		long long int datasize = npts * sizeof(T);//multiply the sum by the size of the template parameter
87da9adc   David Mayerich   fixed issues with...
57
58
  
  		if(datasize + header == get_file_size()) return true;	//if the byte size matches the file size, we're golden
180d7f3a   David Mayerich   added binary file...
59
60
61
62
  		else return false;					//otherwise return an error
  
  	}
  
3725ee1d   David Mayerich   simplify modifica...
63
  	//open the file specified in "name" for binary reading and writing
180d7f3a   David Mayerich   added binary file...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  	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...
80
  	//open a file, given the file name, resolution (as a vector) and header size
180d7f3a   David Mayerich   added binary file...
81
  	bool open(std::string filename, vec<unsigned int, D> r, unsigned int h = 0){
180d7f3a   David Mayerich   added binary file...
82
  
87da9adc   David Mayerich   fixed issues with...
83
  		for(unsigned int i = 0; i < D; i++)		//set the dimensions of the binary file object
180d7f3a   David Mayerich   added binary file...
84
85
  			R[i] = r[i];
  
87da9adc   David Mayerich   fixed issues with...
86
87
88
  		header = h;				//save the header size
  
  		if(!open_file(filename)) return false;	//open the binary file
180d7f3a   David Mayerich   added binary file...
89
90
91
92
  
  		return test_file_size();
  	}
  
f47168a2   heziqi   Ziqi added create...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  	//crete a new binary file
  	bool create(std::string filename, vec<unsigned int, D> r, unsigned int offset = 0){
  
  		std::ofstream target(filename.c_str(), std::ios::binary);
  
  		//initialize binary file
  		T p = 0;
  		for(unsigned int i =0; i < r[0] * r[1] * r[2]; i++){
  			target.write((char*)(&p), sizeof(T));
  		}
  
  		for(unsigned int i = 0; i < D; i++)		//set the dimensions of the binary file object
  			R[i] = r[i];
  
  		header = offset;				//save the header size
  
  		if(!open_file(filename)) return false;	//open the binary file
  
  		return test_file_size();
  	}
  
  	//save one band from the memory to the file
  	bool writeZ( T * p, unsigned int page){
  
  		if(p == NULL){
  			std::cout<<"ERROR: unable to write into file, empty pointer"<<std::endl;
  			exit(1);
  		}
  
  		file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg);   //write into memory from the binary file
  		file.write((char *)p, R[0] * R[1] * sizeof(T));
  
  		return true;
  	}
  
5d5ce39a   heziqi   Ziqi convert the ...
128
129
  	//save one band of the file into the memory, and return the pointer
  	bool saveZ( T * p, unsigned int page){
c7da85e3   heziqi   he,ziqi add metho...
130
  
5d5ce39a   heziqi   Ziqi convert the ...
131
  		if (page >= R[2]){										//make sure the bank number is right
2ad0ce4f   David Mayerich   syntactic edits b...
132
  			std::cout<<"ERROR: page out of range"<<std::endl;
5d5ce39a   heziqi   Ziqi convert the ...
133
  			return false;
c7da85e3   heziqi   he,ziqi add metho...
134
135
  		}
  
3b972fb3   David Mayerich   wrote a general f...
136
  		file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg);   //write into memory from the binary file
2ad0ce4f   David Mayerich   syntactic edits b...
137
  		file.read((char *)p, R[0] * R[1] * sizeof(T));
c7da85e3   heziqi   he,ziqi add metho...
138
  
5d5ce39a   heziqi   Ziqi convert the ...
139
  		return true;
c7da85e3   heziqi   he,ziqi add metho...
140
141
  	}
  
3b972fb3   David Mayerich   wrote a general f...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  	//saves a hyperplane orthogonal to dimension d at intersection n
  	bool getSlice(T * dest, unsigned int d, unsigned int n){
  
  		//reset the file pointer back to the beginning of the file
  		file.seekg(0, std::ios::beg);
  
  		//compute the contiguous size C for each readable block
  		unsigned int C = 1;
  		for(unsigned int i = 0; i < d; i++)		//for each dimension less than d
  			C *= R[i];							//compute the product
  
  		//compute the non-contiguous size NC for each readable block
  		unsigned int NC = 1;
  		for(unsigned int i = d + 1; i < D; i++)
  			NC *= R[i];
  
  		//for all noncontiguous blocks, read each contiguous block that makes up the hyper-plane
  		for(unsigned int nc = 0; nc < NC; nc++){
  			file.seekg(n * C * sizeof(T), std::ios::cur);	//skip n contiguous blocks
  			file.read( (char*)&dest[nc * C], C * sizeof(T));	//read one contiguous block
  			file.seekg( (R[d] - n - 1) * C * sizeof(T), std::ios::cur);	//skip R[d] - n contiguous blocks
  		}
  
  		return true;
  
  	}
  
5d5ce39a   heziqi   Ziqi convert the ...
169
170
  	//save one pixel of the file into the memory, and return the pointer
  	bool saveXY(T * p, unsigned x, unsigned y){
c7da85e3   heziqi   he,ziqi add metho...
171
  
c7da85e3   heziqi   he,ziqi add metho...
172
173
  		unsigned int i;
  
5d5ce39a   heziqi   Ziqi convert the ...
174
175
176
  		if ( x >= R[0] || y >= R[1]){							//make sure the sample and line number is right
  			std::cout<<"ERROR: sample or line out of range"<<std::endl;
  			return false;
51f94485   heziqi   Ziqi added saveZ,...
177
178
  		}
  
3b972fb3   David Mayerich   wrote a general f...
179
  		file.seekg((x + y * R[0]) * sizeof(T), std::ios::beg);           //point to the certain sample and line
5d5ce39a   heziqi   Ziqi convert the ...
180
  		for (i = 0; i < R[2]; i++)
c7da85e3   heziqi   he,ziqi add metho...
181
  		{
5d5ce39a   heziqi   Ziqi convert the ...
182
  			file.read((char *)(p + i), sizeof(T));
3b972fb3   David Mayerich   wrote a general f...
183
  			file.seekg((R[1] * R[0] - 1) * sizeof(T), std::ios::cur);    //go to the next band
c7da85e3   heziqi   he,ziqi add metho...
184
185
  		}
  
5d5ce39a   heziqi   Ziqi convert the ...
186
  		return true;	
c7da85e3   heziqi   he,ziqi add metho...
187
188
  	}
  
3725ee1d   David Mayerich   simplify modifica...
189
  	/*bool open(std::string filename, unsigned int X, unsigned int h = 0){
180d7f3a   David Mayerich   added binary file...
190
191
192
193
194
  		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...
195
196
197
198
  	}
  
  	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...
199
  	}*/
180d7f3a   David Mayerich   added binary file...
200
201
202
203
204
205
  
  };
  
  }
  
  #endif