ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageIOBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 itkImageIOBase_h
19 #define itkImageIOBase_h
20 #include "ITKIOImageBaseExport.h"
21 
22 #include "itkIOConfigure.h"
23 
24 #include "itkLightProcessObject.h"
25 #include "itkIndent.h"
26 #include "itkImageIORegion.h"
27 #include "itkRGBPixel.h"
28 #include "itkRGBAPixel.h"
29 #include "itkCovariantVector.h"
31 #include "itkDiffusionTensor3D.h"
33 
34 #include "vnl/vnl_vector.h"
35 #include "vcl_compiler.h"
36 
37 #include <fstream>
38 #include <string>
39 
40 namespace itk
41 {
42 // Forward reference for VariableLengthVector
43 template <typename TValue> class VariableLengthVector;
44 
73 class ITKIOImageBase_EXPORT ImageIOBase:public LightProcessObject
74 {
75 public:
76  ITK_DISALLOW_COPY_AND_ASSIGN(ImageIOBase);
77 
79  using Self = ImageIOBase;
83 
85  itkTypeMacro(ImageIOBase, Superclass);
86 
88  itkSetStringMacro(FileName);
89  itkGetStringMacro(FileName);
91 
95 
101  class UnknownType {};
102 
106  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
107  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
108  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
109 
114  // NOTE unsigned char, char, and signed char are 3 distinct types in C++
115  // the enum value UCHAR represents 'unsigned char'
116  // the enum value CHAR represents 'signed char'
117  // and the 'char' type maps to one of UCHAR or CHAR based on the platforms 'signededness'
118  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
119  ULONG, LONG, ULONGLONG, LONGLONG, FLOAT, DOUBLE } IOComponentType;
120 
124  void SetNumberOfDimensions(unsigned int);
125 
126  itkGetConstMacro(NumberOfDimensions, unsigned int);
127 
131  virtual void SetDimensions(unsigned int i, SizeValueType dim);
133 
134  virtual SizeValueType GetDimensions(unsigned int i) const
135  { return m_Dimensions[i]; }
136 
139  virtual void SetOrigin(unsigned int i, double origin);
140 
141  virtual double GetOrigin(unsigned int i) const
142  {
143  return m_Origin[i];
144  }
145 
148  virtual void SetSpacing(unsigned int i, double spacing);
149 
150  virtual double GetSpacing(unsigned int i) const
151  {
152  return m_Spacing[i];
153  }
154 
157  virtual void SetDirection(unsigned int i, const std::vector< double > & direction);
158 
159  virtual void SetDirection(unsigned int i, const vnl_vector< double > & direction);
160 
161  virtual std::vector< double > GetDirection(unsigned int i) const
162  {
163  return m_Direction[i];
164  }
165 
168  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
169 
176  itkSetMacro(IORegion, ImageIORegion);
177  itkGetConstReferenceMacro(IORegion, ImageIORegion);
179 
185  itkSetEnumMacro(PixelType, IOPixelType);
186  itkGetEnumMacro(PixelType, IOPixelType);
188 
191  itkSetEnumMacro(ComponentType, IOComponentType);
192  itkGetEnumMacro(ComponentType, IOComponentType);
193 
199  virtual const std::type_info & GetComponentTypeInfo() const;
200 
205  itkSetMacro(NumberOfComponents, unsigned int);
206  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
208 
210  itkSetMacro(UseCompression, bool);
211  itkGetConstMacro(UseCompression, bool);
212  itkBooleanMacro(UseCompression);
214 
216  itkSetMacro(UseStreamedReading, bool);
217  itkGetConstMacro(UseStreamedReading, bool);
218  itkBooleanMacro(UseStreamedReading);
220 
222  itkSetMacro(UseStreamedWriting, bool);
223  itkGetConstMacro(UseStreamedWriting, bool);
224  itkBooleanMacro(UseStreamedWriting);
226 
231  itkSetMacro(ExpandRGBPalette, bool);
232  itkGetConstMacro(ExpandRGBPalette, bool);
233  itkBooleanMacro(ExpandRGBPalette);
235 
240  itkGetConstMacro(IsReadAsScalarPlusPalette, bool);
241 
244  static std::string GetComponentTypeAsString(IOComponentType);
245 
247  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
248 
251  static std::string GetPixelTypeAsString(IOPixelType);
252 
254  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
255 
258  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
259 
262  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
263 
266  itkSetEnumMacro(FileType, FileType);
267  itkGetEnumMacro(FileType, FileType);
269  {
270  this->SetFileType(ASCII);
271  }
273 
275  {
276  this->SetFileType(Binary);
277  }
278 
290  itkSetEnumMacro(ByteOrder, ByteOrder);
291  itkGetEnumMacro(ByteOrder, ByteOrder);
293  {
294  this->SetByteOrder(BigEndian);
295  }
297 
299  {
300  this->SetByteOrder(LittleEndian);
301  }
302 
305  std::string GetFileTypeAsString(FileType) const;
306 
309  std::string GetByteOrderAsString(ByteOrder) const;
310 
313 
317 
324  virtual SizeType GetPixelStride() const;
325 
327  SizeType GetImageSizeInPixels() const;
328 
330  SizeType GetImageSizeInBytes() const;
331 
334  SizeType GetImageSizeInComponents() const;
335 
340  virtual unsigned int GetComponentSize() const;
341 
342  /*-------- This part of the interfaces deals with reading data ----- */
343 
346  virtual bool CanReadFile(const char *) = 0;
347 
352  virtual bool CanStreamRead()
353  {
354  return false;
355  }
356 
359  virtual void ReadImageInformation() = 0;
360 
362  virtual void Read(void *buffer) = 0;
363 
364  /*-------- This part of the interfaces deals with writing data ----- */
365 
368  virtual bool CanWriteFile(const char *) = 0;
369 
376  virtual bool CanStreamWrite()
377  {
378  return false;
379  }
380 
383  virtual void WriteImageInformation() = 0;
384 
388  virtual void Write(const void *buffer) = 0;
389 
390  /* --- Support reading and writing data as a series of files. --- */
391 
397  virtual bool SupportsDimension(unsigned long dim)
398  {
399  return ( dim == 2 );
400  }
401 
413  virtual ImageIORegion
414  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
415 
430  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
431  const ImageIORegion & pasteRegion,
432  const ImageIORegion & largestPossibleRegion);
433 
440  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
441  unsigned int numberOfActualSplits,
442  const ImageIORegion & pasteRegion,
443  const ImageIORegion & largestPossibleRegion);
444 
446  using ArrayOfExtensionsType = std::vector< std::string >;
447 
452  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
453 
458  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
459 
460  template <typename TPixel>
461  void SetTypeInfo(const TPixel *);
462 
464  template <typename TPixel>
466  {
467  static constexpr IOComponentType CType =
468  UNKNOWNCOMPONENTTYPE;
469  };
470  template <typename TPixel>
471  void SetPixelTypeInfo(const TPixel *)
472  {
473  this->SetNumberOfComponents(1);
474  this->SetPixelType(SCALAR);
475  this->SetComponentType(MapPixelType<TPixel>::CType);
476  }
477  template <typename TPixel>
479  {
480  this->SetNumberOfComponents(3);
481  this->SetPixelType(RGB);
482  this->SetComponentType(MapPixelType<TPixel>::CType);
483  }
484  template <typename TPixel>
486  {
487  this->SetNumberOfComponents(4);
488  this->SetPixelType(RGBA);
489  this->SetComponentType(MapPixelType<TPixel>::CType);
490  }
491  template <typename TPixel, unsigned VLength>
493  {
494  this->SetNumberOfComponents(VLength);
495  this->SetPixelType(VECTOR);
496  this->SetComponentType(MapPixelType<TPixel>::CType);
497  }
498  template <typename TPixel>
500  {
501  this->SetNumberOfComponents(1);
502  this->SetPixelType(VECTOR);
503  this->SetComponentType(MapPixelType<TPixel>::CType);
504  }
505  template <typename TPixel, unsigned VLength>
507  {
508  this->SetNumberOfComponents(VLength);
509  this->SetPixelType(COVARIANTVECTOR);
510  this->SetComponentType(MapPixelType<TPixel>::CType);
511  }
512  template <typename TPixel,unsigned VLength>
514  {
515  this->SetNumberOfComponents(VLength);
516  this->SetPixelType(COVARIANTVECTOR);
517  this->SetComponentType(MapPixelType<TPixel>::CType);
518  }
520 
521  template <typename TPixel, unsigned VLength>
523  {
524  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
525  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
526  this->SetComponentType(MapPixelType<TPixel>::CType);
527  }
528 
529  template <typename TPixel>
531  {
532  this->SetNumberOfComponents(6);
533  this->SetPixelType(DIFFUSIONTENSOR3D);
534  this->SetComponentType(MapPixelType<TPixel>::CType);
535  }
536 
537  template <typename TPixel, unsigned VLength>
539  {
540  this->SetNumberOfComponents(VLength * VLength);
541  this->SetPixelType(MATRIX);
542  this->SetComponentType(MapPixelType<TPixel>::CType);
543  }
544 
545  template <typename TPixel>
546  void SetPixelTypeInfo(const std::complex< TPixel > *)
547  {
548  this->SetNumberOfComponents(2);
549  this->SetPixelType(COMPLEX);
550  this->SetComponentType(MapPixelType<TPixel>::CType);
551  }
552 
553  template <unsigned VLength>
555  {
556  this->SetNumberOfComponents(VLength);
557  this->SetPixelType(ImageIOBase::OFFSET);
558  this->SetComponentType(ImageIOBase::LONG);
559  }
560 
561 protected:
562  ImageIOBase();
563  ~ImageIOBase() override;
564  void PrintSelf(std::ostream & os, Indent indent) const override;
565 
566  virtual const ImageRegionSplitterBase* GetImageRegionSplitter() const;
567 
572  virtual bool HasSupportedReadExtension( const char * fileName, bool ignoreCase = true );
573  virtual bool HasSupportedWriteExtension( const char * fileName, bool ignoreCase = true );
575 
577  IOPixelType m_PixelType{SCALAR};
578 
581  IOComponentType m_ComponentType{UNKNOWNCOMPONENTTYPE};
582 
584  ByteOrder m_ByteOrder{OrderNotApplicable};
585 
586  FileType m_FileType{TypeNotApplicable};
587 
590 
592  std::string m_FileName;
593 
596  unsigned int m_NumberOfComponents;
597 
599  unsigned int m_NumberOfDimensions{0};
600 
603 
606 
609 
612 
616 
620 
622  std::vector< SizeValueType > m_Dimensions;
623 
626  std::vector< double > m_Spacing;
627 
629  std::vector< double > m_Origin;
630 
632  std::vector< std::vector< double > > m_Direction;
633 
636  std::vector< SizeType > m_Strides;
637 
639  virtual void Reset(const bool freeDynamic = true);
640 
642  void Resize(const unsigned int numDimensions,
643  const unsigned int *dimensions);
644 
647  virtual unsigned int GetPixelSize() const;
648 
655  void ComputeStrides();
656 
659  SizeType GetComponentStride() const;
660 
663  SizeType GetRowStride() const;
664 
667  SizeType GetSliceStride() const;
668 
680  virtual void OpenFileForReading(std::ifstream & inputStream, const std::string & filename,
681  bool ascii = false);
682 
698  virtual void OpenFileForWriting(std::ofstream & outputStream, const std::string & filename,
699  bool truncate = true, bool ascii = false);
700 
702  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
703  IOComponentType ctype,
704  SizeType numberOfBytesToWrite);
705 
707  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
708  IOComponentType ctype,
709  SizeType numberOfBytesToBeRead);
710 
712  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
713 
715  void AddSupportedReadExtension(const char *extension);
716 
718  void AddSupportedWriteExtension(const char *extension);
719 
722  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
723  const ImageIORegion & pasteRegion) const;
724 
727  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
728  unsigned int numberOfActualSplits,
729  const ImageIORegion & pasteRegion) const;
730 
731 private:
732 
733  bool HasSupportedExtension( const char * , const ArrayOfExtensionsType &, bool tolower = true );
734 
737 };
738 
739 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
740  template <> struct ImageIOBase::MapPixelType<type> \
741  { \
742  static constexpr IOComponentType CType = ctype; \
743  }
744 
745 // the following typemaps are not platform independent
746 IMAGEIOBASE_TYPEMAP(signed char, IOComponentType::CHAR);
747 IMAGEIOBASE_TYPEMAP(char, std::numeric_limits<char>::is_signed ? IOComponentType::CHAR : IOComponentType::UCHAR);
748 IMAGEIOBASE_TYPEMAP(unsigned char, IOComponentType::UCHAR);
749 IMAGEIOBASE_TYPEMAP(short, IOComponentType::SHORT);
750 IMAGEIOBASE_TYPEMAP(unsigned short, IOComponentType::USHORT);
751 IMAGEIOBASE_TYPEMAP(int, IOComponentType::INT);
752 IMAGEIOBASE_TYPEMAP(unsigned int, IOComponentType::UINT);
753 IMAGEIOBASE_TYPEMAP(long, IOComponentType::LONG);
754 IMAGEIOBASE_TYPEMAP(unsigned long, IOComponentType::ULONG);
755 IMAGEIOBASE_TYPEMAP(long long, IOComponentType::LONGLONG);
756 IMAGEIOBASE_TYPEMAP(unsigned long long, IOComponentType::ULONGLONG);
757 IMAGEIOBASE_TYPEMAP(float, IOComponentType::FLOAT);
758 IMAGEIOBASE_TYPEMAP(double, IOComponentType::DOUBLE);
759 #undef IMAGIOBASE_TYPEMAP
760 
761 } // end namespace itk
762 
763 #endif // itkImageIOBase_h
void SetPixelTypeInfo(const Offset< VLength > *)
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:49
unsigned int m_NumberOfComponents
Light weight base class for most itk classes.
virtual bool SupportsDimension(unsigned long dim)
void SetPixelTypeInfo(const DiffusionTensor3D< TPixel > *)
An ImageIORegion represents a structured region of data.
virtual SizeValueType GetDimensions(unsigned int i) const
void SetByteOrderToLittleEndian()
Abstract superclass defines image IO interface.
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:59
Represent a symmetric tensor of second rank.
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void SetPixelTypeInfo(const Matrix< TPixel, VLength, VLength > *)
ImageIORegion m_IORegion
std::vector< double > m_Origin
void SetPixelTypeInfo(const VariableLengthVector< TPixel > *)
std::vector< double > m_Spacing
::itk::OffsetValueType BufferSizeType
virtual double GetSpacing(unsigned int i) const
virtual std::vector< double > GetDirection(unsigned int i) const
IMAGEIOBASE_TYPEMAP(signed char, IOComponentType::CHAR)
ArrayOfExtensionsType m_SupportedReadExtensions
void SetFileTypeToBinary()
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
void SetPixelTypeInfo(const Vector< TPixel, VLength > *)
::itk::IndexValueType IndexValueType
void SetPixelTypeInfo(const TPixel *)
::itk::SizeValueType SizeValueType
::itk::intmax_t SizeType
virtual double GetOrigin(unsigned int i) const
Represents an array whose length can be defined at run-time.
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
std::vector< std::vector< double > > m_Direction
signed long IndexValueType
Definition: itkIntTypes.h:90
Divide an image region into several pieces.
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image...
Definition: itkOffset.h:67
void SetPixelTypeInfo(const SymmetricSecondRankTensor< TPixel, VLength > *)
void SetPixelTypeInfo(const std::complex< TPixel > *)
virtual bool CanStreamRead()
::intmax_t intmax_t
Definition: itkIntTypes.h:55
void SetPixelTypeInfo(const CovariantVector< TPixel, VLength > *)
void SetPixelTypeInfo(const RGBPixel< TPixel > *)
std::vector< SizeType > m_Strides
ArrayOfExtensionsType m_SupportedWriteExtensions
LightProcessObject is the base class for all process objects (source, filters, mappers) in the Insigh...
std::string m_FileName
Control indentation during Print() invocation.
Definition: itkIndent.h:49
std::vector< std::string > ArrayOfExtensionsType
void SetByteOrderToBigEndian()
Base class for most ITK classes.
Definition: itkObject.h:60
Represent a diffusion tensor as used in DTI images.
A templated class holding a n-Dimensional covariant vector.
signed long OffsetValueType
Definition: itkIntTypes.h:94
bool m_IsReadAsScalarPlusPalette
void SetPixelTypeInfo(const RGBAPixel< TPixel > *)
std::vector< SizeValueType > m_Dimensions
void SetPixelTypeInfo(const FixedArray< TPixel, VLength > *)
virtual bool CanStreamWrite()