ITK  5.3.0
Insight Toolkit
itkImage.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImage_h
19 #define itkImage_h
20 
21 #include "itkImageRegion.h"
25 #include "itkPoint.h"
26 #include "itkFixedArray.h"
27 #include "itkWeakPointer.h"
29 
30 #include <type_traits> // For is_same
31 
32 namespace itk
33 {
87 template <typename TPixel, unsigned int VImageDimension = 2>
88 class ITK_TEMPLATE_EXPORT Image : public ImageBase<VImageDimension>
89 {
90 public:
91  ITK_DISALLOW_COPY_AND_MOVE(Image);
92 
94  using Self = Image;
99 
101  itkNewMacro(Self);
102 
104  itkTypeMacro(Image, ImageBase);
105 
108  using PixelType = TPixel;
109 
111  using ValueType = TPixel;
112 
117  using InternalPixelType = TPixel;
118 
120 
125 
129 
131  using ImageDimensionType = typename Superclass::ImageDimensionType;
132 
136 
138  using OffsetType = typename Superclass::OffsetType;
139 
141  using SizeType = typename Superclass::SizeType;
143 
146 
149 
153 
156  using SpacingType = typename Superclass::SpacingType;
157  using SpacingValueType = typename Superclass::SpacingValueType;
158 
162 
166 
169 
176  template <typename UPixelType, unsigned int NUImageDimension = VImageDimension>
177  struct Rebind
178  {
180  };
181 
182 
183  template <typename UPixelType, unsigned int NUImageDimension = VImageDimension>
185 
186 
189  void
190  Allocate(bool initializePixels = false) override;
191 
194  void
195  Initialize() override;
196 
199  void
200  FillBuffer(const TPixel & value);
201 
207  void
208  SetPixel(const IndexType & index, const TPixel & value)
209  {
210  OffsetValueType offset = this->FastComputeOffset(index);
211  (*m_Buffer)[offset] = value;
212  }
213 
218  const TPixel &
219  GetPixel(const IndexType & index) const
220  {
221  OffsetValueType offset = this->FastComputeOffset(index);
222  return ((*m_Buffer)[offset]);
223  }
224 
229  TPixel &
230  GetPixel(const IndexType & index)
231  {
232  OffsetValueType offset = this->FastComputeOffset(index);
233  return ((*m_Buffer)[offset]);
234  }
235 
240  TPixel & operator[](const IndexType & index) { return this->GetPixel(index); }
241 
246  const TPixel & operator[](const IndexType & index) const { return this->GetPixel(index); }
247 
250  virtual TPixel *
252  {
253  return m_Buffer ? m_Buffer->GetBufferPointer() : nullptr;
254  }
255  virtual const TPixel *
257  {
258  return m_Buffer ? m_Buffer->GetBufferPointer() : nullptr;
259  }
261 
263  PixelContainer *
265  {
266  return m_Buffer.GetPointer();
267  }
268 
269  const PixelContainer *
271  {
272  return m_Buffer.GetPointer();
273  }
274 
277  void
278  SetPixelContainer(PixelContainer * container);
279 
290  virtual void
291  Graft(const Self * image);
292 
294  AccessorType
296  {
297  return AccessorType();
298  }
299 
301  const AccessorType
303  {
304  return AccessorType();
305  }
306 
308  NeighborhoodAccessorFunctorType
310  {
312  }
313 
315  const NeighborhoodAccessorFunctorType
317  {
319  }
320 
321  unsigned int
322  GetNumberOfComponentsPerPixel() const override;
323 
329  template <typename TEqualityComparable>
330  friend std::enable_if_t<std::is_same<TEqualityComparable, TPixel>::value, bool>
333  {
334  if ((lhs.GetBufferedRegion() != rhs.GetBufferedRegion()) || (lhs.m_Spacing != rhs.m_Spacing) ||
335  (lhs.m_Origin != rhs.m_Origin) || (lhs.m_Direction != rhs.m_Direction) ||
337  {
338  return false;
339  }
340 
341  if (lhs.m_Buffer == rhs.m_Buffer)
342  {
343  return true;
344  }
345 
346  if ((lhs.m_Buffer == nullptr) || (rhs.m_Buffer == nullptr))
347  {
348  return false;
349  }
350 
351  auto & lhsBuffer = *(lhs.m_Buffer);
352  auto & rhsBuffer = *(rhs.m_Buffer);
353 
354  const auto bufferSize = lhsBuffer.Size();
355 
356  if (bufferSize != rhsBuffer.Size())
357  {
358  return false;
359  }
360 
361  const TEqualityComparable * const lhsBufferPointer = lhsBuffer.GetBufferPointer();
362  const TEqualityComparable * const rhsBufferPointer = rhsBuffer.GetBufferPointer();
363 
364  return ((lhsBufferPointer == rhsBufferPointer) ||
365  std::equal(lhsBufferPointer, lhsBufferPointer + bufferSize, rhsBufferPointer));
366  }
367 
369  template <typename TEqualityComparable>
370  friend std::enable_if_t<std::is_same<TEqualityComparable, TPixel>::value, bool>
373  {
374  return !(lhs == rhs);
375  }
376 
377 protected:
378  Image();
379  void
380  PrintSelf(std::ostream & os, Indent indent) const override;
381  void
382  Graft(const DataObject * data) override;
383 
384  ~Image() override = default;
385 
391  void
392  ComputeIndexToPhysicalPointMatrices() override;
393  using Superclass::Graft;
394 
395 private:
398 };
399 } // end namespace itk
400 
401 #ifndef ITK_MANUAL_INSTANTIATION
402 # include "itkImage.hxx"
403 #endif
404 
405 #endif
itk::ImageBase::OffsetValueType
typename OffsetType::OffsetValueType OffsetValueType
Definition: itkImageBase.h:138
itk::Image::GetNeighborhoodAccessor
NeighborhoodAccessorFunctorType GetNeighborhoodAccessor()
Definition: itkImage.h:309
itk::ImageBase::GetBufferedRegion
virtual const RegionType & GetBufferedRegion() const
Definition: itkImageBase.h:277
itk::Image::GetPixelAccessor
const AccessorType GetPixelAccessor() const
Definition: itkImage.h:302
itk::Index< VImageDimension >
itk::Image< TNode *, VImageDimension >::IOPixelType
PixelType IOPixelType
Definition: itkImage.h:119
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itkWeakPointer.h
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::Image::m_Buffer
PixelContainerPointer m_Buffer
Definition: itkImage.h:397
itk::ImageBase
Base class for templated image classes.
Definition: itkImageBase.h:105
itkPoint.h
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itk::ImageBase::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:142
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::Vector< SpacingValueType, VImageDimension >
itk::Image::GetPixel
const TPixel & GetPixel(const IndexType &index) const
Get a pixel (read only version).
Definition: itkImage.h:219
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkNeighborhoodAccessorFunctor.h
itk::SmartPointer< Self >
itk::DefaultPixelAccessor
Give access to partial aspects a type.
Definition: itkDefaultPixelAccessor.h:54
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Image::GetBufferPointer
virtual const TPixel * GetBufferPointer() const
Definition: itkImage.h:256
itk::ImageBase::IndexValueType
typename IndexType::IndexValueType IndexValueType
Definition: itkImageBase.h:133
itk::NeighborhoodAccessorFunctor
Provides accessor interfaces to Get pixels and is meant to be used on pointers contained within Neigh...
Definition: itkNeighborhoodAccessorFunctor.h:41
itkImageRegion.h
itk::Image::GetPixelAccessor
AccessorType GetPixelAccessor()
Definition: itkImage.h:295
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itkDefaultPixelAccessor.h
itk::Image< TNode *, VImageDimension >::ValueType
TNode * ValueType
Definition: itkImage.h:111
itk::DefaultPixelAccessorFunctor
Provides a common API for pixel accessors for Image and VectorImage.
Definition: itkDefaultPixelAccessorFunctor.h:47
itk::ImageBase::ImageDimensionType
unsigned int ImageDimensionType
Definition: itkImageBase.h:123
itk::Image::Rebind
Definition: itkImage.h:177
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::Image::SetPixel
void SetPixel(const IndexType &index, const TPixel &value)
Set a pixel value.
Definition: itkImage.h:208
itkFixedArray.h
itk::Image< TNode *, VImageDimension >::InternalPixelType
TNode * InternalPixelType
Definition: itkImage.h:117
itk::Image::GetPixel
TPixel & GetPixel(const IndexType &index)
Get a reference to a pixel (e.g. for editing).
Definition: itkImage.h:230
itkImportImageContainer.h
itk::ImportImageContainer
Defines an itk::Image front-end to a standard C-array.
Definition: itkImportImageContainer.h:45
itk::Matrix< SpacePrecisionType, VImageDimension, VImageDimension >
itk::Image< TNode *, VImageDimension >::PixelType
TNode * PixelType
Definition: itkImage.h:108
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:67
itk::ImageBase::SpacingValueType
SpacePrecisionType SpacingValueType
Definition: itkImageBase.h:151
itk::Image::operator==
friend std::enable_if_t< std::is_same< TEqualityComparable, TPixel >::value, bool > operator==(const Image< TEqualityComparable, VImageDimension > &lhs, const Image< TEqualityComparable, VImageDimension > &rhs)
Definition: itkImage.h:331
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk::ImageBase::m_Spacing
SpacingType m_Spacing
Definition: itkImageBase.h:826
itk::Image::GetPixelContainer
const PixelContainer * GetPixelContainer() const
Definition: itkImage.h:270
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Image< TNode *, VImageDimension >::PixelContainerConstPointer
typename PixelContainer::ConstPointer PixelContainerConstPointer
Definition: itkImage.h:165
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::Image::operator[]
const TPixel & operator[](const IndexType &index) const
Access a pixel. This version can only be an rvalue.
Definition: itkImage.h:246
itk::Image::GetNeighborhoodAccessor
const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
Definition: itkImage.h:316
itk::IndexValueType
signed long IndexValueType
Definition: itkIntTypes.h:90
itkDefaultPixelAccessorFunctor.h
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::Point< PointValueType, VImageDimension >
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::Image::GetBufferPointer
virtual TPixel * GetBufferPointer()
Definition: itkImage.h:251
itk::Image::operator[]
TPixel & operator[](const IndexType &index)
Access a pixel. This version can be an lvalue.
Definition: itkImage.h:240
itk::Image< TNode *, VImageDimension >::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImage.h:164
itk::Image::GetPixelContainer
PixelContainer * GetPixelContainer()
Definition: itkImage.h:264
itk::ImageBase::m_Origin
PointType m_Origin
Definition: itkImageBase.h:827
itk::ImageBase::m_InverseDirection
DirectionType m_InverseDirection
Definition: itkImageBase.h:829
itk::ImageBase::m_Direction
DirectionType m_Direction
Definition: itkImageBase.h:828
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::Image::operator!=
friend std::enable_if_t< std::is_same< TEqualityComparable, TPixel >::value, bool > operator!=(const Image< TEqualityComparable, VImageDimension > &lhs, const Image< TEqualityComparable, VImageDimension > &rhs)
Definition: itkImage.h:371