ITK  4.4.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"
33 
34 #include "vnl/vnl_vector.h"
35 
36 #include <string>
37 
38 namespace itk
39 {
68 class ITK_EXPORT ImageIOBase:public LightProcessObject
69 {
70 public:
72  typedef ImageIOBase Self;
75 
77  itkTypeMacro(ImageIOBase, Superclass);
78 
80  itkSetStringMacro(FileName);
81  itkGetStringMacro(FileName);
83 
87 
93  class UnknownType {};
94 
98  typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR,
99  POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR,
100  DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType;
101 
106  typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT,
107  ULONG, LONG, FLOAT, DOUBLE } IOComponentType;
108 
112  void SetNumberOfDimensions(unsigned int);
113 
114  itkGetConstMacro(NumberOfDimensions, unsigned int);
115 
119  virtual void SetDimensions(unsigned int i, unsigned int dim);
121 
122  virtual itk::SizeValueType GetDimensions(unsigned int i) const
123  { return m_Dimensions[i]; }
124 
127  virtual void SetOrigin(unsigned int i, double origin);
128 
129  virtual double GetOrigin(unsigned int i) const
130  {
131  return m_Origin[i];
132  }
133 
136  virtual void SetSpacing(unsigned int i, double spacing);
137 
138  virtual double GetSpacing(unsigned int i) const
139  {
140  return m_Spacing[i];
141  }
142 
145  virtual void SetDirection(unsigned int i, std::vector< double > & direction);
146 
147  virtual void SetDirection(unsigned int i, vnl_vector< double > & direction);
148 
149  virtual std::vector< double > GetDirection(unsigned int i) const
150  {
151  return m_Direction[i];
152  }
153 
156  virtual std::vector< double > GetDefaultDirection(unsigned int i) const;
157 
164  itkSetMacro(IORegion, ImageIORegion);
165  itkGetConstReferenceMacro(IORegion, ImageIORegion);
167 
173  itkSetEnumMacro(PixelType, IOPixelType);
174  itkGetEnumMacro(PixelType, IOPixelType);
176 
179  itkSetEnumMacro(ComponentType, IOComponentType);
180  itkGetEnumMacro(ComponentType, IOComponentType);
181 
187  virtual const std::type_info & GetComponentTypeInfo() const;
188 
193  itkSetMacro(NumberOfComponents, unsigned int);
194  itkGetConstReferenceMacro(NumberOfComponents, unsigned int);
196 
198  itkSetMacro(UseCompression, bool);
199  itkGetConstMacro(UseCompression, bool);
200  itkBooleanMacro(UseCompression);
202 
204  itkSetMacro(UseStreamedReading, bool);
205  itkGetConstMacro(UseStreamedReading, bool);
206  itkBooleanMacro(UseStreamedReading);
208 
210  itkSetMacro(UseStreamedWriting, bool);
211  itkGetConstMacro(UseStreamedWriting, bool);
212  itkBooleanMacro(UseStreamedWriting);
214 
217  static std::string GetComponentTypeAsString(IOComponentType);
218 
220  static IOComponentType GetComponentTypeFromString(const std::string &typeString);
221 
224  static std::string GetPixelTypeAsString(IOPixelType);
225 
227  static IOPixelType GetPixelTypeFromString(const std::string &pixelString);
228 
231  typedef enum { ASCII, Binary, TypeNotApplicable } FileType;
232 
235  typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder;
236 
239  itkSetEnumMacro(FileType, FileType);
240  itkGetEnumMacro(FileType, FileType);
241  void SetFileTypeToASCII()
242  {
243  this->SetFileType(ASCII);
244  }
246 
247  void SetFileTypeToBinary()
248  {
249  this->SetFileType(Binary);
250  }
251 
263  itkSetEnumMacro(ByteOrder, ByteOrder);
264  itkGetEnumMacro(ByteOrder, ByteOrder);
265  void SetByteOrderToBigEndian()
266  {
267  this->SetByteOrder(BigEndian);
268  }
270 
271  void SetByteOrderToLittleEndian()
272  {
273  this->SetByteOrder(LittleEndian);
274  }
275 
278  std::string GetFileTypeAsString(FileType) const;
279 
282  std::string GetByteOrderAsString(ByteOrder) const;
283 
286 
290 
297  virtual SizeType GetPixelStride() const;
298 
300  SizeType GetImageSizeInPixels() const;
301 
303  SizeType GetImageSizeInBytes() const;
304 
307  SizeType GetImageSizeInComponents() const;
308 
313  virtual unsigned int GetComponentSize() const;
314 
315  /*-------- This part of the interfaces deals with reading data ----- */
316 
319  virtual bool CanReadFile(const char *) = 0;
320 
325  virtual bool CanStreamRead()
326  {
327  return false;
328  }
329 
332  virtual void ReadImageInformation() = 0;
333 
335  virtual void Read(void *buffer) = 0;
336 
337  /*-------- This part of the interfaces deals with writing data ----- */
338 
341  virtual bool CanWriteFile(const char *) = 0;
342 
349  virtual bool CanStreamWrite()
350  {
351  return false;
352  }
353 
356  virtual void WriteImageInformation() = 0;
357 
361  virtual void Write(const void *buffer) = 0;
362 
363  /* --- Support reading and writing data as a series of files. --- */
364 
370  virtual bool SupportsDimension(unsigned long dim)
371  {
372  return ( dim == 2 );
373  }
374 
386  virtual ImageIORegion
387  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const;
388 
403  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
404  const ImageIORegion & pasteRegion,
405  const ImageIORegion & largestPossibleRegion);
406 
413  virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
414  unsigned int numberOfActualSplits,
415  const ImageIORegion & pasteRegion,
416  const ImageIORegion & largestPossibleRegion);
417 
419  typedef std::vector< std::string > ArrayOfExtensionsType;
420 
425  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
426 
431  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
432 
433  template <typename TPixel>
434  void SetTypeInfo(const TPixel *);
435 
437  template <typename TPixel>
439  {
440  static const IOComponentType CType =
441  UNKNOWNCOMPONENTTYPE;
442  };
443  template <typename TPixel>
444  void SetPixelTypeInfo(const TPixel *)
445  {
446  this->SetNumberOfComponents(1);
447  this->SetPixelType(SCALAR);
448  this->SetComponentType(MapPixelType<TPixel>::CType);
449  }
450  template <typename TPixel>
451  void SetPixelTypeInfo(const RGBPixel< TPixel > *)
452  {
453  this->SetNumberOfComponents(3);
454  this->SetPixelType(RGB);
455  this->SetComponentType(MapPixelType<TPixel>::CType);
456  }
457  template <typename TPixel>
458  void SetPixelTypeInfo(const RGBAPixel< TPixel > *)
459  {
460  this->SetNumberOfComponents(4);
461  this->SetPixelType(RGBA);
462  this->SetComponentType(MapPixelType<TPixel>::CType);
463  }
464  template <typename TPixel, unsigned VLength>
465  void SetPixelTypeInfo(const Vector< TPixel , VLength > *)
466  {
467  this->SetNumberOfComponents(VLength);
468  this->SetPixelType(VECTOR);
469  this->SetComponentType(MapPixelType<TPixel>::CType);
470  }
471  template <typename TPixel>
472  void SetPixelTypeInfo(const VariableLengthVector< TPixel > *)
473  {
474  this->SetNumberOfComponents(1);
475  this->SetPixelType(VECTOR);
476  this->SetComponentType(MapPixelType<TPixel>::CType);
477  }
478  template <typename TPixel, unsigned VLength>
479  void SetPixelTypeInfo(const CovariantVector< TPixel,VLength > *)
480  {
481  this->SetNumberOfComponents(VLength);
482  this->SetPixelType(COVARIANTVECTOR);
483  this->SetComponentType(MapPixelType<TPixel>::CType);
484  }
485  template <typename TPixel,unsigned VLength>
486  void SetPixelTypeInfo(const FixedArray< TPixel,VLength > *)
487  {
488  this->SetNumberOfComponents(VLength);
489  this->SetPixelType(COVARIANTVECTOR);
490  this->SetComponentType(MapPixelType<TPixel>::CType);
491  }
493 
494  template <class TPixel, unsigned VLength>
495  void SetPixelTypeInfo(const SymmetricSecondRankTensor<TPixel,VLength> *)
496  {
497  this->SetNumberOfComponents(VLength * (VLength + 1) / 2 );
498  this->SetPixelType(SYMMETRICSECONDRANKTENSOR);
499  this->SetComponentType(MapPixelType<TPixel>::CType);
500  }
501 
502  template <typename TPixel>
503  inline void SetPixelTypeInfo(const DiffusionTensor3D< TPixel > *)
504  {
505  this->SetNumberOfComponents(6);
506  this->SetPixelType(DIFFUSIONTENSOR3D);
507  this->SetComponentType(MapPixelType<TPixel>::CType);
508  }
509 
510  template <typename TPixel, unsigned VLength>
511  void SetPixelTypeInfo(const Matrix< TPixel,VLength, VLength > *)
512  {
513  this->SetNumberOfComponents(VLength * VLength);
514  this->SetPixelType(MATRIX);
515  this->SetComponentType(MapPixelType<TPixel>::CType);
516  }
517 
518  template <typename TPixel>
519  void SetPixelTypeInfo(const std::complex< TPixel > *)
520  {
521  this->SetNumberOfComponents(2);
522  this->SetPixelType(COMPLEX);
523  this->SetComponentType(MapPixelType<TPixel>::CType);
524  }
525 
526  template <unsigned VLength>
527  void SetPixelTypeInfo(const Offset< VLength > *)
528  {
529  this->SetNumberOfComponents(VLength);
530  this->SetPixelType(ImageIOBase::OFFSET);
531  this->SetComponentType(ImageIOBase::LONG);
532  }
533 
534 protected:
535  ImageIOBase();
536  ~ImageIOBase();
537  void PrintSelf(std::ostream & os, Indent indent) const;
538 
539  virtual const ImageRegionSplitterBase* GetImageRegionSplitter(void) const;
540 
543 
547 
550 
552 
555 
557  std::string m_FileName;
558 
561  unsigned int m_NumberOfComponents;
562 
564  unsigned int m_NumberOfDimensions;
565 
568 
571 
574 
578 
580  std::vector< SizeValueType > m_Dimensions;
581 
584  std::vector< double > m_Spacing;
585 
587  std::vector< double > m_Origin;
588 
590  std::vector< std::vector< double > > m_Direction;
591 
594  std::vector< SizeType > m_Strides;
595 
597  virtual void Reset(const bool freeDynamic = true);
598 
600  void Resize(const unsigned int numDimensions,
601  const unsigned int *dimensions);
602 
605  virtual unsigned int GetPixelSize() const;
606 
613  void ComputeStrides();
614 
617  SizeType GetComponentStride() const;
618 
621  SizeType GetRowStride() const;
622 
625  SizeType GetSliceStride() const;
626 
628  virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer,
629  IOComponentType ctype,
630  SizeType numberOfBytesToWrite);
631 
633  virtual void ReadBufferAsASCII(std::istream & os, void *buffer,
634  IOComponentType ctype,
635  SizeType numberOfBytesToBeRead);
636 
638  bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead);
639 
641  void AddSupportedReadExtension(const char *extension);
642 
644  void AddSupportedWriteExtension(const char *extension);
645 
648  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
649  const ImageIORegion & pasteRegion) const;
650 
653  virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
654  unsigned int numberOfActualSplits,
655  const ImageIORegion & pasteRegion) const;
656 
657 private:
658  ImageIOBase(const Self &); //purposely not implemented
659  void operator=(const Self &); //purposely not implemented
660 
663 };
664 
665 #define IMAGEIOBASE_TYPEMAP(type,ctype) \
666  template <> struct ImageIOBase::MapPixelType<type> \
667  { \
668  static const IOComponentType CType = ctype; \
669  }
670 
671 IMAGEIOBASE_TYPEMAP(char, CHAR);
672 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR);
673 IMAGEIOBASE_TYPEMAP(short, SHORT);
674 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT);
675 IMAGEIOBASE_TYPEMAP(int, INT);
676 IMAGEIOBASE_TYPEMAP(unsigned int, UINT);
677 IMAGEIOBASE_TYPEMAP(long, LONG);
678 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG);
679 IMAGEIOBASE_TYPEMAP(float, FLOAT);
680 IMAGEIOBASE_TYPEMAP(double, DOUBLE);
681 #undef IMAGIOBASE_TYPEMAP
682 
683 
684 } // end namespace itk
685 
686 #endif // __itkImageIOBase_h
687