Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkImagePixelWriteAccessor.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 MITKIMAGEPIXELWRITEACCESSOR_H
18 #define MITKIMAGEPIXELWRITEACCESSOR_H
19 
20 #include "mitkImagePixelAccessor.h"
21 #include "mitkImageWriteAccessor.h"
22 
23 namespace mitk
24 {
33  template <class TPixel, unsigned int VDimension = 3>
34  class ImagePixelWriteAccessor : public ImagePixelAccessor<TPixel, VDimension>
35  {
36  friend class Image;
37 
38  public:
41 
58  ImagePixelWriteAccessor(ImagePointer iP,
59  const ImageDataItem *iDI = nullptr,
60  int OptionFlags = ImageAccessorBase::DefaultBehavior)
61  : ImagePixelAccessor<TPixel, VDimension>(iP.GetPointer(), iDI), m_WriteAccessor(iP, iDI, OptionFlags)
62  {
63  }
64 
66  virtual inline TPixel *GetData() const { return static_cast<TPixel *>(m_WriteAccessor.m_AddressBegin); }
68  void SetPixelByIndex(const itk::Index<VDimension> &idx, const TPixel &value)
69  {
71 
72  *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset) = value;
73  }
74 
76  void SetPixelByIndexSafe(const itk::Index<VDimension> &idx, const TPixel &value)
77  {
78  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
79 
80  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
81 
82  if (targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd)
83  {
84  *targetAddress = value;
85  }
86  else
87  {
88  // printf("image dimensions = %d, %d, %d\n", imageDims[0], imageDims[1], imageDims[2]);
89  // printf("m_AddressBegin: %p, m_AddressEnd: %p, offset: %u\n", m_WriteAccessor.m_AddressBegin,
90  // m_WriteAccessor.m_AddressEnd, offset);
91  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
92  }
93  }
94 
96  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
97  {
98  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
99 
100  return *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset);
101  }
102 
106  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
107  {
108  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
109 
110  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
111 
112  if (!(targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd))
113  {
114  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
115  }
116 
117  return *targetAddress;
118  }
119 
124  {
125  itk::Index<3> itkIndex;
126  m_WriteAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
127 
128  return GetPixelByIndex(itkIndex);
129  }
130 
132  void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep = 0);
133 
135  private:
136  ImageWriteAccessor m_WriteAccessor;
137 
138  ImagePixelWriteAccessor &operator=(const ImagePixelWriteAccessor &); // Not implemented on purpose.
140  };
141 }
142 #endif // MITKIMAGEWRITEACCESSOR_H
Gives locked and index-based write access for a particular image part. The class provides several set...
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
void SetPixelByIndexSafe(const itk::Index< VDimension > &idx, const TPixel &value)
Provides templated image access for all inheriting classes.
DataCollection - Class to facilitate loading/accessing structured data.
ImagePixelWriteAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageWriteAccessor (see its doxygen page for more details)
itk::SmartPointer< mitk::Image > ImagePointer
static Vector3D offset
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
virtual TPixel * GetData() const
Gives full data access.
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:76
void SetPixelByIndex(const itk::Index< VDimension > &idx, const TPixel &value)
Sets a pixel value at given index.
void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep=0)
unsigned int GetOffset(const IndexType &idx) const
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
ImageWriteAccessor class to get locked write-access for a particular image part.
Internal class for managing references on sub-images.
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
virtual const Image * GetImage() const override
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.