diff --git a/CGAL_ImageIO/examples/CGALimageIO/CMakeLists.txt b/CGAL_ImageIO/examples/CGALimageIO/CMakeLists.txt index 58de82637bb..402cdf97137 100644 --- a/CGAL_ImageIO/examples/CGALimageIO/CMakeLists.txt +++ b/CGAL_ImageIO/examples/CGALimageIO/CMakeLists.txt @@ -16,6 +16,7 @@ if(CGAL_ImageIO_FOUND) create_single_source_cgal_program( "convert_raw_image_to_inr.cpp" ) create_single_source_cgal_program( "test_imageio.cpp" ) + create_single_source_cgal_program( "extract_a_sub_image.cpp" ) else() message(STATUS "NOTICE: This demo needs the CGAL ImageIO library, and will not be compiled.") endif() diff --git a/CGAL_ImageIO/examples/CGALimageIO/extract_a_sub_image.cpp b/CGAL_ImageIO/examples/CGALimageIO/extract_a_sub_image.cpp new file mode 100644 index 00000000000..d626bd113fa --- /dev/null +++ b/CGAL_ImageIO/examples/CGALimageIO/extract_a_sub_image.cpp @@ -0,0 +1,46 @@ +#include +#include + +int main(int argc, char **argv) { + if (argc != 9) { + std::cerr << "Usage: extract_a_sub_image " + " \n"; + return argc != 1; + } + _image *image = ::_readImage(argv[1]); + if (!image) + return 2; + const auto xmin = std::stoul(argv[3]); + const auto xmax = std::stoul(argv[4]); + const auto ymin = std::stoul(argv[5]); + const auto ymax = std::stoul(argv[6]); + const auto zmin = std::stoul(argv[7]); + const auto zmax = std::stoul(argv[8]); + assert(xmin >= 0); + assert(ymin >= 0); + assert(zmin >= 0); + assert(xmax < image->xdim); + assert(ymax < image->ydim); + assert(zmax < image->zdim); + + const int new_xdim = xmax + 1 - xmin; + const int new_ydim = ymax + 1 - ymin; + const int new_zdim = zmax + 1 - zmin; + + auto *new_image = ::_createImage(new_xdim, new_ydim, new_zdim, image->vx, + image->vx, image->vx, image->vz, image->wdim, + image->wordKind, image->sign); + const auto* const data = static_cast(image->data); + auto* new_data = static_cast(new_image->data); + for (auto k = zmin; k < zmax; ++k) + for (auto j = ymin; j <= ymax; ++j) + for (auto i = xmin; i <= xmax; ++i) { + auto pos = data + image->wdim * (i + image->xdim * (j + image->zdim * k)); + std::copy(pos, pos + image->wdim, new_data); + new_data += image->wdim; + } + auto r = ::_writeImage(new_image, argv[2]); + if(r != ImageIO_NO_ERROR) return 3; + ::_freeImage(image); + ::_freeImage(new_image); +}