Add a new tool extract_a_sub_image.cpp to extract a sub-image

This commit is contained in:
Laurent Rineau 2019-07-24 14:33:20 +02:00
parent 3d1450b71f
commit eaef60dcfc
2 changed files with 47 additions and 0 deletions

View File

@ -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()

View File

@ -0,0 +1,46 @@
#include <CGAL/ImageIO.h>
#include <iostream>
int main(int argc, char **argv) {
if (argc != 9) {
std::cerr << "Usage: extract_a_sub_image <input> <output> <xmin> <xmax> "
"<ymin> <ymax> <zmin> <zmax>\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<char*>(image->data);
auto* new_data = static_cast<char*>(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);
}