Commit f6169dea23a742c01a4642b185c6e58cbbcf0708

Authored by heziqi
1 parent c25e7d0d

Ziqi completed bil bip bsq

Showing 3 changed files with 116 additions and 54 deletions   Show diff stats
@@ -17,7 +17,6 @@ public: @@ -17,7 +17,6 @@ public:
17 17
18 using binary<T>::open; 18 using binary<T>::open;
19 using binary<T>::file; 19 using binary<T>::file;
20 - using binary<T>::getSlice;  
21 20
22 //open a file, given the file and its header's names 21 //open a file, given the file and its header's names
23 bool open(std::string filename, std::string headername){ 22 bool open(std::string filename, std::string headername){
@@ -207,7 +206,7 @@ public: @@ -207,7 +206,7 @@ public:
207 206
208 double ai, bi; //stores the two baseline points wavelength surrounding the current band 207 double ai, bi; //stores the two baseline points wavelength surrounding the current band
209 double ci; //stores the current band's wavelength 208 double ci; //stores the current band's wavelength
210 - unsigned control=0; 209 + unsigned control;
211 210
212 if (a == NULL || b == NULL || c == NULL){ 211 if (a == NULL || b == NULL || c == NULL){
213 std::cout<<"ERROR: error allocating memory"; 212 std::cout<<"ERROR: error allocating memory";
@@ -222,6 +221,7 @@ public: @@ -222,6 +221,7 @@ public:
222 221
223 //initialize lownum, highnum, low, high 222 //initialize lownum, highnum, low, high
224 ai = header.wavelength[0]; 223 ai = header.wavelength[0];
  224 + control=0;
225 //if no baseline point is specified at band 0, 225 //if no baseline point is specified at band 0,
226 //set the baseline point at band 0 to 0 226 //set the baseline point at band 0 to 0
227 if(wls[0] != header.wavelength[0]){ 227 if(wls[0] != header.wavelength[0]){
@@ -266,16 +266,6 @@ public: @@ -266,16 +266,6 @@ public:
266 bi = header.wavelength[B - 1]; 266 bi = header.wavelength[B - 1];
267 } 267 }
268 } 268 }
269 -  
270 - //get the current YZ line  
271 - //band_index(c, cii);???????????????????????????????????  
272 - /*  
273 - file.seekg( (k * header.bands * X + cii)* sizeof(T), std::ios::beg);  
274 - for(unsigned j = 0; j < X; j++)  
275 - {  
276 - file.read((char *)(c + j), sizeof(T));  
277 - file.seekg((B - 1) * sizeof(T), std::ios::cur);  
278 - }*/  
279 269
280 ci = header.wavelength[cii]; 270 ci = header.wavelength[cii];
281 271
@@ -288,7 +278,6 @@ public: @@ -288,7 +278,6 @@ public:
288 } 278 }
289 279
290 }//loop for YZ line end 280 }//loop for YZ line end
291 - std::cout<<k<<std::endl;  
292 target.write(reinterpret_cast<const char*>(c), L); //write the corrected data into destination 281 target.write(reinterpret_cast<const char*>(c), L); //write the corrected data into destination
293 }//loop for Y slice end 282 }//loop for Y slice end
294 283
@@ -302,7 +291,7 @@ public: @@ -302,7 +291,7 @@ public:
302 291
303 } 292 }
304 293
305 - // normalize the BIP file 294 + // normalize the BIL file
306 bool normalize(std::string outname, double band) 295 bool normalize(std::string outname, double band)
307 { 296 {
308 unsigned int B = header.bands; //calculate the number of bands 297 unsigned int B = header.bands; //calculate the number of bands
@@ -327,11 +316,11 @@ public: @@ -327,11 +316,11 @@ public:
327 for(unsigned j = 0; j < Y; j++) 316 for(unsigned j = 0; j < Y; j++)
328 { 317 {
329 getY(c, j); 318 getY(c, j);
330 - for(unsigned i = 0; i < X; i++) 319 + for(unsigned i = 0; i < B; i++)
331 { 320 {
332 - for(unsigned m = 0; m < B; m++) 321 + for(unsigned m = 0; m < X; m++)
333 { 322 {
334 - c[m + i * B] = c[m + i * B] / b[i + j * X]; 323 + c[m + i * X] = c[m + i * X] / b[m + j * X];
335 } 324 }
336 } 325 }
337 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination 326 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination
@@ -344,7 +333,7 @@ public: @@ -344,7 +333,7 @@ public:
344 return true; 333 return true;
345 } 334 }
346 335
347 - //convert BIP file to BSQ file and save it 336 + //convert BIL file to BSQ file and save it
348 bool bsq(std::string outname) 337 bool bsq(std::string outname)
349 { 338 {
350 unsigned int S = header.samples * header.lines * sizeof(T); //calculate the number of bytes in a band 339 unsigned int S = header.samples * header.lines * sizeof(T); //calculate the number of bytes in a band
@@ -360,6 +349,7 @@ public: @@ -360,6 +349,7 @@ public:
360 band_index(p, i); 349 band_index(p, i);
361 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file 350 target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file
362 } 351 }
  352 +
363 header.interleave = rts::envi::interleaveType::BSQ; //change the type of file in header file 353 header.interleave = rts::envi::interleaveType::BSQ; //change the type of file in header file
364 header.save(headername); 354 header.save(headername);
365 355
@@ -368,5 +358,46 @@ public: @@ -368,5 +358,46 @@ public:
368 return true; 358 return true;
369 } 359 }
370 360
  361 + //convert bil file to bip file and save it
  362 + bool bip(std::string outname)
  363 + {
  364 + unsigned int S = header.samples * header.bands * sizeof(T); //calculate the number of bytes in a ZX slice
  365 +
  366 + std::ofstream target(outname.c_str(), std::ios::binary);
  367 + std::string headername = outname + ".hdr";
  368 +
  369 + T * p; //pointer to the current XZ slice for bil file
  370 + p = (T*)malloc(S);
  371 + T * q; //pointer to the current ZX slice for bip file
  372 + q = (T*)malloc(S);
  373 +
  374 + for ( unsigned i = 0; i < header.lines; i++)
  375 + {
  376 + getY(p, i);
  377 + for ( unsigned k = 0; k < header.bands; k++)
  378 + {
  379 + unsigned ks = k * header.samples;
  380 + for ( unsigned j = 0; j < header.samples; j++)
  381 + q[k + j * header.bands] = p[ks + j];
  382 + }
  383 +
  384 + target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
  385 + }
  386 +
  387 + header.interleave = rts::envi::interleaveType::BIP; //change the type of file in header file
  388 + header.save(headername);
  389 +
  390 + free(p);
  391 + free(q);
  392 + target.close();
  393 + return true;
  394 + }
  395 +
  396 + //close the file
  397 + bool close(){
  398 + file.close();
  399 + return true;
  400 + }
  401 +
