Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPixelType.h
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 #ifndef mitkPixelType_h
18 #define mitkPixelType_h
19 
20 #include "mitkCommon.h"
21 #include "mitkPixelTypeTraits.h"
22 #include <MitkCoreExports.h>
23 
24 #include <string>
25 #include <typeinfo>
26 
27 #include <itkImage.h>
28 #include <itkImageIOBase.h>
29 #include <vtkImageData.h>
30 
31 namespace mitk
32 {
33  template <typename T>
35  {
36  return itk::ImageIOBase::GetComponentTypeAsString(itk::ImageIOBase::MapPixelType<T>::CType);
37  }
38 
39  template <typename PixelT>
40  std::string PixelTypeToString()
41  {
42  return std::string();
43  }
44 
56  {
57  public:
58  typedef itk::ImageIOBase::IOPixelType ItkIOPixelType;
59  typedef itk::ImageIOBase::IOComponentType ItkIOComponentType;
60 
61  PixelType(const mitk::PixelType &aPixelType);
62  PixelType &operator=(const PixelType &other);
63 
64  itk::ImageIOBase::IOPixelType GetPixelType() const;
65 
71  int GetComponentType() const;
72 
76  std::string GetPixelTypeAsString() const;
77 
81  std::string GetComponentTypeAsString() const;
82 
86  std::string GetTypeAsString() const;
87 
93  size_t GetSize() const;
94 
105  size_t GetBpe() const;
106 
112  size_t GetNumberOfComponents() const;
113 
118  size_t GetBitsPerComponent() const;
119 
120  bool operator==(const PixelType &rhs) const;
121  bool operator!=(const PixelType &rhs) const;
122 
123  ~PixelType();
124 
125  private:
126  friend PixelType MakePixelType(const itk::ImageIOBase *imageIO);
127 
128  template <typename ComponentT, typename PixelT>
129  friend PixelType MakePixelType(std::size_t numberOfComponents);
130 
131  template <typename ItkImageType>
132  friend PixelType MakePixelType();
133 
134  template <typename ItkImageType>
135  friend PixelType MakePixelType(size_t);
136 
137  PixelType(const int componentType,
138  const ItkIOPixelType pixelType,
139  std::size_t bytesPerComponent,
140  std::size_t numberOfComponents,
141  const std::string &componentTypeName,
142  const std::string &pixelTypeName);
143 
144  // default constructor is disabled on purpose
145  PixelType(void);
146 
150  int m_ComponentType;
151 
152  ItkIOPixelType m_PixelType;
153 
154  std::string m_ComponentTypeName;
155 
156  std::string m_PixelTypeName;
157 
158  std::size_t m_NumberOfComponents;
159 
160  std::size_t m_BytesPerComponent;
161  };
162 
169  MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata);
170 
174  template <typename ComponentT, typename PixelT>
175  PixelType MakePixelType(std::size_t numOfComponents)
176  {
177  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
178  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
179  sizeof(ComponentT),
180  numOfComponents,
181  PixelComponentTypeToString<ComponentT>(),
182  PixelTypeToString<PixelT>());
183  }
184 
190  template <typename ComponentT, typename PixelT, std::size_t numOfComponents>
192  {
193  return MakePixelType<ComponentT, PixelT>(numOfComponents);
194  }
195 
202  template <typename ItkImageType>
204  {
205  };
206 
207  // The itk::VariableLengthVector pixel type is not supported in MITK if it is
208  // used with an itk::Image (it cannot be represented as a mitk::Image object).
209  // Use a itk::VectorImage instead.
210  template <typename TPixelType, unsigned int VImageDimension>
211  struct AssertImageTypeIsValid<itk::Image<itk::VariableLengthVector<TPixelType>, VImageDimension>>;
212 
217  template <typename ItkImageType>
218  PixelType MakePixelType(std::size_t numOfComponents)
219  {
221 
222  // define new type, since the ::PixelType is used to distinguish between simple and compound types
223  typedef typename ItkImageType::PixelType ImportPixelType;
224 
225  // get the component type ( is either directly ImportPixelType or ImportPixelType::ValueType for compound types )
226  typedef typename GetComponentType<ImportPixelType>::ComponentType ComponentT;
227 
228  // The PixelType is the same as the ComponentT for simple types
229  typedef typename ItkImageType::PixelType PixelT;
230 
231  // call the constructor
232  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
233  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
234  sizeof(ComponentT),
235  numOfComponents,
236  PixelComponentTypeToString<ComponentT>(),
237  PixelTypeToString<PixelT>());
238  }
239 
245  template <typename TPixelType, unsigned int VImageDimension>
246  PixelType MakePixelType(std::size_t numOfComponents)
247  {
248  typedef typename ImageTypeTrait<TPixelType, VImageDimension>::ImageType ItkImageType;
249  return MakePixelType<ItkImageType>(numOfComponents);
250  }
251 
259  template <typename ItkImageType>
261  {
263  {
264  mitkThrow() << " Variable pixel type given but the length is not specified. Use the parametric MakePixelType( "
265  "size_t ) method instead.";
266  }
267 
268  // Use the InternalPixelType to get "1" for the number of components in case of
269  // a itk::VectorImage
270  typedef typename ItkImageType::InternalPixelType PixelT;
271 
272  const std::size_t numComp = ComponentsTrait<isPrimitiveType<PixelT>::value, ItkImageType>::Size;
273 
274  // call the constructor
275  return MakePixelType<ItkImageType>(numComp);
276  }
277 
281  inline PixelType MakePixelType(const itk::ImageIOBase *imageIO)
282  {
283  return mitk::PixelType(imageIO->GetComponentType(),
284  imageIO->GetPixelType(),
285  imageIO->GetComponentSize(),
286  imageIO->GetNumberOfComponents(),
287  imageIO->GetComponentTypeAsString(imageIO->GetComponentType()),
288  imageIO->GetPixelTypeAsString(imageIO->GetPixelType()));
289  }
290 
295  template <typename T>
297  {
298  return MakePixelType<T, T, 1>();
299  }
300 
301 } // namespace mitk
302 
303 #endif /* mitkPixelType_h */
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
#define MITKCORE_EXPORT
itk::Image< unsigned char, 3 > ImageType
PixelTypeTrait< isPrimitiveType< T >::value, T >::ValueType ComponentType
PixelType MakeScalarPixelType()
An interface to the MakePixelType method for creating scalar pixel types.
DataCollection - Class to facilitate loading/accessing structured data.
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
itk::ImageIOBase::IOPixelType ItkIOPixelType
Definition: mitkPixelType.h:58
Object for compile-time resolving of the number of components for given type.
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:76
itk::ImageIOBase::IOComponentType ItkIOComponentType
Definition: mitkPixelType.h:59
std::string PixelTypeToString()
Definition: mitkPixelType.h:40
This is an implementation of a type trait to provide a compile-time check for PixelType used in the i...
A helper template for compile-time checking of supported ITK image types.
unsigned short PixelType
Compile time resolving of the type of a component.
Object for compile-time translation of a pixel type into an itk::ImageIOBase::IOPixelType information...
std::string PixelComponentTypeToString()
Definition: mitkPixelType.h:34
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55