ITK  4.3.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 
21 #include "itkIOConfigure.h"
22 
23 #include "itkLightProcessObject.h"
24 #include "itkIndent.h"
25 #include "itkImageIORegion.h"
26 #include "itkRGBPixel.h"
27 #include "itkRGBAPixel.h"
29 #include "itkCovariantVector.h"
31 #include "itkDiffusionTensor3D.h"
32 
33 #include "vnl/vnl_vector.h"
34 
35 #include <string>
36 
37 namespace itk
38 {
67 class ITK_EXPORT ImageIOBase:public LightProcessObject
68 {
69 public:
71  typedef ImageIOBase Self;
74 
76  itkTypeMacro(ImageIOBase, Superclass);
77 
79  itkSetStringMacro(FileName);
80  itkGetStringMacro(FileName);
82 
86 
92  class UnknownType {};
93 
97  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
98  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
99  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
100 
105  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
106  ULONG, LONG, FLOAT, DOUBLE } IOComponentType;
107 
111  void SetNumberOfDimensions(unsigned int);
112 
113  itkGetConstMacro(NumberOfDimensions, unsigned int);
114 
118  virtual void SetDimensions(unsigned int i, unsigned int dim);
120 
121  virtual unsigned int GetDimensions(unsigned int i) const
122  { return m_Dimensions[i]; }
123 
126  virtual void SetOrigin(unsigned int i, double origin);
127 
128  virtual double GetOrigin(unsigned int i) const
129  {
130  return m_Origin[i];
131  }
132 
135  virtual void SetSpacing(unsigned int i, double spacing);
136 
137  virtual double GetSpacing(unsigned int i) const
138  {
139  return m_Spacing[i];
140  }
141 
144  virtual void SetDirection(unsigned int i, std::vector< double > & direction);
145 
146  virtual void SetDirection(unsigned int i, vnl_vector< double > & direction);
147 
148  virtual std::vector< double > GetDirection(unsigned int i) const
149  {
150  return m_Direction[i];
151  }
152 
155  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
156 
163  itkSetMacro(IORegion, ImageIORegion);
164  itkGetConstReferenceMacro(IORegion, ImageIORegion);
166 
172  itkSetEnumMacro(PixelType, IOPixelType);
173  itkGetEnumMacro(PixelType, IOPixelType);
175 
178  itkSetEnumMacro(ComponentType, IOComponentType);
179  itkGetEnumMacro(ComponentType, IOComponentType);
180 
186  virtual const std::type_info & GetComponentTypeInfo() const;
187 
192  itkSetMacro(NumberOfComponents, unsigned int);
193  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
195 
197  itkSetMacro(UseCompression, bool);
198  itkGetConstMacro(UseCompression, bool);
199  itkBooleanMacro(UseCompression);
201 
203  itkSetMacro(UseStreamedReading, bool);
204  itkGetConstMacro(UseStreamedReading, bool);
205  itkBooleanMacro(UseStreamedReading);
207 
209  itkSetMacro(UseStreamedWriting, bool);
210  itkGetConstMacro(UseStreamedWriting, bool);
211  itkBooleanMacro(UseStreamedWriting);
213 
216  static std::string GetComponentTypeAsString(IOComponentType);
217 
219  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
220 
223  static std::string GetPixelTypeAsString(IOPixelType);
224 
226  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
227 
230  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
231 
234  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
235 
238  itkSetEnumMacro(FileType, FileType);
239  itkGetEnumMacro(FileType, FileType);
240  void SetFileTypeToASCII()
241  {
242  this->SetFileType(ASCII);
243  }
245 
246  void SetFileTypeToBinary()
247  {
248  this->SetFileType(Binary);
249  }
250 
262  itkSetEnumMacro(ByteOrder, ByteOrder);
263  itkGetEnumMacro(ByteOrder, ByteOrder);
264  void SetByteOrderToBigEndian()
265  {
266  this->SetByteOrder(BigEndian);
267  }
269 
270  void SetByteOrderToLittleEndian()
271  {
272  this->SetByteOrder(LittleEndian);
273  }
274 
277  std::string GetFileTypeAsString(FileType) const;
278 
281  std::string GetByteOrderAsString(ByteOrder) const;
282 
285 
289 
296  virtual SizeType GetPixelStride() const;
297 
299  SizeType GetImageSizeInPixels() const;
300 
302  SizeType GetImageSizeInBytes() const;
303 
306  SizeType GetImageSizeInComponents() const;
307 
312  virtual unsigned int GetComponentSize() const;
313 
314  /*-------- This part of the interfaces deals with reading data ----- */
315 
318  virtual bool CanReadFile(const char *) = 0;
319 
324  virtual bool CanStreamRead()
325  {
326  return false;
327  }
328 
331  virtual void ReadImageInformation() = 0;
332 
334  virtual void Read(void *buffer) = 0;
335 
336  /*-------- This part of the interfaces deals with writing data ----- */
337 
340  virtual bool CanWriteFile(const char *) = 0;
341 
348  virtual bool CanStreamWrite()
349  {
350  return false;
351  }
352 
355  virtual void WriteImageInformation() = 0;
356 
360  virtual void Write(const void *buffer) = 0;
361 
362  /* --- Support reading and writing data as a series of files. --- */
363 
369  virtual bool SupportsDimension(unsigned long dim)
370  {
371  return ( dim == 2 );
372  }
373 
385  virtual ImageIORegion
386  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
387 
402  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
403  const ImageIORegion & pasteRegion,
404  const ImageIORegion & largestPossibleRegion);
405 
412  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
413  unsigned int numberOfActualSplits,
414  const ImageIORegion & pasteRegion,
415  const ImageIORegion & largestPossibleRegion);
416 
418  typedef std::vector< std::string > ArrayOfExtensionsType;
419 
424  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
425 
430  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
431 
432  template <typename TPixel>
433  void SetTypeInfo(const TPixel *);
434 
436  template <typename TPixel>
438  {
439  static const IOComponentType CType =
440  UNKNOWNCOMPONENTTYPE;
441  };
442  template <typename TPixel>
443  void SetPixelTypeInfo(const TPixel *)
444  {
445  this->SetNumberOfComponents(1);
446  this->SetPixelType(SCALAR);
447  this->SetComponentType(MapPixelType<TPixel>::CType);
448  }
449  template <typename TPixel>
450  void SetPixelTypeInfo(const RGBPixel< TPixel > *)
451  {
452  this->SetNumberOfComponents(3);
453  this->SetPixelType(RGB);
454  this->SetComponentType(MapPixelType<TPixel>::CType);
455  }
456  template <typename TPixel>
457  void SetPixelTypeInfo(const RGBAPixel< TPixel > *)
458  {
459  this->SetNumberOfComponents(4);
460  this->SetPixelType(RGBA);
461  this->SetComponentType(MapPixelType<TPixel>::CType);
462  }
463  template <typename TPixel, unsigned VLength>
464  void SetPixelTypeInfo(const Vector< TPixel , VLength > *)
465  {
466  this->SetNumberOfComponents(VLength);
467  this->SetPixelType(VECTOR);
468  this->SetComponentType(MapPixelType<TPixel>::CType);
469  }
470  template <typename TPixel>
471  void SetPixelTypeInfo(const VariableLengthVector< TPixel > *)
472  {
473  this->SetNumberOfComponents(1);
474  this->SetPixelType(VECTOR);
475  this->SetComponentType(MapPixelType<TPixel>::CType);
476  }
477  template <typename TPixel, unsigned VLength>
478  void SetPixelTypeInfo(const CovariantVector< TPixel,VLength > *)
479  {
480  this->SetNumberOfComponents(VLength);
481  this->SetPixelType(COVARIANTVECTOR);
482  this->SetComponentType(MapPixelType<TPixel>::CType);
483  }
484  template <typename TPixel,unsigned VLength>
485  void SetPixelTypeInfo(const FixedArray< TPixel,VLength > *)
486  {
487  this->SetNumberOfComponents(VLength);
488  this->SetPixelType(COVARIANTVECTOR);
489  this->SetComponentType(MapPixelType<TPixel>::CType);
490  }
492 
493  template <class TPixel, unsigned VLength>
494  void SetPixelTypeInfo(const SymmetricSecondRankTensor<TPixel,VLength> *)
495  {
496  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
497  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
498  this->SetComponentType(MapPixelType<TPixel>::CType);
499  }
500 
501  template <typename TPixel>
502  inline void SetPixelTypeInfo(const DiffusionTensor3D< TPixel > *)
503  {
504  this->SetNumberOfComponents(6);
505  this->SetPixelType(DIFFUSIONTENSOR3D);
506  this->SetComponentType(MapPixelType<TPixel>::CType);
507  }
508 
509  template <typename TPixel, unsigned VLength>
510  void SetPixelTypeInfo(const Matrix< TPixel,VLength, VLength > *)
511  {
512  this->SetNumberOfComponents(VLength * VLength);
513  this->SetPixelType(MATRIX);
514  this->SetComponentType(MapPixelType<TPixel>::CType);
515  }
516 
517  template <typename TPixel>
518  void SetPixelTypeInfo(const std::complex< TPixel > *)
519  {
520  this->SetNumberOfComponents(2);
521  this->SetPixelType(COMPLEX);
522  this->SetComponentType(MapPixelType<TPixel>::CType);
523  }
524 
525  template <unsigned VLength>
526  void SetPixelTypeInfo(const Offset< VLength > *)
527  {
528  this->SetNumberOfComponents(VLength);
529  this->SetPixelType(ImageIOBase::OFFSET);
530  this->SetComponentType(ImageIOBase::LONG);
531  }
532 
533 protected:
534  ImageIOBase();
535  ~ImageIOBase();
536  void PrintSelf(std::ostream & os, Indent indent) const;
537 
540 
544 
547 
549 
552 
554  std::string m_FileName;
555 
558  unsigned int m_NumberOfComponents;
559 
561  unsigned int m_NumberOfDimensions;
562 
565 
568 
571 
575 
577  std::vector< SizeValueType > m_Dimensions;
578 
581  std::vector< double > m_Spacing;
582 
584  std::vector< double > m_Origin;
585 
587  std::vector< std::vector< double > > m_Direction;
588 
591  std::vector< SizeType > m_Strides;
592 
594  virtual void Reset(const bool freeDynamic = true);
595 
597  void Resize(const unsigned int numDimensions,
598  const unsigned int *dimensions);
599 
602  virtual unsigned int GetPixelSize() const;
603 
610  void ComputeStrides();
611 
614  SizeType GetComponentStride() const;
615 
618  SizeType GetRowStride() const;
619 
622  SizeType GetSliceStride() const;
623 
625  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
626  IOComponentType ctype,
627  SizeType numberOfBytesToWrite);
628 
630  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
631  IOComponentType ctype,
632  SizeType numberOfBytesToBeRead);
633 
635  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
636 
638  void AddSupportedReadExtension(const char *extension);
639 
641  void AddSupportedWriteExtension(const char *extension);
642 
645  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
646  const ImageIORegion & pasteRegion) const;
647 
650  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
651  unsigned int numberOfActualSplits,
652  const ImageIORegion & pasteRegion) const;
653 
654 private:
655  ImageIOBase(const Self &); //purposely not implemented
656  void operator=(const Self &); //purposely not implemented
657 
660 };
661 
662 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
663  template <> struct ImageIOBase::MapPixelType<type> \
664  { \
665  static const IOComponentType CType = ctype; \
666  }
667 
668 IMAGEIOBASE_TYPEMAP(char, CHAR);
669 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR);
670 IMAGEIOBASE_TYPEMAP(short, SHORT);
671 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT);
672 IMAGEIOBASE_TYPEMAP(int, INT);
673 IMAGEIOBASE_TYPEMAP(unsigned int, UINT);
674 IMAGEIOBASE_TYPEMAP(long, LONG);
675 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG);
676 IMAGEIOBASE_TYPEMAP(float, FLOAT);
677 IMAGEIOBASE_TYPEMAP(double, DOUBLE);
678 #undef IMAGIOBASE_TYPEMAP
679 
680 
681 } // end namespace itk
682 
683 #endif // __itkImageIOBase_h
684