371 }; 402 };
372 } 403 }
373 \ No newline at end of file 404 \ No newline at end of file
@@ -17,7 +17,6 @@ public: @@ -17,7 +17,6 @@ public:
17 17
18 using binary<T>::open; 18 using binary<T>::open;
19 using binary<T>::file; 19 using binary<T>::file;
20 - using binary<T>::getSlice;  
21 20
22 //open a file, given the file and its header's names 21 //open a file, given the file and its header's names
23 bool open(std::string filename, std::string headername){ 22 bool open(std::string filename, std::string headername){
@@ -231,14 +230,10 @@ public: @@ -231,14 +230,10 @@ public:
231 file.seekg((header.bands - 1) * sizeof(T), std::ios::cur); 230 file.seekg((header.bands - 1) * sizeof(T), std::ios::cur);
232 } 231 }
233 } 232 }
234 -  
235 - //free(p1);  
236 - //free(p2);  
237 return true; 233 return true;
238 } 234 }
239 235
240 //save one pixel of the BIP file into the memory, and return the pointer 236 //save one pixel of the BIP file into the memory, and return the pointer
241 - //I have not used it so far  
242 bool getSpectrum(T * p, unsigned x, unsigned y){ 237 bool getSpectrum(T * p, unsigned x, unsigned y){
243 238
244 if ( x >= header.samples || y >= header.lines){ //make sure the sample and line number is right 239 if ( x >= header.samples || y >= header.lines){ //make sure the sample and line number is right
@@ -294,7 +289,7 @@ public: @@ -294,7 +289,7 @@ public:
294 289
295 double ai, bi; //stores the two baseline points wavelength surrounding the current band 290 double ai, bi; //stores the two baseline points wavelength surrounding the current band
296 double ci; //stores the current band's wavelength 291 double ci; //stores the current band's wavelength
297 - unsigned control=0; 292 + unsigned control;
298 293
299 if (a == NULL || b == NULL || c == NULL){ 294 if (a == NULL || b == NULL || c == NULL){
300 std::cout<<"ERROR: error allocating memory"; 295 std::cout<<"ERROR: error allocating memory";
@@ -308,6 +303,7 @@ public: @@ -308,6 +303,7 @@ public:
308 getY(c, k); 303 getY(c, k);
309 304
310 //initialize lownum, highnum, low, high 305 //initialize lownum, highnum, low, high
  306 + control=0;
311 ai = header.wavelength[0]; 307 ai = header.wavelength[0];
312 //if no baseline point is specified at band 0, 308 //if no baseline point is specified at band 0,
313 //set the baseline point at band 0 to 0 309 //set the baseline point at band 0 to 0
@@ -327,7 +323,6 @@ public: @@ -327,7 +323,6 @@ public:
327 //correct every YZ line 323 //correct every YZ line
328 324
329 for(unsigned cii = 0; cii < B; cii++){ 325 for(unsigned cii = 0; cii < B; cii++){
330 -  
331 //update baseline points, if necessary 326 //update baseline points, if necessary
332 if( header.wavelength[cii] >= bi && cii != B - 1) { 327 if( header.wavelength[cii] >= bi && cii != B - 1) {
333 //if the high band is now on the last BL point? 328 //if the high band is now on the last BL point?
@@ -402,11 +397,13 @@ public: @@ -402,11 +397,13 @@ public:
402 for(unsigned j = 0; j < Y; j++) 397 for(unsigned j = 0; j < Y; j++)
403 { 398 {
404 getY(c, j); 399 getY(c, j);
  400 + unsigned jX = j * X; //to avoid calculating it many times
405 for(unsigned i = 0; i < X; i++) 401 for(unsigned i = 0; i < X; i++)
406 { 402 {
  403 + unsigned iB = i * B;
407 for(unsigned m = 0; m < B; m++) 404 for(unsigned m = 0; m < B; m++)
408 { 405 {
409 - c[m + i * B] = c[m + i * B] / b[i + j * X]; 406 + c[m + iB] = c[m + iB] / b[i + jX]; //perform normalization
410 } 407 }
411 } 408 }
412 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination 409 target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination
@@ -421,27 +418,64 @@ public: @@ -421,27 +418,64 @@ public:
421 418
422 //convert BIP file to BSQ file and save it 419 //convert BIP file to BSQ file and save it
423 bool bsq(std::string outname) 420 bool bsq(std::string outname)
  421 + {
  422 + std::string temp = outname + "_temp";
  423 + std::string headtemp = temp + ".hdr";
  424 + //first creat a temporary bil file and convert bip file to bil file
  425 + bil(temp);
  426 +
  427 + rts::bil<T> n;
  428 + if(n.open(temp, headtemp)==false){ //open infile
  429 + std::cout<<"ERROR: unable to open input file"<<std::endl;
  430 + exit(1);
  431 + }
  432 + //then convert bil file to bsq file
  433 + n.bsq(outname);
  434 + n.close();
  435 + remove(temp.c_str());
  436 + remove(headtemp.c_str());
  437 + return true;
  438 + }
  439 +
  440 + //convert bip file to bil file and save it
  441 + bool bil(std::string outname)
424 { 442 {
425 - unsigned int S = header.samples * header.lines * sizeof(T); //calculate the number of bytes in a band 443 + unsigned int S = header.samples * header.bands * sizeof(T); //calculate the number of bytes in a ZX slice
426 444
427 std::ofstream target(outname.c_str(), std::ios::binary); 445 std::ofstream target(outname.c_str(), std::ios::binary);
428 std::string headername = outname + ".hdr"; 446 std::string headername = outname + ".hdr";
429 447
430 - T * p; //pointer to the current band 448 + T * p; //pointer to the current ZX slice for bip file
431 p = (T*)malloc(S); 449 p = (T*)malloc(S);
  450 + T * q; //pointer to the current XZ slice for bil file
  451 + q = (T*)malloc(S);
432 452
433 - for ( unsigned i = 0; i < header.bands; i++) 453 + for ( unsigned i = 0; i < header.lines; i++)
434 { 454 {
435 - band_index(p, i);  
436 - target.write(reinterpret_cast<const char*>(p), S); //write a band data into target file 455 + getY(p, i);
  456 + for ( unsigned k = 0; k < header.bands; k++)
  457 + {
  458 + unsigned ks = k * header.samples;
  459 + for ( unsigned j = 0; j < header.samples; j++)
  460 + q[ks + j] = p[k + j * header.bands];
  461 + }
  462 + target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
437 } 463 }
438 - header.interleave = rts::envi::interleaveType::BSQ; //change the type of file in header file 464 +
  465 + header.interleave = rts::envi::interleaveType::BIL; //change the type of file in header file
439 header.save(headername); 466 header.save(headername);
440 467
441 free(p); 468 free(p);
  469 + free(q);
442 target.close(); 470 target.close();
443 return true; 471 return true;
444 } 472 }
445 473
  474 + //close the file
  475 + bool close(){
  476 + file.close();
  477 + return true;
  478 + }
  479 +
446 }; 480 };
447 } 481 }
448 \ No newline at end of file 482 \ No newline at end of file
@@ -85,8 +85,6 @@ public: @@ -85,8 +85,6 @@ public:
85 getSlice(p, 2, page); 85 getSlice(p, 2, page);
86 } 86 }
87 87
88 - free(p1);  
89 - free(p2);  
90 return true; 88 return true;
91 } 89 }
92 90
@@ -252,27 +250,21 @@ public: @@ -252,27 +250,21 @@ public:
252 //convert BSQ file to BIP file and save it 250 //convert BSQ file to BIP file and save it
253 bool bip(std::string outname) 251 bool bip(std::string outname)
254 { 252 {
255 - unsigned int L = header.bands * sizeof(T); //calculate the number of bytes in a spectrum  
256 -  
257 - std::ofstream target(outname.c_str(), std::ios::binary);  
258 - std::string headername = outname + ".hdr";  
259 -  
260 - T * p; //pointer to the current spectrum  
261 - p = (T*)malloc(L);  
262 -  
263 - for ( unsigned i = 0; i < header.lines; i++)  
264 - {  
265 - for ( unsigned j = 0; j < header.samples; j++ )  
266 - {  
267 - getSpectrum(p, j, i);  
268 - target.write(reinterpret_cast<const char*>(p), L); //write spectrum data into target file  
269 - } 253 + std::string temp = outname + "_temp";
  254 + std::string headtemp = temp + ".hdr";
  255 + //first creat a temporary bil file and convert bsq file to bil file
  256 + bil(temp);
  257 +
  258 + rts::bil<T> n;
  259 + if(n.open(temp, headtemp)==false){ //open infile
  260 + std::cout<<"ERROR: unable to open input file"<<std::endl;
  261 + exit(1);
270 } 262 }
271 - header.interleave = rts::envi::interleaveType::BIP; //change the type of file in header file  
272 - header.save(headername);  
273 -  
274 - free(p);  
275 - target.close(); 263 + //then convert bil file to bip file
  264 + n.bip(outname);
  265 + n.close();
  266 + remove(temp.c_str());
  267 + remove(headtemp.c_str());
276 return true; 268 return true;
277 } 269 }
278 270
@@ -307,6 +299,11 @@ public: @@ -307,6 +299,11 @@ public:
307 return true; 299 return true;
308 } 300 }
309 301
  302 + //close the file
  303 + bool close(){
  304 + file.close();
  305 + return true;
  306 + }
310 307
311 }; 308 };
312 } 309 }