From f6169dea23a742c01a4642b185c6e58cbbcf0708 Mon Sep 17 00:00:00 2001 From: heziqi Date: Tue, 7 Oct 2014 23:34:43 +0800 Subject: [PATCH] Ziqi completed bil bip bsq --- envi/bil.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++------------------ envi/bip.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- envi/bsq.h | 41 +++++++++++++++++++---------------------- 3 files changed, 116 insertions(+), 54 deletions(-) diff --git a/envi/bil.h b/envi/bil.h index 2d8e421..d2a0c88 100644 --- a/envi/bil.h +++ b/envi/bil.h @@ -17,7 +17,6 @@ public: using binary::open; using binary::file; - using binary::getSlice; //open a file, given the file and its header's names bool open(std::string filename, std::string headername){ @@ -207,7 +206,7 @@ public: double ai, bi; //stores the two baseline points wavelength surrounding the current band double ci; //stores the current band's wavelength - unsigned control=0; + unsigned control; if (a == NULL || b == NULL || c == NULL){ std::cout<<"ERROR: error allocating memory"; @@ -222,6 +221,7 @@ public: //initialize lownum, highnum, low, high ai = header.wavelength[0]; + control=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]){ @@ -266,16 +266,6 @@ public: bi = header.wavelength[B - 1]; } } - - //get the current YZ line - //band_index(c, cii);??????????????????????????????????? - /* - file.seekg( (k * header.bands * X + cii)* sizeof(T), std::ios::beg); - for(unsigned j = 0; j < X; j++) - { - file.read((char *)(c + j), sizeof(T)); - file.seekg((B - 1) * sizeof(T), std::ios::cur); - }*/ ci = header.wavelength[cii]; @@ -288,7 +278,6 @@ public: } }//loop for YZ line end - std::cout<(c), L); //write the corrected data into destination }//loop for Y slice end @@ -302,7 +291,7 @@ public: } - // normalize the BIP file + // normalize the BIL file bool normalize(std::string outname, double band) { unsigned int B = header.bands; //calculate the number of bands @@ -327,11 +316,11 @@ public: for(unsigned j = 0; j < Y; j++) { getY(c, j); - for(unsigned i = 0; i < X; i++) + for(unsigned i = 0; i < B; i++) { - for(unsigned m = 0; m < B; m++) + for(unsigned m = 0; m < X; m++) { - c[m + i * B] = c[m + i * B] / b[i + j * X]; + c[m + i * X] = c[m + i * X] / b[m + j * X]; } } target.write(reinterpret_cast(c), L); //write normalized data into destination @@ -344,7 +333,7 @@ public: return true; } - //convert BIP file to BSQ file and save it + //convert BIL file to BSQ file and save it bool bsq(std::string outname) { unsigned int S = header.samples * header.lines * sizeof(T); //calculate the number of bytes in a band @@ -360,6 +349,7 @@ public: band_index(p, i); target.write(reinterpret_cast(p), S); //write a band data into target file } + header.interleave = rts::envi::interleaveType::BSQ; //change the type of file in header file header.save(headername); @@ -368,5 +358,46 @@ public: return true; } + //convert bil file to bip file and save it + bool bip(std::string outname) + { + unsigned int S = header.samples * header.bands * sizeof(T); //calculate the number of bytes in a ZX slice + + std::ofstream target(outname.c_str(), std::ios::binary); + std::string headername = outname + ".hdr"; + + T * p; //pointer to the current XZ slice for bil file + p = (T*)malloc(S); + T * q; //pointer to the current ZX slice for bip file + q = (T*)malloc(S); + + for ( unsigned i = 0; i < header.lines; i++) + { + getY(p, i); + for ( unsigned k = 0; k < header.bands; k++) + { + unsigned ks = k * header.samples; + for ( unsigned j = 0; j < header.samples; j++) + q[k + j * header.bands] = p[ks + j]; + } + + target.write(reinterpret_cast(q), S); //write a band data into target file + } + + header.interleave = rts::envi::interleaveType::BIP; //change the type of file in header file + header.save(headername); + + free(p); + free(q); + target.close(); + return true; + } + + //close the file + bool close(){ + file.close(); + return true; + } + }; } \ No newline at end of file diff --git a/envi/bip.h b/envi/bip.h index caaaeca..34349c1 100644 --- a/envi/bip.h +++ b/envi/bip.h @@ -17,7 +17,6 @@ public: using binary::open; using binary::file; - using binary::getSlice; //open a file, given the file and its header's names bool open(std::string filename, std::string headername){ @@ -231,14 +230,10 @@ public: file.seekg((header.bands - 1) * sizeof(T), std::ios::cur); } } - - //free(p1); - //free(p2); return true; } //save one pixel of the BIP file into the memory, and return the pointer - //I have not used it so far bool getSpectrum(T * p, unsigned x, unsigned y){ if ( x >= header.samples || y >= header.lines){ //make sure the sample and line number is right @@ -294,7 +289,7 @@ public: double ai, bi; //stores the two baseline points wavelength surrounding the current band double ci; //stores the current band's wavelength - unsigned control=0; + unsigned control; if (a == NULL || b == NULL || c == NULL){ std::cout<<"ERROR: error allocating memory"; @@ -308,6 +303,7 @@ public: getY(c, k); //initialize lownum, highnum, low, high + control=0; ai = header.wavelength[0]; //if no baseline point is specified at band 0, //set the baseline point at band 0 to 0 @@ -327,7 +323,6 @@ public: //correct every YZ line 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? @@ -402,11 +397,13 @@ public: for(unsigned j = 0; j < Y; j++) { getY(c, j); + unsigned jX = j * X; //to avoid calculating it many times for(unsigned i = 0; i < X; i++) { + unsigned iB = i * B; for(unsigned m = 0; m < B; m++) { - c[m + i * B] = c[m + i * B] / b[i + j * X]; + c[m + iB] = c[m + iB] / b[i + jX]; //perform normalization } } target.write(reinterpret_cast(c), L); //write normalized data into destination @@ -421,27 +418,64 @@ public: //convert BIP file to BSQ file and save it bool bsq(std::string outname) + { + std::string temp = outname + "_temp"; + std::string headtemp = temp + ".hdr"; + //first creat a temporary bil file and convert bip file to bil file + bil(temp); + + rts::bil n; + if(n.open(temp, headtemp)==false){ //open infile + std::cout<<"ERROR: unable to open input file"<(p), S); //write a band data into target file + getY(p, i); + for ( unsigned k = 0; k < header.bands; k++) + { + unsigned ks = k * header.samples; + for ( unsigned j = 0; j < header.samples; j++) + q[ks + j] = p[k + j * header.bands]; + } + target.write(reinterpret_cast(q), S); //write a band data into target file } - header.interleave = rts::envi::interleaveType::BSQ; //change the type of file in header file + + header.interleave = rts::envi::interleaveType::BIL; //change the type of file in header file header.save(headername); free(p); + free(q); target.close(); return true; } + //close the file + bool close(){ + file.close(); + return true; + } + }; } \ No newline at end of file diff --git a/envi/bsq.h b/envi/bsq.h index 324e189..82d1345 100644 --- a/envi/bsq.h +++ b/envi/bsq.h @@ -85,8 +85,6 @@ public: getSlice(p, 2, page); } - free(p1); - free(p2); return true; } @@ -252,27 +250,21 @@ public: //convert BSQ file to BIP file and save it bool bip(std::string outname) { - unsigned int L = header.bands * sizeof(T); //calculate the number of bytes in a spectrum - - std::ofstream target(outname.c_str(), std::ios::binary); - std::string headername = outname + ".hdr"; - - T * p; //pointer to the current spectrum - p = (T*)malloc(L); - - for ( unsigned i = 0; i < header.lines; i++) - { - for ( unsigned j = 0; j < header.samples; j++ ) - { - getSpectrum(p, j, i); - target.write(reinterpret_cast(p), L); //write spectrum data into target file - } + std::string temp = outname + "_temp"; + std::string headtemp = temp + ".hdr"; + //first creat a temporary bil file and convert bsq file to bil file + bil(temp); + + rts::bil n; + if(n.open(temp, headtemp)==false){ //open infile + std::cout<<"ERROR: unable to open input file"<