Medical Imaging Interaction Toolkit  2024.06.00
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkPixelType_h
14 #define mitkPixelType_h
15 
16 #include "mitkCommon.h"
17 #include "mitkPixelTypeTraits.h"
18 #include <MitkCoreExports.h>
19 
20 #include <string>
21 #include <typeinfo>
22 
23 #include <itkImage.h>
24 #include <itkImageIOBase.h>
25 #include <vtkImageData.h>
26 
27 namespace mitk
28 {
29  template <typename T>
31  {
32  return itk::ImageIOBase::GetComponentTypeAsString(itk::ImageIOBase::MapPixelType<T>::CType);
33  }
34 
35  template <typename PixelT>
36  std::string PixelTypeToString()
37  {
38  return std::string();
39  }
40 
52  {
53  public:
54  typedef itk::IOPixelEnum ItkIOPixelType;
55  typedef itk::IOComponentEnum ItkIOComponentType;
56 
57  PixelType(const mitk::PixelType &aPixelType);
58  PixelType &operator=(const PixelType &other);
59 
61 
68 
72  std::string GetPixelTypeAsString() const;
73 
77  std::string GetComponentTypeAsString() const;
78 
82  std::string GetTypeAsString() const;
83 
89  size_t GetSize() const;
90 
101  size_t GetBpe() const;
102 
108  size_t GetNumberOfComponents() const;
109 
114  size_t GetBitsPerComponent() const;
115 
116  bool operator==(const PixelType &rhs) const;
117  bool operator!=(const PixelType &rhs) const;
118 
119  ~PixelType();
120 
121  private:
122  friend PixelType MakePixelType(const itk::ImageIOBase *imageIO);
123 
124  template <typename ComponentT, typename PixelT>
125  friend PixelType MakePixelType(std::size_t numOfComponents);
126 
127  template <typename ItkImageType>
128  friend PixelType MakePixelType();
129 
130  template <typename ItkImageType>
131  friend PixelType MakePixelType(size_t);
132 
133  PixelType(ItkIOComponentType componentType,
134  ItkIOPixelType pixelType,
135  std::size_t bytesPerComponent,
136  std::size_t numOfComponents,
137  const std::string &componentTypeName,
138  const std::string &pixelTypeName);
139 
140  // default constructor is disabled on purpose
141  PixelType(void);
142 
146  ItkIOComponentType m_ComponentType;
147 
148  ItkIOPixelType m_PixelType;
149 
150  std::string m_ComponentTypeName;
151 
152  std::string m_PixelTypeName;
153 
154  std::size_t m_NumberOfComponents;
155 
156  std::size_t m_BytesPerComponent;
157  };
158 
165  MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata);
166 
170  template <typename ComponentT, typename PixelT>
171  PixelType MakePixelType(std::size_t numOfComponents)
172  {
173  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
174  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
175  sizeof(ComponentT),
176  numOfComponents,
177  PixelComponentTypeToString<ComponentT>(),
178  PixelTypeToString<PixelT>());
179  }
180 
186  template <typename ComponentT, typename PixelT, std::size_t numOfComponents>
188  {
189  return MakePixelType<ComponentT, PixelT>(numOfComponents);
190  }
191 
198  template <typename ItkImageType>
200  {
201  };
202 
203  // The itk::VariableLengthVector pixel type is not supported in MITK if it is
204  // used with an itk::Image (it cannot be represented as a mitk::Image object).
205  // Use a itk::VectorImage instead.
206  template <typename TPixelType, unsigned int VImageDimension>
207  struct AssertImageTypeIsValid<itk::Image<itk::VariableLengthVector<TPixelType>, VImageDimension>>;
208 
213  template <typename ItkImageType>
214  PixelType MakePixelType(std::size_t numOfComponents)
215  {
217 
218  // define new type, since the ::PixelType is used to distinguish between simple and compound types
219  typedef typename ItkImageType::PixelType ImportPixelType;
220 
221  // get the component type ( is either directly ImportPixelType or ImportPixelType::ValueType for compound types )
222  typedef typename GetComponentType<ImportPixelType>::ComponentType ComponentT;
223 
224  // The PixelType is the same as the ComponentT for simple types
225  typedef typename ItkImageType::PixelType PixelT;
226 
227  // call the constructor
228  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
229  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
230  sizeof(ComponentT),
231  numOfComponents,
232  PixelComponentTypeToString<ComponentT>(),
233  PixelTypeToString<PixelT>());
234  }
235 
241  template <typename TPixelType, unsigned int VImageDimension>
242  PixelType MakePixelType(std::size_t numOfComponents)
243  {
244  typedef typename ImageTypeTrait<TPixelType, VImageDimension>::ImageType ItkImageType;
245  return MakePixelType<ItkImageType>(numOfComponents);
246  }
247 
255  template <typename ItkImageType>
257  {
259  {
260  mitkThrow() << " Variable pixel type given but the length is not specified. Use the parametric MakePixelType( "
261  "size_t ) method instead.";
262  }
263 
264  // Use the InternalPixelType to get "1" for the number of components in case of
265  // a itk::VectorImage
266  typedef typename ItkImageType::InternalPixelType PixelT;
267 
268  const std::size_t numComp = ComponentsTrait<isPrimitiveType<PixelT>::value, ItkImageType>::Size;
269 
270  // call the constructor
271  return MakePixelType<ItkImageType>(numComp);
272  }
273 
277  inline PixelType MakePixelType(const itk::ImageIOBase *imageIO)
278  {
279  return mitk::PixelType(imageIO->GetComponentType(),
280  imageIO->GetPixelType(),
281  imageIO->GetComponentSize(),
282  imageIO->GetNumberOfComponents(),
283  imageIO->GetComponentTypeAsString(imageIO->GetComponentType()),
284  imageIO->GetPixelTypeAsString(imageIO->GetPixelType()));
285  }
286 
291  template <typename T>
293  {
294  return MakePixelType<T, T, 1>();
295  }
296 
297 } // namespace mitk
298 
299 #endif
mitk::PixelType::ItkIOComponentType
itk::IOComponentEnum ItkIOComponentType
Definition: mitkPixelType.h:55
mitk::MakeScalarPixelType
PixelType MakeScalarPixelType()
An interface to the MakePixelType method for creating scalar pixel types.
Definition: mitkPixelType.h:292
mitk::MakePixelType
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
mitk::ComponentsTrait
Object for compile-time resolving of the number of components for given type.
Definition: mitkPixelTypeTraits.h:162
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
mitk::GetComponentType
Compile time resolving of the type of a component.
Definition: mitkPixelTypeTraits.h:152
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::operator!=
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
mitk::PixelComponentTypeToString
std::string PixelComponentTypeToString()
Definition: mitkPixelType.h:30
mitk::ImageTypeTrait::ImageType
itk::Image< TPixelType, VDimension > ImageType
Definition: mitkPixelTypeTraits.h:108
mitk::AssertImageTypeIsValid
A helper template for compile-time checking of supported ITK image types.
Definition: mitkPixelType.h:199
mitk::GetPixelType
Definition: mitkPixelTypeList.h:89
mitkPixelTypeTraits.h
mitk::operator==
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
MitkCoreExports.h
mitkThrow
#define mitkThrow()
Definition: mitkExceptionMacro.h:27
mitk::PixelType
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
mitkCommon.h
mitk::isPrimitiveType
This is an implementation of a type trait to provide a compile-time check for PixelType used in the i...
Definition: mitkPixelTypeTraits.h:69
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
mitk::PixelTypeToString
std::string PixelTypeToString()
Definition: mitkPixelType.h:36
mitk::GetComponentType::ComponentType
PixelTypeTrait< isPrimitiveType< T >::value, T >::ValueType ComponentType
Definition: mitkPixelTypeTraits.h:154
mitk::PixelType::ItkIOPixelType
itk::IOPixelEnum ItkIOPixelType
Definition: mitkPixelType.h:54
mitk::ImageTypeTrait
Definition: mitkPixelTypeTraits.h:106
mitk::MapPixelType
Object for compile-time translation of a pixel type into an itk::ImageIOBase::IOPixelType information...
Definition: mitkPixelTypeTraits.h:256
MITKCORE_EXPORT
#define MITKCORE_EXPORT
Definition: MitkCoreExports.h:15