Medical Imaging Interaction Toolkit  2026.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 {
34  template <typename T>
36  {
37  return itk::ImageIOBase::GetComponentTypeAsString(itk::ImageIOBase::MapPixelType<T>::CType);
38  }
39 
49  template <typename PixelT>
50  std::string PixelTypeToString()
51  {
52  return std::string();
53  }
54 
75  {
76  public:
77  typedef itk::IOPixelEnum ItkIOPixelType;
78  typedef itk::IOComponentEnum ItkIOComponentType;
79 
84  PixelType(const mitk::PixelType &aPixelType);
85 
91  PixelType &operator=(const PixelType &other);
92 
101 
111 
116  std::string GetPixelTypeAsString() const;
117 
122  std::string GetComponentTypeAsString() const;
123 
131  std::string GetTypeAsString() const;
132 
141  size_t GetSize() const;
142 
152  size_t GetBpe() const;
153 
162  size_t GetNumberOfComponents() const;
163 
172  size_t GetBitsPerComponent() const;
173 
183  bool operator==(const PixelType &rhs) const;
184 
190  bool operator!=(const PixelType &rhs) const;
191 
194 
195  private:
196  friend PixelType MakePixelType(const itk::ImageIOBase *imageIO);
197 
198  template <typename ComponentT, typename PixelT>
199  friend PixelType MakePixelType(std::size_t numOfComponents);
200 
201  template <typename ItkImageType>
202  friend PixelType MakePixelType();
203 
204  template <typename ItkImageType>
205  friend PixelType MakePixelType(size_t);
206 
207  PixelType(ItkIOComponentType componentType,
208  ItkIOPixelType pixelType,
209  std::size_t bytesPerComponent,
210  std::size_t numOfComponents,
211  const std::string &componentTypeName,
212  const std::string &pixelTypeName);
213 
214  // default constructor is disabled on purpose
215  PixelType(void);
216 
220  ItkIOComponentType m_ComponentType;
221 
222  ItkIOPixelType m_PixelType;
223 
224  std::string m_ComponentTypeName;
225 
226  std::string m_PixelTypeName;
227 
228  std::size_t m_NumberOfComponents;
229 
230  std::size_t m_BytesPerComponent;
231  };
232 
244  MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata);
245 
254  template <typename ComponentT, typename PixelT>
255  PixelType MakePixelType(std::size_t numOfComponents)
256  {
257  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
258  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
259  sizeof(ComponentT),
260  numOfComponents,
261  PixelComponentTypeToString<ComponentT>(),
262  PixelTypeToString<PixelT>());
263  }
264 
273  template <typename ComponentT, typename PixelT, std::size_t numOfComponents>
275  {
276  return MakePixelType<ComponentT, PixelT>(numOfComponents);
277  }
278 
288  template <typename ItkImageType>
290  {
291  };
292 
293  // The itk::VariableLengthVector pixel type is not supported in MITK if it is
294  // used with an itk::Image (it cannot be represented as a mitk::Image object).
295  // Use a itk::VectorImage instead.
296  template <typename TPixelType, unsigned int VImageDimension>
297  struct AssertImageTypeIsValid<itk::Image<itk::VariableLengthVector<TPixelType>, VImageDimension>>;
298 
306  template <typename ItkImageType>
307  PixelType MakePixelType(std::size_t numOfComponents)
308  {
310 
311  // define new type, since the ::PixelType is used to distinguish between simple and compound types
312  typedef typename ItkImageType::PixelType ImportPixelType;
313 
314  // get the component type ( is either directly ImportPixelType or ImportPixelType::ValueType for compound types )
315  typedef typename GetComponentType<ImportPixelType>::ComponentType ComponentT;
316 
317  // The PixelType is the same as the ComponentT for simple types
318  typedef typename ItkImageType::PixelType PixelT;
319 
320  // call the constructor
321  return PixelType(MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOComponentType,
322  MapPixelType<PixelT, isPrimitiveType<PixelT>::value>::IOPixelType,
323  sizeof(ComponentT),
324  numOfComponents,
325  PixelComponentTypeToString<ComponentT>(),
326  PixelTypeToString<PixelT>());
327  }
328 
338  template <typename TPixelType, unsigned int VImageDimension>
339  PixelType MakePixelType(std::size_t numOfComponents)
340  {
341  typedef typename ImageTypeTrait<TPixelType, VImageDimension>::ImageType ItkImageType;
342  return MakePixelType<ItkImageType>(numOfComponents);
343  }
344 
356  template <typename ItkImageType>
358  {
360  {
361  mitkThrow() << " Variable pixel type given but the length is not specified. Use the parametric MakePixelType( "
362  "size_t ) method instead.";
363  }
364 
365  // Use the InternalPixelType to get "1" for the number of components in case of
366  // a itk::VectorImage
367  typedef typename ItkImageType::InternalPixelType PixelT;
368 
369  const std::size_t numComp = ComponentsTrait<isPrimitiveType<PixelT>::value, ItkImageType>::Size;
370 
371  // call the constructor
372  return MakePixelType<ItkImageType>(numComp);
373  }
374 
383  inline PixelType MakePixelType(const itk::ImageIOBase *imageIO)
384  {
385  return mitk::PixelType(imageIO->GetComponentType(),
386  imageIO->GetPixelType(),
387  imageIO->GetComponentSize(),
388  imageIO->GetNumberOfComponents(),
389  imageIO->GetComponentTypeAsString(imageIO->GetComponentType()),
390  imageIO->GetPixelTypeAsString(imageIO->GetPixelType()));
391  }
392 
406  template <typename T>
408  {
409  return MakePixelType<T, T, 1>();
410  }
411 
412 } // namespace mitk
413 
414 #endif
#define MITKCORE_EXPORT
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
Describes the data type of image pixels.
Definition: mitkPixelType.h:75
ItkIOPixelType GetPixelType() const
Get the ITK I/O pixel type enumeration value.
std::string GetTypeAsString() const
Get a combined string of pixel type and component type.
PixelType(const mitk::PixelType &aPixelType)
Copy constructor.
itk::IOPixelEnum ItkIOPixelType
ITK I/O pixel type enumeration.
Definition: mitkPixelType.h:77
size_t GetBitsPerComponent() const
Get the number of bits per single scalar component.
~PixelType()
Destructor.
std::string GetPixelTypeAsString() const
Get the ITK pixel type as a human-readable string.
std::string GetComponentTypeAsString() const
Get the scalar component type as a human-readable string.
PixelType & operator=(const PixelType &other)
Copy assignment operator.
bool operator==(const PixelType &rhs) const
Compare two PixelType objects for equality.
friend PixelType MakePixelType(size_t)
itk::IOComponentEnum ItkIOComponentType
ITK I/O component type enumeration.
Definition: mitkPixelType.h:78
ItkIOComponentType GetComponentType() const
Get the scalar component type.
size_t GetNumberOfComponents() const
Get the number of components per pixel.
size_t GetSize() const
Get the total size of one pixel in bytes.
size_t GetBpe() const
Get the total number of bits per pixel element.
bool operator!=(const PixelType &rhs) const
Compare two PixelType objects for inequality.
#define mitkThrow()
Throw a generic mitk::Exception with source location information.
Compile-time type traits for resolving pixel component types, composite pixel types,...
Find image slices visible on a given plane.
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
Deduce and create a PixelType from a vtkImageData object.
PixelType MakeScalarPixelType()
Create a scalar (single-component) PixelType from a C++ type.
std::string PixelComponentTypeToString()
Return the ITK component type name for the given C++ scalar type.
Definition: mitkPixelType.h:35
std::string PixelTypeToString()
Return the ITK pixel type name for the given pixel type.
Definition: mitkPixelType.h:50
Compile-time check for supported ITK image types.
Object for compile-time resolving of the number of components for given type.
PixelTypeTrait< isPrimitiveType< T >::value, T >::ValueType ComponentType
Compile-time trait that maps a pixel type and dimension to the corresponding ITK image type.
itk::Image< TPixelType, VDimension > ImageType
Object for compile-time translation of a pixel type into an itk::ImageIOBase::IOPixelType information...
This is an implementation of a type trait to provide a compile-time check for PixelType used in the i...