Medical Imaging Interaction Toolkit  2018.4.99-701c8286
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 
54  ImagePixelWriteAccessor(ImagePointer iP,
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 // MITKIMAGEWRITEACCESSOR_H
const TPixel & GetPixelByIndexSafe(const itk::Index< VDimension > &idx) const
Gives locked and index-based write access for a particular image part. The class provides several set...
const TPixel & GetPixelByWorldCoordinates(mitk::Point3D position)
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)
const TPixel & GetPixelByIndex(const itk::Index< VDimension > &idx) const
itk::SmartPointer< mitk::Image > ImagePointer
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:72
unsigned int GetOffset(const IndexType &idx) const
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.
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)
const Image * GetImage() const override
virtual TPixel * GetData() const
Gives full data access.
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:143