Medical Imaging Interaction Toolkit  2023.12.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 
31  template <class TPixel, unsigned int VDimension = 3>
32  class ImagePixelReadAccessor : public ImagePixelAccessor<TPixel, VDimension>
33  {
34  friend class Image;
35 
36  public:
40 
54  const ImageDataItem *iDI = nullptr,
55  int OptionFlags = ImageAccessorBase::DefaultBehavior)
56  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
57  {
58  }
59 
61  const ImageDataItem *iDI = nullptr,
62  int OptionFlags = ImageAccessorBase::DefaultBehavior)
63  : ImagePixelAccessor<TPixel, VDimension>(iP.GetPointer(), iDI), m_ReadAccessor(iP, iDI, OptionFlags)
64  {
65  }
66 
68  const ImageDataItem *iDI = nullptr,
69  int OptionFlags = ImageAccessorBase::DefaultBehavior)
70  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(mitk::Image::ConstPointer(iP), iDI, OptionFlags)
71  {
72  }
73 
75  const ImageDataItem *iDI = nullptr,
76  int OptionFlags = ImageAccessorBase::DefaultBehavior)
77  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
78  {
79  }
80 
84  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
85  {
86  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
87 
88  return *(((TPixel *)m_ReadAccessor.m_AddressBegin) + offset);
89  }
90 
92  int nrComponents) const
93  {
95  (TPixel *)m_ReadAccessor.m_AddressBegin +
97  nrComponents);
98  }
99 
103  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
104  {
105  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
106 
107  TPixel *targetAddress = ((TPixel *)m_ReadAccessor.m_AddressBegin) + offset;
108 
109  if (!(targetAddress >= m_ReadAccessor.m_AddressBegin && targetAddress < m_ReadAccessor.m_AddressEnd))
110  {
111  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
112  }
113 
114  return *targetAddress;
115  }
116 
121  {
122  itk::Index<3> itkIndex;
123  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
124 
125  return GetPixelByIndex(itkIndex);
126  }
127 
131  const TPixel &GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
132  {
133  itk::Index<3> itkIndex;
134  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
135  if (m_ReadAccessor.GetImage()->GetTimeSteps() < timestep)
136  {
137  timestep = m_ReadAccessor.GetImage()->GetTimeSteps();
138  }
139  itk::Index<4> itk4Index;
140  for (int i = 0; i < 3; ++i)
141  itk4Index[i] = itkIndex[i];
142 
143  itk4Index[3] = timestep;
144  return GetPixelByIndex(itk4Index);
145  }
146 
148  inline const TPixel *GetData() const { return static_cast<const TPixel *>(m_ReadAccessor.m_AddressBegin); }
149  protected:
150  // protected members
151 
152  private:
153  ImageReadAccessor m_ReadAccessor;
154 
155  ImagePixelReadAccessor &operator=(const ImagePixelReadAccessor &); // Not implemented on purpose.
157  };
158 
163  template <class TPixel>
166  ImageDataItem *item,
167  itk::Index<3> idx,
168  mitk::ScalarType &val,
169  int component = 0)
170  {
172  val = imAccess.GetConsecutivePixelsAsVector(idx, component + 1).GetElement(component);
173  return val;
174  }
175 
177  template <class TPixel>
180  const ImageDataItem* item,
181  itk::Index<3> idx,
182  mitk::ScalarType& val,
183  int component = 0)
184  {
186  val = imAccess.GetConsecutivePixelsAsVector(idx, component + 1).GetElement(component);
187  return val;
188  }
189 
190 }
191 
192 #endif
mitk::ImagePixelReadAccessor::ImagePointer
itk::SmartPointer< mitk::Image > ImagePointer
Definition: mitkImagePixelReadAccessor.h:38
mitk::ImagePixelReadAccessor::ImageConstPointer
itk::SmartPointer< const mitk::Image > ImageConstPointer
Definition: mitkImagePixelReadAccessor.h:39
mitk::ImagePixelAccessor::GetOffset
unsigned int GetOffset(const IndexType &idx) const
Definition: mitkImagePixelAccessor.h:117
mitk::ImageAccessorBase::DefaultBehavior
@ DefaultBehavior
Definition: mitkImageAccessorBase.h:69
mitk::ImageDataItem
Internal class for managing references on sub-images.
Definition: mitkImageDataItem.h:43
mitk::ImagePixelReadAccessor::GetData
const TPixel * GetData() const
Gives const access to the data.
Definition: mitkImagePixelReadAccessor.h:148
mitk::ImagePixelReadAccessor::GetPixelByWorldCoordinates
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
Definition: mitkImagePixelReadAccessor.h:131
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
mitk::ImageReadAccessor::GetImage
const Image * GetImage() const override
mitk::ImagePixelReadAccessor::ImagePixelAccessorType
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
Definition: mitkImagePixelReadAccessor.h:37
itk::SmartPointer
Definition: mitkIFileReader.h:30
mitk::FastSinglePixelAccess
mitk::ScalarType FastSinglePixelAccess(mitk::PixelType, mitk::Image::Pointer im, ImageDataItem *item, itk::Index< 3 > idx, mitk::ScalarType &val, int component=0)
Definition: mitkImagePixelReadAccessor.h:164
mitk::PixelType::GetNumberOfComponents
size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
mitk::ImagePixelReadAccessor
Gives locked and index-based read access for a particular image part. The class provides several set-...
Definition: mitkImagePixelReadAccessor.h:32
mitk::ImagePixelReadAccessor::ImagePixelReadAccessor
ImagePixelReadAccessor(const Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Definition: mitkImagePixelReadAccessor.h:74
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::ImageReadAccessor
ImageReadAccessor class to get locked read access for a particular image part.
Definition: mitkImageReadAccessor.h:27
mitk::Image::GetPixelType
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
mitk::ImagePixelReadAccessor::ImagePixelReadAccessor
ImagePixelReadAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Definition: mitkImagePixelReadAccessor.h:60
mitk::ImagePixelReadAccessor::GetPixelByIndex
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
Definition: mitkImagePixelReadAccessor.h:84
mitk::ImagePixelReadAccessor::GetPixelByWorldCoordinates
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
Definition: mitkImagePixelReadAccessor.h:120
mitk::ImagePixelReadAccessor::~ImagePixelReadAccessor
~ImagePixelReadAccessor() override
Definition: mitkImagePixelReadAccessor.h:82
mitk::BaseData::GetTimeSteps
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
Definition: mitkBaseData.h:338
mitk::Point< ScalarType, 3 >
mitk::ImageAccessorBase::m_AddressEnd
void * m_AddressEnd
Definition: mitkImageAccessorBase.h:114
mitk::ImagePixelAccessor
Provides templated image access for all inheriting classes.
Definition: mitkImagePixelAccessor.h:30
mitkThrow
#define mitkThrow()
Definition: mitkExceptionMacro.h:27
itk::VariableLengthVector
Definition: mitkPixelTypeTraits.h:34
mitk::PixelType
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
mitk::ImagePixelReadAccessor::ImagePixelReadAccessor
ImagePixelReadAccessor(Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Definition: mitkImagePixelReadAccessor.h:67
mitk::ImagePixelReadAccessor::GetConsecutivePixelsAsVector
itk::VariableLengthVector< TPixel > GetConsecutivePixelsAsVector(const itk::Index< VDimension > &idx, int nrComponents) const
Definition: mitkImagePixelReadAccessor.h:91
mitk::BaseData::GetGeometry
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:121
mitk::BaseGeometry::WorldToIndex
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.
mitkImagePixelAccessor.h
mitk::ImagePixelReadAccessor::GetPixelByIndexSafe
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
Definition: mitkImagePixelReadAccessor.h:103
mitkImageReadAccessor.h
itk::Image
class ITK_EXPORT Image
Definition: mitkGeometryClipImageFilter.h:25
mitk::ImagePixelReadAccessor::ImagePixelReadAccessor
ImagePixelReadAccessor(ImageConstPointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor (see its doxygen page for more details)
Definition: mitkImagePixelReadAccessor.h:53
itk::Index
Definition: mitkGrabCutOpenCVImageFilter.h:32
mitk::ImageAccessorBase::IgnoreLock
@ IgnoreLock
Definition: mitkImageAccessorBase.h:79
mitk::ImageAccessorBase::m_AddressBegin
void * m_AddressBegin
Definition: mitkImageAccessorBase.h:111
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20