Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef MITKIMAGEPIXELREADACCESSOR_H
18 #define MITKIMAGEPIXELREADACCESSOR_H
19 
20 #include "mitkImagePixelAccessor.h"
21 #include "mitkImageReadAccessor.h"
22 
23 namespace mitk
24 {
25  class Image;
26 
35  template <class TPixel, unsigned int VDimension = 3>
36  class ImagePixelReadAccessor : public ImagePixelAccessor<TPixel, VDimension>
37  {
38  friend class Image;
39 
40  public:
44 
57  ImagePixelReadAccessor(ImageConstPointer iP,
58  const ImageDataItem *iDI = nullptr,
59  int OptionFlags = ImageAccessorBase::DefaultBehavior)
60  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
61  {
62  }
63 
64  ImagePixelReadAccessor(ImagePointer iP,
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 
72  const ImageDataItem *iDI = nullptr,
73  int OptionFlags = ImageAccessorBase::DefaultBehavior)
74  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(mitk::Image::ConstPointer(iP), iDI, OptionFlags)
75  {
76  }
77 
79  const ImageDataItem *iDI = nullptr,
80  int OptionFlags = ImageAccessorBase::DefaultBehavior)
81  : ImagePixelAccessor<TPixel, VDimension>(iP, iDI), m_ReadAccessor(iP, iDI, OptionFlags)
82  {
83  }
84 
88  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
89  {
90  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
91 
92  return *(((TPixel *)m_ReadAccessor.m_AddressBegin) + offset);
93  }
94 
96  int nrComponents) const
97  {
99  (TPixel *)m_ReadAccessor.m_AddressBegin +
101  nrComponents);
102  }
103 
107  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
108  {
109  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
110 
111  TPixel *targetAddress = ((TPixel *)m_ReadAccessor.m_AddressBegin) + offset;
112 
113  if (!(targetAddress >= m_ReadAccessor.m_AddressBegin && targetAddress < m_ReadAccessor.m_AddressEnd))
114  {
115  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
116  }
117 
118  return *targetAddress;
119  }
120 
125  {
126  itk::Index<3> itkIndex;
127  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
128 
129  return GetPixelByIndex(itkIndex);
130  }
131 
135  const TPixel &GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
136  {
137  itk::Index<3> itkIndex;
138  m_ReadAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
139  if (m_ReadAccessor.GetImage()->GetTimeSteps() < timestep)
140  {
141  timestep = m_ReadAccessor.GetImage()->GetTimeSteps();
142  }
143  itk::Index<4> itk4Index;
144  for (int i = 0; i < 3; ++i)
145  itk4Index[i] = itkIndex[i];
146 
147  itk4Index[3] = timestep;
148  return GetPixelByIndex(itk4Index);
149  }
150 
152  inline const TPixel *GetData() const { return static_cast<const TPixel *>(m_ReadAccessor.m_AddressBegin); }
153  protected:
154  // protected members
155 
156  private:
157  ImageReadAccessor m_ReadAccessor;
158 
159  ImagePixelReadAccessor &operator=(const ImagePixelReadAccessor &); // Not implemented on purpose.
161  };
162 
167  template <class TPixel>
170  ImageDataItem *item,
171  itk::Index<3> idx,
172  mitk::ScalarType &val,
173  int component = 0)
174  {
176  val = imAccess.GetConsecutivePixelsAsVector(idx, component + 1).GetElement(component);
177  return val;
178  }
179 }
180 
181 #endif // MITKIMAGEPIXELREADACCESSOR_H
mitk::ScalarType FastSinglePixelAccess(mitk::PixelType, mitk::Image::Pointer im, ImageDataItem *item, itk::Index< 3 > idx, mitk::ScalarType &val, int component=0)
virtual const Image * GetImage() const override
ImagePixelReadAccessor(Image *iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
Gives locked and index-based read access for a particular image part. The class provides several set-...
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
ImagePixelReadAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
double ScalarType
Provides templated image access for all inheriting classes.
DataCollection - Class to facilitate loading/accessing structured data.
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:346
itk::VariableLengthVector< TPixel > GetConsecutivePixelsAsVector(const itk::Index< VDimension > &idx, int nrComponents) 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:76
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:105
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position, unsigned int timestep)
const TPixel * GetData() const
Gives const access to the data.
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)
unsigned int GetOffset(const IndexType &idx) const
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
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:129
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.