Blame view

envi/binary.h 3.79 KB
180d7f3a   David Mayerich   added binary file...
1
2
3
4
  #ifndef RTS_BINARY_H
  #define RTS_BINARY_H
  
  #include "../envi/envi.h"
4dcc9392   heziqi   Ziqi's first chan...
5
  #include "../math/vector.h"
180d7f3a   David Mayerich   added binary file...
6
7
8
9
10
11
12
13
14
15
  #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:
180d7f3a   David Mayerich   added binary file...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  	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...
30
  	//	reads the file size from disk and returns it (in bytes)
180d7f3a   David Mayerich   added binary file...
31
32
33
34
35
36
37
38
  	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...
39
40
  	//make sure that the specified file size matches the file size on disk
  	//	returns true/false
180d7f3a   David Mayerich   added binary file...
41
  	bool test_file_size(){
87da9adc   David Mayerich   fixed issues with...
42
43
44
45
46
47
  		unsigned int npts = 1;				//initialize the number of data points to 1
  		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
  		unsigned int datasize = npts * sizeof(T);//multiply the sum by the size of the template parameter
  
  		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...
48
49
50
51
  		else return false;					//otherwise return an error
  
  	}
  
3725ee1d   David Mayerich   simplify modifica...
52
  	//open the file specified in "name" for binary reading and writing
180d7f3a   David Mayerich   added binary file...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  	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...
69
  	//open a file, given the file name, resolution (as a vector) and header size
180d7f3a   David Mayerich   added binary file...
70
  	bool open(std::string filename, vec<unsigned int, D> r, unsigned int h = 0){
180d7f3a   David Mayerich   added binary file...
71
  
87da9adc   David Mayerich   fixed issues with...
72
  		for(unsigned int i = 0; i < D; i++)		//set the dimensions of the binary file object
180d7f3a   David Mayerich   added binary file...
73
74
  			R[i] = r[i];
  
87da9adc   David Mayerich   fixed issues with...
75
76
77
  		header = h;				//save the header size
  
  		if(!open_file(filename)) return false;	//open the binary file
180d7f3a   David Mayerich   added binary file...
78
79
80
81
  
  		return test_file_size();
  	}
  
c7da85e3   heziqi   he,ziqi add metho...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  	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;
  	}
  
51f94485   heziqi   Ziqi added saveZ,...
102
  	T * saveXY(unsigned x, unsigned y){
c7da85e3   heziqi   he,ziqi add metho...
103
104
105
106
  
  		T * px;
  		unsigned int i;
  
51f94485   heziqi   Ziqi added saveZ,...
107
108
109
110
111
112
  		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;
  		}
  
c7da85e3   heziqi   he,ziqi add metho...
113
114
115
116
117
118
119
120
121
122
123
124
  		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<R[2];i++)
  		{
  			file.read((char *)(px+i), sizeof(T));
  			file.seekg((R[1]*R[0]-1)*sizeof(T),ios::cur);    //go to the next band
c7da85e3   heziqi   he,ziqi add metho...
125
126
127
128
129
130
  		}
  
  		return px;
  	
  	}
  
3725ee1d   David Mayerich   simplify modifica...
131
  	/*bool open(std::string filename, unsigned int X, unsigned int h = 0){
180d7f3a   David Mayerich   added binary file...
132
133
134
135
136
  		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...
137
138
139
140
  	}
  
  	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...
141
  	}*/
180d7f3a   David Mayerich   added binary file...
142
143
144
145
146
147
  
  };
  
  }
  
  #endif