ITK  4.5.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 
37 #include <string>
38 
39 namespace itk
40 {
69 class ITKIOImageBase_EXPORT ImageIOBase:public LightProcessObject
70 {
71 public:
73  typedef ImageIOBase Self;
76 
78  itkTypeMacro(ImageIOBase, Superclass);
79 
81  itkSetStringMacro(FileName);
82  itkGetStringMacro(FileName);
84 
88 
94  class UnknownType {};
95 
99  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
100  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
101  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
102 
107  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
108  ULONG, LONG, FLOAT, DOUBLE } IOComponentType;
109 
113  void SetNumberOfDimensions(unsigned int);
114 
115  itkGetConstMacro(NumberOfDimensions, unsigned int);
116 
120  virtual void SetDimensions(unsigned int i, unsigned int dim);
122 
123  virtual itk::SizeValueType GetDimensions(unsigned int i) const
124  { return m_Dimensions[i]; }
125 
128  virtual void SetOrigin(unsigned int i, double origin);
129 
130  virtual double GetOrigin(unsigned int i) const
131  {
132  return m_Origin[i];
133  }
134 
137  virtual void SetSpacing(unsigned int i, double spacing);
138 
139  virtual double GetSpacing(unsigned int i) const
140  {
141  return m_Spacing[i];
142  }
143 
146  virtual void SetDirection(unsigned int i, const std::vector< double > & direction);
147 
148  virtual void SetDirection(unsigned int i, const vnl_vector< double > & direction);
149 
150  virtual std::vector< double > GetDirection(unsigned int i) const
151  {
152  return m_Direction[i];
153  }
154 
157  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
158 
165  itkSetMacro(IORegion, ImageIORegion);
166  itkGetConstReferenceMacro(IORegion, ImageIORegion);
168 
174  itkSetEnumMacro(PixelType, IOPixelType);
175  itkGetEnumMacro(PixelType, IOPixelType);
177 
180  itkSetEnumMacro(ComponentType, IOComponentType);
181  itkGetEnumMacro(ComponentType, IOComponentType);
182 
188  virtual const std::type_info & GetComponentTypeInfo() const;
189 
194  itkSetMacro(NumberOfComponents, unsigned int);
195  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
197 
199  itkSetMacro(UseCompression, bool);
200  itkGetConstMacro(UseCompression, bool);
201  itkBooleanMacro(UseCompression);
203 
205  itkSetMacro(UseStreamedReading, bool);
206  itkGetConstMacro(UseStreamedReading, bool);
207  itkBooleanMacro(UseStreamedReading);
209 
211  itkSetMacro(UseStreamedWriting, bool);
212  itkGetConstMacro(UseStreamedWriting, bool);
213  itkBooleanMacro(UseStreamedWriting);
215 
218  static std::string GetComponentTypeAsString(IOComponentType);
219 
221  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
222 
225  static std::string GetPixelTypeAsString(IOPixelType);
226 
228  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
229 
232  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
233 
236  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
237 
240  itkSetEnumMacro(FileType, FileType);
241  itkGetEnumMacro(FileType, FileType);
243  {
244  this->SetFileType(ASCII);
245  }
247 
249  {
250  this->SetFileType(Binary);
251  }
252 
264  itkSetEnumMacro(ByteOrder, ByteOrder);
265  itkGetEnumMacro(ByteOrder, ByteOrder);
267  {
268  this->SetByteOrder(BigEndian);
269  }
271 
273  {
274  this->SetByteOrder(LittleEndian);
275  }
276 
279  std::string GetFileTypeAsString(FileType) const;
280 
283  std::string GetByteOrderAsString(ByteOrder) const;
284 
287 
291 
298  virtual SizeType GetPixelStride() const;
299 
301  SizeType GetImageSizeInPixels() const;
302 
304  SizeType GetImageSizeInBytes() const;
305 
308  SizeType GetImageSizeInComponents() const;
309 
314  virtual unsigned int GetComponentSize() const;
315 
316  /*-------- This part of the interfaces deals with reading data ----- */
317 
320  virtual bool CanReadFile(const char *) = 0;
321 
326  virtual bool CanStreamRead()
327  {
328  return false;
329  }
330 
333  virtual void ReadImageInformation() = 0;
334 
336  virtual void Read(void *buffer) = 0;
337 
338  /*-------- This part of the interfaces deals with writing data ----- */
339 
342  virtual bool CanWriteFile(const char *) = 0;
343 
350  virtual bool CanStreamWrite()
351  {
352  return false;
353  }
354 
357  virtual void WriteImageInformation() = 0;
358 
362  virtual void Write(const void *buffer) = 0;
363 
364  /* --- Support reading and writing data as a series of files. --- */
365 
371  virtual bool SupportsDimension(unsigned long dim)
372  {
373  return ( dim == 2 );
374  }
375 
387  virtual ImageIORegion
388  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
389 
404  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
405  const ImageIORegion & pasteRegion,
406  const ImageIORegion & largestPossibleRegion);
407 
414  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
415  unsigned int numberOfActualSplits,
416  const ImageIORegion & pasteRegion,
417  const ImageIORegion & largestPossibleRegion);
418 
420  typedef std::vector< std::string > ArrayOfExtensionsType;
421 
426  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
427 
432  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
433 
434  template <typename TPixel>
435  void SetTypeInfo(const TPixel *);
436 
438  template <typename TPixel>
440  {
441  static const IOComponentType CType =
442  UNKNOWNCOMPONENTTYPE;
443  };
444  template <typename TPixel>
445  void SetPixelTypeInfo(const TPixel *)
446  {
447  this->SetNumberOfComponents(1);
448  this->SetPixelType(SCALAR);
449  this->SetComponentType(MapPixelType<TPixel>::CType);
450  }
451  template <typename TPixel>
453  {
454  this->SetNumberOfComponents(3);
455  this->SetPixelType(RGB);
456  this->SetComponentType(MapPixelType<TPixel>::CType);
457  }
458  template <typename TPixel>
460  {
461  this->SetNumberOfComponents(4);
462  this->SetPixelType(RGBA);
463  this->SetComponentType(MapPixelType<TPixel>::CType);
464  }
465  template <typename TPixel, unsigned VLength>
467  {
468  this->SetNumberOfComponents(VLength);
469  this->SetPixelType(VECTOR);
470  this->SetComponentType(MapPixelType<TPixel>::CType);
471  }
472  template <typename TPixel>
474  {
475  this->SetNumberOfComponents(1);
476  this->SetPixelType(VECTOR);
477  this->SetComponentType(MapPixelType<TPixel>::CType);
478  }
479  template <typename TPixel, unsigned VLength>
481  {
482  this->SetNumberOfComponents(VLength);
483  this->SetPixelType(COVARIANTVECTOR);
484  this->SetComponentType(MapPixelType<TPixel>::CType);
485  }
486  template <typename TPixel,unsigned VLength>
488  {
489  this->SetNumberOfComponents(VLength);
490  this->SetPixelType(COVARIANTVECTOR);
491  this->SetComponentType(MapPixelType<TPixel>::CType);
492  }
494 
495  template <typename TPixel, unsigned VLength>
497  {
498  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
499  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
500  this->SetComponentType(MapPixelType<TPixel>::CType);
501  }
502 
503  template <typename TPixel>
505  {
506  this->SetNumberOfComponents(6);
507  this->SetPixelType(DIFFUSIONTENSOR3D);
508  this->SetComponentType(MapPixelType<TPixel>::CType);
509  }
510 
511  template <typename TPixel, unsigned VLength>
513  {
514  this->SetNumberOfComponents(VLength * VLength);
515  this->SetPixelType(MATRIX);
516  this->SetComponentType(MapPixelType<TPixel>::CType);
517  }
518 
519  template <typename TPixel>
520  void SetPixelTypeInfo(const std::complex< TPixel > *)
521  {
522  this->SetNumberOfComponents(2);
523  this->SetPixelType(COMPLEX);
524  this->SetComponentType(MapPixelType<TPixel>::CType);
525  }
526 
527  template <unsigned VLength>
529  {
530  this->SetNumberOfComponents(VLength);
531  this->SetPixelType(ImageIOBase::OFFSET);
532  this->SetComponentType(ImageIOBase::LONG);
533  }
534 
535 protected:
536  ImageIOBase();
537  ~ImageIOBase();
538  void PrintSelf(std::ostream & os, Indent indent) const;
539 
540  virtual const ImageRegionSplitterBase* GetImageRegionSplitter(void) const;
541 
544 
548 
551 
553 
556 
558  std::string m_FileName;
559 
562  unsigned int m_NumberOfComponents;
563 
565  unsigned int m_NumberOfDimensions;
566 
569 
572 
575 
579 
581  std::vector< SizeValueType > m_Dimensions;
582 
585  std::vector< double > m_Spacing;
586 
588  std::vector< double > m_Origin;
589 
591  std::vector< std::vector< double > > m_Direction;
592 
595  std::vector< SizeType > m_Strides;
596 
598  virtual void Reset(const bool freeDynamic = true);
599 
601  void Resize(const unsigned int numDimensions,
602  const unsigned int *dimensions);
603 
606  virtual unsigned int GetPixelSize() const;
607 
614  void ComputeStrides();
615 
618  SizeType GetComponentStride() const;
619 
622  SizeType GetRowStride() const;
623 
626  SizeType GetSliceStride() const;
627 
629  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
630  IOComponentType ctype,
631  SizeType numberOfBytesToWrite);
632 
634  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
635  IOComponentType ctype,
636  SizeType numberOfBytesToBeRead);
637 
639  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
640 
642  void AddSupportedReadExtension(const char *extension);
643 
645  void AddSupportedWriteExtension(const char *extension);
646 
649  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
650  const ImageIORegion & pasteRegion) const;
651 
654  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
655  unsigned int numberOfActualSplits,
656  const ImageIORegion & pasteRegion) const;
657 
658 private:
659  ImageIOBase(const Self &); //purposely not implemented
660  void operator=(const Self &); //purposely not implemented
661 
664 };
665 
666 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
667  template <> struct ImageIOBase::MapPixelType<type> \
668  { \
669  static const IOComponentType CType = ctype; \
670  }
671 
672 IMAGEIOBASE_TYPEMAP(char, CHAR);
673 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR);
674 IMAGEIOBASE_TYPEMAP(short, SHORT);
675 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT);
676 IMAGEIOBASE_TYPEMAP(int, INT);
677 IMAGEIOBASE_TYPEMAP(unsigned int, UINT);
678 IMAGEIOBASE_TYPEMAP(long, LONG);
679 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG);
680 IMAGEIOBASE_TYPEMAP(float, FLOAT);
681 IMAGEIOBASE_TYPEMAP(double, DOUBLE);
682 #undef IMAGIOBASE_TYPEMAP
683 
684 
685 } // end namespace itk
686 
687 #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()