diff --git a/stim/envi/bip.h b/stim/envi/bip.h index b1941cd..d7d8d56 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -1479,9 +1479,9 @@ public: bool PROGRESS = false){ //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; + unsigned long long samples = x1 - x0 + 1; + unsigned long long lines = y1 - y0 + 1; + unsigned long long bands = b1 - b0 + 1; //calculate the length of one cropped spectrum unsigned long long L = bands * sizeof(T); @@ -1489,32 +1489,36 @@ public: //unsigned long long L = Z() * sizeof(T); //allocate space for the spectrum - T* temp = (T*)malloc(L); + char* temp = (char*)malloc(L); //open an output file for binary writing std::ofstream out(outfile.c_str(), std::ios::binary); //seek to the first pixel in the cropped image - file.seekg( (y0 * X() * Z() + x0 * Z() + b0) * sizeof(T), std::ios::beg); + size_t startx = x0 * Z(); + size_t starty = y0 * X() * Z(); + size_t startb = b0; + file.seekg( (starty + startx + startb) * sizeof(T), std::ios::beg); //distance between sample spectra in the same line - unsigned long long jump_sample = ( (Z() - b1) + b0 ) * sizeof(T); + size_t dist_between_samples = Z() - bands; + size_t jump_sample = dist_between_samples * sizeof(T); //distance between sample spectra in adjacent lines - unsigned long long jump_line = ( X() - x1 + x0 ) * Z() * sizeof(T); + //unsigned long long jump_line = ( X() - x1 + x0 ) * Z() * sizeof(T); + size_t dist_between_lines = X() - samples; + size_t jump_line = dist_between_lines * 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 x = 0; x < samples; x++) - { + for (unsigned y = 0; y < lines; y++) { + for (unsigned x = 0; x < samples; x++) { //read the cropped spectral region - file.read( (char*) temp, L ); + file.read(temp, L ); //pixel(temp, sp + x + y * X()); - out.write(reinterpret_cast(temp), L); //write slice data into target file + out.write(temp, L); //write slice data into target file file.seekg(jump_sample, std::ios::cur); @@ -1524,6 +1528,7 @@ public: file.seekg(jump_line, std::ios::cur); } free(temp); + out.close(); return true; } diff --git a/stim/envi/envi.h b/stim/envi/envi.h index adc46fa..3a5e9c7 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -1545,11 +1545,11 @@ public: //save the header for the cropped file stim::envi_header new_header = header; - new_header.samples = x1 - x0; - new_header.lines = y1 - y0; - new_header.bands = b1 - b0; + new_header.samples = x1 - x0 + 1; + new_header.lines = y1 - y0 + 1; + new_header.bands = b1 - b0 + 1; std::vector::const_iterator first = new_header.wavelength.begin() + b0; - std::vector::const_iterator last = new_header.wavelength.begin() + b1; + std::vector::const_iterator last = new_header.wavelength.begin() + b1 + 1; new_header.wavelength = std::vector(first, last); new_header.save(outfile + ".hdr"); -- libgit2 0.21.4