Commit f990d89d62566aa69e6ef293acd33b9a504b3f32
1 parent
65c1aac0
added an append command for concatenating bands from multiple files
Showing
4 changed files
with
113 additions
and
0 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -1336,6 +1336,27 @@ public: |
1336 | 1336 | } |
1337 | 1337 | } |
1338 | 1338 | |
1339 | + ///Append two files together along the band dimension | |
1340 | + void append(std::string outfile, bil<T>* C, bool PROGRESS = false) { | |
1341 | + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing | |
1342 | + file.seekg(0, std::ios::beg); //move to the beginning of both files | |
1343 | + C->file.seekg(0, std::ios::beg); | |
1344 | + size_t a_bytes = X() * Z() * sizeof(T); //calculate the number of bytes in a single plane of this file | |
1345 | + size_t b_bytes = C->X() * C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file | |
1346 | + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file | |
1347 | + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file | |
1348 | + if (PROGRESS) progress = 0; | |
1349 | + for (size_t y = 0; y < Y(); y++) { | |
1350 | + read_plane_xz(a, y); //read a plane from the current file | |
1351 | + out.write((char*)a, a_bytes); //write the plane to disk | |
1352 | + C->read_plane_xz(b, y); //read a plane from the appending file | |
1353 | + out.write((char*)b, b_bytes); | |
1354 | + if (PROGRESS) progress = (double)(y + 1) / (double)(Y()) * 100; | |
1355 | + } | |
1356 | + | |
1357 | + out.close(); | |
1358 | + } | |
1359 | + | |
1339 | 1360 | /// Convolve the given band range with a kernel specified by a vector of coefficients. |
1340 | 1361 | |
1341 | 1362 | /// @param outfile is an already open stream to the output file | ... | ... |
stim/envi/bip.h
... | ... | @@ -1562,6 +1562,26 @@ public: |
1562 | 1562 | } |
1563 | 1563 | } |
1564 | 1564 | |
1565 | + ///Append two files together along the band dimension | |
1566 | + void append(std::string outfile, bip<T>* C, bool PROGRESS = false) { | |
1567 | + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing | |
1568 | + file.seekg(0, std::ios::beg); //move to the beginning of both files | |
1569 | + C->file.seekg(0, std::ios::beg); | |
1570 | + size_t a_bytes = Z() * sizeof(T); //calculate the number of bytes in a single plane of this file | |
1571 | + size_t b_bytes = C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file | |
1572 | + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file | |
1573 | + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file | |
1574 | + if (PROGRESS) progress = 0; | |
1575 | + for (size_t xy = 0; xy < X()*Y(); xy++) { | |
1576 | + spectrum(a, xy); //read a plane from the current file | |
1577 | + out.write((char*)a, a_bytes); //write the plane to disk | |
1578 | + C->spectrum(b, xy); //read a plane from the appending file | |
1579 | + out.write((char*)b, b_bytes); | |
1580 | + if (PROGRESS) progress = (double)(xy + 1) / (double)(X() * Y()) * 100; | |
1581 | + } | |
1582 | + | |
1583 | + out.close(); | |
1584 | + } | |
1565 | 1585 | /// Convolve the given band range with a kernel specified by a vector of coefficients. |
1566 | 1586 | |
1567 | 1587 | /// @param outfile is an already open stream to the output file | ... | ... |
stim/envi/bsq.h
... | ... | @@ -1359,6 +1359,20 @@ public: |
1359 | 1359 | out.close(); |
1360 | 1360 | } |
1361 | 1361 | |
1362 | + /// Append an image to this one along the band dimension | |
1363 | + void append(std::string outfile, bsq<T>* C, bool PROGRESS = false) { | |
1364 | + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing | |
1365 | + file.seekg(0, std::ios::beg); //move to the beginning of both files | |
1366 | + C->file.seekg(0, std::ios::beg); | |
1367 | + | |
1368 | + if (PROGRESS) progress = 0; | |
1369 | + out << file.rdbuf(); //copy the data from this ENVI file | |
1370 | + if (PROGRESS) progress = (double)(Z() + 1) / (double)(Z() + C->Z()) * 100; | |
1371 | + out << C->file.rdbuf(); //copy the data from the appending file | |
1372 | + if (PROGRESS) progress = 100; | |
1373 | + out.close(); | |
1374 | + } | |
1375 | + | |
1362 | 1376 | /// Convolve the given band range with a kernel specified by a vector of coefficients. |
1363 | 1377 | |
1364 | 1378 | /// @param outfile is an already open stream to the output file | ... | ... |
stim/envi/envi.h
... | ... | @@ -1728,6 +1728,64 @@ public: |
1728 | 1728 | } |
1729 | 1729 | } |
1730 | 1730 | |
1731 | + void append(std::string outfile, envi C, bool PROGRESS = false) { | |
1732 | + if (C.header.samples != header.samples || //verify that the images are the same size | |
1733 | + C.header.lines != header.lines) { | |
1734 | + 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; | |
1735 | + exit(1); | |
1736 | + } | |
1737 | + if (C.header.interleave != header.interleave) { | |
1738 | + std::cout << "ERROR - appended images must have the same interleave format" << std::endl; | |
1739 | + exit(1); | |
1740 | + } | |
1741 | + | |
1742 | + stim::envi_header new_header = header; //create a header for the output image | |
1743 | + new_header.bands = header.bands + C.header.bands; //calculate the number of bands in the new image | |
1744 | + | |
1745 | + if (header.wavelength.size() != 0 && C.header.wavelength.size() != 0) { //if both files contain wavelength information | |
1746 | + for (size_t b = 0; b < C.header.wavelength.size(); b++) | |
1747 | + new_header.wavelength.push_back(C.header.wavelength[b]); //append the wavelength labels to the new header array | |
1748 | + } | |
1749 | + if (header.band_names.size() != 0 && C.header.band_names.size() != 0) { //if both files contain band name information | |
1750 | + for (size_t b = 0; b < C.header.band_names.size(); b++) | |
1751 | + new_header.band_names.push_back(C.header.band_names[b]); //append the wavelength labels to the new header array | |
1752 | + } | |
1753 | + | |
1754 | + new_header.save(outfile + ".hdr"); //save the output | |
1755 | + | |
1756 | + if (header.interleave == envi_header::BSQ) { | |
1757 | + if (header.data_type == envi_header::float32) | |
1758 | + ((bsq<float>*)file)->append(outfile, (bsq<float>*)C.file, PROGRESS); | |
1759 | + else if (header.data_type == envi_header::float64) | |
1760 | + ((bsq<double>*)file)->append(outfile, (bsq<double>*)C.file, PROGRESS); | |
1761 | + else { | |
1762 | + std::cout << "ERROR: unidentified data type" << std::endl; | |
1763 | + exit(1); | |
1764 | + } | |
1765 | + } | |
1766 | + else if (header.interleave == envi_header::BIL) { | |
1767 | + if (header.data_type == envi_header::float32) | |
1768 | + ((bil<float>*)file)->append(outfile, (bil<float>*)C.file, PROGRESS); | |
1769 | + else if (header.data_type == envi_header::float64) | |
1770 | + ((bil<double>*)file)->append(outfile, (bil<double>*)C.file, PROGRESS); | |
1771 | + else { | |
1772 | + std::cout << "ERROR: unidentified data type" << std::endl; | |
1773 | + exit(1); | |
1774 | + } | |
1775 | + } | |
1776 | + else if (header.interleave == envi_header::BIP) { | |
1777 | + if (header.data_type == envi_header::float32) | |
1778 | + ((bip<float>*)file)->append(outfile, (bip<float>*)C.file, PROGRESS); | |
1779 | + else if (header.data_type == envi_header::float64) | |
1780 | + ((bip<double>*)file)->append(outfile, (bip<double>*)C.file, PROGRESS); | |
1781 | + else { | |
1782 | + std::cout << "ERROR: unidentified data type" << std::endl; | |
1783 | + exit(1); | |
1784 | + } | |
1785 | + } | |
1786 | + | |
1787 | + } | |
1788 | + | |
1731 | 1789 | /// Convolve the given band range with a kernel specified by a vector of coefficients. |
1732 | 1790 | |
1733 | 1791 | /// @param outfile is the combined file to be output | ... | ... |