Commit c1078e19cba3f7205f60e9d86e66759585d550b0
1 parent
63fc1df8
HSIproc bug fixes and speed enhancements
Showing
4 changed files
with
25 additions
and
52 deletions
Show diff stats
stim/envi/bil.h
@@ -419,7 +419,7 @@ public: | @@ -419,7 +419,7 @@ public: | ||
419 | /// @param outname is the name of the output BIP file to be saved to disk. | 419 | /// @param outname is the name of the output BIP file to be saved to disk. |
420 | bool bip(std::string outname, bool PROGRESS = false) | 420 | bool bip(std::string outname, bool PROGRESS = false) |
421 | { | 421 | { |
422 | - unsigned int S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice | 422 | + unsigned long long S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice |
423 | 423 | ||
424 | std::ofstream target(outname.c_str(), std::ios::binary); | 424 | std::ofstream target(outname.c_str(), std::ios::binary); |
425 | std::string headername = outname + ".hdr"; | 425 | std::string headername = outname + ".hdr"; |
@@ -844,7 +844,7 @@ public: | @@ -844,7 +844,7 @@ public: | ||
844 | 844 | ||
845 | /// Saves to disk only those spectra corresponding to mask values != 0 | 845 | /// Saves to disk only those spectra corresponding to mask values != 0 |
846 | /// Unlike the BIP and BSQ versions of this function, this version saves a different format: the BIL file is saved as a BIP | 846 | /// Unlike the BIP and BSQ versions of this function, this version saves a different format: the BIL file is saved as a BIP |
847 | - bool sift(std::string outfile, unsigned char* p){ | 847 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false){ |
848 | // Assume X() = X, Y() = Y, Z() = Z. | 848 | // Assume X() = X, Y() = Y, Z() = Z. |
849 | std::ofstream target(outfile.c_str(), std::ios::binary); | 849 | std::ofstream target(outfile.c_str(), std::ios::binary); |
850 | 850 | ||
@@ -878,14 +878,13 @@ public: | @@ -878,14 +878,13 @@ public: | ||
878 | target.write((char*)spec, B * sizeof(T)); //write that spectrum to disk. Size is L2. | 878 | target.write((char*)spec, B * sizeof(T)); //write that spectrum to disk. Size is L2. |
879 | } | 879 | } |
880 | 880 | ||
881 | - progress = (double) (y * X() + x) / (Y() * X()) * 100; | 881 | + if(PROGRESS) progress = (double) ((y+1) * X() + x+1) / (Y() * X()) * 100; |
882 | } | 882 | } |
883 | } | 883 | } |
884 | target.close(); | 884 | target.close(); |
885 | free(slice); | 885 | free(slice); |
886 | free(spec); | 886 | free(spec); |
887 | 887 | ||
888 | - progress = 100; | ||
889 | return true; | 888 | return true; |
890 | } | 889 | } |
891 | 890 |
stim/envi/bip.h
@@ -836,7 +836,7 @@ public: | @@ -836,7 +836,7 @@ public: | ||
836 | 836 | ||
837 | 837 | ||
838 | /// Saves to disk only those spectra corresponding to mask values != 0 | 838 | /// Saves to disk only those spectra corresponding to mask values != 0 |
839 | - bool sift(std::string outfile, unsigned char* mask){ | 839 | + bool sift(std::string outfile, unsigned char* mask, bool PROGRESS = false){ |
840 | 840 | ||
841 | //reset the file pointer to the beginning of the file | 841 | //reset the file pointer to the beginning of the file |
842 | file.seekg(0, std::ios::beg); | 842 | file.seekg(0, std::ios::beg); |
@@ -875,7 +875,7 @@ public: | @@ -875,7 +875,7 @@ public: | ||
875 | //write this pixel out | 875 | //write this pixel out |
876 | target.write((char *)spectrum, B * sizeof(T)); | 876 | target.write((char *)spectrum, B * sizeof(T)); |
877 | 877 | ||
878 | - progress = (double) x / XY * 100; | 878 | + if(PROGRESS) progress = (double) (x+1) / XY * 100; |
879 | } | 879 | } |
880 | 880 | ||
881 | } | 881 | } |
@@ -884,8 +884,6 @@ public: | @@ -884,8 +884,6 @@ public: | ||
884 | target.close(); | 884 | target.close(); |
885 | free(spectrum); | 885 | free(spectrum); |
886 | 886 | ||
887 | - progress = 100; | ||
888 | - | ||
889 | return true; | 887 | return true; |
890 | } | 888 | } |
891 | 889 |
stim/envi/bsq.h
@@ -324,59 +324,35 @@ public: | @@ -324,59 +324,35 @@ public: | ||
324 | return true; | 324 | return true; |
325 | } | 325 | } |
326 | 326 | ||
327 | - /// Convert the current BSQ file to a BIP file with the specified file name. | ||
328 | - | ||
329 | - /// @param outname is the name of the output BIP file to be saved to disk. | ||
330 | - /*bool bip(std::string outname) | ||
331 | - { | ||
332 | - std::string temp = outname + "_temp"; | ||
333 | - std::string headtemp = temp + ".hdr"; | ||
334 | - //first creat a temporary bil file and convert bsq file to bil file | ||
335 | - bil(temp); | ||
336 | - | ||
337 | - stim::bil<T> n; | ||
338 | - if(n.open(temp, X(), Y(), Z(), offset, w)==false){ //open infile | ||
339 | - std::cout<<"ERROR: unable to open input file"<<std::endl; | ||
340 | - exit(1); | ||
341 | - } | ||
342 | - //then convert bil file to bip file | ||
343 | - progress = 0; | ||
344 | - n.bip(outname); | ||
345 | - n.close(); | ||
346 | - remove(temp.c_str()); | ||
347 | - remove(headtemp.c_str()); | ||
348 | - return true; | ||
349 | - }*/ | ||
350 | - | ||
351 | /// Convert the current BSQ file to a BIL file with the specified file name. | 327 | /// Convert the current BSQ file to a BIL file with the specified file name. |
352 | 328 | ||
353 | /// @param outname is the name of the output BIL file to be saved to disk. | 329 | /// @param outname is the name of the output BIL file to be saved to disk. |
354 | bool bil(std::string outname, bool PROGRESS = false) | 330 | bool bil(std::string outname, bool PROGRESS = false) |
355 | { | 331 | { |
356 | //simplify image resolution | 332 | //simplify image resolution |
357 | - unsigned int L = X() * Z() * sizeof(T); //calculate the number of bytes of a ZX slice | 333 | + //unsigned int L = X() * Z() * sizeof(T); //calculate the number of bytes of a ZX slice |
358 | unsigned int jump = (Y() - 1) * X() * sizeof(T); | 334 | unsigned int jump = (Y() - 1) * X() * sizeof(T); |
359 | 335 | ||
360 | std::ofstream target(outname.c_str(), std::ios::binary); | 336 | std::ofstream target(outname.c_str(), std::ios::binary); |
361 | std::string headername = outname + ".hdr"; | 337 | std::string headername = outname + ".hdr"; |
362 | 338 | ||
363 | - unsigned int xz_bytes = X() * Z() * sizeof(T); | ||
364 | - T * xz_slice; //pointer to the current spectrum | ||
365 | - xz_slice = (T*)malloc(xz_bytes); | 339 | + unsigned int L = X(); |
340 | + T* line = (T*)malloc(sizeof(T) * L); | ||
366 | 341 | ||
367 | for ( unsigned y = 0; y < Y(); y++) //for each y position | 342 | for ( unsigned y = 0; y < Y(); y++) //for each y position |
368 | { | 343 | { |
369 | file.seekg(y * X() * sizeof(T), std::ios::beg); //seek to the beginning of the xz slice | 344 | file.seekg(y * X() * sizeof(T), std::ios::beg); //seek to the beginning of the xz slice |
370 | for ( unsigned z = 0; z < Z(); z++ ) //for each band | 345 | for ( unsigned z = 0; z < Z(); z++ ) //for each band |
371 | { | 346 | { |
372 | - file.read((char *)(xz_slice + z * X()), sizeof(T) * X()); //read a line | 347 | + file.read((char *)line, sizeof(T) * X()); //read a line |
348 | + target.write((char*)line, sizeof(T) * X()); //write the line to the output file | ||
373 | file.seekg(jump, std::ios::cur); //seek to the next band | 349 | file.seekg(jump, std::ios::cur); //seek to the next band |
374 | - if(PROGRESS) progress = (double)(y * Z() + z + 1) / (Z() * Y()) * 100; //update the progress counter | 350 | + if(PROGRESS) progress = (double)((y+1) * Z() + z + 1) / (Z() * Y()) * 100; //update the progress counter |
375 | } | 351 | } |
376 | - target.write(reinterpret_cast<const char*>(xz_slice), xz_bytes); //write the generated XZ slice to the target file | 352 | + //std::cout<<progress<<": "<<y<<std::endl; |
377 | } | 353 | } |
378 | 354 | ||
379 | - free(xz_slice); | 355 | + free(line); |
380 | target.close(); | 356 | target.close(); |
381 | 357 | ||
382 | return true; | 358 | return true; |
@@ -842,16 +818,17 @@ public: | @@ -842,16 +818,17 @@ public: | ||
842 | /// Saves to disk only those spectra corresponding to mask values != 0 | 818 | /// Saves to disk only those spectra corresponding to mask values != 0 |
843 | /// @param outfile is the name of the sifted ENVI file to be written to disk | 819 | /// @param outfile is the name of the sifted ENVI file to be written to disk |
844 | /// @param unsigned char* p is the mask file used for sifting | 820 | /// @param unsigned char* p is the mask file used for sifting |
845 | - bool sift(std::string outfile, unsigned char* p){ | 821 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false){ |
846 | std::ofstream target(outfile.c_str(), std::ios::binary); | 822 | std::ofstream target(outfile.c_str(), std::ios::binary); |
847 | // open a band (XY plane) | 823 | // open a band (XY plane) |
848 | unsigned long XY = X() * Y(); //Number of XY pixels | 824 | unsigned long XY = X() * Y(); //Number of XY pixels |
849 | unsigned long L = XY * sizeof(T); //size of XY pixels | 825 | unsigned long L = XY * sizeof(T); //size of XY pixels |
826 | + unsigned long long B = Z(); | ||
850 | 827 | ||
851 | T * temp = (T*)malloc(L); //allocate memory for a band | 828 | T * temp = (T*)malloc(L); //allocate memory for a band |
852 | T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel | 829 | T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel |
853 | 830 | ||
854 | - for (unsigned long i = 0; i < Z(); i++) //for each spectral bin | 831 | + for (unsigned long i = 0; i < B; i++) //for each spectral bin |
855 | { | 832 | { |
856 | band_index(temp, i); //get the specified band (XY sheet by index) | 833 | band_index(temp, i); //get the specified band (XY sheet by index) |
857 | for (unsigned long j = 0; j < XY; j++) // for each pixel | 834 | for (unsigned long j = 0; j < XY; j++) // for each pixel |
@@ -864,8 +841,9 @@ public: | @@ -864,8 +841,9 @@ public: | ||
864 | continue; | 841 | continue; |
865 | } | 842 | } |
866 | 843 | ||
867 | - progress = (double)(i * XY + j) / (XY * Z()) * 100; | 844 | + |
868 | } | 845 | } |
846 | + if(PROGRESS) progress = (double)(i+1)/ B * 100; | ||
869 | } | 847 | } |
870 | target.close(); | 848 | target.close(); |
871 | free(temp); | 849 | free(temp); |
stim/envi/envi.h
@@ -573,7 +573,7 @@ public: | @@ -573,7 +573,7 @@ public: | ||
573 | /// Saves in an array only those spectra corresponding to nonzero values of the mask. | 573 | /// Saves in an array only those spectra corresponding to nonzero values of the mask. |
574 | /// @param outfile is the name of the sifted output file | 574 | /// @param outfile is the name of the sifted output file |
575 | /// @param p is the mask | 575 | /// @param p is the mask |
576 | - bool sift(std::string outfile, unsigned char* p) | 576 | + bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false) |
577 | { | 577 | { |
578 | 578 | ||
579 | //calculate the number of non-zero values in the mask | 579 | //calculate the number of non-zero values in the mask |
@@ -594,31 +594,29 @@ public: | @@ -594,31 +594,29 @@ public: | ||
594 | new_header.samples = nnz; | 594 | new_header.samples = nnz; |
595 | new_header.save(outfile + ".hdr"); | 595 | new_header.save(outfile + ".hdr"); |
596 | 596 | ||
597 | - std::cout<<"Saved: "<<outfile+".hdr"<<std::endl; | ||
598 | - | ||
599 | if (header.interleave == envi_header::BSQ){ //if the infile is bsq file | 597 | if (header.interleave == envi_header::BSQ){ //if the infile is bsq file |
600 | if (header.data_type == envi_header::float32) | 598 | if (header.data_type == envi_header::float32) |
601 | - return ((bsq<float>*)file)->sift(outfile, p); | 599 | + return ((bsq<float>*)file)->sift(outfile, p, PROGRESS); |
602 | else if (header.data_type == envi_header::float64) | 600 | else if (header.data_type == envi_header::float64) |
603 | - return ((bsq<double>*)file)->sift(outfile, p); | 601 | + return ((bsq<double>*)file)->sift(outfile, p, PROGRESS); |
604 | else | 602 | else |
605 | std::cout << "ERROR: unidentified data type" << std::endl; | 603 | std::cout << "ERROR: unidentified data type" << std::endl; |
606 | } | 604 | } |
607 | 605 | ||
608 | else if (header.interleave == envi_header::BIL){ //if the infile is bil file | 606 | else if (header.interleave == envi_header::BIL){ //if the infile is bil file |
609 | if (header.data_type == envi_header::float32) | 607 | if (header.data_type == envi_header::float32) |
610 | - return ((bil<float>*)file)->sift(outfile, p); | 608 | + return ((bil<float>*)file)->sift(outfile, p, PROGRESS); |
611 | else if (header.data_type == envi_header::float64) | 609 | else if (header.data_type == envi_header::float64) |
612 | - return ((bil<double>*)file)->sift(outfile, p); | 610 | + return ((bil<double>*)file)->sift(outfile, p, PROGRESS); |
613 | else | 611 | else |
614 | std::cout << "ERROR: unidentified data type" << std::endl; | 612 | std::cout << "ERROR: unidentified data type" << std::endl; |
615 | } | 613 | } |
616 | 614 | ||
617 | else if (header.interleave == envi_header::BIP){ //if the infile is bip file | 615 | else if (header.interleave == envi_header::BIP){ //if the infile is bip file |
618 | if (header.data_type == envi_header::float32) | 616 | if (header.data_type == envi_header::float32) |
619 | - return ((bip<float>*)file)->sift(outfile, p); | 617 | + return ((bip<float>*)file)->sift(outfile, p, PROGRESS); |
620 | else if (header.data_type == envi_header::float64) | 618 | else if (header.data_type == envi_header::float64) |
621 | - return ((bip<double>*)file)->sift(outfile, p); | 619 | + return ((bip<double>*)file)->sift(outfile, p, PROGRESS); |
622 | else | 620 | else |
623 | std::cout << "ERROR: unidentified data type" << std::endl; | 621 | std::cout << "ERROR: unidentified data type" << std::endl; |
624 | } | 622 | } |