int main( int argc, char *argv[] )
{
if (argc < 4 )
{
std::cerr << "Usage: ImageReadImageSeriesWrite inputFile outputPrefix outputExtension" << std::endl;
return EXIT_FAILURE;
}
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
WriterType::Pointer writer = WriterType::New();
writer->SetInput( reader->GetOutput() );
NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
std::string format = argv[2];
format += "%03d.";
format += argv[3];
nameGenerator->SetSeriesFormat( format.c_str() );
try
{
reader->Update();
}
{
std::cerr << "Exception thrown while reading the image" << std::endl;
std::cerr << excp << std::endl;
}
ImageType::ConstPointer inputImage = reader->GetOutput();
ImageType::RegionType region = inputImage->GetLargestPossibleRegion();
ImageType::IndexType start = region.GetIndex();
ImageType::SizeType size = region.GetSize();
const unsigned int firstSlice = start[2];
const unsigned int lastSlice = start[2] + size[2] - 1;
nameGenerator->SetStartIndex( firstSlice );
nameGenerator->SetEndIndex( lastSlice );
nameGenerator->SetIncrementIndex( 1 );
writer->SetFileNames( nameGenerator->GetFileNames() );
try
{
writer->Update();
}
{
std::cerr << "Exception thrown while reading the image" << std::endl;
std::cerr << excp << std::endl;
}
return EXIT_SUCCESS;
}