Commit f0d5a76974f4e7c17194dcbb839694d8946543b6

Authored by heziqi
1 parent 0e48cc9c

Ziqi changed the baseline to double float

Showing 1 changed file with 119 additions and 17 deletions   Show diff stats
@@ -85,8 +85,8 @@ public: @@ -85,8 +85,8 @@ public:
85 getSlice(p, 2, page); 85 getSlice(p, 2, page);
86 } 86 }
87 87
88 - free(p1);  
89 - free(p2); 88 +// free(p1);
  89 +// free(p2);
90 return true; 90 return true;
91 } 91 }
92 92
@@ -107,13 +107,11 @@ public: @@ -107,13 +107,11 @@ public:
107 file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur); //go to the next band 107 file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur); //go to the next band
108 } 108 }
109 109
110 - free(p1);  
111 - free(p2);  
112 return true; 110 return true;
113 } 111 }
114 -  
115 - //baseline correction and save it into file  
116 - bool baseline(std::string outname, std::vector<unsigned> bands ) 112 + /*
  113 + // for bands only
  114 + bool baselineold(std::string outname, std::vector<unsigned> bands )
117 { 115 {
118 unsigned N = bands.size(); //get the number of baseline points 116 unsigned N = bands.size(); //get the number of baseline points
119 117
@@ -153,33 +151,36 @@ public: @@ -153,33 +151,36 @@ public:
153 //else get the low band 151 //else get the low band
154 else{ 152 else{
155 control += 1; 153 control += 1;
156 - getBand(a, ai); 154 + band_index(a, ai);
157 bi = bands[control]; 155 bi = bands[control];
158 } 156 }
159 //get the high band 157 //get the high band
160 - getBand(b, bi); 158 + band_index(b, bi);
161 159
162 //correct every band 160 //correct every band
163 for(unsigned ci = 0; ci < B; ci++){ 161 for(unsigned ci = 0; ci < B; ci++){
164 162
165 //update baseline points, if necessary 163 //update baseline points, if necessary
166 - if( ci == bi && ci != B - 1) { 164 + if( ci == bi && ci != B - 1)
  165 + {
167 //if the high band is now on the last BL point? 166 //if the high band is now on the last BL point?
168 - if (control != N-1) { 167 + if (control != N-1)
  168 + {
169 169
170 control++; //increment the index 170 control++; //increment the index
171 171
172 - std::swap<T*>(a, b); //swap the baseline band pointers 172 + std::swap(a, b); //swap the baseline band pointers
173 173
174 ai = bi; 174 ai = bi;
175 bi = bands[control]; 175 bi = bands[control];
176 - getBand(b, bi); 176 + band_index(b, bi);
177 177
178 } 178 }
179 //if the last BL point on the last band of the file? 179 //if the last BL point on the last band of the file?
180 - else if ( bands[control] != B - 1) { 180 + else if ( bands[control] != B - 1)
  181 + {
181 182
182 - std::swap<T*>(a, b); //swap the baseline band pointers 183 + std::swap(a, b); //swap the baseline band pointers
183 184
184 memset(b, (char)0, S); //clear the high band 185 memset(b, (char)0, S); //clear the high band
185 186
@@ -189,7 +190,7 @@ public: @@ -189,7 +190,7 @@ public:
189 } 190 }
190 191
191 //get the current band 192 //get the current band
192 - getBand(c, ci); 193 + band_index(c, ci);
193 194
194 //perform the baseline correction 195 //perform the baseline correction
195 for(unsigned i=0; i < XY; i++){ 196 for(unsigned i=0; i < XY; i++){
@@ -199,7 +200,7 @@ public: @@ -199,7 +200,7 @@ public:
199 200
200 target.write(reinterpret_cast<const char*>(c), S); //write the corrected data into destination 201 target.write(reinterpret_cast<const char*>(c), S); //write the corrected data into destination
201 202
202 - } 203 + }
203 204
204 free(a); 205 free(a);
205 free(b); 206 free(b);
@@ -207,7 +208,108 @@ public: @@ -207,7 +208,108 @@ public:
207 target.close(); 208 target.close();
208 return true; 209 return true;
209 } 210 }
  211 + */
  212 +
  213 + //baseline correction and save it into file
  214 +
  215 + bool baseline(std::string outname, std::vector<double> wls )
  216 + {
  217 + unsigned N = wls.size(); //get the number of baseline points
210 218
  219 + std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
  220 +
  221 + //simplify image resolution
  222 + unsigned int B = header.bands; //calculate the number of bands
  223 + unsigned int XY = header.samples * header.lines; //calculate the number of pixels in a band
  224 + unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
  225 +
  226 + double ai, bi; //stores the two baseline points wavelength surrounding the current band
  227 + double ci; //stores the current band's wavelength
  228 +// unsigned aii, bii; //stores the two baseline points number surrounding the current band
  229 + unsigned control=0;
  230 +
  231 + T * a; //pointers to the high and low band images
  232 + T * b;
  233 + T * c; //pointer to the current image
  234 +
  235 + a = (T*)malloc( S ); //memory allocation
  236 + b = (T*)malloc( S );
  237 + c = (T*)malloc( S );
  238 +
  239 + if (a == NULL || b == NULL || c == NULL){
  240 + std::cout<<"ERROR: error allocating memory";
  241 + exit(1);
  242 + }
  243 +
  244 +
  245 + //initialize lownum, highnum, low, high
  246 + ai=header.wavelength[0];
  247 +
  248 + //if no baseline point is specified at band 0,
  249 + //set the baseline point at band 0 to 0
  250 + if(wls[0] != header.wavelength[0]){
  251 + bi = wls[control];
  252 + memset(a, (char)0, S);
  253 + }
  254 + //else get the low band
  255 + else{
  256 + control += 1;
  257 + getBand(a, ai);
  258 + bi = wls[control];
  259 + }
  260 + //get the high band
  261 + getBand(b, bi);
  262 +
  263 + //correct every band
  264 + for(unsigned cii = 0; cii < B; cii++){
  265 +
  266 + //update baseline points, if necessary
  267 + if( header.wavelength[cii] >= bi && cii != B - 1) {
  268 + //if the high band is now on the last BL point?
  269 + if (control != N-1) {
  270 +
  271 + control++; //increment the index
  272 +
  273 + std::swap(a, b); //swap the baseline band pointers
  274 +
  275 + ai = bi;
  276 + bi = wls[control];
  277 + getBand(b, bi);
  278 +
  279 + }
  280 + //if the last BL point on the last band of the file?
  281 + else if ( wls[control] < header.wavelength[B - 1]) {
  282 +
  283 + std::swap(a, b); //swap the baseline band pointers
  284 +
  285 + memset(b, (char)0, S); //clear the high band
  286 +
  287 + ai = bi;
  288 + bi = header.wavelength[B - 1];
  289 + }
  290 + }
  291 +
  292 + //get the current band
  293 + band_index(c, cii);
  294 + ci = header.wavelength[cii];
  295 +
  296 + //perform the baseline correction
  297 + for(unsigned i=0; i < XY; i++){
  298 + double r = (double) (ci - ai) / (double) (bi - ai);
  299 + (float) c[i] =(float) ( c[i] - (b[i] - a[i]) * r - a[i] );
  300 + }
  301 +
  302 + target.write(reinterpret_cast<const char*>(c), S); //write the corrected data into destination
  303 +
  304 + }
  305 +
  306 + free(a);
  307 + free(b);
  308 + free(c);
  309 + target.close();
  310 + return true;
  311 + }
  312 +
211 313
212 }; 314 };
213 } 315 }