Blame view

envi/bsq.h 5.14 KB
3fc1d461   heziqi   Ziqi added saveBa...
1
2
  #include "../envi/envi.h"
  #include "../envi/binary.h"
fc712213   David Mayerich   code simplifications
3
4
  #include <cstring>
  #include <utility>
3fc1d461   heziqi   Ziqi added saveBa...
5
6
7
8
9
10
11
12
13
14
15
16
17
  
  namespace rts{
  
  template <typename T>
  
  class bsq: public binary<T> {
  
  protected:
  	
  	envi header;
  
  public:
  
fc712213   David Mayerich   code simplifications
18
19
20
21
  	using binary<T>::open;
  	using binary<T>::file;
  	using binary<T>::getSlice;
  
3fc1d461   heziqi   Ziqi added saveBa...
22
23
24
25
26
27
28
29
  	//open a file, given the file and its header's names
  	bool open(std::string filename, std::string headername){
  
  		if (header.load(headername)==false){
  			std::cout<<"ERROR: unable to load head file"<<std::endl;
  			return false;
  		}
  
fc712213   David Mayerich   code simplifications
30
  		open(filename, vec<unsigned int>(header.samples, header.lines, header.bands), header.header_offset);
3fc1d461   heziqi   Ziqi added saveBa...
31
32
33
34
35
  		return true;
  		
  	}
  
  	//save one band of the file into the memory, and return the pointer
0e48cc9c   heziqi   Ziqi added getBan...
36
  	bool band_index( T * p, unsigned int page){
3fc1d461   heziqi   Ziqi added saveBa...
37
38
39
40
41
42
  
  		if (page >= header.bands){										//make sure the bank number is right
  			std::cout<<"ERROR: page out of range"<<std::endl;
  			return false;
  		}
  
31394e2a   heziqi   Ziqi added method...
43
  		getSlice(p, 2, page);
3fc1d461   heziqi   Ziqi added saveBa...
44
45
46
  		return true;
  	}
  
0e48cc9c   heziqi   Ziqi added getBan...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  	bool getBand( T * p, double wavelength){
  
  		unsigned int XY = header.samples * header.lines;	//calculate the number of pixels in a band
  
  		unsigned page=0;                      //bands around the wavelength
  		T * p1;
  		T * p2;
  
  		//get the bands numbers around the wavelength
  
  		//if wavelength is smaller than the first one in header file
  		if ( header.wavelength[page] > wavelength ){
  			band_index(p, page);
  			return true;
  		}
  
  		while( header.wavelength[page] < wavelength )
  		{
  			page++;
  			//if wavelength is larger than the last wavelength in header file
  			if (page == header.bands) {
  				getSlice(p, 2, header.bands-1);
  				return true;
  			}
  		}
  		if ( wavelength < header.wavelength[page] )
  		{
  			p1=(T*)malloc( XY * sizeof(T));                     //memory allocation
  			p2=(T*)malloc( XY * sizeof(T));
  			band_index(p1, page - 1);
  			band_index(p2, page );
  			for(unsigned i=0; i < XY; i++){
  				double r = (double) (wavelength - header.wavelength[page-1]) / (double) (header.wavelength[page] - header.wavelength[page-1]);
  				p[i] = (p2[i] - p1[i]) * r + p1[i];
  			}
  		}
  		else                           //if the wavelength is equal to a wavelength in header file
  		{
  			getSlice(p, 2, page);
  		}
  
  		free(p1);
  		free(p2);
  		return true;
  	}
  
3fc1d461   heziqi   Ziqi added saveBa...
93
  	//save one pixel of the file into the memory, and return the pointer
31394e2a   heziqi   Ziqi added method...
94
  	bool getSpectrum(T * p, unsigned x, unsigned y){
3fc1d461   heziqi   Ziqi added saveBa...
95
96
97
98
99
100
101
102
  
  		unsigned int i;
  
  		if ( x >= header.samples || y >= header.lines){							//make sure the sample and line number is right
  			std::cout<<"ERROR: sample or line out of range"<<std::endl;
  			return false;
  		}
  
31394e2a   heziqi   Ziqi added method...
103
  		file.seekg((x + y * header.samples) * sizeof(T), std::ios::beg);           //point to the certain sample and line
3fc1d461   heziqi   Ziqi added saveBa...
104
105
106
  		for (i = 0; i < header.bands; i++)
  		{
  			file.read((char *)(p + i), sizeof(T));
31394e2a   heziqi   Ziqi added method...
107
  			file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur);    //go to the next band
3fc1d461   heziqi   Ziqi added saveBa...
108
109
  		}
  
0e48cc9c   heziqi   Ziqi added getBan...
110
111
  		free(p1);
  		free(p2);
3fc1d461   heziqi   Ziqi added saveBa...
112
113
114
  		return true;	
  	}
  
31394e2a   heziqi   Ziqi added method...
115
116
117
  	//baseline correction and save it into file
  	bool baseline(std::string outname, std::vector<unsigned> bands )
  	{
fc712213   David Mayerich   code simplifications
118
119
120
  		unsigned N = bands.size();			//get the number of baseline points
  
  		std::ofstream target(outname.c_str(), std::ios::binary);	//open the target binary file
31394e2a   heziqi   Ziqi added method...
121
  
fc712213   David Mayerich   code simplifications
122
123
124
125
  		//simplify image resolution
  		unsigned int B = header.bands;		//calculate the number of bands
  		unsigned int XY = header.samples * header.lines;	//calculate the number of pixels in a band
  		unsigned int S = XY * sizeof(T);		//calculate the number of bytes in a band
31394e2a   heziqi   Ziqi added method...
126
  
fc712213   David Mayerich   code simplifications
127
  		unsigned ai, bi;	//stores the two baseline points surrounding the current band
31394e2a   heziqi   Ziqi added method...
128
129
  		unsigned control=0;
  
fc712213   David Mayerich   code simplifications
130
131
132
  		T * a;					//pointers to the high and low band images
  		T * b;
  		T * c;				//pointer to the current image
31394e2a   heziqi   Ziqi added method...
133
  
fc712213   David Mayerich   code simplifications
134
135
136
  		a = (T*)malloc( S );     //memory allocation
  		b = (T*)malloc( S ); 
  		c = (T*)malloc( S ); 
31394e2a   heziqi   Ziqi added method...
137
  
fc712213   David Mayerich   code simplifications
138
139
140
141
  		if (a == NULL || b == NULL || c == NULL){
  			std::cout<<"ERROR: error allocating memory";
  			exit(1);
  		}
31394e2a   heziqi   Ziqi added method...
142
  
31394e2a   heziqi   Ziqi added method...
143
144
  
  		//initialize lownum, highnum, low, high		
fc712213   David Mayerich   code simplifications
145
146
147
148
  		ai=0;
  
  		//if no baseline point is specified at band 0,
  			//set the baseline point at band 0 to 0
31394e2a   heziqi   Ziqi added method...
149
  		if(bands[0] != 0){
fc712213   David Mayerich   code simplifications
150
151
  			bi = bands[control];			
  			memset(a, (char)0, S);
31394e2a   heziqi   Ziqi added method...
152
  		}
fc712213   David Mayerich   code simplifications
153
  		//else get the low band
31394e2a   heziqi   Ziqi added method...
154
155
  		else{
  			control += 1;
fc712213   David Mayerich   code simplifications
156
157
  			getBand(a, ai);
  			bi = bands[control];
31394e2a   heziqi   Ziqi added method...
158
  		}
fc712213   David Mayerich   code simplifications
159
160
  		//get the high band
  		getBand(b, bi);
31394e2a   heziqi   Ziqi added method...
161
162
  
  		//correct every band 
fc712213   David Mayerich   code simplifications
163
164
165
166
  		for(unsigned ci = 0; ci < B; ci++){
  
  			//update baseline points, if necessary
  			if( ci == bi && ci != B - 1) {
31394e2a   heziqi   Ziqi added method...
167
168
169
  				//if the high band is now on the last BL point?
  				if (control != N-1) {
  
fc712213   David Mayerich   code simplifications
170
  					control++;		//increment the index
31394e2a   heziqi   Ziqi added method...
171
  
fc712213   David Mayerich   code simplifications
172
  					std::swap<T*>(a, b);	//swap the baseline band pointers
31394e2a   heziqi   Ziqi added method...
173
  
fc712213   David Mayerich   code simplifications
174
175
176
  					ai = bi;
  					bi = bands[control];
  					getBand(b, bi);
31394e2a   heziqi   Ziqi added method...
177
178
179
  
  				}
  				//if the last BL point on the last band of the file?
fc712213   David Mayerich   code simplifications
180
  				else if ( bands[control] != B - 1) {
31394e2a   heziqi   Ziqi added method...
181
  
fc712213   David Mayerich   code simplifications
182
  					std::swap<T*>(a, b);	//swap the baseline band pointers
31394e2a   heziqi   Ziqi added method...
183
  
fc712213   David Mayerich   code simplifications
184
  					memset(b, (char)0, S);	//clear the high band
31394e2a   heziqi   Ziqi added method...
185
  
fc712213   David Mayerich   code simplifications
186
187
  					ai = bi;
  					bi = B - 1;
31394e2a   heziqi   Ziqi added method...
188
189
  				}
  			}
fc712213   David Mayerich   code simplifications
190
191
192
193
194
195
196
197
  
  			//get the current band
  			getBand(c, ci);
  
  			//perform the baseline correction
  			for(unsigned i=0; i < XY; i++){
  				double r = (double) (ci - ai) / (double) (bi - ai);
  				c[i] = c[i] - (b[i] - a[i]) * r - a[i];
31394e2a   heziqi   Ziqi added method...
198
199
  			}
  			
fc712213   David Mayerich   code simplifications
200
  			target.write(reinterpret_cast<const char*>(c), S);   //write the corrected data into destination
31394e2a   heziqi   Ziqi added method...
201
202
203
  		
  		}	
  		
fc712213   David Mayerich   code simplifications
204
205
206
207
  		free(a);
  		free(b);
  		free(c);
  		target.close();
31394e2a   heziqi   Ziqi added method...
208
209
210
211
  		return true;
  	}
  
  
3fc1d461   heziqi   Ziqi added saveBa...
212
213
  	};
  }
31394e2a   heziqi   Ziqi added method...
214
  
fc712213   David Mayerich   code simplifications
215