Blame view

envi/bsq.h 3.89 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
31394e2a   heziqi   Ziqi added method...
36
  	bool getBand( 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
47
  		return true;
  	}
  
  	//save one pixel of the file into the memory, and return the pointer
31394e2a   heziqi   Ziqi added method...
48
  	bool getSpectrum(T * p, unsigned x, unsigned y){
3fc1d461   heziqi   Ziqi added saveBa...
49
50
51
52
53
54
55
56
  
  		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...
57
  		file.seekg((x + y * header.samples) * sizeof(T), std::ios::beg);           //point to the certain sample and line
3fc1d461   heziqi   Ziqi added saveBa...
58
59
60
  		for (i = 0; i < header.bands; i++)
  		{
  			file.read((char *)(p + i), sizeof(T));
31394e2a   heziqi   Ziqi added method...
61
  			file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur);    //go to the next band
3fc1d461   heziqi   Ziqi added saveBa...
62
63
64
65
66
  		}
  
  		return true;	
  	}
  
31394e2a   heziqi   Ziqi added method...
67
68
69
  	//baseline correction and save it into file
  	bool baseline(std::string outname, std::vector<unsigned> bands )
  	{
fc712213   David Mayerich   code simplifications
70
71
72
  		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...
73
  
fc712213   David Mayerich   code simplifications
74
75
76
77
  		//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...
78
  
fc712213   David Mayerich   code simplifications
79
  		unsigned ai, bi;	//stores the two baseline points surrounding the current band
31394e2a   heziqi   Ziqi added method...
80
81
  		unsigned control=0;
  
fc712213   David Mayerich   code simplifications
82
83
84
  		T * a;					//pointers to the high and low band images
  		T * b;
  		T * c;				//pointer to the current image
31394e2a   heziqi   Ziqi added method...
85
  
fc712213   David Mayerich   code simplifications
86
87
88
  		a = (T*)malloc( S );     //memory allocation
  		b = (T*)malloc( S ); 
  		c = (T*)malloc( S ); 
31394e2a   heziqi   Ziqi added method...
89
  
fc712213   David Mayerich   code simplifications
90
91
92
93
  		if (a == NULL || b == NULL || c == NULL){
  			std::cout<<"ERROR: error allocating memory";
  			exit(1);
  		}
31394e2a   heziqi   Ziqi added method...
94
  
31394e2a   heziqi   Ziqi added method...
95
96
  
  		//initialize lownum, highnum, low, high		
fc712213   David Mayerich   code simplifications
97
98
99
100
  		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...
101
  		if(bands[0] != 0){
fc712213   David Mayerich   code simplifications
102
103
  			bi = bands[control];			
  			memset(a, (char)0, S);
31394e2a   heziqi   Ziqi added method...
104
  		}
fc712213   David Mayerich   code simplifications
105
  		//else get the low band
31394e2a   heziqi   Ziqi added method...
106
107
  		else{
  			control += 1;
fc712213   David Mayerich   code simplifications
108
109
  			getBand(a, ai);
  			bi = bands[control];
31394e2a   heziqi   Ziqi added method...
110
  		}
fc712213   David Mayerich   code simplifications
111
112
  		//get the high band
  		getBand(b, bi);
31394e2a   heziqi   Ziqi added method...
113
114
  
  		//correct every band 
fc712213   David Mayerich   code simplifications
115
116
117
118
  		for(unsigned ci = 0; ci < B; ci++){
  
  			//update baseline points, if necessary
  			if( ci == bi && ci != B - 1) {
31394e2a   heziqi   Ziqi added method...
119
120
121
  				//if the high band is now on the last BL point?
  				if (control != N-1) {
  
fc712213   David Mayerich   code simplifications
122
  					control++;		//increment the index
31394e2a   heziqi   Ziqi added method...
123
  
fc712213   David Mayerich   code simplifications
124
  					std::swap<T*>(a, b);	//swap the baseline band pointers
31394e2a   heziqi   Ziqi added method...
125
  
fc712213   David Mayerich   code simplifications
126
127
128
  					ai = bi;
  					bi = bands[control];
  					getBand(b, bi);
31394e2a   heziqi   Ziqi added method...
129
130
131
  
  				}
  				//if the last BL point on the last band of the file?
fc712213   David Mayerich   code simplifications
132
  				else if ( bands[control] != B - 1) {
31394e2a   heziqi   Ziqi added method...
133
  
fc712213   David Mayerich   code simplifications
134
  					std::swap<T*>(a, b);	//swap the baseline band pointers
31394e2a   heziqi   Ziqi added method...
135
  
fc712213   David Mayerich   code simplifications
136
  					memset(b, (char)0, S);	//clear the high band
31394e2a   heziqi   Ziqi added method...
137
  
fc712213   David Mayerich   code simplifications
138
139
  					ai = bi;
  					bi = B - 1;
31394e2a   heziqi   Ziqi added method...
140
141
  				}
  			}
fc712213   David Mayerich   code simplifications
142
143
144
145
146
147
148
149
  
  			//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...
150
151
  			}
  			
fc712213   David Mayerich   code simplifications
152
  			target.write(reinterpret_cast<const char*>(c), S);   //write the corrected data into destination
31394e2a   heziqi   Ziqi added method...
153
154
155
  		
  		}	
  		
fc712213   David Mayerich   code simplifications
156
157
158
159
  		free(a);
  		free(b);
  		free(c);
  		target.close();
31394e2a   heziqi   Ziqi added method...
160
161
162
163
  		return true;
  	}
  
  
3fc1d461   heziqi   Ziqi added saveBa...
164
165
  	};
  }
31394e2a   heziqi   Ziqi added method...
166
  
fc712213   David Mayerich   code simplifications
167