ITK  4.13.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:
77  typedef ImageIOBase Self;
80 
82  itkTypeMacro(ImageIOBase, Superclass);
83 
85  itkSetStringMacro(FileName);
86  itkGetStringMacro(FileName);
88 
92 
98  class UnknownType {};
99 
103  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
104  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
105  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
106 
111  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
112  ULONG, LONG, ULONGLONG, LONGLONG, FLOAT, DOUBLE } IOComponentType;
113 
117  void SetNumberOfDimensions(unsigned int);
118 
119  itkGetConstMacro(NumberOfDimensions, unsigned int);
120 
124  virtual void SetDimensions(unsigned int i, SizeValueType dim);
126 
127  virtual SizeValueType GetDimensions(unsigned int i) const
128  { return m_Dimensions[i]; }
129 
132  virtual void SetOrigin(unsigned int i, double origin);
133 
134  virtual double GetOrigin(unsigned int i) const
135  {
136  return m_Origin[i];
137  }
138 
141  virtual void SetSpacing(unsigned int i, double spacing);
142 
143  virtual double GetSpacing(unsigned int i) const
144  {
145  return m_Spacing[i];
146  }
147 
150  virtual void SetDirection(unsigned int i, const std::vector< double > & direction);
151 
152  virtual void SetDirection(unsigned int i, const vnl_vector< double > & direction);
153 
154  virtual std::vector< double > GetDirection(unsigned int i) const
155  {
156  return m_Direction[i];
157  }
158 
161  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
162 
169  itkSetMacro(IORegion, ImageIORegion);
170  itkGetConstReferenceMacro(IORegion, ImageIORegion);
172 
178  itkSetEnumMacro(PixelType, IOPixelType);
179  itkGetEnumMacro(PixelType, IOPixelType);
181 
184  itkSetEnumMacro(ComponentType, IOComponentType);
185  itkGetEnumMacro(ComponentType, IOComponentType);
186 
192  virtual const std::type_info & GetComponentTypeInfo() const;
193 
198  itkSetMacro(NumberOfComponents, unsigned int);
199  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
201 
203  itkSetMacro(UseCompression, bool);
204  itkGetConstMacro(UseCompression, bool);
205  itkBooleanMacro(UseCompression);
207 
209  itkSetMacro(UseStreamedReading, bool);
210  itkGetConstMacro(UseStreamedReading, bool);
211  itkBooleanMacro(UseStreamedReading);
213 
215  itkSetMacro(UseStreamedWriting, bool);
216  itkGetConstMacro(UseStreamedWriting, bool);
217  itkBooleanMacro(UseStreamedWriting);
219 
224  itkSetMacro(ExpandRGBPalette, bool);
225  itkGetConstMacro(ExpandRGBPalette, bool);
226  itkBooleanMacro(ExpandRGBPalette);
228 
233  itkGetConstMacro(IsReadAsScalarPlusPalette, bool);
234 
237  static std::string GetComponentTypeAsString(IOComponentType);
238 
240  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
241 
244  static std::string GetPixelTypeAsString(IOPixelType);
245 
247  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
248 
251  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
252 
255  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
256 
259  itkSetEnumMacro(FileType, FileType);
260  itkGetEnumMacro(FileType, FileType);
262  {
263  this->SetFileType(ASCII);
264  }
266 
268  {
269  this->SetFileType(Binary);
270  }
271 
283  itkSetEnumMacro(ByteOrder, ByteOrder);
284  itkGetEnumMacro(ByteOrder, ByteOrder);
286  {
287  this->SetByteOrder(BigEndian);
288  }
290 
292  {
293  this->SetByteOrder(LittleEndian);
294  }
295 
298  std::string GetFileTypeAsString(FileType) const;
299 
302  std::string GetByteOrderAsString(ByteOrder) const;
303 
306 
310 
317  virtual SizeType GetPixelStride() const;
318 
320  SizeType GetImageSizeInPixels() const;
321 
323  SizeType GetImageSizeInBytes() const;
324 
327  SizeType GetImageSizeInComponents() const;
328 
333  virtual unsigned int GetComponentSize() const;
334 
335  /*-------- This part of the interfaces deals with reading data ----- */
336 
339  virtual bool CanReadFile(const char *) = 0;
340 
345  virtual bool CanStreamRead()
346  {
347  return false;
348  }
349 
352  virtual void ReadImageInformation() = 0;
353 
355  virtual void Read(void *buffer) = 0;
356 
357  /*-------- This part of the interfaces deals with writing data ----- */
358 
361  virtual bool CanWriteFile(const char *) = 0;
362 
369  virtual bool CanStreamWrite()
370  {
371  return false;
372  }
373 
376  virtual void WriteImageInformation() = 0;
377 
381  virtual void Write(const void *buffer) = 0;
382 
383  /* --- Support reading and writing data as a series of files. --- */
384 
390  virtual bool SupportsDimension(unsigned long dim)
391  {
392  return ( dim == 2 );
393  }
394 
406  virtual ImageIORegion
407  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
408 
423  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
424  const ImageIORegion & pasteRegion,
425  const ImageIORegion & largestPossibleRegion);
426 
433  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
434  unsigned int numberOfActualSplits,
435  const ImageIORegion & pasteRegion,
436  const ImageIORegion & largestPossibleRegion);
437 
439  typedef std::vector< std::string > ArrayOfExtensionsType;
440 
445  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
446 
451  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
452 
453  template <typename TPixel>
454  void SetTypeInfo(const TPixel *);
455 
457  template <typename TPixel>
459  {
460  static ITK_CONSTEXPR_VAR IOComponentType CType =
461  UNKNOWNCOMPONENTTYPE;
462  };
463  template <typename TPixel>
464  void SetPixelTypeInfo(const TPixel *)
465  {
466  this->SetNumberOfComponents(1);
467  this->SetPixelType(SCALAR);
468  this->SetComponentType(MapPixelType<TPixel>::CType);
469  }
470  template <typename TPixel>
472  {
473  this->SetNumberOfComponents(3);
474  this->SetPixelType(RGB);
475  this->SetComponentType(MapPixelType<TPixel>::CType);
476  }
477  template <typename TPixel>
479  {
480  this->SetNumberOfComponents(4);
481  this->SetPixelType(RGBA);
482  this->SetComponentType(MapPixelType<TPixel>::CType);
483  }
484  template <typename TPixel, unsigned VLength>
486  {
487  this->SetNumberOfComponents(VLength);
488  this->SetPixelType(VECTOR);
489  this->SetComponentType(MapPixelType<TPixel>::CType);
490  }
491  template <typename TPixel>
493  {
494  this->SetNumberOfComponents(1);
495  this->SetPixelType(VECTOR);
496  this->SetComponentType(MapPixelType<TPixel>::CType);
497  }
498  template <typename TPixel, unsigned VLength>
500  {
501  this->SetNumberOfComponents(VLength);
502  this->SetPixelType(COVARIANTVECTOR);
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  }
513 
514  template <typename TPixel, unsigned VLength>
516  {
517  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
518  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
519  this->SetComponentType(MapPixelType<TPixel>::CType);
520  }
521 
522  template <typename TPixel>
524  {
525  this->SetNumberOfComponents(6);
526  this->SetPixelType(DIFFUSIONTENSOR3D);
527  this->SetComponentType(MapPixelType<TPixel>::CType);
528  }
529 
530  template <typename TPixel, unsigned VLength>
532  {
533  this->SetNumberOfComponents(VLength * VLength);
534  this->SetPixelType(MATRIX);
535  this->SetComponentType(MapPixelType<TPixel>::CType);
536  }
537 
538  template <typename TPixel>
539  void SetPixelTypeInfo(const std::complex< TPixel > *)
540  {
541  this->SetNumberOfComponents(2);
542  this->SetPixelType(COMPLEX);
543  this->SetComponentType(MapPixelType<TPixel>::CType);
544  }
545 
546  template <unsigned VLength>
548  {
549  this->SetNumberOfComponents(VLength);
550  this->SetPixelType(ImageIOBase::OFFSET);
551  this->SetComponentType(ImageIOBase::LONG);
552  }
553 
554 protected:
555  ImageIOBase();
556  ~ImageIOBase() ITK_OVERRIDE;
557  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
558 
559  virtual const ImageRegionSplitterBase* GetImageRegionSplitter() const;
560 
562  IOPixelType m_PixelType;
563 
566  IOComponentType m_ComponentType;
567 
569  ByteOrder m_ByteOrder;
570 
571  FileType m_FileType;
572 
574  bool m_Initialized;
575 
577  std::string m_FileName;
578 
581  unsigned int m_NumberOfComponents;
582 
584  unsigned int m_NumberOfDimensions;
585 
587  bool m_UseCompression;
588 
590  bool m_UseStreamedReading;
591 
593  bool m_UseStreamedWriting;
594 
596  bool m_ExpandRGBPalette;
597 
600  bool m_IsReadAsScalarPlusPalette;
601 
604  ImageIORegion m_IORegion;
605 
607  std::vector< SizeValueType > m_Dimensions;
608 
611  std::vector< double > m_Spacing;
612 
614  std::vector< double > m_Origin;
615 
617  std::vector< std::vector< double > > m_Direction;
618 
621  std::vector< SizeType > m_Strides;
622 
624  virtual void Reset(const bool freeDynamic = true);
625 
627  void Resize(const unsigned int numDimensions,
628  const unsigned int *dimensions);
629 
632  virtual unsigned int GetPixelSize() const;
633 
640  void ComputeStrides();
641 
644  SizeType GetComponentStride() const;
645 
648  SizeType GetRowStride() const;
649 
652  SizeType GetSliceStride() const;
653 
665  virtual void OpenFileForReading(std::ifstream & inputStream, const std::string & filename,
666  bool ascii = false);
667 
683  virtual void OpenFileForWriting(std::ofstream & outputStream, const std::string & filename,
684  bool truncate = true, bool ascii = false);
685 
687  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
688  IOComponentType ctype,
689  SizeType numberOfBytesToWrite);
690 
692  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
693  IOComponentType ctype,
694  SizeType numberOfBytesToBeRead);
695 
697  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
698 
700  void AddSupportedReadExtension(const char *extension);
701 
703  void AddSupportedWriteExtension(const char *extension);
704 
707  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
708  const ImageIORegion & pasteRegion) const;
709 
712  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
713  unsigned int numberOfActualSplits,
714  const ImageIORegion & pasteRegion) const;
715 
716 private:
717  ITK_DISALLOW_COPY_AND_ASSIGN(ImageIOBase);
718 
719  ArrayOfExtensionsType m_SupportedReadExtensions;
720  ArrayOfExtensionsType m_SupportedWriteExtensions;
721 };
722 
723 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
724  template <> struct ImageIOBase::MapPixelType<type> \
725  { \
726  static ITK_CONSTEXPR_VAR IOComponentType CType = ctype; \
727  }
728 
729 // the following typemaps are not platform independent
730 #if VCL_CHAR_IS_SIGNED
731 IMAGEIOBASE_TYPEMAP(signed char, CHAR);
732 #endif // VCL_CHAR_IS_SIGNED
733 IMAGEIOBASE_TYPEMAP(char, CHAR);
734 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR);
735 IMAGEIOBASE_TYPEMAP(short, SHORT);
736 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT);
737 IMAGEIOBASE_TYPEMAP(int, INT);
738 IMAGEIOBASE_TYPEMAP(unsigned int, UINT);
739 IMAGEIOBASE_TYPEMAP(long, LONG);
740 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG);
741 IMAGEIOBASE_TYPEMAP(long long, LONGLONG);
742 IMAGEIOBASE_TYPEMAP(unsigned long long, ULONGLONG);
743 IMAGEIOBASE_TYPEMAP(float, FLOAT);
744 IMAGEIOBASE_TYPEMAP(double, DOUBLE);
745 #undef IMAGIOBASE_TYPEMAP
746 
747 
748 } // end namespace itk
749 
750 #endif // itkImageIOBase_h
void SetPixelTypeInfo(const Offset< VLength > *)
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
ImageIOBase Self
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:56
virtual bool SupportsDimension(unsigned long dim)
int64_t intmax_t
Definition: itkIntTypes.h:114
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.
signed long OffsetValueType
Definition: itkIntTypes.h:154
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:59
Represent a symmetric tensor of second rank.
signed long IndexValueType
Definition: itkIntTypes.h:150
void SetPixelTypeInfo(const Matrix< TPixel, VLength, VLength > *)
void SetPixelTypeInfo(const VariableLengthVector< TPixel > *)
LightProcessObject Superclass
virtual double GetSpacing(unsigned int i) const
std::vector< std::string > ArrayOfExtensionsType
unsigned long SizeValueType
Definition: itkIntTypes.h:143
virtual std::vector< double > GetDirection(unsigned int i) const
void SetFileTypeToBinary()
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
void SetPixelTypeInfo(const Vector< TPixel, VLength > *)
void SetPixelTypeInfo(const TPixel *)
::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
Divide an image region into several pieces.
void SetPixelTypeInfo(const SymmetricSecondRankTensor< TPixel, VLength > *)
void SetPixelTypeInfo(const std::complex< TPixel > *)
virtual bool CanStreamRead()
::itk::SizeValueType SizeValueType
void SetPixelTypeInfo(const CovariantVector< TPixel, VLength > *)
::itk::OffsetValueType BufferSizeType
void SetPixelTypeInfo(const RGBPixel< TPixel > *)
::itk::IndexValueType IndexValueType
LightProcessObject is the base class for all process objects (source, filters, mappers) in the Insigh...
Control indentation during Print() invocation.
Definition: itkIndent.h:49
IMAGEIOBASE_TYPEMAP(char, CHAR)
SmartPointer< Self > Pointer
void SetByteOrderToBigEndian()
Base class for most ITK classes.
Definition: itkObject.h:59
Represent a diffusion tensor as used in DTI images.
A templated class holding a n-Dimensional covariant vector.
void SetPixelTypeInfo(const RGBAPixel< TPixel > *)
void SetPixelTypeInfo(const FixedArray< TPixel, VLength > *)
virtual bool CanStreamWrite()