Medical Imaging Interaction Toolkit  2018.4.99-6a3ea89d
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 
53  ImagePixelReadAccessor(ImageConstPointer iP,
54  const ImageDataItem *iDI = nullptr,
55  int OptionFlags = ImageAccessorBase::DefaultBehavior)
56  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
57  {
58  }
59 
60  ImagePixelReadAccessor(ImagePointer iP,
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 #endif // MITKIMAGEPIXELREADACCESSOR_H
mitk::ScalarType FastSinglePixelAccess(mitk::PixelType, mitk::Image::Pointer im, ImageDataItem *item, itk::Index< 3 > idx, mitk::ScalarType &val, int component=0)
const Image * GetImage() const override
ImagePixelReadAccessor(Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Gives locked and index-based read access for a particular image part. The class provides several set-...
ImagePixelReadAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
double ScalarType
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
Provides templated image access for all inheriting classes.
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
DataCollection - Class to facilitate loading/accessing structured data.
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
static Vector3D offset
itk::SmartPointer< mitk::Image > ImagePointer
itk::SmartPointer< const mitk::Image > ImageConstPointer
class ITK_EXPORT Image
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:72
unsigned int GetOffset(const IndexType &idx) const
const TPixel * GetData() const
Gives const access to the data.
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.
ImagePixelReadAccessor(ImageConstPointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageReadAccessor (see its doxygen page for more details)
ImagePixelReadAccessor(const Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
itk::VariableLengthVector< TPixel > GetConsecutivePixelsAsVector(const itk::Index< VDimension > &idx, int nrComponents) const
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:355
Internal class for managing references on sub-images.
ImageReadAccessor class to get locked read access for a particular image part.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)