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;
}
typedef unsigned char PixelType;
ImageType::RegionType inputRegion;
inputStart[0] = ::atoi( argv[3] );
inputStart[1] = ::atoi( argv[4] );
size[0] = ::atoi( argv[5] );
size[1] = ::atoi( argv[6] );
inputRegion.SetSize( size );
inputRegion.SetIndex( inputStart );
ImageType::RegionType outputRegion;
outputStart[0] = 0;
outputStart[1] = 0;
outputRegion.SetSize( size );
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
if ( ! reader->GetOutput()->GetRequestedRegion().IsInside( inputRegion ) )
{
std::cerr << "Error" << std::endl;
std::cerr << "The region " << inputRegion << "is not contained within the input image region "
<< reader->GetOutput()->GetRequestedRegion() << std::endl;
return EXIT_FAILURE;
}
ImageType::Pointer outputImage = ImageType::New();
outputImage->SetRegions( outputRegion );
const ImageType::SpacingType& spacing = reader->GetOutput()->GetSpacing();
{
outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
}
outputImage->SetSpacing( spacing );
outputImage->SetOrigin( outputOrigin );
outputImage->Allocate();
ConstIteratorType inputIt( reader->GetOutput(), inputRegion );
IteratorType outputIt( outputImage, outputRegion );
inputIt.GoToBegin();
outputIt.GoToBegin();
while( !inputIt.IsAtEnd() )
{
outputIt.Set( inputIt.Get() );
++inputIt;
++outputIt;
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[2] );
writer->SetInput( outputImage );
try
{
writer->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}