From a63668e42882632c9f6e5d039465fb260f985098 Mon Sep 17 00:00:00 2001 From: Jose Tascon Date: Tue, 6 Jul 2021 18:12:24 +0200 Subject: [PATCH] STYLE: Use itk::ReadImage and itk::WriteImage in src/Core examples --- .../Common/AddNoiseToBinaryImage/Code.cxx | 16 ++++----------- src/Core/Common/ConvertArrayToImage/Code.cxx | 8 +------- .../CropImageBySpecifyingRegion/Code.cxx | 1 - src/Core/Common/DisplayImage/Code.cxx | 5 +---- src/Core/Common/FilterImage/Code.cxx | 2 -- .../Common/FilterImageWithoutCopying/Code.cxx | 2 -- src/Core/Common/FindMaxAndMinInImage/Code.cxx | 11 +++------- src/Core/Common/GetImageSize/Code.cxx | 7 +------ .../GetOrSetMemberVariableOfITKClass/Code.cxx | 2 -- src/Core/Common/ImageRegionOverlap/Code.cxx | 8 +------- src/Core/Common/InPlaceFilterOfImage/Code.cxx | 10 +--------- .../IterateImageStartingAtSeed/Code.cxx | 2 -- .../Common/IterateLineThroughImage/Code.cxx | 6 +----- .../Code.cxx | 1 - .../Common/IterateOverSpecificRegion/Code.cxx | 1 - .../Code.cxx | 7 +------ .../Code.cxx | 7 +------ .../IterateRegionWithNeighborhood/Code.cxx | 7 +------ .../IterateRegionWithWriteAccess/Code.cxx | 7 +------ .../IterateRegionWithoutWriteAccess/Code.cxx | 7 +------ .../Code.cxx | 7 +------ .../Code.cxx | 1 - .../Common/MultiThreadOilPainting/Code.cxx | 10 ++++------ .../MultipleInputsOfDifferentType/Code.cxx | 11 ++-------- .../Common/MultipleInputsOfSameType/Code.cxx | 2 -- .../MultipleOutputsOfDifferentType/Code.cxx | 16 ++------------- .../Common/MultipleOutputsOfSameType/Code.cxx | 16 ++------------- .../RandomSelectOfPixelsFromRegion/Code.cxx | 1 - .../Code.cxx | 1 - src/Core/Common/ReadWriteVectorImage/Code.cxx | 11 ++-------- .../Common/SetPixelValueInOneImage/Code.cxx | 7 +------ .../Common/UseParallelizeImageRegion/Code.cxx | 13 +++--------- .../ComputeMedianOfImageAtPixel/Code.cxx | 6 +----- .../ResampleSegmentedImage/Code.cxx | 20 ++++++------------- .../ConvertTriangleMeshToBinaryImage/Code.cxx | 12 +++-------- src/Core/Mesh/ExtractIsoSurface/Code.cxx | 6 ++---- .../ContourSpatialObject/Code.cxx | 6 +----- .../ConvertSpatialObjectToImage/Code.cxx | 8 +------- .../CreateALineSpatialObject/Code.cxx | 6 +----- src/Core/SpatialObjects/Ellipse/Code.cxx | 8 +------- .../TestKernel/GenerateRandomImage/Code.cxx | 6 +----- .../Code.cxx | 20 ++++--------------- .../Code.cxx | 10 ++-------- .../MutualInformationAffine/Code.cxx | 17 +++------------- src/Core/Transform/ScaleAnImage/Code.cxx | 1 - src/Core/Transform/TranslateImage/Code.cxx | 15 ++++---------- 46 files changed, 65 insertions(+), 289 deletions(-) diff --git a/src/Core/Common/AddNoiseToBinaryImage/Code.cxx b/src/Core/Common/AddNoiseToBinaryImage/Code.cxx index a13a65555..b6d579fd3 100644 --- a/src/Core/Common/AddNoiseToBinaryImage/Code.cxx +++ b/src/Core/Common/AddNoiseToBinaryImage/Code.cxx @@ -39,18 +39,14 @@ main(int argc, char * argv[]) } } using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; using IteratorType = itk::ImageRandomNonRepeatingIteratorWithIndex; - using WriterType = itk::ImageFileWriter; // Read the binary file - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); + const auto input = itk::ReadImage(argv[1]); // At x% of the pixels, add a uniform random value between 0 and 255 - IteratorType it(reader->GetOutput(), reader->GetOutput()->GetLargestPossibleRegion()); - it.SetNumberOfSamples(reader->GetOutput()->GetLargestPossibleRegion().GetNumberOfPixels() * percent); + IteratorType it(input, input->GetLargestPossibleRegion()); + it.SetNumberOfSamples(input->GetLargestPossibleRegion().GetNumberOfPixels() * percent); std::cout << "Number of random samples: " << it.GetNumberOfSamples() << std::endl; using GeneratorType = itk::Statistics::MersenneTwisterRandomVariateGenerator; GeneratorType::Pointer random = GeneratorType::New(); @@ -62,11 +58,7 @@ main(int argc, char * argv[]) ++it; } - // Write the file - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(argv[2]); - writer->SetInput(reader->GetOutput()); - writer->Update(); + itk::WriteImage(input, argv[2]); return EXIT_SUCCESS; } diff --git a/src/Core/Common/ConvertArrayToImage/Code.cxx b/src/Core/Common/ConvertArrayToImage/Code.cxx index d6f12fa3e..aea1f0e8a 100644 --- a/src/Core/Common/ConvertArrayToImage/Code.cxx +++ b/src/Core/Common/ConvertArrayToImage/Code.cxx @@ -85,13 +85,7 @@ main(int, char *[]) const bool importImageFilterWillOwnTheBuffer = true; importFilter->SetImportPointer(localBuffer, numberOfPixels, importImageFilterWillOwnTheBuffer); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - - writer->SetFileName("test.png"); - - writer->SetInput(importFilter->GetOutput()); - writer->Update(); + itk::WriteImage(importFilter->GetOutput(), "test.mhd"); return EXIT_SUCCESS; } diff --git a/src/Core/Common/CropImageBySpecifyingRegion/Code.cxx b/src/Core/Common/CropImageBySpecifyingRegion/Code.cxx index c85fbfd84..7d8142d62 100644 --- a/src/Core/Common/CropImageBySpecifyingRegion/Code.cxx +++ b/src/Core/Common/CropImageBySpecifyingRegion/Code.cxx @@ -18,7 +18,6 @@ // This is different from CropImageFilter only in the way // that the region to crop is specified. #include "itkImage.h" -#include #include int diff --git a/src/Core/Common/DisplayImage/Code.cxx b/src/Core/Common/DisplayImage/Code.cxx index c85167df0..9a7de32ea 100644 --- a/src/Core/Common/DisplayImage/Code.cxx +++ b/src/Core/Common/DisplayImage/Code.cxx @@ -42,10 +42,7 @@ main(int argc, char * argv[]) } else { - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - image = reader->GetOutput(); + image = itk::ReadImage(argv[1]); } using RescaleFilterType = itk::RescaleIntensityImageFilter; diff --git a/src/Core/Common/FilterImage/Code.cxx b/src/Core/Common/FilterImage/Code.cxx index 852cb5138..ab4fd5bb8 100644 --- a/src/Core/Common/FilterImage/Code.cxx +++ b/src/Core/Common/FilterImage/Code.cxx @@ -16,8 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" -#include "itkImageFileWriter.h" #include "ImageFilter.h" diff --git a/src/Core/Common/FilterImageWithoutCopying/Code.cxx b/src/Core/Common/FilterImageWithoutCopying/Code.cxx index 3d81fd105..709e7b3ca 100644 --- a/src/Core/Common/FilterImageWithoutCopying/Code.cxx +++ b/src/Core/Common/FilterImageWithoutCopying/Code.cxx @@ -16,8 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" -#include "itkImageFileWriter.h" #include "MyInPlaceImageFilter.h" diff --git a/src/Core/Common/FindMaxAndMinInImage/Code.cxx b/src/Core/Common/FindMaxAndMinInImage/Code.cxx index 3749921b8..c9e24143b 100644 --- a/src/Core/Common/FindMaxAndMinInImage/Code.cxx +++ b/src/Core/Common/FindMaxAndMinInImage/Code.cxx @@ -46,24 +46,19 @@ main(int argc, char * argv[]) return EXIT_FAILURE; } - std::string inputFilename = argv[1]; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - - reader->SetFileName(inputFilename.c_str()); - reader->Update(); + const auto input = itk::ReadImage(argv[1]); using ImageCalculatorFilterType = itk::MinimumMaximumImageCalculator; ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New(); - imageCalculatorFilter->SetImage(reader->GetOutput()); + imageCalculatorFilter->SetImage(input); imageCalculatorFilter->Compute(); using ConnectorType = itk::ImageToVTKImageFilter; ConnectorType::Pointer originalConnector = ConnectorType::New(); - originalConnector->SetInput(reader->GetOutput()); + originalConnector->SetInput(input); vtkSmartPointer originalActor = vtkSmartPointer::New(); #if VTK_MAJOR_VERSION <= 5 diff --git a/src/Core/Common/GetImageSize/Code.cxx b/src/Core/Common/GetImageSize/Code.cxx index 6fddf35f2..58bfc94e4 100644 --- a/src/Core/Common/GetImageSize/Code.cxx +++ b/src/Core/Common/GetImageSize/Code.cxx @@ -33,13 +33,8 @@ main(int argc, char * argv[]) using PixelType = unsigned char; using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::RegionType region = image->GetLargestPossibleRegion(); diff --git a/src/Core/Common/GetOrSetMemberVariableOfITKClass/Code.cxx b/src/Core/Common/GetOrSetMemberVariableOfITKClass/Code.cxx index 6546d7ff4..f1c91fc5a 100644 --- a/src/Core/Common/GetOrSetMemberVariableOfITKClass/Code.cxx +++ b/src/Core/Common/GetOrSetMemberVariableOfITKClass/Code.cxx @@ -16,8 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" -#include "itkImageFileWriter.h" #include "ImageFilterX.h" diff --git a/src/Core/Common/ImageRegionOverlap/Code.cxx b/src/Core/Common/ImageRegionOverlap/Code.cxx index a597cb8cd..9f12ec09e 100644 --- a/src/Core/Common/ImageRegionOverlap/Code.cxx +++ b/src/Core/Common/ImageRegionOverlap/Code.cxx @@ -136,15 +136,9 @@ main(int argc, char * argv[]) ++itC; } - // Writer - using FileWriterType = itk::ImageFileWriter; - FileWriterType::Pointer writer = FileWriterType::New(); - writer->SetFileName(out_file_name); - writer->SetInput(image); - try { - writer->Update(); + itk::WriteImage(image, out_file_name); } catch (itk::ExceptionObject & error) { diff --git a/src/Core/Common/InPlaceFilterOfImage/Code.cxx b/src/Core/Common/InPlaceFilterOfImage/Code.cxx index 8a1738294..8b4da077e 100644 --- a/src/Core/Common/InPlaceFilterOfImage/Code.cxx +++ b/src/Core/Common/InPlaceFilterOfImage/Code.cxx @@ -46,15 +46,7 @@ main(int argc, char * argv[]) return EXIT_FAILURE; } - std::string inputFilename = argv[1]; - - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - - reader->SetFileName(inputFilename.c_str()); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ApplyThresholding(image); diff --git a/src/Core/Common/IterateImageStartingAtSeed/Code.cxx b/src/Core/Common/IterateImageStartingAtSeed/Code.cxx index 31a389dba..2772c4b08 100644 --- a/src/Core/Common/IterateImageStartingAtSeed/Code.cxx +++ b/src/Core/Common/IterateImageStartingAtSeed/Code.cxx @@ -16,10 +16,8 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileWriter.h" #include "itkFloodFilledImageFunctionConditionalIterator.h" #include "itkBinaryThresholdImageFunction.h" -#include "itkImageFileWriter.h" using ImageType = itk::Image; diff --git a/src/Core/Common/IterateLineThroughImage/Code.cxx b/src/Core/Common/IterateLineThroughImage/Code.cxx index 08ee4d2fb..60c97d3dd 100644 --- a/src/Core/Common/IterateLineThroughImage/Code.cxx +++ b/src/Core/Common/IterateLineThroughImage/Code.cxx @@ -45,11 +45,7 @@ main(int argc, char * argv[]) } else { - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - image = reader->GetOutput(); + image = itk::ReadImage(argv[1]); inputFilename = argv[1]; } diff --git a/src/Core/Common/IterateLineThroughImageWithoutWriteAccess/Code.cxx b/src/Core/Common/IterateLineThroughImageWithoutWriteAccess/Code.cxx index 034919c3f..649c281a5 100644 --- a/src/Core/Common/IterateLineThroughImageWithoutWriteAccess/Code.cxx +++ b/src/Core/Common/IterateLineThroughImageWithoutWriteAccess/Code.cxx @@ -16,7 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" #include "itkImageRegionIterator.h" #include "itkLineConstIterator.h" diff --git a/src/Core/Common/IterateOverSpecificRegion/Code.cxx b/src/Core/Common/IterateOverSpecificRegion/Code.cxx index 3bd07be65..4c72947a7 100644 --- a/src/Core/Common/IterateOverSpecificRegion/Code.cxx +++ b/src/Core/Common/IterateOverSpecificRegion/Code.cxx @@ -16,7 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" #include "itkImageRegionExclusionConstIteratorWithIndex.h" namespace diff --git a/src/Core/Common/IterateRegionWithAccessToIndexWithWriteAccess/Code.cxx b/src/Core/Common/IterateRegionWithAccessToIndexWithWriteAccess/Code.cxx index 9488cf325..5e8d8f4d2 100644 --- a/src/Core/Common/IterateRegionWithAccessToIndexWithWriteAccess/Code.cxx +++ b/src/Core/Common/IterateRegionWithAccessToIndexWithWriteAccess/Code.cxx @@ -34,12 +34,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 5; diff --git a/src/Core/Common/IterateRegionWithAccessToIndexWithoutWriteAccess/Code.cxx b/src/Core/Common/IterateRegionWithAccessToIndexWithoutWriteAccess/Code.cxx index 4d479d97f..0b2cb72d5 100644 --- a/src/Core/Common/IterateRegionWithAccessToIndexWithoutWriteAccess/Code.cxx +++ b/src/Core/Common/IterateRegionWithAccessToIndexWithoutWriteAccess/Code.cxx @@ -34,12 +34,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 5; diff --git a/src/Core/Common/IterateRegionWithNeighborhood/Code.cxx b/src/Core/Common/IterateRegionWithNeighborhood/Code.cxx index 2b7b147c8..f268ec80f 100644 --- a/src/Core/Common/IterateRegionWithNeighborhood/Code.cxx +++ b/src/Core/Common/IterateRegionWithNeighborhood/Code.cxx @@ -41,12 +41,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 50; diff --git a/src/Core/Common/IterateRegionWithWriteAccess/Code.cxx b/src/Core/Common/IterateRegionWithWriteAccess/Code.cxx index da72a881c..55755c072 100644 --- a/src/Core/Common/IterateRegionWithWriteAccess/Code.cxx +++ b/src/Core/Common/IterateRegionWithWriteAccess/Code.cxx @@ -41,12 +41,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 5; diff --git a/src/Core/Common/IterateRegionWithoutWriteAccess/Code.cxx b/src/Core/Common/IterateRegionWithoutWriteAccess/Code.cxx index d13483e17..d8904c15a 100644 --- a/src/Core/Common/IterateRegionWithoutWriteAccess/Code.cxx +++ b/src/Core/Common/IterateRegionWithoutWriteAccess/Code.cxx @@ -30,12 +30,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 5; diff --git a/src/Core/Common/IterateWithNeighborhoodWithoutAccess/Code.cxx b/src/Core/Common/IterateWithNeighborhoodWithoutAccess/Code.cxx index 55d63c4a1..172c2fead 100644 --- a/src/Core/Common/IterateWithNeighborhoodWithoutAccess/Code.cxx +++ b/src/Core/Common/IterateWithNeighborhoodWithoutAccess/Code.cxx @@ -30,12 +30,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); - - ImageType::Pointer image = reader->GetOutput(); + ImageType::Pointer image = itk::ReadImage(argv[1]); ImageType::SizeType regionSize; regionSize[0] = 50; diff --git a/src/Core/Common/MakeOutOfBoundsPixelsReturnConstValue/Code.cxx b/src/Core/Common/MakeOutOfBoundsPixelsReturnConstValue/Code.cxx index 5ed4994fc..88874c051 100644 --- a/src/Core/Common/MakeOutOfBoundsPixelsReturnConstValue/Code.cxx +++ b/src/Core/Common/MakeOutOfBoundsPixelsReturnConstValue/Code.cxx @@ -16,7 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" #include "itkConstNeighborhoodIterator.h" #include "itkConstantBoundaryCondition.h" #include "itkImageRegionIterator.h" diff --git a/src/Core/Common/MultiThreadOilPainting/Code.cxx b/src/Core/Common/MultiThreadOilPainting/Code.cxx index 0486440a2..00b7813c1 100644 --- a/src/Core/Common/MultiThreadOilPainting/Code.cxx +++ b/src/Core/Common/MultiThreadOilPainting/Code.cxx @@ -47,20 +47,18 @@ main(int argc, char * argv[]) using ImageType = itk::Image; using FilterType = itk::OilPaintingImageFilter; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - reader->Update(); + std::string inputFileName = argv[1]; + const auto input = itk::ReadImage(inputFileName); FilterType::Pointer filter = FilterType::New(); - filter->SetInput(reader->GetOutput()); + filter->SetInput(input); filter->SetNumberOfBins(numberOfBins); filter->SetRadius(radius); filter->Update(); #ifdef ENABLE_QUICKVIEW QuickView viewer; - viewer.AddImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(reader->GetFileName())); + viewer.AddImage(input, true, itksys::SystemTools::GetFilenameName(inputFileName)); std::stringstream desc; desc << "OilPaintingImageFilter, bins = " << numberOfBins << " radius = " << radius; diff --git a/src/Core/Common/MultipleInputsOfDifferentType/Code.cxx b/src/Core/Common/MultipleInputsOfDifferentType/Code.cxx index 223f990fd..b288468d1 100644 --- a/src/Core/Common/MultipleInputsOfDifferentType/Code.cxx +++ b/src/Core/Common/MultipleInputsOfDifferentType/Code.cxx @@ -30,21 +30,14 @@ main(int, char *[]) using ScalarImageType = itk::Image; using FilterType = itk::ImageFilterMultipleInputsDifferentType; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName("Test.jpg"); - reader->Update(); + const auto input = itk::ReadImage("Test.jpg"); // Create and the filter FilterType::Pointer filter = FilterType::New(); filter->SetInput(reader->GetOutput()); filter->Update(); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("TestOutput.jpg"); - writer->SetInput(filter->GetOutput()); - writer->Update(); + itk::WriteImage(filter->GetOutput(), "TestOutput.jpg"); return EXIT_SUCCESS; } diff --git a/src/Core/Common/MultipleInputsOfSameType/Code.cxx b/src/Core/Common/MultipleInputsOfSameType/Code.cxx index 25e4107e8..c6f6536a6 100644 --- a/src/Core/Common/MultipleInputsOfSameType/Code.cxx +++ b/src/Core/Common/MultipleInputsOfSameType/Code.cxx @@ -16,8 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" -#include "itkImageFileWriter.h" #include "itkImageFilterMultipleInputs.h" diff --git a/src/Core/Common/MultipleOutputsOfDifferentType/Code.cxx b/src/Core/Common/MultipleOutputsOfDifferentType/Code.cxx index fa12d9e93..e22f18119 100644 --- a/src/Core/Common/MultipleOutputsOfDifferentType/Code.cxx +++ b/src/Core/Common/MultipleOutputsOfDifferentType/Code.cxx @@ -34,21 +34,9 @@ main(int, char *[]) FilterType::Pointer filter = FilterType::New(); filter->Update(); - { - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("TestOutput1.jpg"); - writer->SetInput(filter->GetOutput1()); - writer->Update(); - } + itk::WriteImage(filter->GetOutput1(), "TestOutput1.jpg"); - { - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("TestOutput2.jpg"); - writer->SetInput(filter->GetOutput2()); - writer->Update(); - } + itk::WriteImage(filter->GetOutput2(), "TestOutput2.jpg"); return EXIT_SUCCESS; } diff --git a/src/Core/Common/MultipleOutputsOfSameType/Code.cxx b/src/Core/Common/MultipleOutputsOfSameType/Code.cxx index 07fabc00c..db34acaa9 100644 --- a/src/Core/Common/MultipleOutputsOfSameType/Code.cxx +++ b/src/Core/Common/MultipleOutputsOfSameType/Code.cxx @@ -32,21 +32,9 @@ main(int, char *[]) FilterType::Pointer filter = FilterType::New(); filter->Update(); - { - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("TestOutput1.jpg"); - writer->SetInput(filter->GetOutput1()); - writer->Update(); - } + itk::WriteImage(filter->GetOutput1(), "TestOutput1.jpg"); - { - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("TestOutput2.jpg"); - writer->SetInput(filter->GetOutput2()); - writer->Update(); - } + itk::WriteImage(filter->GetOutput2(), "TestOutput2.jpg"); return EXIT_SUCCESS; } diff --git a/src/Core/Common/RandomSelectOfPixelsFromRegion/Code.cxx b/src/Core/Common/RandomSelectOfPixelsFromRegion/Code.cxx index 599290fcb..e7ec0932e 100644 --- a/src/Core/Common/RandomSelectOfPixelsFromRegion/Code.cxx +++ b/src/Core/Common/RandomSelectOfPixelsFromRegion/Code.cxx @@ -16,7 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" #include "itkImageRandomConstIteratorWithIndex.h" int diff --git a/src/Core/Common/RandomSelectPixelFromRegionWithoutReplacee/Code.cxx b/src/Core/Common/RandomSelectPixelFromRegionWithoutReplacee/Code.cxx index 4bfe88213..95e080a26 100644 --- a/src/Core/Common/RandomSelectPixelFromRegionWithoutReplacee/Code.cxx +++ b/src/Core/Common/RandomSelectPixelFromRegionWithoutReplacee/Code.cxx @@ -16,7 +16,6 @@ * *=========================================================================*/ #include "itkImage.h" -#include "itkImageFileReader.h" #include "itkImageRandomNonRepeatingConstIteratorWithIndex.h" int diff --git a/src/Core/Common/ReadWriteVectorImage/Code.cxx b/src/Core/Common/ReadWriteVectorImage/Code.cxx index e3bdb3b77..b3c46b427 100644 --- a/src/Core/Common/ReadWriteVectorImage/Code.cxx +++ b/src/Core/Common/ReadWriteVectorImage/Code.cxx @@ -39,18 +39,11 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(argv[2]); - writer->SetInput(reader->GetOutput()); + const auto input = itk::ReadImage(argv[1]); try { - writer->Update(); + itk::WriteImage(input, argv[2]); } catch (itk::ExceptionObject & err) { diff --git a/src/Core/Common/SetPixelValueInOneImage/Code.cxx b/src/Core/Common/SetPixelValueInOneImage/Code.cxx index 49ff3d17a..689c101bf 100644 --- a/src/Core/Common/SetPixelValueInOneImage/Code.cxx +++ b/src/Core/Common/SetPixelValueInOneImage/Code.cxx @@ -105,14 +105,9 @@ main(int argc, char * argv[]) image->SetPixel(pixelIndex, 200); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetInput(image); - writer->SetFileName(argv[1]); - try { - writer->Update(); + itk::WriteImage(image, argv[1]); } catch (itk::ExceptionObject & error) { diff --git a/src/Core/Common/UseParallelizeImageRegion/Code.cxx b/src/Core/Common/UseParallelizeImageRegion/Code.cxx index 8ef8dca17..4b0347dba 100644 --- a/src/Core/Common/UseParallelizeImageRegion/Code.cxx +++ b/src/Core/Common/UseParallelizeImageRegion/Code.cxx @@ -86,19 +86,12 @@ main(int argc, char * argv[]) return EXIT_FAILURE; } - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(argv[1]); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(argv[2]); - writer->UseCompressionOn(); + const auto input = itk::ReadImage(argv[1]); try { - OutputImageType::Pointer outImage = segmentationAndCustomColorization(reader->GetOutput()); - writer->SetInput(outImage); - writer->Update(); + OutputImageType::Pointer outImage = segmentationAndCustomColorization(input); + itk::WriteImage(outImage, argv[2], true); // compression } catch (itk::ExceptionObject & error) { diff --git a/src/Core/ImageFunction/ComputeMedianOfImageAtPixel/Code.cxx b/src/Core/ImageFunction/ComputeMedianOfImageAtPixel/Code.cxx index d28e9858c..06f89b46b 100644 --- a/src/Core/ImageFunction/ComputeMedianOfImageAtPixel/Code.cxx +++ b/src/Core/ImageFunction/ComputeMedianOfImageAtPixel/Code.cxx @@ -70,9 +70,5 @@ CreateImage(UnsignedCharImageType::Pointer image) ++imageIterator; } - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("input.png"); - writer->SetInput(image); - writer->Update(); + itk::WriteImage(image, "input.png"); } diff --git a/src/Core/ImageFunction/ResampleSegmentedImage/Code.cxx b/src/Core/ImageFunction/ResampleSegmentedImage/Code.cxx index b6d2ae803..e936f04c4 100644 --- a/src/Core/ImageFunction/ResampleSegmentedImage/Code.cxx +++ b/src/Core/ImageFunction/ResampleSegmentedImage/Code.cxx @@ -46,14 +46,11 @@ main(int argc, char * argv[]) using PixelType = unsigned char; using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(inputImageFile); - reader->Update(); + const auto input = itk::ReadImage(inputImageFile); using ResampleFilterType = itk::ResampleImageFilter; ResampleFilterType::Pointer resizeFilter = ResampleFilterType::New(); - resizeFilter->SetInput(reader->GetOutput()); + resizeFilter->SetInput(input); // Compute and set the output size // @@ -72,7 +69,7 @@ main(int argc, char * argv[]) // or we specify new spacing and compute new height and width // and computations that follows need to be modified a little (as it is - const ImageType::SpacingType inputSpacing{ reader->GetOutput()->GetSpacing() }; + const ImageType::SpacingType inputSpacing{ input->GetSpacing() }; ImageType::SpacingType outputSpacing; for (unsigned int dim = 0; dim < Dimension; ++dim) { @@ -80,7 +77,7 @@ main(int argc, char * argv[]) } resizeFilter->SetOutputSpacing(outputSpacing); - const ImageType::RegionType inputRegion{ reader->GetOutput()->GetLargestPossibleRegion() }; + const ImageType::RegionType inputRegion{ input->GetLargestPossibleRegion() }; const ImageType::SizeType inputSize{ inputRegion.GetSize() }; ImageType::SizeType outputSize; for (unsigned int dim = 0; dim < Dimension; ++dim) @@ -100,13 +97,9 @@ main(int argc, char * argv[]) gaussianInterpolator->SetAlpha(3.0); resizeFilter->SetInterpolator(gaussianInterpolator); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetInput(resizeFilter->GetOutput()); - writer->SetFileName(outputImageFileLabelImageInterpolator); try { - writer->Update(); + itk::WriteImage(resizeFilter->GetOutput(), outputImageFileLabelImageInterpolator, true); // compress } catch (itk::ExceptionObject & error) { @@ -118,10 +111,9 @@ main(int argc, char * argv[]) NearestNeighborInterpolatorType::Pointer nearestNeighborInterpolator = NearestNeighborInterpolatorType::New(); resizeFilter->SetInterpolator(nearestNeighborInterpolator); - writer->SetFileName(outputImageFileNearestNeighborInterpolator); try { - writer->Update(); + itk::WriteImage(resizeFilter->GetOutput(), outputImageFileNearestNeighborInterpolator, true); // compress } catch (itk::ExceptionObject & error) { diff --git a/src/Core/Mesh/ConvertTriangleMeshToBinaryImage/Code.cxx b/src/Core/Mesh/ConvertTriangleMeshToBinaryImage/Code.cxx index 9cdf99db1..fe1d553fa 100644 --- a/src/Core/Mesh/ConvertTriangleMeshToBinaryImage/Code.cxx +++ b/src/Core/Mesh/ConvertTriangleMeshToBinaryImage/Code.cxx @@ -53,17 +53,15 @@ main(int argc, char * argv[]) using InputPixelType = unsigned char; using InputImageType = itk::Image; - using ImageReaderType = itk::ImageFileReader; - ImageReaderType::Pointer imageReader = ImageReaderType::New(); - imageReader->SetFileName(inputImageName); + const auto input = itk::ReadImage(inputImageName); using OutputPixelType = unsigned char; using OutputImageType = itk::Image; using CastFilterType = itk::CastImageFilter; CastFilterType::Pointer cast = CastFilterType::New(); - cast->SetInput(imageReader->GetOutput()); + cast->SetInput(input); using FilterType = itk::TriangleMeshToBinaryImageFilter; FilterType::Pointer filter = FilterType::New(); @@ -80,13 +78,9 @@ main(int argc, char * argv[]) return EXIT_FAILURE; } - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(outputImageName); - writer->SetInput(filter->GetOutput()); try { - writer->Update(); + itk::WriteImage(filter->GetOutput(), outputImageName); } catch (itk::ExceptionObject & error) { diff --git a/src/Core/Mesh/ExtractIsoSurface/Code.cxx b/src/Core/Mesh/ExtractIsoSurface/Code.cxx index 0c8a6105d..a2730def0 100644 --- a/src/Core/Mesh/ExtractIsoSurface/Code.cxx +++ b/src/Core/Mesh/ExtractIsoSurface/Code.cxx @@ -44,16 +44,14 @@ main(int argc, char * argv[]) using PixelType = unsigned char; using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(inputFileName); + const auto input = itk::ReadImage(inputFileName); auto lowerThreshold = static_cast(std::stoi(argv[3])); auto upperThreshold = static_cast(std::stoi(argv[4])); using BinaryThresholdFilterType = itk::BinaryThresholdImageFilter; BinaryThresholdFilterType::Pointer threshold = BinaryThresholdFilterType::New(); - threshold->SetInput(reader->GetOutput()); + threshold->SetInput(input); threshold->SetLowerThreshold(lowerThreshold); threshold->SetUpperThreshold(upperThreshold); threshold->SetOutsideValue(0); diff --git a/src/Core/SpatialObjects/ContourSpatialObject/Code.cxx b/src/Core/SpatialObjects/ContourSpatialObject/Code.cxx index fcc852fe1..f0d2eee80 100644 --- a/src/Core/SpatialObjects/ContourSpatialObject/Code.cxx +++ b/src/Core/SpatialObjects/ContourSpatialObject/Code.cxx @@ -63,11 +63,7 @@ main(int /*argc*/, char * /*argv*/[]) imageFilter->SetInput(contour); imageFilter->Update(); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("contour.png"); - writer->SetInput(imageFilter->GetOutput()); - writer->Update(); + itk::WriteImage(imageFilter->GetOutput(), "contour.png"); #ifdef ENABLE_QUICKVIEW QuickView viewer; diff --git a/src/Core/SpatialObjects/ConvertSpatialObjectToImage/Code.cxx b/src/Core/SpatialObjects/ConvertSpatialObjectToImage/Code.cxx index 5dd7a3013..206d10ab3 100644 --- a/src/Core/SpatialObjects/ConvertSpatialObjectToImage/Code.cxx +++ b/src/Core/SpatialObjects/ConvertSpatialObjectToImage/Code.cxx @@ -87,16 +87,10 @@ main(int argc, char * argv[]) imageFilter->SetOutsideValue(0); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - - writer->SetFileName(argv[1]); - writer->SetInput(imageFilter->GetOutput()); - try { imageFilter->Update(); - writer->Update(); + itk::WriteImage(imageFilter->GetOutput(), argv[1], true); // compress } catch (itk::ExceptionObject & excp) { diff --git a/src/Core/SpatialObjects/CreateALineSpatialObject/Code.cxx b/src/Core/SpatialObjects/CreateALineSpatialObject/Code.cxx index 8b41a9e69..6efb7f296 100644 --- a/src/Core/SpatialObjects/CreateALineSpatialObject/Code.cxx +++ b/src/Core/SpatialObjects/CreateALineSpatialObject/Code.cxx @@ -59,11 +59,7 @@ main(int itkNotUsed(argc), char * itkNotUsed(argv)[]) imageFilter->SetInput(line); imageFilter->Update(); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName("line.png"); - writer->SetInput(imageFilter->GetOutput()); - writer->Update(); + itk::WriteImage(imageFilter->GetOutput(), "line.png"); return EXIT_SUCCESS; } diff --git a/src/Core/SpatialObjects/Ellipse/Code.cxx b/src/Core/SpatialObjects/Ellipse/Code.cxx index 5dd7a3013..e7f45ede3 100644 --- a/src/Core/SpatialObjects/Ellipse/Code.cxx +++ b/src/Core/SpatialObjects/Ellipse/Code.cxx @@ -87,16 +87,10 @@ main(int argc, char * argv[]) imageFilter->SetOutsideValue(0); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - - writer->SetFileName(argv[1]); - writer->SetInput(imageFilter->GetOutput()); - try { imageFilter->Update(); - writer->Update(); + itk::WriteImage(imageFilter->GetOutput(), argv[1]); } catch (itk::ExceptionObject & excp) { diff --git a/src/Core/TestKernel/GenerateRandomImage/Code.cxx b/src/Core/TestKernel/GenerateRandomImage/Code.cxx index 3025ee9a2..29f8f5640 100644 --- a/src/Core/TestKernel/GenerateRandomImage/Code.cxx +++ b/src/Core/TestKernel/GenerateRandomImage/Code.cxx @@ -45,13 +45,9 @@ main(int argc, char * argv[]) randomImageSource->SetNumberOfWorkUnits(1); // to produce reproducible results randomImageSource->SetSize(size); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(outputFileName); - writer->SetInput(randomImageSource->GetOutput()); try { - writer->Update(); + itk::WriteImage(randomImageSource->GetOutput(), outputFileName); } catch (itk::ExceptionObject & error) { diff --git a/src/Core/Transform/GlobalRegistrationTwoImagesAffine/Code.cxx b/src/Core/Transform/GlobalRegistrationTwoImagesAffine/Code.cxx index 46a016549..3f3942487 100644 --- a/src/Core/Transform/GlobalRegistrationTwoImagesAffine/Code.cxx +++ b/src/Core/Transform/GlobalRegistrationTwoImagesAffine/Code.cxx @@ -84,17 +84,8 @@ main(int, char *[]) CreateEllipseImage(movingImage); // Write the two synthetic inputs - using WriterType = itk::ImageFileWriter; - - WriterType::Pointer fixedWriter = WriterType::New(); - fixedWriter->SetFileName("fixed.png"); - fixedWriter->SetInput(fixedImage); - fixedWriter->Update(); - - WriterType::Pointer movingWriter = WriterType::New(); - movingWriter->SetFileName("moving.png"); - movingWriter->SetInput(movingImage); - movingWriter->Update(); + itk::WriteImage(fixedImage, "fixed.png"); + itk::WriteImage(movingImage, "moving.png"); // Set the registration inputs registration->SetFixedImage(fixedImage); @@ -197,13 +188,10 @@ main(int, char *[]) using CastFilterType = itk::CastImageFilter; - WriterType::Pointer writer = WriterType::New(); CastFilterType::Pointer caster = CastFilterType::New(); - writer->SetFileName("output.png"); - caster->SetInput(resampler->GetOutput()); - writer->SetInput(caster->GetOutput()); - writer->Update(); + + itk::WriteImage(caster->GetOutput(), "output.png"); return EXIT_SUCCESS; } diff --git a/src/Core/Transform/GlobalRegistrationTwoImagesBSpline/Code.cxx b/src/Core/Transform/GlobalRegistrationTwoImagesBSpline/Code.cxx index 28990a1b4..59126984f 100644 --- a/src/Core/Transform/GlobalRegistrationTwoImagesBSpline/Code.cxx +++ b/src/Core/Transform/GlobalRegistrationTwoImagesBSpline/Code.cxx @@ -227,18 +227,12 @@ main(int itkNotUsed(argc), char * itkNotUsed(argv)[]) using OutputWriterType = itk::ImageFileWriter; - OutputWriterType::Pointer writer = OutputWriterType::New(); - CastFilterType::Pointer caster = CastFilterType::New(); - - - writer->SetFileName("output.png"); - + CastFilterType::Pointer caster = CastFilterType::New(); caster->SetInput(resample->GetOutput()); - writer->SetInput(caster->GetOutput()); try { - writer->Update(); + itk::WriteImage(caster->GetOutput(), "output.png"); } catch (itk::ExceptionObject & err) { diff --git a/src/Core/Transform/MutualInformationAffine/Code.cxx b/src/Core/Transform/MutualInformationAffine/Code.cxx index b961b3c42..448b29547 100644 --- a/src/Core/Transform/MutualInformationAffine/Code.cxx +++ b/src/Core/Transform/MutualInformationAffine/Code.cxx @@ -44,15 +44,9 @@ main(int argc, char * argv[]) std::cout << "Usage: " << argv[0] << " imageFile1 imageFile2 outputFile" << std::endl; return EXIT_FAILURE; } - ReaderType::Pointer fixedReader = ReaderType::New(); - fixedReader->SetFileName(argv[1]); - fixedReader->Update(); - ImageType::Pointer fixedImage = fixedReader->GetOutput(); + ImageType::Pointer fixedImage = itk::ReadImage(argv[1]); - ReaderType::Pointer movingReader = ReaderType::New(); - movingReader->SetFileName(argv[2]); - movingReader->Update(); - ImageType::Pointer movingImage = movingReader->GetOutput(); + ImageType::Pointer movingImage = itk::ReadImage(argv[2]); // We use floats internally using InternalImageType = itk::Image; @@ -255,12 +249,7 @@ main(int argc, char * argv[]) resample->SetOutputDirection(fixedImage->GetDirection()); resample->SetDefaultPixelValue(100); - using WriterType = itk::ImageFileWriter; - - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(argv[3]); - writer->SetInput(resample->GetOutput()); - writer->Update(); + itk::WriteImage(resample->GetOutput(), argv[3]); return EXIT_SUCCESS; } diff --git a/src/Core/Transform/ScaleAnImage/Code.cxx b/src/Core/Transform/ScaleAnImage/Code.cxx index a56c4ccb5..0e0500cc9 100644 --- a/src/Core/Transform/ScaleAnImage/Code.cxx +++ b/src/Core/Transform/ScaleAnImage/Code.cxx @@ -17,7 +17,6 @@ *=========================================================================*/ #include "itkImage.h" #include "itkScaleTransform.h" -#include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkResampleImageFilter.h" diff --git a/src/Core/Transform/TranslateImage/Code.cxx b/src/Core/Transform/TranslateImage/Code.cxx index 8c5bc9cb5..f7c6aeed1 100644 --- a/src/Core/Transform/TranslateImage/Code.cxx +++ b/src/Core/Transform/TranslateImage/Code.cxx @@ -41,10 +41,7 @@ main(int argc, char * argv[]) using PixelType = unsigned char; using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - ReaderType::Pointer reader = ReaderType::New(); - reader->SetFileName(inputFileName); - reader->UpdateOutputInformation(); + const auto input = itk::ReadImage(inputFileName); using TransformType = itk::TranslationTransform; @@ -58,16 +55,12 @@ main(int argc, char * argv[]) using ResampleImageFilterType = itk::ResampleImageFilter; ResampleImageFilterType::Pointer resampleFilter = ResampleImageFilterType::New(); resampleFilter->SetTransform(translation.GetPointer()); - resampleFilter->SetInput(reader->GetOutput()); - resampleFilter->SetSize(reader->GetOutput()->GetLargestPossibleRegion().GetSize()); + resampleFilter->SetInput(input); + resampleFilter->SetSize(input->GetLargestPossibleRegion().GetSize()); - using WriterType = itk::ImageFileWriter; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(outputFileName); - writer->SetInput(resampleFilter->GetOutput()); try { - writer->Update(); + itk::WriteImage(resampleFilter->GetOutput(), outputFileName); } catch (itk::ExceptionObject & error) {