Medical Imaging Interaction Toolkit  2018.4.99-663e373e
Medical Imaging Interaction Toolkit
mitkImagePixelAccessor.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 MITKIMAGEPIXELACCESSOR_H
14 #define MITKIMAGEPIXELACCESSOR_H
15 
16 #include "mitkImage.h"
17 #include "mitkImageDataItem.h"
18 
19 #include <typeinfo>
20 
21 namespace mitk
22 {
29  template <class TPixel, unsigned int VDimension = 3>
31  {
32  public:
36 
38  int GetDimension(int i) const { return m_ImageDataItem->GetDimension(i); }
39 
40  private:
41  friend class Image;
42 
43  protected:
45  ImagePixelAccessor(ImageConstPointer iP, const mitk::ImageDataItem *iDI) : m_ImageDataItem(iDI)
46  {
47  if (iDI == nullptr)
48  {
49  m_ImageDataItem = iP->GetChannelData();
50  }
51  CheckData(iP.GetPointer());
52  }
53 
55  virtual ~ImagePixelAccessor() {}
56 
57  void CheckData( const Image *image )
58  {
59  // Check if Dimensions are correct
60  if ( m_ImageDataItem == nullptr )
61  {
62  if ( image->GetDimension() != VDimension )
63  {
64  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and Image are not equal."
65  << " They have to be equal if an entire image is requested."
66  << " image->GetDimension(): " << image->GetDimension() << " , VDimension: " << VDimension;
67  }
68  }
69  else
70  {
71  if ( m_ImageDataItem->GetDimension() != VDimension )
72  {
73  mitkThrow() << "Invalid ImageAccessor: The Dimensions of ImageAccessor and ImageDataItem are not equal."
74  << " m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension() << " , VDimension: " << VDimension;
75  }
76  }
77 
78  if (!( image->GetPixelType() == mitk::MakePixelType< itk::Image< TPixel, VDimension > >() ||
79  image->GetPixelType() == mitk::MakePixelType< itk::VectorImage< TPixel, VDimension > >
80  ( image->GetPixelType().GetNumberOfComponents() )
81  ) )
82  {
83  mitkThrow() << "Invalid ImageAccessor: PixelTypes of Image and ImageAccessor are not equal."
84  << " image->GetPixelType(): " << typeid(image->GetPixelType()).name()
85  << "\n m_ImageDataItem->GetDimension(): " << m_ImageDataItem->GetDimension()
86  << " , VDimension: " << VDimension
87  << " , TPixel: " << typeid(TPixel).name()
88  << " , NumberOfComponents: " << image->GetPixelType().GetNumberOfComponents() << std::endl;
89  }
90  }
91 
94 
100 
106  itk::ImageRegion<VDimension> *m_SubRegion;
107 
112 
114  unsigned int GetOffset(const IndexType &idx) const
115  {
116  const unsigned int *imageDims = m_ImageDataItem->m_Dimensions;
117 
118  unsigned int offset = 0;
119  switch (VDimension)
120  {
121  case 4:
122  offset += idx[3] * imageDims[0] * imageDims[1] * imageDims[2];
123  /* FALLTHRU */
124  case 3:
125  offset += idx[2] * imageDims[0] * imageDims[1];
126  /* FALLTHRU */
127  case 2:
128  offset += idx[0] + idx[1] * imageDims[0];
129  /* FALLTHRU */
130  default:
131  break;
132  }
133  return offset;
134  }
135  };
136 }
137 
138 #endif // MITKIMAGEACCESSOR_H
ImagePixelAccessor< TPixel, VDimension > ImagePixelAccessorType
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
int m_Options
Stores all extended properties of an ImageAccessor. The different flags in mitk::ImageAccessorBase::O...
Provides templated image access for all inheriting classes.
DataCollection - Class to facilitate loading/accessing structured data.
BaseGeometry::Pointer m_Geometry
Pointer to the used Geometry. Since Geometry can be different to the Image (if memory was forced to b...
unsigned int GetDimension() const
Get dimension of the image.
itk::Index< VDimension > IndexType
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:72
unsigned int GetOffset(const IndexType &idx) const
void CheckData(const Image *image)
itk::ImageRegion< VDimension > * m_SubRegion
A Subregion defines an arbitrary area within the image. If no SubRegion is defined, the whole ImageDataItem or Image is regarded. A subregion (e.g. subvolume) can lead to non-coherent memory access where every dimension has a start- and end-offset.
Image::ConstPointer ImageConstPointer
const ImageDataItem * m_ImageDataItem
Internal class for managing references on sub-images.
ImagePixelAccessor(ImageConstPointer iP, const mitk::ImageDataItem *iDI)