Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkImagePixelAccessor.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 mitkImagePixelAccessor_h
14 #define mitkImagePixelAccessor_h
15 
16 #include <mitkImage.h>
17 #include <mitkImageDataItem.h>
18 
19 #include <typeinfo>
20 
21 namespace mitk
22 {
33  template <class TPixel, unsigned int VDimension = 3>
35  {
36  public:
37  typedef itk::Index<VDimension> IndexType;
39  typedef Image::ConstPointer ImageConstPointer;
40 
47  int GetDimension(int i) const { return m_ImageDataItem->GetDimension(i); }
48 
49  private:
50  friend class Image;
51 
52  protected:
63  {
64  if (iDI == nullptr)
65  {
66  m_ImageDataItem = iP->GetChannelData();
67  }
68  CheckData(iP.GetPointer());
69  }
70 
72  virtual ~ImagePixelAccessor() {}
73 
80  void CheckData( const Image *image )
81  {
82  // Check if Dimensions are correct
83  if ( m_ImageDataItem == nullptr )
84  {
85  if ( image->GetDimension() != VDimension )
86  {
87  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and Image are not equal."
88  << " They have to be equal if an entire image is requested."
89  << " image->GetDimension(): " << image->GetDimension() << " , VDimension: " << VDimension;
90  }
91  }
92  else
93  {
94  if ( m_ImageDataItem->GetDimension() != VDimension )
95  {
96  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and ImageDataItem are not equal."
97  << " m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension() << " , VDimension: " << VDimension;
98  }
99  }
100 
101  if (!( image->GetPixelType() == mitk::MakePixelType< itk::Image< TPixel, VDimension > >() ||
102  image->GetPixelType() == mitk::MakePixelType< itk::VectorImage< TPixel, VDimension > >
103  ( image->GetPixelType().GetNumberOfComponents() )
104  ) )
105  {
106  mitkThrow() << "Invalid ImageAccessor: PixelTypes of Image and ImageAccessor are not equal."
107  << " image->GetPixelType(): " << typeid(image->GetPixelType()).name()
108  << "\n m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension()
109  << " , VDimension: " << VDimension
110  << " , TPixel: " << typeid(TPixel).name()
111  << " , NumberOfComponents: " << image->GetPixelType().GetNumberOfComponents() << std::endl;
112  }
113  }
114 
117 
122  BaseGeometry::Pointer m_Geometry;
123 
129  itk::ImageRegion<VDimension> *m_SubRegion;
130 
135 
142  unsigned int GetOffset(const IndexType &idx) const
143  {
144  const unsigned int *imageDims = m_ImageDataItem->m_Dimensions;
145 
146  unsigned int offset = 0;
147  switch (VDimension)
148  {
149  case 4:
150  offset += idx[3] * imageDims[0] * imageDims[1] * imageDims[2];
151  /* FALLTHRU */
152  case 3:
153  offset += idx[2] * imageDims[0] * imageDims[1];
154  /* FALLTHRU */
155  case 2:
156  offset += idx[0] + idx[1] * imageDims[0];
157  /* FALLTHRU */
158  default:
159  break;
160  }
161  return offset;
162  }
163  };
164 }
165 
166 #endif
Internal container for image data used by mitk::Image.
int GetDimension() const
Get the number of spatial dimensions.
Provides templated image access for all inheriting classes.
ImagePixelAccessor(ImageConstPointer iP, const mitk::ImageDataItem *iDI)
Construct the accessor from an image and an optional ImageDataItem.
int GetDimension(int i) const
Get the size of the specified dimension from the underlying ImageDataItem.
virtual ~ImagePixelAccessor()
Destructor.
unsigned int GetOffset(const IndexType &idx) const
Compute the linear memory offset for a given multi-dimensional image index.
int m_Options
Stores all extended properties of an ImageAccessor. The different flags in mitk::ImageAccessorBase::O...
itk::Index< VDimension > IndexType
void CheckData(const Image *image)
Validate that the accessor dimensions and pixel type match the image.
itk::ImageRegion< VDimension > * m_SubRegion
A Subregion defines an arbitrary area within the image. If no SubRegion is defined,...
BaseGeometry::Pointer m_Geometry
Pointer to the used Geometry. Since Geometry can be different to the Image (if memory was forced to b...
const ImageDataItem * m_ImageDataItem
Holds the specified ImageDataItem for pixel data access.
Image::ConstPointer ImageConstPointer
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
unsigned int GetDimension() const
Get the number of dimensions of the image.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
size_t GetNumberOfComponents() const
Get the number of components per pixel.
#define mitkThrow()
Throw a generic mitk::Exception with source location information.
Find image slices visible on a given plane.
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
Deduce and create a PixelType from a vtkImageData object.