Medical Imaging Interaction Toolkit  2026.06.00
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 (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 mitkImagePixelWriteAccessor_h
14 #define mitkImagePixelWriteAccessor_h
15 
16 #include <mitkImagePixelAccessor.h>
17 #include <mitkImageWriteAccessor.h>
18 
19 namespace mitk
20 {
32  template <class TPixel, unsigned int VDimension = 3>
33  class ImagePixelWriteAccessor : public ImagePixelAccessor<TPixel, VDimension>
34  {
35  friend class Image;
36 
37  public:
40 
58  const ImageDataItem *iDI = nullptr,
59  int OptionFlags = ImageAccessorBase::DefaultBehavior)
60  : ImagePixelAccessor<TPixel, VDimension>(iP.GetPointer(), iDI), m_WriteAccessor(iP, iDI, OptionFlags)
61  {
62  }
63 
65  virtual inline TPixel *GetData() const { return static_cast<TPixel *>(m_WriteAccessor.m_AddressBegin); }
67  void SetPixelByIndex(const itk::Index<VDimension> &idx, const TPixel &value)
68  {
69  unsigned int offset = ImagePixelAccessor<TPixel, VDimension>::GetOffset(idx);
70 
71  *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset) = value;
72  }
73 
75  void SetPixelByIndexSafe(const itk::Index<VDimension> &idx, const TPixel &value)
76  {
77  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
78 
79  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
80 
81  if (targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd)
82  {
83  *targetAddress = value;
84  }
85  else
86  {
87  // printf("image dimensions = %d, %d, %d\n", imageDims[0], imageDims[1], imageDims[2]);
88  // printf("m_AddressBegin: %p, m_AddressEnd: %p, offset: %u\n", m_WriteAccessor.m_AddressBegin,
89  // m_WriteAccessor.m_AddressEnd, offset);
90  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
91  }
92  }
93 
95  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
96  {
97  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
98 
99  return *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset);
100  }
101 
105  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
106  {
107  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
108 
109  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
110 
111  if (!(targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd))
112  {
113  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
114  }
115 
116  return *targetAddress;
117  }
118 
123  {
124  itk::Index<3> itkIndex;
125  m_WriteAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
126 
127  return GetPixelByIndex(itkIndex);
128  }
129 
131  void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep = 0);
132 
135 
136  private:
137  ImageWriteAccessor m_WriteAccessor;
138 
139  ImagePixelWriteAccessor &operator=(const ImagePixelWriteAccessor &); // Not implemented on purpose.
141  };
142 }
143 #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
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.
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 write access for a particular image part.
ImagePixelWriteAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageWriteAccessor (see its doxygen page for more details)
void SetPixelByIndex(const itk::Index< VDimension > &idx, const TPixel &value)
Sets a pixel value at the given index.
virtual TPixel * GetData() const
Gives full data access.
void SetPixelByIndexSafe(const itk::Index< VDimension > &idx, const TPixel &value)
void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep=0)
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
~ImagePixelWriteAccessor() override
Destructor informs Image to unlock memory.
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
itk::SmartPointer< mitk::Image > ImagePointer
Provides locked write 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
#define mitkThrow()
Throw a generic mitk::Exception with source location information.
Find image slices visible on a given plane.