Medical Imaging Interaction Toolkit  2023.04.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 {
29  template <class TPixel, unsigned int VDimension = 3>
30  class ImagePixelWriteAccessor : public ImagePixelAccessor<TPixel, VDimension>
31  {
32  friend class Image;
33 
34  public:
37 
55  const ImageDataItem *iDI = nullptr,
56  int OptionFlags = ImageAccessorBase::DefaultBehavior)
57  : ImagePixelAccessor<TPixel, VDimension>(iP.GetPointer(), iDI), m_WriteAccessor(iP, iDI, OptionFlags)
58  {
59  }
60 
62  virtual inline TPixel *GetData() const { return static_cast<TPixel *>(m_WriteAccessor.m_AddressBegin); }
64  void SetPixelByIndex(const itk::Index<VDimension> &idx, const TPixel &value)
65  {
66  unsigned int offset = ImagePixelAccessor<TPixel, VDimension>::GetOffset(idx);
67 
68  *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset) = value;
69  }
70 
72  void SetPixelByIndexSafe(const itk::Index<VDimension> &idx, const TPixel &value)
73  {
74  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
75 
76  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
77 
78  if (targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd)
79  {
80  *targetAddress = value;
81  }
82  else
83  {
84  // printf("image dimensions = %d, %d, %d\n", imageDims[0], imageDims[1], imageDims[2]);
85  // printf("m_AddressBegin: %p, m_AddressEnd: %p, offset: %u\n", m_WriteAccessor.m_AddressBegin,
86  // m_WriteAccessor.m_AddressEnd, offset);
87  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
88  }
89  }
90 
92  const TPixel &GetPixelByIndex(const itk::Index<VDimension> &idx) const
93  {
94  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
95 
96  return *(((TPixel *)m_WriteAccessor.m_AddressBegin) + offset);
97  }
98 
102  const TPixel &GetPixelByIndexSafe(const itk::Index<VDimension> &idx) const
103  {
104  unsigned int offset = ImagePixelAccessorType::GetOffset(idx);
105 
106  TPixel *targetAddress = ((TPixel *)m_WriteAccessor.m_AddressBegin) + offset;
107 
108  if (!(targetAddress >= m_WriteAccessor.m_AddressBegin && targetAddress < m_WriteAccessor.m_AddressEnd))
109  {
110  mitkThrow() << "ImageAccessor Overflow: image access exceeds the requested image area at " << idx << ".";
111  }
112 
113  return *targetAddress;
114  }
115 
120  {
121  itk::Index<3> itkIndex;
122  m_WriteAccessor.GetImage()->GetGeometry()->WorldToIndex(position, itkIndex);
123 
124  return GetPixelByIndex(itkIndex);
125  }
126 
128  void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep = 0);
129 
131  private:
132  ImageWriteAccessor m_WriteAccessor;
133 
134  ImagePixelWriteAccessor &operator=(const ImagePixelWriteAccessor &); // Not implemented on purpose.
136  };
137 }
138 #endif
mitk::ImagePixelWriteAccessor::GetData
virtual TPixel * GetData() const
Gives full data access.
Definition: mitkImagePixelWriteAccessor.h:62
mitk::ImageWriteAccessor::GetImage
const Image * GetImage() const override
mitk::ImagePixelAccessor::GetOffset
unsigned int GetOffset(const IndexType &idx) const
Definition: mitkImagePixelAccessor.h:117
mitk::ImageWriteAccessor
ImageWriteAccessor class to get locked write-access for a particular image part.
Definition: mitkImageWriteAccessor.h:25
mitk::ImageAccessorBase::DefaultBehavior
@ DefaultBehavior
Definition: mitkImageAccessorBase.h:69
mitk::ImageDataItem
Internal class for managing references on sub-images.
Definition: mitkImageDataItem.h:43
mitk::ImagePixelWriteAccessor::SetPixelByIndex
void SetPixelByIndex(const itk::Index< VDimension > &idx, const TPixel &value)
Sets a pixel value at given index.
Definition: mitkImagePixelWriteAccessor.h:64
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
itk::SmartPointer
Definition: mitkIFileReader.h:30
mitk::ImagePixelWriteAccessor::GetPixelByIndexSafe
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
Definition: mitkImagePixelWriteAccessor.h:102
mitk::ImagePixelWriteAccessor::ImagePixelWriteAccessor
ImagePixelWriteAccessor(ImagePointer iP, const ImageDataItem *iDI=nullptr, int OptionFlags=ImageAccessorBase::DefaultBehavior)
Instantiates a mitk::ImageWriteAccessor (see its doxygen page for more details)
Definition: mitkImagePixelWriteAccessor.h:54
mitkImageWriteAccessor.h
mitk::ImagePixelWriteAccessor::SetPixelByWorldCoordinates
void SetPixelByWorldCoordinates(const mitk::Point3D &, const TPixel &value, unsigned int timestep=0)
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::ImagePixelWriteAccessor::ImagePointer
itk::SmartPointer< mitk::Image > ImagePointer
Definition: mitkImagePixelWriteAccessor.h:36
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
mitk::ImagePixelWriteAccessor::SetPixelByIndexSafe
void SetPixelByIndexSafe(const itk::Index< VDimension > &idx, const TPixel &value)
Definition: mitkImagePixelWriteAccessor.h:72
mitk::ImagePixelWriteAccessor
Gives locked and index-based write access for a particular image part. The class provides several set...
Definition: mitkImagePixelWriteAccessor.h:30
mitk::ImagePixelWriteAccessor::GetPixelByWorldCoordinates
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
Definition: mitkImagePixelWriteAccessor.h:119
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::ImagePixelWriteAccessor::ImagePixelAccessorType
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
Definition: mitkImagePixelWriteAccessor.h:35
mitk::ImagePixelWriteAccessor::GetPixelByIndex
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
Definition: mitkImagePixelWriteAccessor.h:92
itk::Index
Definition: mitkGrabCutOpenCVImageFilter.h:32
mitk::ImageAccessorBase::m_AddressBegin
void * m_AddressBegin
Definition: mitkImageAccessorBase.h:111
mitk::ImagePixelWriteAccessor::~ImagePixelWriteAccessor
~ImagePixelWriteAccessor() override
Definition: mitkImagePixelWriteAccessor.h:130