SimpleITK  
sitkImage.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 sitkImage_h
19 #define sitkImage_h
20 
21 #include "sitkCommon.h"
22 #include "sitkTemplateFunctions.h"
23 #include "sitkDetail.h"
24 #include "sitkPixelIDTokens.h"
25 #include "sitkInterpolator.h"
26 
27 #include <vector>
28 #include <memory>
29 #include <type_traits>
30 
31 namespace itk
32 {
33 
34 // Forward declaration for pointer
35 class DataObject;
36 
37 template<class T>
38 class SmartPointer;
39 
40 namespace simple
41 {
42 
43  // This is the forward declaration of a class used internally to the
44  // Image class, but the actual interface is not exposed to simple
45  // ITK. A pointer to the implementation is used as per the pimple
46  // idiom.
47  class PimpleImageBase;
48 
77  {
78  public:
79  using Self = Image;
80 
81  virtual ~Image( );
82 
84  Image( );
85 
86  // copy constructor
87  Image( const Image &img );
88 
89  Image& operator=( const Image &img );
90 
91 #ifndef SWIG
92 
98  Image( Image &&img ) noexcept;
99  Image& operator=( Image &&img ) noexcept;
100 
120  Image ProxyForInPlaceOperation();
121 #endif
122 
123 
139  Image( unsigned int width, unsigned int height, PixelIDValueEnum valueEnum );
140  Image( unsigned int width, unsigned int height, unsigned int depth, PixelIDValueEnum valueEnum );
141  Image( const std::vector< unsigned int > &size, PixelIDValueEnum valueEnum, unsigned int numberOfComponents = 0 );
161  template <typename TImageType>
163  : Image( image.GetPointer() )
164  {}
165 
166  template <typename TImageType>
167  explicit Image( TImageType* image )
168  : Image()
169  {
171  const unsigned int dimension = TImageType::ImageDimension;
172 
173  static_assert(type != sitkUnknown, "invalid pixel type");
174  static_assert(dimension >= 2 && dimension <= SITK_MAX_DIMENSION, "Unsupported image dimension.");
175 
176  this->InternalInitialization(type, dimension, image);
177  }
194  itk::DataObject* GetITKBase( );
195  const itk::DataObject* GetITKBase( ) const;
204  PixelIDValueEnum GetPixelID( ) const;
205  PixelIDValueType GetPixelIDValue( ) const;
206 
208  std::string GetPixelIDTypeAsString( ) const;
209 
217  unsigned int GetDimension( ) const;
218 
225  unsigned int GetNumberOfComponentsPerPixel( ) const;
226 
235  uint64_t GetNumberOfPixels( ) const;
236 
241  unsigned int GetSizeOfPixelComponent( ) const;
242 
246  std::vector< double > GetOrigin( ) const;
247  void SetOrigin( const std::vector< double > &origin );
256  std::vector< double > GetSpacing( ) const;
257  void SetSpacing( const std::vector< double > &spacing );
267  std::vector< double > GetDirection() const;
268  void SetDirection ( const std::vector< double > &direction );
272  std::vector< double > TransformIndexToPhysicalPoint( const std::vector< int64_t > &index ) const;
273 
275  std::vector< int64_t > TransformPhysicalPointToIndex( const std::vector< double >& point ) const;
276 
278  std::vector< double > TransformPhysicalPointToContinuousIndex( const std::vector< double >& point ) const;
279 
281  std::vector< double > TransformContinuousIndexToPhysicalPoint( const std::vector< double > &index) const;
282 
294  std::vector<double> EvaluateAtContinuousIndex( const std::vector<double> &index, InterpolatorEnum interp = sitkLinear) const;
295 
307  std::vector<double> EvaluateAtPhysicalPoint( const std::vector<double> &point, InterpolatorEnum interp = sitkLinear) const;
308 
312  std::vector< unsigned int > GetSize( ) const;
313 
315  unsigned int GetWidth( ) const;
316 
318  unsigned int GetHeight( ) const;
319 
322  unsigned int GetDepth( ) const;
323 
324 
335  void CopyInformation( const Image &srcImage );
336 
343  std::vector<std::string> GetMetaDataKeys( ) const;
344 
347  bool HasMetaDataKey( const std::string &key ) const;
348 
357  std::string GetMetaData( const std::string &key ) const;
358 
363  void SetMetaData( const std::string &key, const std::string &value);
364 
370  bool EraseMetaData( const std::string &key );
371 
372  std::string ToString( ) const;
373 
391  Image ToVectorImage(bool inPlace = true);
392 
411  Image ToScalarImage(bool inPlace = true);
412 
428  int8_t GetPixelAsInt8( const std::vector<uint32_t> &idx) const;
429  uint8_t GetPixelAsUInt8( const std::vector<uint32_t> &idx) const;
430  int16_t GetPixelAsInt16( const std::vector<uint32_t> &idx ) const;
431  uint16_t GetPixelAsUInt16( const std::vector<uint32_t> &idx ) const;
432  int32_t GetPixelAsInt32( const std::vector<uint32_t> &idx ) const;
433  uint32_t GetPixelAsUInt32( const std::vector<uint32_t> &idx ) const;
434  int64_t GetPixelAsInt64( const std::vector<uint32_t> &idx ) const;
435  uint64_t GetPixelAsUInt64( const std::vector<uint32_t> &idx ) const;
436  float GetPixelAsFloat( const std::vector<uint32_t> &idx ) const;
437  double GetPixelAsDouble( const std::vector<uint32_t> &idx ) const;
438 
439  std::vector<int8_t> GetPixelAsVectorInt8( const std::vector<uint32_t> &idx) const;
440  std::vector<uint8_t> GetPixelAsVectorUInt8( const std::vector<uint32_t> &idx) const;
441  std::vector<int16_t> GetPixelAsVectorInt16( const std::vector<uint32_t> &idx ) const;
442  std::vector<uint16_t> GetPixelAsVectorUInt16( const std::vector<uint32_t> &idx ) const;
443  std::vector<int32_t> GetPixelAsVectorInt32( const std::vector<uint32_t> &idx ) const;
444  std::vector<uint32_t> GetPixelAsVectorUInt32( const std::vector<uint32_t> &idx ) const;
445  std::vector<int64_t> GetPixelAsVectorInt64( const std::vector<uint32_t> &idx ) const;
446  std::vector<uint64_t> GetPixelAsVectorUInt64( const std::vector<uint32_t> &idx ) const;
447  std::vector<float> GetPixelAsVectorFloat32( const std::vector<uint32_t> &idx ) const;
448  std::vector<double> GetPixelAsVectorFloat64( const std::vector<uint32_t> &idx ) const;
449 
450  std::complex<float> GetPixelAsComplexFloat32( const std::vector<uint32_t> &idx ) const;
451  std::complex<double> GetPixelAsComplexFloat64( const std::vector<uint32_t> &idx ) const;
470  void SetPixelAsInt8( const std::vector<uint32_t> &idx, int8_t v );
471  void SetPixelAsUInt8( const std::vector<uint32_t> &idx, uint8_t v );
472  void SetPixelAsInt16( const std::vector<uint32_t> &idx, int16_t v );
473  void SetPixelAsUInt16( const std::vector<uint32_t> &idx, uint16_t v );
474  void SetPixelAsInt32( const std::vector<uint32_t> &idx, int32_t v );
475  void SetPixelAsUInt32( const std::vector<uint32_t> &idx, uint32_t v );
476  void SetPixelAsInt64( const std::vector<uint32_t> &idx, int64_t v );
477  void SetPixelAsUInt64( const std::vector<uint32_t> &idx, uint64_t v );
478  void SetPixelAsFloat( const std::vector<uint32_t> &idx, float v );
479  void SetPixelAsDouble( const std::vector<uint32_t> &idx, double v );
480 
481  void SetPixelAsVectorInt8( const std::vector<uint32_t> &idx, const std::vector<int8_t> &v );
482  void SetPixelAsVectorUInt8( const std::vector<uint32_t> &idx, const std::vector<uint8_t> &v );
483  void SetPixelAsVectorInt16( const std::vector<uint32_t> &idx, const std::vector<int16_t> &v );
484  void SetPixelAsVectorUInt16( const std::vector<uint32_t> &idx, const std::vector<uint16_t> &v );
485  void SetPixelAsVectorInt32( const std::vector<uint32_t> &idx, const std::vector<int32_t> &v );
486  void SetPixelAsVectorUInt32( const std::vector<uint32_t> &idx, const std::vector<uint32_t> &v );
487  void SetPixelAsVectorInt64( const std::vector<uint32_t> &idx, const std::vector<int64_t> &v );
488  void SetPixelAsVectorUInt64( const std::vector<uint32_t> &idx, const std::vector<uint64_t> &v );
489  void SetPixelAsVectorFloat32( const std::vector<uint32_t> &idx, const std::vector<float> &v );
490  void SetPixelAsVectorFloat64( const std::vector<uint32_t> &idx, const std::vector<double> &v );
491 
492  void SetPixelAsComplexFloat32( const std::vector<uint32_t> &idx, const std::complex<float> v );
493  void SetPixelAsComplexFloat64( const std::vector<uint32_t> &idx, const std::complex<double> v );
494 
523  int8_t *GetBufferAsInt8( );
524  uint8_t *GetBufferAsUInt8( );
525  int16_t *GetBufferAsInt16( );
526  uint16_t *GetBufferAsUInt16( );
527  int32_t *GetBufferAsInt32( );
528  uint32_t *GetBufferAsUInt32( );
529  int64_t *GetBufferAsInt64( );
530  uint64_t *GetBufferAsUInt64( );
531  float *GetBufferAsFloat( );
532  double *GetBufferAsDouble( );
533  void *GetBufferAsVoid();
534 
535  const int8_t *GetBufferAsInt8( ) const;
536  const uint8_t *GetBufferAsUInt8( ) const;
537  const int16_t *GetBufferAsInt16( ) const;
538  const uint16_t *GetBufferAsUInt16( ) const;
539  const int32_t *GetBufferAsInt32( ) const;
540  const uint32_t *GetBufferAsUInt32( ) const;
541  const int64_t *GetBufferAsInt64( ) const;
542  const uint64_t *GetBufferAsUInt64( ) const;
543  const float *GetBufferAsFloat( ) const;
544  const double *GetBufferAsDouble( ) const;
545  const void *GetBufferAsVoid() const;
555  void MakeUnique( );
556 
559  bool IsUnique( ) const;
560 
561  protected:
562 
569  void Allocate ( const std::vector<unsigned int > &size, PixelIDValueEnum valueEnum, unsigned int numberOfComponents );
570 
578  template<class TImageType>
579  typename std::enable_if<IsBasic<TImageType>::Value>::type
580  AllocateInternal ( const std::vector<unsigned int > &size, unsigned int numberOfComponents );
581 
582  template<class TImageType>
583  typename std::enable_if<IsVector<TImageType>::Value>::type
584  AllocateInternal ( const std::vector<unsigned int > &size, unsigned int numberOfComponents );
585 
586  template<class TImageType>
587  typename std::enable_if<IsLabel<TImageType>::Value>::type
588  AllocateInternal ( const std::vector<unsigned int > &size, unsigned int numberOfComponents );
594  template<class TImageType>
595  std::enable_if_t<IsVector<TImageType>::Value, Image>
596  ToVectorInternal(bool inPlace);
597 
598  template<class TImageType>
599  std::enable_if_t<IsBasic<TImageType>::Value, Image>
600  ToVectorInternal(bool inPlace);
601 
602  template<class TImageType>
603  std::enable_if_t<IsVector<TImageType>::Value, Image>
604  ToScalarInternal(bool inPlace);
605 
606  template<class TImageType>
607  std::enable_if_t<IsBasic<TImageType>::Value, Image>
608  ToScalarInternal(bool inPlace);
611  private:
612 
621  void InternalInitialization( PixelIDValueType type, unsigned int dimension, itk::DataObject *image );
622 
623 
624  Image( std::unique_ptr<PimpleImageBase> pimpleImage );
625 
626 
627  template <typename TImageType>
628  PimpleImageBase * DispatchedInternalInitialization(itk::DataObject *image);
629 
630 
631  friend struct DispatchedInternalInitialiationAddressor;
632  friend struct AllocateMemberFunctionAddressor;
633  friend struct ToVectorAddressor;
634  friend struct ToScalarAddressor;
635 
636 
637  std::unique_ptr<PimpleImageBase> m_PimpleImage;
638  };
639 
640 }
641 }
642 
643 #endif
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:76
itk::simple::sitkLinear
@ sitkLinear
N-D linear interpolation.
Definition: sitkInterpolator.h:37
itk::simple::InterpolatorEnum
InterpolatorEnum
Definition: sitkInterpolator.h:28
sitkTemplateFunctions.h
itk::simple::PimpleImageBase
Private implementation idiom image base class.
Definition: sitkPimpleImageBase.h:38
sitkCommon.h
itk::SmartPointer
Definition: sitkImage.h:38
itk::simple::PixelIDValueEnum
PixelIDValueEnum
Enumerated values of pixelIDs.
Definition: sitkPixelIDValues.h:91
itk::simple::Image::m_PimpleImage
std::unique_ptr< PimpleImageBase > m_PimpleImage
Definition: sitkImage.h:637
itk::simple::ImageTypeToPixelIDValue
Definition: sitkPixelIDValues.h:40
sitkDetail.h
SITK_MAX_DIMENSION
#define SITK_MAX_DIMENSION
Definition: sitkConfigure.h:30
itk::simple::Image::Image
Image(itk::SmartPointer< TImageType > image)
Construct an SimpleITK Image from an pointer to an ITK image.
Definition: sitkImage.h:162
SITKCommon_EXPORT
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
itk::point
*par Constraints *The filter requires an image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
itk::DataObject
class ITK_FORWARD_EXPORT DataObject
sitkInterpolator.h
itk::simple::Image::Image
Image(TImageType *image)
Construct an SimpleITK Image from an pointer to an ITK image.
Definition: sitkImage.h:167
sitkPixelIDTokens.h
itk
itk::simple::sitkUnknown
@ sitkUnknown
Definition: sitkPixelIDValues.h:92
itk::simple::PixelIDValueType
int PixelIDValueType
Definition: sitkPixelIDValues.h:30
itk::DataObject