Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkItkPictureWrite.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <MitkLegacyIOExports.h>
18 
19 #include "mitkItkPictureWrite.h"
21 
22 #include <itkImageSeriesWriter.h>
23 #include <itkNumericSeriesFileNames.h>
24 #include <itkRescaleIntensityImageFilter.h>
25 
26 #include <itkRGBAPixel.h>
27 
29 template <class WriterType>
30 void SetOutputNames(typename WriterType::Pointer writer, const std::string &baseFileName, unsigned int numberOfImages)
31 {
32  if (numberOfImages > 1)
33  {
35 
36  std::string finalFileName = baseFileName;
37  std::string::size_type pos = baseFileName.find_last_of(".", baseFileName.length() - 1);
38  if (pos == std::string::npos)
39  finalFileName.append(".%d.png");
40  else
41  finalFileName.insert(pos, ".%d");
42 
43  MITK_DEBUG << "Filename: " << finalFileName;
44 
45  numericFileNameWriter->SetEndIndex(numberOfImages);
46  numericFileNameWriter->SetSeriesFormat(finalFileName.c_str());
47  numericFileNameWriter->Modified();
48  writer->SetFileNames(numericFileNameWriter->GetFileNames());
49  }
50  // if the given image is an 2D-png image, do not use the numericFileNameWriter
51  // to generate the name, since it alters the fileName given as parameter
52  else
53  {
54  writer->SetFileName(baseFileName.c_str());
55  }
56 }
57 
58 template <typename TPixel, unsigned int VImageDimension>
59 void _mitkItkPictureWrite(itk::Image<TPixel, VImageDimension> *itkImage, const std::string &fileName)
60 {
61  typedef itk::Image<TPixel, VImageDimension> TImageType;
62 
63  typedef itk::Image<unsigned char, 3> UCharOutputImage3DType;
64  typedef itk::Image<unsigned short, 3> ShortOutputImage3DType;
65  typedef itk::Image<unsigned char, 2> OutputImage2D_8bitType;
66  typedef itk::Image<unsigned short, 2> OutputImage2D_16bitType;
67 
68  typedef itk::ImageSeriesWriter<UCharOutputImage3DType, OutputImage2D_8bitType> UCharWriterType;
69  typedef itk::ImageSeriesWriter<ShortOutputImage3DType, OutputImage2D_16bitType> ShortWriterType;
70 
71  typedef itk::RescaleIntensityImageFilter<TImageType, UCharOutputImage3DType> UCharRescalerFilterType;
72  typedef itk::RescaleIntensityImageFilter<TImageType, ShortOutputImage3DType> ShortRescalerFilterType;
73 
74  // get the size info
75  size_t inputTypeSize = sizeof(TPixel);
76  size_t supportedOutputMaxSize = 1; // default value 8bit
77 
78  // the PNG and TIFF formats can handle up-to 16-bit images
79  if (fileName.find(".png") != std::string::npos || fileName.find(".tif") != std::string::npos)
80  {
81  supportedOutputMaxSize = 2;
82  }
83 
84  // get the dimension info
85  unsigned int numberOfImages = 1;
86  if (itkImage->GetImageDimension() > 2)
87  numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2];
88 
90  sh_rescaler->SetInput(itkImage);
91  sh_rescaler->SetOutputMinimum(0);
92  sh_rescaler->SetOutputMaximum(65535);
93 
95  rescaler->SetInput(itkImage);
96  rescaler->SetOutputMinimum(0);
97  rescaler->SetOutputMaximum(255);
98 
99  try
100  {
101  // input is 8 bit
102  if (inputTypeSize == 1)
103  {
105  SetOutputNames<UCharWriterType>(writer, fileName, numberOfImages);
106  writer->SetInput(rescaler->GetOutput());
107  writer->Update();
108  }
109  // input pixel type is 16bit -> writer can handle 16bit images
110  else if (inputTypeSize == supportedOutputMaxSize && supportedOutputMaxSize == 2)
111  {
113  SetOutputNames<ShortWriterType>(writer, fileName, numberOfImages);
114  writer->SetInput(sh_rescaler->GetOutput());
115  writer->Update();
116  }
117  // rescaling input to maximum of supported format
118  else
119  {
120  if (supportedOutputMaxSize == 2)
121  {
123  SetOutputNames<ShortWriterType>(writer, fileName, numberOfImages);
124  writer->SetInput(sh_rescaler->GetOutput());
125  writer->Update();
126  }
127  else
128  {
130  SetOutputNames<UCharWriterType>(writer, fileName, numberOfImages);
131  writer->SetInput(rescaler->GetOutput());
132  writer->Update();
133  }
134  }
135  }
136  catch (const itk::ExceptionObject &e)
137  {
138  MITK_ERROR << "ITK Exception occured: " << e.what();
139  mitkThrow() << "Caught ITK exception while writing image with scalar type \n" << e.what();
140  }
141 }
142 
143 template <typename TPixel, unsigned int VImageDimension>
144 void _mitkItkPictureWriteComposite(itk::Image<TPixel, VImageDimension> *itkImage, const std::string &fileName)
145 {
146  typedef itk::Image<TPixel, VImageDimension> TImageType;
147  typedef itk::Image<TPixel, 2> TImageType2D;
148 
149  typedef itk::ImageSeriesWriter<TImageType, TImageType2D> WriterType;
150  typename WriterType::Pointer writer = WriterType::New();
151 
152  // get the dimension info
153  unsigned int numberOfImages = 1;
154  if (itkImage->GetImageDimension() > 2)
155  numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2];
156 
157  // create output name(s)
158  SetOutputNames<WriterType>(writer, fileName, numberOfImages);
159 
160  writer->SetInput(itkImage);
161  try
162  {
163  writer->Update();
164  }
165  catch (const itk::ExceptionObject &e)
166  {
167  MITK_ERROR << "ITK Exception occured: " << e.what();
168  mitkThrow() << "Caught ITK exception while writing image with composite type \n" << e.what();
169  }
170 }
171 
172 #define InstantiateAccessFunction__mitkItkPictureWrite(pixelType, dim) \
173  template MITKLEGACYIO_EXPORT void _mitkItkPictureWrite(itk::Image<pixelType, dim> *, const std::string &);
174 
175 #define InstantiateAccessFunction__mitkItkPictureWriteComposite(pixelType, dim) \
176  template MITKLEGACYIO_EXPORT void _mitkItkPictureWriteComposite(itk::Image<pixelType, dim> *, const std::string &);
177 
179 
void SetOutputNames(typename WriterType::Pointer writer, const std::string &baseFileName, unsigned int numberOfImages)
itk::SmartPointer< Self > Pointer
InstantiateAccessFunction(_mitkItkPictureWrite) InstantiateAccessFunctionForFixedPixelType(_mitkItkPictureWriteComposite
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#define MITK_ACCESSBYITK_PIXEL_TYPES_SEQ
Definition: mitkConfig.h:25
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
InstantiateAccessFunctionForFixedPixelType(AccessItkImage,(float)(double)) InstantiateAccessFunctionForIntegralPixelTypes(AccessItkImage) int mitkInstantiateAccessFunctionTest(int
#define mitkThrow()
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
Definition: mitkConfig.h:23
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.