ITK  5.2.0
Insight Toolkit
SphinxExamples/src/IO/ImageBase/ReadUnknownImageType/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
*
* http://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.
*
*=========================================================================*/
#include "itkImageIOBase.h"
template <class TImage>
int
ReadImage(const char * fileName, typename TImage::Pointer image)
{
using ImageType = TImage;
using ImageReaderType = itk::ImageFileReader<ImageType>;
typename ImageReaderType::Pointer reader = ImageReaderType::New();
reader->SetFileName(fileName);
try
{
reader->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
image->Graft(reader->GetOutput());
return EXIT_SUCCESS;
}
template <unsigned int VDimension>
int
ReadScalarImage(const char * inputFileName, const itk::IOComponentEnum componentType)
{
switch (componentType)
{
default:
std::cerr << "Unknown and unsupported component type!" << std::endl;
return EXIT_FAILURE;
{
using PixelType = unsigned char;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = char;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = unsigned short;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = short;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = unsigned int;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = int;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = unsigned long;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = long;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = float;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
{
using PixelType = double;
typename ImageType::Pointer image = ImageType::New();
if (ReadImage<ImageType>(inputFileName, image) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
std::cout << image << std::endl;
break;
}
}
return EXIT_SUCCESS;
}
int
main(int argc, char * argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
imageIO->SetFileName(inputFileName);
imageIO->ReadImageInformation();
using IOPixelType = itk::IOPixelEnum;
const IOPixelType pixelType = imageIO->GetPixelType();
std::cout << "Pixel Type is " << itk::ImageIOBase::GetPixelTypeAsString(pixelType) << std::endl;
using IOComponentType = itk::IOComponentEnum;
const IOComponentType componentType = imageIO->GetComponentType();
std::cout << "Component Type is " << imageIO->GetComponentTypeAsString(componentType) << std::endl;
const unsigned int imageDimension = imageIO->GetNumberOfDimensions();
std::cout << "Image Dimension is " << imageDimension << std::endl;
switch (pixelType)
{
{
if (imageDimension == 2)
{
return ReadScalarImage<2>(inputFileName, componentType);
}
else if (imageDimension == 3)
{
return ReadScalarImage<3>(inputFileName, componentType);
}
else if (imageDimension == 4)
{
return ReadScalarImage<4>(inputFileName, componentType);
}
}
default:
std::cerr << "not implemented yet!" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itk::IOPixelEnum
CommonEnums::IOPixel IOPixelEnum
Definition: itkCommonEnums.h:156
itk::CommonEnums::IOComponent::CHAR
itk::ImageIOFactory::CreateImageIO
static ImageIOBasePointer CreateImageIO(const char *path, IOFileModeEnum mode)
itk::CommonEnums::IOComponent::USHORT
itk::CommonEnums::IOComponent::SHORT
itk::CommonEnums::IOPixel::SCALAR
itk::CommonEnums::IOComponent::ULONG
itk::CommonEnums::IOComponent::UINT
itkImageFileReader.h
itk::IOComponentEnum
CommonEnums::IOComponent IOComponentEnum
Definition: itkCommonEnums.h:157
itk::SmartPointer< Self >
itk::CommonEnums::IOComponent
IOComponent
Definition: itkCommonEnums.h:76
itk::CommonEnums::IOComponent::UCHAR
itk::CommonEnums::IOComponent::DOUBLE
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::CommonEnums::IOComponent::LONG
itk::ImageIOBase::GetPixelTypeAsString
static std::string GetPixelTypeAsString(IOPixelEnum)
itk::CommonEnums::IOComponent::INT
itkImageIOBase.h
itkImageFileWriter.h
itk::CommonEnums::IOComponent::UNKNOWNCOMPONENTTYPE
itk::Math::e
static constexpr double e
Definition: itkMath.h:54
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::CommonEnums::IOComponent::FLOAT
fileName
std::string fileName
Definition: itkTestDriverInclude.h:118
itk::CommonEnums::IOFileMode::ReadMode