Commit f7e9951742e72ef1037da1b62bc3203364b15f92
1 parent
90933f7f
added sifting for BIL files, but it sifts as a BIP
Showing
2 changed files
with
26 additions
and
16 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -825,40 +825,46 @@ public: |
825 | 825 | return true; |
826 | 826 | } |
827 | 827 | |
828 | - ///Saves to disk only those spectra corresponding to mask values != 0 | |
828 | + /// Saves to disk only those spectra corresponding to mask values != 0 | |
829 | + /// Unlike the BIP and BSQ versions of this function, this version saves a different format: the BIL file is saved as a BIP | |
829 | 830 | bool sift(std::string outfile, unsigned char* p){ |
830 | 831 | // Assume X() = X, Y() = Y, Z() = Z. |
831 | 832 | std::ofstream target(outfile.c_str(), std::ios::binary); |
832 | 833 | |
833 | 834 | //for loading pages: |
834 | 835 | unsigned XZ = X() * Z(); //calculate the number of values in an XZ page on disk |
836 | + unsigned int B = Z(); //calculate the number of bands | |
835 | 837 | unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) |
836 | - T * temp = (T*)malloc(L); //allocate memory for a temporary page | |
837 | 838 | |
838 | - //for saving spectra: | |
839 | - unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes) | |
840 | - T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum | |
841 | - spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image | |
839 | + //allocate temporary memory for a XZ slice | |
840 | + T* slice = (T*) malloc(L); | |
841 | + | |
842 | + //allocate temporary memory for a spectrum | |
843 | + T* spec = (T*) malloc(B * sizeof(T)); | |
842 | 844 | |
843 | - for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y() | |
845 | + //for each slice along the y axis | |
846 | + for (unsigned int y = 0; y < Y(); y++) //Select a page by choosing Y coordinate, Y() | |
844 | 847 | { |
845 | - read_plane_y(temp, i); //retrieve an ZX page, store in "temp" | |
846 | - for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X() | |
848 | + read_plane_y(slice, y); //retrieve an ZX page, store in "slice" | |
849 | + | |
850 | + //for each sample along X | |
851 | + for (unsigned x = 0; x < X(); x++) //Select a pixel by choosing X coordinate in the page, X() | |
847 | 852 | { |
848 | - if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel | |
853 | + //if the mask != 0 at that xy pixel | |
854 | + if (p[y * X() + x] != 0) //if the mask != 0 at that XY pixel | |
849 | 855 | { |
850 | - for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel | |
856 | + //for each band at that pixel | |
857 | + for (unsigned int b = 0; b < B; b++) //Select a voxel by choosing Z coordinate at the pixel | |
851 | 858 | { |
852 | - tempZ[k] = temp[k*X() + i]; //Pass the correct spectral value from XZ page into the spectrum to be saved. | |
859 | + spec[b] = slice[b*X() + x]; //Pass the correct spectral value from XZ page into the spectrum to be saved. | |
853 | 860 | } |
854 | - target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2. | |
861 | + target.write((char*)spec, B * sizeof(T)); //write that spectrum to disk. Size is L2. | |
855 | 862 | } |
856 | - else | |
857 | - continue; | |
858 | 863 | } |
859 | 864 | } |
860 | 865 | target.close(); |
861 | - free(temp); | |
866 | + free(slice); | |
867 | + free(spec); | |
862 | 868 | return true; |
863 | 869 | } |
864 | 870 | ... | ... |
stim/envi/envi.h
... | ... | @@ -385,6 +385,10 @@ public: |
385 | 385 | //create a new header |
386 | 386 | envi_header new_header = header; |
387 | 387 | |
388 | + //if a BIL file is sifted, it's saved as a BIP | |
389 | + if(header.interleave == envi_header::BIL) | |
390 | + new_header.interleave = envi_header::BIP; | |
391 | + | |
388 | 392 | //set the number of lines to 1 (this is a matrix with 1 line and N samples) |
389 | 393 | new_header.lines = 1; |
390 | 394 | new_header.samples = nnz; | ... | ... |