Medical Imaging Interaction Toolkit  2021.10.99-1c4db16f
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 {
29  template <class TPixel, unsigned int VDimension = 3>
31  {
32  public:
36 
38  int GetDimension(int i) const { return m_ImageDataItem->GetDimension(i); }
39 
40  private:
41  friend class Image;
42 
43  protected:
48  ImagePixelAccessor(ImageConstPointer iP, const mitk::ImageDataItem *iDI) : m_ImageDataItem(iDI)
49  {
50  if (iDI == nullptr)
51  {
52  m_ImageDataItem = iP->GetChannelData();
53  }
54  CheckData(iP.GetPointer());
55  }
56 
58  virtual ~ImagePixelAccessor() {}
59 
60  void CheckData( const Image *image )
61  {
62  // Check if Dimensions are correct
63  if ( m_ImageDataItem == nullptr )
64  {
65  if ( image->GetDimension() != VDimension )
66  {
67  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and Image are not equal."
68  << " They have to be equal if an entire image is requested."
69  << " image->GetDimension(): " << image->GetDimension() << " , VDimension: " << VDimension;
70  }
71  }
72  else
73  {
74  if ( m_ImageDataItem->GetDimension() != VDimension )
75  {
76  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and ImageDataItem are not equal."
77  << " m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension() << " , VDimension: " << VDimension;
78  }
79  }
80 
81  if (!( image->GetPixelType() == mitk::MakePixelType< itk::Image< TPixel, VDimension > >() ||
82  image->GetPixelType() == mitk::MakePixelType< itk::VectorImage< TPixel, VDimension > >
83  ( image->GetPixelType().GetNumberOfComponents() )
84  ) )
85  {
86  mitkThrow() << "Invalid ImageAccessor: PixelTypes of Image and ImageAccessor are not equal."
87  << " image->GetPixelType(): " << typeid(image->GetPixelType()).name()
88  << "\n m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension()
89  << " , VDimension: " << VDimension
90  << " , TPixel: " << typeid(TPixel).name()
91  << " , NumberOfComponents: " << image->GetPixelType().GetNumberOfComponents() << std::endl;
92  }
93  }
94 
97 
103 
109  itk::ImageRegion<VDimension> *m_SubRegion;
110 
115 
117  unsigned int GetOffset(const IndexType &idx) const
118  {
119  const unsigned int *imageDims = m_ImageDataItem->m_Dimensions;
120 
121  unsigned int offset = 0;
122  switch (VDimension)
123  {
124  case 4:
125  offset += idx[3] * imageDims[0] * imageDims[1] * imageDims[2];
126  /* FALLTHRU */
127  case 3:
128  offset += idx[2] * imageDims[0] * imageDims[1];
129  /* FALLTHRU */
130  case 2:
131  offset += idx[0] + idx[1] * imageDims[0];
132  /* FALLTHRU */
133  default:
134  break;
135  }
136  return offset;
137  }
138  };
139 }
140 
141 #endif // MITKIMAGEACCESSOR_H
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
int m_Options
Stores all extended properties of an ImageAccessor. The different flags in mitk::ImageAccessorBase::O...
Provides templated image access for all inheriting classes.
DataCollection - Class to facilitate loading/accessing structured data.
BaseGeometry::Pointer m_Geometry
Pointer to the used Geometry. Since Geometry can be different to the Image (if memory was forced to b...
unsigned int GetDimension() const
Get dimension of the image.
itk::Index< VDimension > IndexType
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:69
unsigned int GetOffset(const IndexType &idx) const
void CheckData(const Image *image)
itk::ImageRegion< VDimension > * m_SubRegion
A Subregion defines an arbitrary area within the image. If no SubRegion is defined, the whole ImageDataItem or Image is regarded. A subregion (e.g. subvolume) can lead to non-coherent memory access where every dimension has a start- and end-offset.
Image::ConstPointer ImageConstPointer
const ImageDataItem * m_ImageDataItem
Internal class for managing references on sub-images.
ImagePixelAccessor(ImageConstPointer iP, const mitk::ImageDataItem *iDI)