diff --git a/stim/envi/bil.h b/stim/envi/bil.h index de68ac4..5bef1e0 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -1012,27 +1012,48 @@ public: unsigned long long b0, unsigned long long b1){ - //calculate the new number of samples and lines - unsigned long long sam = x1 - x0; //samples - unsigned long long lin = y1 - y0; //lines - unsigned long long L = sam * Z() * sizeof(T); - //get specified band and save - T* temp = (T*)malloc(L); + //calculate the new image parameters + unsigned long long samples = x1 - x0; + unsigned long long lines = y1 - y0; + unsigned long long bands = b1 - b0; + + //calculate the size of a line + unsigned long long L = samples * sizeof(T); + + + //allocate space for a line + T* temp = (T*)malloc(bands * L); + + //create an output stream to store the cropped file std::ofstream out(outfile.c_str(), std::ios::binary); - unsigned long long jumpb = (X() - sam) * sizeof(T); //jump pointer to the next band - //get start - file.seekg((y0 * X() * Z() + x0) * sizeof(T), std::ios::beg); - for (unsigned i = 0; i < lin; i++) + + //calculate the distance between bands + unsigned long long jumpb = (X() - samples) * sizeof(T); + + //distance needed to jump from the previous line of the last band to the next line of the first band + unsigned long long longjump = ((Z() - b1) * X() + b0 * X()) * sizeof(T); + + //set the start position for the cropped region + file.seekg((y0 * X() * Z() + b0 * X() + x0) * sizeof(T), std::ios::beg); + + for (unsigned x = 0; x < lines; x++) { - for (unsigned j = 0; j < Z(); j++) + for (unsigned z = b0; z < b1; z++) { - file.read((char *)(temp + j * sam), sizeof(T) * sam); + file.read((char *)(temp + z * samples), sizeof(T) * samples); file.seekg(jumpb, std::ios::cur); //go to the next band - thread_data = (double)(i * Z() + j) / (lin * Z()) * 100; + thread_data = (double)(x * Z() + z) / (lines * Z()) * 100; } - out.write(reinterpret_cast(temp), L); //write slice data into target file + + //write slice data into target file + out.write(reinterpret_cast(temp), bands * L); + + //seek to the beginning of the next X-Z slice + file.seekg(longjump, std::ios::cur); } + + //free the temporary frame free(temp); thread_data = 100; diff --git a/stim/envi/bip.h b/stim/envi/bip.h index 897fbe0..82b7eeb 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -1068,24 +1068,50 @@ public: unsigned long long b0, unsigned long long b1){ - //calculate the new number of samples and lines - unsigned long long sam = x1 - x0; //samples - unsigned long long lin = y1 - y0; //lines - unsigned long long L = Z() * sizeof(T); - //get specified band and save + //calculate the new number of samples, lines, and bands + unsigned long long samples = x1 - x0; + unsigned long long lines = y1 - y0; + unsigned long long bands = b1 - b0; + + //calculate the length of one cropped spectrum + unsigned long long L = bands * sizeof(T); + + //unsigned long long L = Z() * sizeof(T); + + //allocate space for the spectrum T* temp = (T*)malloc(L); + + //open an output file for binary writing std::ofstream out(outfile.c_str(), std::ios::binary); - //get start - unsigned long long sp = y0 * X() + x0; //start pixel - for (unsigned i = 0; i < lin; i++) + + //seek to the first pixel in the cropped image + file.seekg( (y0 * X() * Z() + x0 * Z() + b0) * sizeof(T), std::ios::beg); + + //distance between sample spectra in the same line + unsigned long long jump_sample = ( (Z() - b1) + b0 ) * sizeof(T); + + //distance between sample spectra in adjacent lines + unsigned long long jump_line = (X() - x1) * Z() * sizeof(T); + + + //unsigned long long sp = y0 * X() + x0; //start pixel + + //for each pixel in the image + for (unsigned y = 0; y < lines; y++) { - for (unsigned j = 0; j < sam; j++) + for (unsigned x = 0; x < samples; x++) { - pixel(temp, sp + j + i * X()); + //read the cropped spectral region + file.read( (char*) temp, L ); + //pixel(temp, sp + x + y * X()); out.write(reinterpret_cast(temp), L); //write slice data into target file - thread_data = (double)(i * sam + j) / (lin * sam) * 100; + file.seekg(jump_sample, std::ios::cur); + + thread_data = (double)(y * samples + x) / (lines * samples) * 100; } + + file.seekg(jump_line, std::ios::cur); } free(temp); -- libgit2 0.21.4