Commit 8550e94f60fe751491ee61e6ebc242b085f6ae24

Authored by David Mayerich
1 parent 2fcab4f0

added sifting and unsifting support for BIP files

Showing 2 changed files with 93 additions and 27 deletions   Show diff stats
stim/envi/bip.h
... ... @@ -837,40 +837,101 @@ public:
837 837  
838 838  
839 839 /// Saves to disk only those spectra corresponding to mask values != 0
840   - bool sift(std::string outfile, unsigned char* p){
841   - // Assume X() = X, Y() = Y, Z() = Z.
  840 + bool sift(std::string outfile, unsigned char* mask){
  841 +
  842 + //reset the file pointer to the beginning of the file
  843 + file.seekg(0, std::ios::beg);
  844 +
  845 + // open an output stream
842 846 std::ofstream target(outfile.c_str(), std::ios::binary);
843 847  
844   - //for loading pages:
845   - unsigned ZX = X() * Z(); //calculate the number of values in an XZ page on disk
846   - unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes)
847   - T * temp = (T*)malloc(L); //allocate memory for a temporary page
  848 + //allocate space for a single spectrum
  849 + unsigned int B = Z();
  850 + T* spectrum = (T*) malloc(B * sizeof(T));
848 851  
849   - //for saving spectra:
850   - unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes)
851   - T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
852   - spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image
  852 + //calculate the number of pixels in a band
  853 + unsigned int XY = X() * Y();
853 854  
854   - for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y()
855   - {
856   - read_plane_y(temp, i); //retrieve an ZX page, store in "temp"
857   - for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X()
858   - {
859   - if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel
860   - {
861   - for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel
862   - {
863   - tempZ[k] = temp[i*Z() + k]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
864   - }
865   - target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
866   - }
867   - else
868   - continue;
  855 + //for each pixel
  856 + unsigned int skip = 0; //number of spectra to skip
  857 + for(unsigned int x = 0; x < XY; x++){
  858 +
  859 + //if the current pixel isn't masked
  860 + if( mask[x] == 0){
  861 + //increment the number of skipped pixels
  862 + skip++;
869 863 }
  864 + //if the current pixel is masked
  865 + else{
  866 +
  867 + //skip the intermediate pixels
  868 + file.seekg(skip * B * sizeof(T), std::ios::cur);
  869 +
  870 + //set the skip value to zero
  871 + skip = 0;
  872 +
  873 + //read this pixel into memory
  874 + file.read((char *)spectrum, B * sizeof(T));
  875 +
  876 + //write this pixel out
  877 + target.write((char *)spectrum, B * sizeof(T));
  878 + }
  879 +
870 880 }
  881 +
  882 + //close the output file
871 883 target.close();
872   - free(temp);
  884 + free(spectrum);
  885 + }
  886 +
  887 + bool unsift(std::string outfile, unsigned char* mask, unsigned int samples, unsigned int lines){
  888 +
  889 + // open an output stream
  890 + std::ofstream target(outfile.c_str(), std::ios::binary);
  891 +
  892 + //reset the file pointer to the beginning of the file
  893 + file.seekg(0, std::ios::beg);
  894 +
  895 + //allocate space for a single spectrum
  896 + unsigned int B = Z();
  897 + T* spectrum = (T*) malloc(B * sizeof(T));
  898 +
  899 + //allocate space for a spectrum of zeros
  900 + T* zeros = (T*) malloc(B * sizeof(T));
  901 + memset(zeros, 0, B * sizeof(T));
  902 +
  903 + //calculate the number of pixels in a band
  904 + unsigned int XY = samples * lines;
  905 +
  906 + //for each pixel
  907 + unsigned int skip = 0; //number of spectra to skip
  908 + for(unsigned int x = 0; x < XY; x++){
  909 +
  910 + //if the current pixel isn't masked
  911 + if( mask[x] == 0){
  912 +
  913 + //write a bunch of zeros
  914 + target.write((char *)zeros, B * sizeof(T));
  915 + }
  916 + //if the current pixel is masked
  917 + else{
  918 +
  919 + //read a pixel into memory
  920 + file.read((char *)spectrum, B * sizeof(T));
  921 +
  922 + //write this pixel out
  923 + target.write((char *)spectrum, B * sizeof(T));
  924 + }
  925 +
  926 + }
  927 +
  928 + //close the output file
  929 + target.close();
  930 + free(spectrum);
  931 +
873 932 return true;
  933 +
  934 +
874 935 }
875 936  
876 937  
... ...
stim/envi/envi.h
... ... @@ -455,7 +455,12 @@ public:
455 455  
456 456 else if (header.interleave == envi_header::BIP){ //if the infile is bip file
457 457  
458   - std::cout << "ERROR in stim::envi::unsift - BIP files aren't supported yet" << std::endl;
  458 + if (header.data_type == envi_header::float32)
  459 + return ((bip<float>*)file)->unsift(outfile, mask, samples, lines);
  460 + else if (header.data_type == envi_header::float64)
  461 + return ((bip<double>*)file)->unsift(outfile, mask, samples, lines);
  462 + else
  463 + std::cout << "ERROR: unidentified data type" << std::endl;
459 464 }
460 465  
461 466 else{
... ...