diff --git a/stim/envi/bil.h b/stim/envi/bil.h index 0807f30..465b187 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -825,40 +825,46 @@ public: return true; } - ///Saves to disk only those spectra corresponding to mask values != 0 + /// Saves to disk only those spectra corresponding to mask values != 0 + /// Unlike the BIP and BSQ versions of this function, this version saves a different format: the BIL file is saved as a BIP bool sift(std::string outfile, unsigned char* p){ // Assume X() = X, Y() = Y, Z() = Z. std::ofstream target(outfile.c_str(), std::ios::binary); //for loading pages: unsigned XZ = X() * Z(); //calculate the number of values in an XZ page on disk + unsigned int B = Z(); //calculate the number of bands unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes) - T * temp = (T*)malloc(L); //allocate memory for a temporary page - //for saving spectra: - unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes) - T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum - spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image + //allocate temporary memory for a XZ slice + T* slice = (T*) malloc(L); + + //allocate temporary memory for a spectrum + T* spec = (T*) malloc(B * sizeof(T)); - for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y() + //for each slice along the y axis + for (unsigned int y = 0; y < Y(); y++) //Select a page by choosing Y coordinate, Y() { - read_plane_y(temp, i); //retrieve an ZX page, store in "temp" - for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X() + read_plane_y(slice, y); //retrieve an ZX page, store in "slice" + + //for each sample along X + for (unsigned x = 0; x < X(); x++) //Select a pixel by choosing X coordinate in the page, X() { - if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel + //if the mask != 0 at that xy pixel + if (p[y * X() + x] != 0) //if the mask != 0 at that XY pixel { - for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel + //for each band at that pixel + for (unsigned int b = 0; b < B; b++) //Select a voxel by choosing Z coordinate at the pixel { - tempZ[k] = temp[k*X() + i]; //Pass the correct spectral value from XZ page into the spectrum to be saved. + spec[b] = slice[b*X() + x]; //Pass the correct spectral value from XZ page into the spectrum to be saved. } - target.write(reinterpret_cast(tempZ), LZ); //write that spectrum to disk. Size is L2. + target.write((char*)spec, B * sizeof(T)); //write that spectrum to disk. Size is L2. } - else - continue; } } target.close(); - free(temp); + free(slice); + free(spec); return true; } diff --git a/stim/envi/envi.h b/stim/envi/envi.h index 5fad591..591605d 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -385,6 +385,10 @@ public: //create a new header envi_header new_header = header; + //if a BIL file is sifted, it's saved as a BIP + if(header.interleave == envi_header::BIL) + new_header.interleave = envi_header::BIP; + //set the number of lines to 1 (this is a matrix with 1 line and N samples) new_header.lines = 1; new_header.samples = nnz; -- libgit2 0.21.4