18 #include <itkImageSeriesWriter.h> 19 #include <itkNumericSeriesFileNames.h> 20 #include <itkRescaleIntensityImageFilter.h> 22 #include <itkRGBAPixel.h> 25 template <
class WriterType>
26 void SetOutputNames(
typename WriterType::Pointer writer,
const std::string &baseFileName,
unsigned int numberOfImages)
28 if (numberOfImages > 1)
30 itk::NumericSeriesFileNames::Pointer numericFileNameWriter = itk::NumericSeriesFileNames::New();
32 std::string finalFileName = baseFileName;
33 std::string::size_type pos = baseFileName.find_last_of(
".", baseFileName.length() - 1);
34 if (pos == std::string::npos)
35 finalFileName.append(
".%d.png");
37 finalFileName.insert(pos,
".%d");
41 numericFileNameWriter->SetEndIndex(numberOfImages);
42 numericFileNameWriter->SetSeriesFormat(finalFileName.c_str());
43 numericFileNameWriter->Modified();
44 writer->SetFileNames(numericFileNameWriter->GetFileNames());
50 writer->SetFileName(baseFileName.c_str());
54 template <
typename TPixel,
unsigned int VImageDimension>
57 typedef itk::Image<TPixel, VImageDimension> TImageType;
59 typedef itk::Image<unsigned char, 3> UCharOutputImage3DType;
60 typedef itk::Image<unsigned short, 3> ShortOutputImage3DType;
61 typedef itk::Image<unsigned char, 2> OutputImage2D_8bitType;
62 typedef itk::Image<unsigned short, 2> OutputImage2D_16bitType;
64 typedef itk::ImageSeriesWriter<UCharOutputImage3DType, OutputImage2D_8bitType> UCharWriterType;
65 typedef itk::ImageSeriesWriter<ShortOutputImage3DType, OutputImage2D_16bitType> ShortWriterType;
67 typedef itk::RescaleIntensityImageFilter<TImageType, UCharOutputImage3DType> UCharRescalerFilterType;
68 typedef itk::RescaleIntensityImageFilter<TImageType, ShortOutputImage3DType> ShortRescalerFilterType;
71 size_t inputTypeSize =
sizeof(TPixel);
72 size_t supportedOutputMaxSize = 1;
75 if (fileName.find(
".png") != std::string::npos || fileName.find(
".tif") != std::string::npos)
77 supportedOutputMaxSize = 2;
81 unsigned int numberOfImages = 1;
82 if (itkImage->GetImageDimension() > 2)
83 numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2];
85 typename ShortRescalerFilterType::Pointer sh_rescaler = ShortRescalerFilterType::New();
86 sh_rescaler->SetInput(itkImage);
87 sh_rescaler->SetOutputMinimum(0);
88 sh_rescaler->SetOutputMaximum(65535);
90 typename UCharRescalerFilterType::Pointer rescaler = UCharRescalerFilterType::New();
91 rescaler->SetInput(itkImage);
92 rescaler->SetOutputMinimum(0);
93 rescaler->SetOutputMaximum(255);
98 if (inputTypeSize == 1)
100 UCharWriterType::Pointer writer = UCharWriterType::New();
101 SetOutputNames<UCharWriterType>(writer, fileName, numberOfImages);
102 writer->SetInput(rescaler->GetOutput());
106 else if (inputTypeSize == supportedOutputMaxSize && supportedOutputMaxSize == 2)
108 ShortWriterType::Pointer writer = ShortWriterType::New();
109 SetOutputNames<ShortWriterType>(writer, fileName, numberOfImages);
110 writer->SetInput(sh_rescaler->GetOutput());
116 if (supportedOutputMaxSize == 2)
118 typename ShortWriterType::Pointer writer = ShortWriterType::New();
119 SetOutputNames<ShortWriterType>(writer, fileName, numberOfImages);
120 writer->SetInput(sh_rescaler->GetOutput());
125 typename UCharWriterType::Pointer writer = UCharWriterType::New();
126 SetOutputNames<UCharWriterType>(writer, fileName, numberOfImages);
127 writer->SetInput(rescaler->GetOutput());
132 catch (
const itk::ExceptionObject &e)
134 MITK_ERROR <<
"ITK Exception occured: " << e.what();
135 mitkThrow() <<
"Caught ITK exception while writing image with scalar type \n" << e.what();
139 template <
typename TPixel,
unsigned int VImageDimension>
142 typedef itk::Image<TPixel, VImageDimension> TImageType;
143 typedef itk::Image<TPixel, 2> TImageType2D;
145 typedef itk::ImageSeriesWriter<TImageType, TImageType2D> WriterType;
146 typename WriterType::Pointer writer = WriterType::New();
149 unsigned int numberOfImages = 1;
150 if (itkImage->GetImageDimension() > 2)
151 numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2];
154 SetOutputNames<WriterType>(writer, fileName, numberOfImages);
156 writer->SetInput(itkImage);
161 catch (
const itk::ExceptionObject &e)
163 MITK_ERROR <<
"ITK Exception occured: " << e.what();
164 mitkThrow() <<
"Caught ITK exception while writing image with composite type \n" << e.what();
168 #define InstantiateAccessFunction__mitkItkPictureWrite(pixelType, dim) \ 169 template MITKLEGACYIO_EXPORT void _mitkItkPictureWrite(itk::Image<pixelType, dim> *, const std::string &); 171 #define InstantiateAccessFunction__mitkItkPictureWriteComposite(pixelType, dim) \ 172 template MITKLEGACYIO_EXPORT void _mitkItkPictureWriteComposite(itk::Image<pixelType, dim> *, const std::string &); void SetOutputNames(typename WriterType::Pointer writer, const std::string &baseFileName, unsigned int numberOfImages)
InstantiateAccessFunction(_mitkItkPictureWrite) InstantiateAccessFunctionForFixedPixelType(_mitkItkPictureWriteComposite
#define MITK_ACCESSBYITK_PIXEL_TYPES_SEQ
InstantiateAccessFunctionForFixedPixelType(AccessItkImage,(float)(double)) InstantiateAccessFunctionForIntegralPixelTypes(AccessItkImage) int mitkInstantiateAccessFunctionTest(int
void _mitkItkPictureWriteComposite(itk::Image< TPixel, VImageDimension > *itkImage, const std::string &fileName)
ITK-Like method to be called for writing an image.
void _mitkItkPictureWrite(itk::Image< TPixel, VImageDimension > *itkImage, const std::string &fileName)
ITK-Like method to be called for writing an single-component image using the AccessByItk Macros...
#define MITK_ACCESSBYITK_COMPOSITE_PIXEL_TYPES_SEQ