ITK  4.6.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"
30 #include "itkCovariantVector.h"
32 #include "itkDiffusionTensor3D.h"
34 
35 #include "vnl/vnl_vector.h"
36 #include "vcl_compiler.h"
37 
38 #include <fstream>
39 #include <string>
40 
41 namespace itk
42 {
71 class ITKIOImageBase_EXPORT ImageIOBase:public LightProcessObject
72 {
73 public:
75  typedef ImageIOBase Self;
78 
80  itkTypeMacro(ImageIOBase, Superclass);
81 
83  itkSetStringMacro(FileName);
84  itkGetStringMacro(FileName);
86 
90 
96  class UnknownType {};
97 
101  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
102  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
103  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
104 
109  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
110  ULONG, LONG, FLOAT, DOUBLE } IOComponentType;
111 
115  void SetNumberOfDimensions(unsigned int);
116 
117  itkGetConstMacro(NumberOfDimensions, unsigned int);
118 
122  virtual void SetDimensions(unsigned int i, unsigned int dim);
124 
125  virtual itk::SizeValueType GetDimensions(unsigned int i) const
126  { return m_Dimensions[i]; }
127 
130  virtual void SetOrigin(unsigned int i, double origin);
131 
132  virtual double GetOrigin(unsigned int i) const
133  {
134  return m_Origin[i];
135  }
136 
139  virtual void SetSpacing(unsigned int i, double spacing);
140 
141  virtual double GetSpacing(unsigned int i) const
142  {
143  return m_Spacing[i];
144  }
145 
148  virtual void SetDirection(unsigned int i, const std::vector< double > & direction);
149 
150  virtual void SetDirection(unsigned int i, const vnl_vector< double > & direction);
151 
152  virtual std::vector< double > GetDirection(unsigned int i) const
153  {
154  return m_Direction[i];
155  }
156 
159  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
160 
167  itkSetMacro(IORegion, ImageIORegion);
168  itkGetConstReferenceMacro(IORegion, ImageIORegion);
170 
176  itkSetEnumMacro(PixelType, IOPixelType);
177  itkGetEnumMacro(PixelType, IOPixelType);
179 
182  itkSetEnumMacro(ComponentType, IOComponentType);
183  itkGetEnumMacro(ComponentType, IOComponentType);
184 
190  virtual const std::type_info & GetComponentTypeInfo() const;
191 
196  itkSetMacro(NumberOfComponents, unsigned int);
197  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
199 
201  itkSetMacro(UseCompression, bool);
202  itkGetConstMacro(UseCompression, bool);
203  itkBooleanMacro(UseCompression);
205 
207  itkSetMacro(UseStreamedReading, bool);
208  itkGetConstMacro(UseStreamedReading, bool);
209  itkBooleanMacro(UseStreamedReading);
211 
213  itkSetMacro(UseStreamedWriting, bool);
214  itkGetConstMacro(UseStreamedWriting, bool);
215  itkBooleanMacro(UseStreamedWriting);
217 
220  static std::string GetComponentTypeAsString(IOComponentType);
221 
223  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
224 
227  static std::string GetPixelTypeAsString(IOPixelType);
228 
230  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
231 
234  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
235 
238  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
239 
242  itkSetEnumMacro(FileType, FileType);
243  itkGetEnumMacro(FileType, FileType);
245  {
246  this->SetFileType(ASCII);
247  }
249 
251  {
252  this->SetFileType(Binary);
253  }
254 
266  itkSetEnumMacro(ByteOrder, ByteOrder);
267  itkGetEnumMacro(ByteOrder, ByteOrder);
269  {
270  this->SetByteOrder(BigEndian);
271  }
273 
275  {
276  this->SetByteOrder(LittleEndian);
277  }
278 
281  std::string GetFileTypeAsString(FileType) const;
282 
285  std::string GetByteOrderAsString(ByteOrder) const;
286 
289 
293 
300  virtual SizeType GetPixelStride() const;
301 
303  SizeType GetImageSizeInPixels() const;
304 
306  SizeType GetImageSizeInBytes() const;
307 
310  SizeType GetImageSizeInComponents() const;
311 
316  virtual unsigned int GetComponentSize() const;
317 
318  /*-------- This part of the interfaces deals with reading data ----- */
319 
322  virtual bool CanReadFile(const char *) = 0;
323 
328  virtual bool CanStreamRead()
329  {
330  return false;
331  }
332 
335  virtual void ReadImageInformation() = 0;
336 
338  virtual void Read(void *buffer) = 0;
339 
340  /*-------- This part of the interfaces deals with writing data ----- */
341 
344  virtual bool CanWriteFile(const char *) = 0;
345 
352  virtual bool CanStreamWrite()
353  {
354  return false;
355  }
356 
359  virtual void WriteImageInformation() = 0;
360 
364  virtual void Write(const void *buffer) = 0;
365 
366  /* --- Support reading and writing data as a series of files. --- */
367 
373  virtual bool SupportsDimension(unsigned long dim)
374  {
375  return ( dim == 2 );
376  }
377 
389  virtual ImageIORegion
390  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
391 
406  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
407  const ImageIORegion & pasteRegion,
408  const ImageIORegion & largestPossibleRegion);
409 
416  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
417  unsigned int numberOfActualSplits,
418  const ImageIORegion & pasteRegion,
419  const ImageIORegion & largestPossibleRegion);
420 
422  typedef std::vector< std::string > ArrayOfExtensionsType;
423 
428  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
429 
434  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
435 
436  template <typename TPixel>
437  void SetTypeInfo(const TPixel *);
438 
440  template <typename TPixel>
442  {
443  static const IOComponentType CType =
444  UNKNOWNCOMPONENTTYPE;
445  };
446  template <typename TPixel>
447  void SetPixelTypeInfo(const TPixel *)
448  {
449  this->SetNumberOfComponents(1);
450  this->SetPixelType(SCALAR);
451  this->SetComponentType(MapPixelType<TPixel>::CType);
452  }
453  template <typename TPixel>
455  {
456  this->SetNumberOfComponents(3);
457  this->SetPixelType(RGB);
458  this->SetComponentType(MapPixelType<TPixel>::CType);
459  }
460  template <typename TPixel>
462  {
463  this->SetNumberOfComponents(4);
464  this->SetPixelType(RGBA);
465  this->SetComponentType(MapPixelType<TPixel>::CType);
466  }
467  template <typename TPixel, unsigned VLength>
469  {
470  this->SetNumberOfComponents(VLength);
471  this->SetPixelType(VECTOR);
472  this->SetComponentType(MapPixelType<TPixel>::CType);
473  }
474  template <typename TPixel>
476  {
477  this->SetNumberOfComponents(1);
478  this->SetPixelType(VECTOR);
479  this->SetComponentType(MapPixelType<TPixel>::CType);
480  }
481  template <typename TPixel, unsigned VLength>
483  {
484  this->SetNumberOfComponents(VLength);
485  this->SetPixelType(COVARIANTVECTOR);
486  this->SetComponentType(MapPixelType<TPixel>::CType);
487  }
488  template <typename TPixel,unsigned VLength>
490  {
491  this->SetNumberOfComponents(VLength);
492  this->SetPixelType(COVARIANTVECTOR);
493  this->SetComponentType(MapPixelType<TPixel>::CType);
494  }
496 
497  template <typename TPixel, unsigned VLength>
499  {
500  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
501  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
502  this->SetComponentType(MapPixelType<TPixel>::CType);
503  }
504 
505  template <typename TPixel>
507  {
508  this->SetNumberOfComponents(6);
509  this->SetPixelType(DIFFUSIONTENSOR3D);
510  this->SetComponentType(MapPixelType<TPixel>::CType);
511  }
512 
513  template <typename TPixel, unsigned VLength>
515  {
516  this->SetNumberOfComponents(VLength * VLength);
517  this->SetPixelType(MATRIX);
518  this->SetComponentType(MapPixelType<TPixel>::CType);
519  }
520 
521  template <typename TPixel>
522  void SetPixelTypeInfo(const std::complex< TPixel > *)
523  {
524  this->SetNumberOfComponents(2);
525  this->SetPixelType(COMPLEX);
526  this->SetComponentType(MapPixelType<TPixel>::CType);
527  }
528 
529  template <unsigned VLength>
531  {
532  this->SetNumberOfComponents(VLength);
533  this->SetPixelType(ImageIOBase::OFFSET);
534  this->SetComponentType(ImageIOBase::LONG);
535  }
536 
537 protected:
538  ImageIOBase();
539  ~ImageIOBase();
540  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
541 
542  virtual const ImageRegionSplitterBase* GetImageRegionSplitter(void) const;
543 
546 
550 
553 
555 
558 
560  std::string m_FileName;
561 
564  unsigned int m_NumberOfComponents;
565 
567  unsigned int m_NumberOfDimensions;
568 
571 
574 
577 
581 
583  std::vector< SizeValueType > m_Dimensions;
584 
587  std::vector< double > m_Spacing;
588 
590  std::vector< double > m_Origin;
591 
593  std::vector< std::vector< double > > m_Direction;
594 
597  std::vector< SizeType > m_Strides;
598 
600  virtual void Reset(const bool freeDynamic = true);
601 
603  void Resize(const unsigned int numDimensions,
604  const unsigned int *dimensions);
605 
608  virtual unsigned int GetPixelSize() const;
609 
616  void ComputeStrides();
617 
620  SizeType GetComponentStride() const;
621 
624  SizeType GetRowStride() const;
625 
628  SizeType GetSliceStride() const;
629 
641  virtual void OpenFileForReading(std::ifstream & inputStream, const std::string & filename,
642  bool ascii = false);
643 
659  virtual void OpenFileForWriting(std::ofstream & outputStream, const std::string & filename,
660  bool truncate = true, bool ascii = false);
661 
663  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
664  IOComponentType ctype,
665  SizeType numberOfBytesToWrite);
666 
668  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
669  IOComponentType ctype,
670  SizeType numberOfBytesToBeRead);
671 
673  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
674 
676  void AddSupportedReadExtension(const char *extension);
677 
679  void AddSupportedWriteExtension(const char *extension);
680 
683  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
684  const ImageIORegion & pasteRegion) const;
685 
688  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
689  unsigned int numberOfActualSplits,
690  const ImageIORegion & pasteRegion) const;
691 
692 private:
693  ImageIOBase(const Self &); //purposely not implemented
694  void operator=(const Self &); //purposely not implemented
695 
698 };
699 
700 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
701  template <> struct ImageIOBase::MapPixelType<type> \
702  { \
703  static const IOComponentType CType = ctype; \
704  }
705 
706 // the following typemaps are not platform independent
707 #if VCL_CHAR_IS_SIGNED
708 IMAGEIOBASE_TYPEMAP(signed char, CHAR);
709 #endif // VCL_CHAR_IS_SIGNED
710 IMAGEIOBASE_TYPEMAP(char, CHAR);
711 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR);
712 IMAGEIOBASE_TYPEMAP(short, SHORT);
713 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT);
714 IMAGEIOBASE_TYPEMAP(int, INT);
715 IMAGEIOBASE_TYPEMAP(unsigned int, UINT);
716 IMAGEIOBASE_TYPEMAP(long, LONG);
717 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG);
718 IMAGEIOBASE_TYPEMAP(float, FLOAT);
719 IMAGEIOBASE_TYPEMAP(double, DOUBLE);
720 #undef IMAGIOBASE_TYPEMAP
721 
722 
723 } // end namespace itk
724 
725 #endif // __itkImageIOBase_h
void SetPixelTypeInfo(const Offset< VLength > *)
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:46
unsigned int m_NumberOfComponents
Light weight base class for most itk classes.
virtual itk::SizeValueType GetDimensions(unsigned int i) const
ImageIOBase Self
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:55
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.
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.
ByteOrder m_ByteOrder
signed long IndexValueType
Definition: itkIntTypes.h:150
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
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
IOPixelType m_PixelType
ArrayOfExtensionsType m_SupportedReadExtensions
void SetFileTypeToBinary()
unsigned int m_NumberOfDimensions
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
std::vector< std::vector< double > > m_Direction
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
IOComponentType m_ComponentType
void SetPixelTypeInfo(const CovariantVector< TPixel, VLength > *)
::itk::OffsetValueType BufferSizeType
void SetPixelTypeInfo(const RGBPixel< TPixel > *)
::itk::IndexValueType IndexValueType
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
IMAGEIOBASE_TYPEMAP(char, CHAR)
SmartPointer< Self > Pointer
void SetByteOrderToBigEndian()
Base class for most ITK classes.
Definition: itkObject.h:57
Represent a diffusion tensor as used in DTI images.
A templated class holding a n-Dimensional covariant vector.
void SetPixelTypeInfo(const RGBAPixel< TPixel > *)
std::vector< SizeValueType > m_Dimensions
void SetPixelTypeInfo(const FixedArray< TPixel, VLength > *)
virtual bool CanStreamWrite()