Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkImagePixelReadAccessor.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 mitkImagePixelReadAccessor_h
14 #define mitkImagePixelReadAccessor_h
15 
16 #include <mitkImagePixelAccessor.h>
17 #include <mitkImageReadAccessor.h>
18 
19 namespace mitk
20 {
21  class Image;
22 
34  template <class TPixel, unsigned int VDimension = 3>
35  class ImagePixelReadAccessor : public ImagePixelAccessor<TPixel, VDimension>
36  {
37  friend class Image;
38 
39  public:
43 
57  const ImageDataItem *iDI = nullptr,
58  int OptionFlags = ImageAccessorBase::DefaultBehavior)
59  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
60  {
61  }
62 
65  const ImageDataItem *iDI = nullptr,
66  int OptionFlags = ImageAccessorBase::DefaultBehavior)
67  : ImagePixelAccessor<TPixel, VDimension>(iP.GetPointer(), iDI), m_ReadAccessor(iP, iDI, OptionFlags)
68  {
69  }
70 
73  const ImageDataItem *iDI = nullptr,
74  int OptionFlags = ImageAccessorBase::DefaultBehavior)
75  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(mitk::Image::ConstPointer(iP), iDI, OptionFlags)
76  {
77  }
78 
81  const ImageDataItem *iDI = nullptr,
82  int OptionFlags = ImageAccessorBase::DefaultBehavior)
83  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
84  {
85  }
86 
90  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
91  {
92  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
93 
94  return *(((TPixel *)m_ReadAccessor.m_AddressBegin) + offset);
95  }
96 
103  int nrComponents) const
104  {
106  (TPixel *)m_ReadAccessor.m_AddressBegin +
108  nrComponents);
109  }
110 
114  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
115  {
116  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
117 
118  TPixel *targetAddress = ((TPixel *)m_ReadAccessor.m_AddressBegin) + offset;
119 
120  if (!(targetAddress >= m_ReadAccessor.m_AddressBegin && targetAddress < m_ReadAccessor.m_AddressEnd))
121  {
122  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
123  }
124 
125  return *targetAddress;
126  }
127 
132  {
133  itk::Index<3> itkIndex;
134  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
135 
136  return GetPixelByIndex(itkIndex);
137  }
138 
142  const TPixel &GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
143  {
144  itk::Index<3> itkIndex;
145  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
146  if (m_ReadAccessor.GetImage()->GetTimeSteps() < timestep)
147  {
148  timestep = m_ReadAccessor.GetImage()->GetTimeSteps();
149  }
150  itk::Index<4> itk4Index;
151  for (int i = 0; i < 3; ++i)
152  itk4Index[i] = itkIndex[i];
153 
154  itk4Index[3] = timestep;
155  return GetPixelByIndex(itk4Index);
156  }
157 
159  inline const TPixel *GetData() const { return static_cast<const TPixel *>(m_ReadAccessor.m_AddressBegin); }
160  protected:
161  // protected members
162 
163  private:
164  ImageReadAccessor m_ReadAccessor;
165 
166  ImagePixelReadAccessor &operator=(const ImagePixelReadAccessor &); // Not implemented on purpose.
168  };
169 
174  template <class TPixel>
176  mitk::Image::Pointer im,
177  ImageDataItem *item,
178  itk::Index<3> idx,
179  mitk::ScalarType &val,
180  int component = 0)
181  {
183  val = imAccess.GetConsecutivePixelsAsVector(idx, component + 1).GetElement(component);
184  return val;
185  }
186 
188  template <class TPixel>
190  mitk::Image::ConstPointer im,
191  const ImageDataItem* item,
192  itk::Index<3> idx,
193  mitk::ScalarType& val,
194  int component = 0)
195  {
197  val = imAccess.GetConsecutivePixelsAsVector(idx, component + 1).GetElement(component);
198  return val;
199  }
200 
201 }
202 
203 #endif
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the BaseGeometry of the data at time step t as a non-const pointer.
Definition: mitkBaseData.h:224
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry.
Definition: mitkBaseData.h:510
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (mm) of a point to continuous index coordinates.
@ DefaultBehavior
Default behavior: wait for locked memory to be released.
@ IgnoreLock
Bypass the lock mechanism for immediate access. Only valid for read accessors.
Internal container for image data used by mitk::Image.
Provides templated image access for all inheriting classes.
unsigned int GetOffset(const IndexType &idx) const
Compute the linear memory offset for a given multi-dimensional image index.
Gives locked and index-based read access for a particular image part.
ImagePixelReadAccessor(Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor from a non-const Image raw pointer.
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
itk::SmartPointer< mitk::Image > ImagePointer
itk::SmartPointer< const mitk::Image > ImageConstPointer
ImagePixelReadAccessor(ImageConstPointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor (see its doxygen page for more details)
itk::VariableLengthVector< TPixel > GetConsecutivePixelsAsVector(const itk::Index< VDimension > &idx, int nrComponents) const
Returns consecutive pixel values starting at a given index as a variable length vector.
const TPixel * GetData() const
Gives const access to the data.
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
ImagePixelReadAccessor(const Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor from a const Image raw pointer.
ImagePixelReadAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor from an Image smart pointer.
Provides locked read-only access to a particular region of image data.
const Image * GetImage() const override
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Describes the data type of image pixels.
Definition: mitkPixelType.h:75
size_t GetNumberOfComponents() const
Get the number of components per pixel.
#define mitkThrow()
Throw a generic mitk::Exception with source location information.
class ITK_EXPORT Image
Find image slices visible on a given plane.
mitk::ScalarType FastSinglePixelAccess(mitk::PixelType, mitk::Image::Pointer im, ImageDataItem *item, itk::Index< 3 > idx, mitk::ScalarType &val, int component=0)
double ScalarType
Scalar type used throughout MITK for geometric computations.