diff --git a/envi/bsq.h b/envi/bsq.h index da7d02b..9720599 100644 --- a/envi/bsq.h +++ b/envi/bsq.h @@ -85,8 +85,8 @@ public: getSlice(p, 2, page); } - free(p1); - free(p2); +// free(p1); +// free(p2); return true; } @@ -107,13 +107,11 @@ public: file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur); //go to the next band } - free(p1); - free(p2); return true; } - - //baseline correction and save it into file - bool baseline(std::string outname, std::vector bands ) + /* + // for bands only + bool baselineold(std::string outname, std::vector bands ) { unsigned N = bands.size(); //get the number of baseline points @@ -153,33 +151,36 @@ public: //else get the low band else{ control += 1; - getBand(a, ai); + band_index(a, ai); bi = bands[control]; } //get the high band - getBand(b, bi); + band_index(b, bi); //correct every band for(unsigned ci = 0; ci < B; ci++){ //update baseline points, if necessary - if( ci == bi && ci != B - 1) { + if( ci == bi && ci != B - 1) + { //if the high band is now on the last BL point? - if (control != N-1) { + if (control != N-1) + { control++; //increment the index - std::swap(a, b); //swap the baseline band pointers + std::swap(a, b); //swap the baseline band pointers ai = bi; bi = bands[control]; - getBand(b, bi); + band_index(b, bi); } //if the last BL point on the last band of the file? - else if ( bands[control] != B - 1) { + else if ( bands[control] != B - 1) + { - std::swap(a, b); //swap the baseline band pointers + std::swap(a, b); //swap the baseline band pointers memset(b, (char)0, S); //clear the high band @@ -189,7 +190,7 @@ public: } //get the current band - getBand(c, ci); + band_index(c, ci); //perform the baseline correction for(unsigned i=0; i < XY; i++){ @@ -199,7 +200,7 @@ public: target.write(reinterpret_cast(c), S); //write the corrected data into destination - } + } free(a); free(b); @@ -207,7 +208,108 @@ public: target.close(); return true; } + */ + + //baseline correction and save it into file + + bool baseline(std::string outname, std::vector wls ) + { + unsigned N = wls.size(); //get the number of baseline points + std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file + + //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 + + double ai, bi; //stores the two baseline points wavelength surrounding the current band + double ci; //stores the current band's wavelength +// unsigned aii, bii; //stores the two baseline points number surrounding the current band + unsigned control=0; + + T * a; //pointers to the high and low band images + T * b; + T * c; //pointer to the current image + + a = (T*)malloc( S ); //memory allocation + b = (T*)malloc( S ); + c = (T*)malloc( S ); + + if (a == NULL || b == NULL || c == NULL){ + std::cout<<"ERROR: error allocating memory"; + exit(1); + } + + + //initialize lownum, highnum, low, high + ai=header.wavelength[0]; + + //if no baseline point is specified at band 0, + //set the baseline point at band 0 to 0 + if(wls[0] != header.wavelength[0]){ + bi = wls[control]; + memset(a, (char)0, S); + } + //else get the low band + else{ + control += 1; + getBand(a, ai); + bi = wls[control]; + } + //get the high band + getBand(b, bi); + + //correct every band + for(unsigned cii = 0; cii < B; cii++){ + + //update baseline points, if necessary + if( header.wavelength[cii] >= bi && cii != B - 1) { + //if the high band is now on the last BL point? + if (control != N-1) { + + control++; //increment the index + + std::swap(a, b); //swap the baseline band pointers + + ai = bi; + bi = wls[control]; + getBand(b, bi); + + } + //if the last BL point on the last band of the file? + else if ( wls[control] < header.wavelength[B - 1]) { + + std::swap(a, b); //swap the baseline band pointers + + memset(b, (char)0, S); //clear the high band + + ai = bi; + bi = header.wavelength[B - 1]; + } + } + + //get the current band + band_index(c, cii); + ci = header.wavelength[cii]; + + //perform the baseline correction + for(unsigned i=0; i < XY; i++){ + double r = (double) (ci - ai) / (double) (bi - ai); + (float) c[i] =(float) ( c[i] - (b[i] - a[i]) * r - a[i] ); + } + + target.write(reinterpret_cast(c), S); //write the corrected data into destination + + } + + free(a); + free(b); + free(c); + target.close(); + return true; + } + }; } -- libgit2 0.21.4