int
main(int argc, char * argv[])
{
if (argc < 7)
{
std::cerr << "Missing parameters. " << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0]
<< " inputImageFile outputImageFile startX startY sizeX sizeY"
<< std::endl;
return EXIT_FAILURE;
}
using PixelType = unsigned char;
inputStart[0] = std::stoi(argv[3]);
inputStart[1] = std::stoi(argv[4]);
size[0] = std::stoi(argv[5]);
size[1] = std::stoi(argv[6]);
inputRegion.SetSize(size);
inputRegion.SetIndex(inputStart);
outputStart[0] = 0;
outputStart[1] = 0;
outputRegion.SetSize(size);
try
{
inputImage = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
if (!inputImage->GetRequestedRegion().IsInside(inputRegion))
{
std::cerr << "Error" << std::endl;
std::cerr << "The region " << inputRegion
<< "is not contained within the input image region "
<< inputImage->GetRequestedRegion() << std::endl;
return EXIT_FAILURE;
}
outputImage->SetRegions(outputRegion);
const ImageType::SpacingType & spacing = inputImage->GetSpacing();
{
outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
}
outputImage->SetSpacing(spacing);
outputImage->SetOrigin(outputOrigin);
outputImage->Allocate();
ConstIteratorType inputIt(inputImage, inputRegion);
IteratorType outputIt(outputImage, outputRegion);
inputIt.GoToBegin();
outputIt.GoToBegin();
while (!inputIt.IsAtEnd())
{
outputIt.Set(inputIt.Get());
++inputIt;
++outputIt;
}
try
{
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}