diff --git a/stim/envi/bil.h b/stim/envi/bil.h index 0d55b99..fe194c1 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -1336,6 +1336,27 @@ public: } } + ///Append two files together along the band dimension + void append(std::string outfile, bil* C, bool PROGRESS = false) { + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing + file.seekg(0, std::ios::beg); //move to the beginning of both files + C->file.seekg(0, std::ios::beg); + size_t a_bytes = X() * Z() * sizeof(T); //calculate the number of bytes in a single plane of this file + size_t b_bytes = C->X() * C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file + if (PROGRESS) progress = 0; + for (size_t y = 0; y < Y(); y++) { + read_plane_xz(a, y); //read a plane from the current file + out.write((char*)a, a_bytes); //write the plane to disk + C->read_plane_xz(b, y); //read a plane from the appending file + out.write((char*)b, b_bytes); + if (PROGRESS) progress = (double)(y + 1) / (double)(Y()) * 100; + } + + out.close(); + } + /// Convolve the given band range with a kernel specified by a vector of coefficients. /// @param outfile is an already open stream to the output file diff --git a/stim/envi/bip.h b/stim/envi/bip.h index ede6684..8ff9ab5 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -1562,6 +1562,26 @@ public: } } + ///Append two files together along the band dimension + void append(std::string outfile, bip* C, bool PROGRESS = false) { + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing + file.seekg(0, std::ios::beg); //move to the beginning of both files + C->file.seekg(0, std::ios::beg); + size_t a_bytes = Z() * sizeof(T); //calculate the number of bytes in a single plane of this file + size_t b_bytes = C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file + if (PROGRESS) progress = 0; + for (size_t xy = 0; xy < X()*Y(); xy++) { + spectrum(a, xy); //read a plane from the current file + out.write((char*)a, a_bytes); //write the plane to disk + C->spectrum(b, xy); //read a plane from the appending file + out.write((char*)b, b_bytes); + if (PROGRESS) progress = (double)(xy + 1) / (double)(X() * Y()) * 100; + } + + out.close(); + } /// Convolve the given band range with a kernel specified by a vector of coefficients. /// @param outfile is an already open stream to the output file diff --git a/stim/envi/bsq.h b/stim/envi/bsq.h index db00e0d..f3becca 100644 --- a/stim/envi/bsq.h +++ b/stim/envi/bsq.h @@ -1359,6 +1359,20 @@ public: out.close(); } + /// Append an image to this one along the band dimension + void append(std::string outfile, bsq* C, bool PROGRESS = false) { + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing + file.seekg(0, std::ios::beg); //move to the beginning of both files + C->file.seekg(0, std::ios::beg); + + if (PROGRESS) progress = 0; + out << file.rdbuf(); //copy the data from this ENVI file + if (PROGRESS) progress = (double)(Z() + 1) / (double)(Z() + C->Z()) * 100; + out << C->file.rdbuf(); //copy the data from the appending file + if (PROGRESS) progress = 100; + out.close(); + } + /// Convolve the given band range with a kernel specified by a vector of coefficients. /// @param outfile is an already open stream to the output file diff --git a/stim/envi/envi.h b/stim/envi/envi.h index 3a5e9c7..0c62f80 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -1728,6 +1728,67 @@ public: } } + void append(std::string outfile, envi C, bool PROGRESS = false) { + if (C.header.samples != header.samples || //verify that the images are the same size + C.header.lines != header.lines) { + std::cout << "ERROR - appended images must be the same size: input = [" << header.samples << " x " << header.lines << "], output = [" << C.header.samples << " x " << C.header.lines << "]" << std::endl; + exit(1); + } + if (C.header.interleave != header.interleave) { + std::cout << "ERROR - appended images must have the same interleave format" << std::endl; + exit(1); + } + + stim::envi_header new_header = header; //create a header for the output image + new_header.bands = header.bands + C.header.bands; //calculate the number of bands in the new image + + if (header.wavelength.size() != 0 && C.header.wavelength.size() != 0) { //if both files contain wavelength information + for (size_t b = 0; b < C.header.wavelength.size(); b++) + new_header.wavelength.push_back(C.header.wavelength[b]); //append the wavelength labels to the new header array + } + else new_header.wavelength.clear(); + + if (header.band_names.size() != 0 && C.header.band_names.size() != 0) { //if both files contain band name information + for (size_t b = 0; b < C.header.band_names.size(); b++) + new_header.band_names.push_back(C.header.band_names[b]); //append the wavelength labels to the new header array + } + else new_header.wavelength.clear(); + + new_header.save(outfile + ".hdr"); //save the output + + if (header.interleave == envi_header::BSQ) { + if (header.data_type == envi_header::float32) + ((bsq*)file)->append(outfile, (bsq*)C.file, PROGRESS); + else if (header.data_type == envi_header::float64) + ((bsq*)file)->append(outfile, (bsq*)C.file, PROGRESS); + else { + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIL) { + if (header.data_type == envi_header::float32) + ((bil*)file)->append(outfile, (bil*)C.file, PROGRESS); + else if (header.data_type == envi_header::float64) + ((bil*)file)->append(outfile, (bil*)C.file, PROGRESS); + else { + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIP) { + if (header.data_type == envi_header::float32) + ((bip*)file)->append(outfile, (bip*)C.file, PROGRESS); + else if (header.data_type == envi_header::float64) + ((bip*)file)->append(outfile, (bip*)C.file, PROGRESS); + else { + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + + } + /// Convolve the given band range with a kernel specified by a vector of coefficients. /// @param outfile is the combined file to be output -- libgit2 0.21.4