ITK  5.4.0
Insight Toolkit
SphinxExamples/src/IO/ImageBase/RegisterIOFactories/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
int
main(int argc, char * argv[])
{
if (argc != 3)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <MetaImageFileName> <PNGFileName>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * metaImageFileName = argv[1];
const char * pngFileName = argv[2];
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using ReaderType = itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
using RegisteredObjectsContainerType = std::list<itk::LightObject::Pointer>;
RegisteredObjectsContainerType registeredIOs = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");
std::cout << "When CMake is not used to register the IO classes, there are\n"
<< registeredIOs.size() << " IO objects available to the ImageFileReader.\n"
<< std::endl;
std::cout << "When we try to read a MetaImage, we will ";
reader->SetFileName(metaImageFileName);
try
{
reader->Update();
}
catch (const itk::ImageFileReaderException &)
{
std::cout << "fail.\n" << std::endl;
}
std::cout << "After registering the MetaImageIO object, ";
std::cout << "there are\n";
registeredIOs = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");
std::cout << registeredIOs.size() << " IO objects available to the ImageFileReader.\n" << std::endl;
std::cout << "Now, when we try to read a MetaImage, we will ";
try
{
reader->Update();
std::cout << "succeed.\n" << std::endl;
}
catch (const itk::ImageFileReaderException & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
std::cout << "Every format desired to be supported by the reader\n"
<< "must be registered." << std::endl;
reader->SetFileName(pngFileName);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itk::PNGImageIOFactory::RegisterOneFactory
static void RegisterOneFactory()
Definition: itkPNGImageIOFactory.h:64
itkImageFileReader.h
itkPNGImageIOFactory.h
itk::ObjectFactoryBase::CreateAllInstance
static std::list< LightObject::Pointer > CreateAllInstance(const char *itkclassname)
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::MetaImageIOFactory::RegisterOneFactory
static void RegisterOneFactory()
Definition: itkMetaImageIOFactory.h:58
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itkMetaImageIOFactory.h
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44