ITK
4.1.0
Insight Segmentation and Registration Toolkit
|
00001 /*========================================================================= 00002 * 00003 * Copyright Insight Software Consortium 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0.txt 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 *=========================================================================*/ 00018 #ifndef __itkImageIOBase_h 00019 #define __itkImageIOBase_h 00020 00021 #include "itkIOConfigure.h" 00022 00023 #include "itkLightProcessObject.h" 00024 #include "itkIndent.h" 00025 #include "itkImageIORegion.h" 00026 #include "itkRGBPixel.h" 00027 #include "itkRGBAPixel.h" 00028 #include "itkVariableLengthVector.h" 00029 #include "itkCovariantVector.h" 00030 #include "itkSymmetricSecondRankTensor.h" 00031 #include "itkDiffusionTensor3D.h" 00032 00033 #include "vnl/vnl_vector.h" 00034 00035 #include <string> 00036 00037 namespace itk 00038 { 00067 class ITK_EXPORT ImageIOBase:public LightProcessObject 00068 { 00069 public: 00071 typedef ImageIOBase Self; 00072 typedef LightProcessObject Superclass; 00073 typedef SmartPointer< Self > Pointer; 00074 00076 itkTypeMacro(ImageIOBase, Superclass); 00077 00079 itkSetStringMacro(FileName); 00080 itkGetStringMacro(FileName); 00082 00084 typedef ::itk::IndexValueType IndexValueType; 00085 typedef ::itk::SizeValueType SizeValueType; 00086 00092 class UnknownType {}; 00093 00097 typedef enum { UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR, 00098 POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR, 00099 DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX } IOPixelType; 00100 00105 typedef enum { UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT, 00106 ULONG, LONG, FLOAT, DOUBLE } IOComponentType; 00107 00111 void SetNumberOfDimensions(unsigned int); 00112 00113 itkGetConstMacro(NumberOfDimensions, unsigned int); 00114 00118 virtual void SetDimensions(unsigned int i, unsigned int dim); 00120 00121 virtual unsigned int GetDimensions(unsigned int i) const 00122 { return m_Dimensions[i]; } 00123 00126 virtual void SetOrigin(unsigned int i, double origin); 00127 00128 virtual double GetOrigin(unsigned int i) const 00129 { 00130 return m_Origin[i]; 00131 } 00132 00135 virtual void SetSpacing(unsigned int i, double spacing); 00136 00137 virtual double GetSpacing(unsigned int i) const 00138 { 00139 return m_Spacing[i]; 00140 } 00141 00144 virtual void SetDirection(unsigned int i, std::vector< double > & direction); 00145 00146 virtual void SetDirection(unsigned int i, vnl_vector< double > & direction); 00147 00148 virtual std::vector< double > GetDirection(unsigned int i) const 00149 { 00150 return m_Direction[i]; 00151 } 00152 00155 virtual std::vector< double > GetDefaultDirection(unsigned int i) const; 00156 00163 itkSetMacro(IORegion, ImageIORegion); 00164 itkGetConstReferenceMacro(IORegion, ImageIORegion); 00166 00172 itkSetEnumMacro(PixelType, IOPixelType); 00173 itkGetEnumMacro(PixelType, IOPixelType); 00175 00178 itkSetEnumMacro(ComponentType, IOComponentType); 00179 itkGetEnumMacro(ComponentType, IOComponentType); 00180 00186 virtual const std::type_info & GetComponentTypeInfo() const; 00187 00192 itkSetMacro(NumberOfComponents, unsigned int); 00193 itkGetConstReferenceMacro(NumberOfComponents, unsigned int); 00195 00197 itkSetMacro(UseCompression, bool); 00198 itkGetConstMacro(UseCompression, bool); 00199 itkBooleanMacro(UseCompression); 00201 00203 itkSetMacro(UseStreamedReading, bool); 00204 itkGetConstMacro(UseStreamedReading, bool); 00205 itkBooleanMacro(UseStreamedReading); 00207 00209 itkSetMacro(UseStreamedWriting, bool); 00210 itkGetConstMacro(UseStreamedWriting, bool); 00211 itkBooleanMacro(UseStreamedWriting); 00213 00216 static std::string GetComponentTypeAsString(IOComponentType); 00217 00219 static IOComponentType GetComponentTypeFromString(const std::string &typeString); 00220 00223 static std::string GetPixelTypeAsString(IOPixelType); 00224 00226 static IOPixelType GetPixelTypeFromString(const std::string &pixelString); 00227 00230 typedef enum { ASCII, Binary, TypeNotApplicable } FileType; 00231 00234 typedef enum { BigEndian, LittleEndian, OrderNotApplicable } ByteOrder; 00235 00238 itkSetEnumMacro(FileType, FileType); 00239 itkGetEnumMacro(FileType, FileType); 00240 void SetFileTypeToASCII() 00241 { 00242 this->SetFileType(ASCII); 00243 } 00245 00246 void SetFileTypeToBinary() 00247 { 00248 this->SetFileType(Binary); 00249 } 00250 00262 itkSetEnumMacro(ByteOrder, ByteOrder); 00263 itkGetEnumMacro(ByteOrder, ByteOrder); 00264 void SetByteOrderToBigEndian() 00265 { 00266 this->SetByteOrder(BigEndian); 00267 } 00269 00270 void SetByteOrderToLittleEndian() 00271 { 00272 this->SetByteOrder(LittleEndian); 00273 } 00274 00277 std::string GetFileTypeAsString(FileType) const; 00278 00281 std::string GetByteOrderAsString(ByteOrder) const; 00282 00284 typedef ::itk::intmax_t SizeType; 00285 00288 typedef ::itk::OffsetValueType BufferSizeType; 00289 00296 virtual SizeType GetPixelStride() const; 00297 00299 SizeType GetImageSizeInPixels() const; 00300 00302 SizeType GetImageSizeInBytes() const; 00303 00306 SizeType GetImageSizeInComponents() const; 00307 00312 virtual unsigned int GetComponentSize() const; 00313 00314 /*-------- This part of the interfaces deals with reading data ----- */ 00315 00318 virtual bool CanReadFile(const char *) = 0; 00319 00324 virtual bool CanStreamRead() 00325 { 00326 return false; 00327 } 00328 00331 virtual void ReadImageInformation() = 0; 00332 00334 virtual void Read(void *buffer) = 0; 00335 00336 /*-------- This part of the interfaces deals with writing data ----- */ 00337 00340 virtual bool CanWriteFile(const char *) = 0; 00341 00348 virtual bool CanStreamWrite() 00349 { 00350 return false; 00351 } 00352 00355 virtual void WriteImageInformation() = 0; 00356 00360 virtual void Write(const void *buffer) = 0; 00361 00362 /* --- Support reading and writing data as a series of files. --- */ 00363 00369 virtual bool SupportsDimension(unsigned long dim) 00370 { 00371 return ( dim == 2 ); 00372 } 00373 00385 virtual ImageIORegion 00386 GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requested) const; 00387 00402 virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits, 00403 const ImageIORegion & pasteRegion, 00404 const ImageIORegion & largestPossibleRegion); 00405 00412 virtual ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece, 00413 unsigned int numberOfActualSplits, 00414 const ImageIORegion & pasteRegion, 00415 const ImageIORegion & largestPossibleRegion); 00416 00418 typedef std::vector< std::string > ArrayOfExtensionsType; 00419 00424 const ArrayOfExtensionsType & GetSupportedReadExtensions() const; 00425 00430 const ArrayOfExtensionsType & GetSupportedWriteExtensions() const; 00431 00432 template <typename TPixel> 00433 void SetTypeInfo(const TPixel *); 00434 00436 template <typename TPixel> 00437 struct MapPixelType 00438 { 00439 static const IOComponentType CType = 00440 UNKNOWNCOMPONENTTYPE; 00441 }; 00442 template <typename TPixel> 00443 void SetPixelTypeInfo(const TPixel *) 00444 { 00445 this->SetNumberOfComponents(1); 00446 this->SetPixelType(SCALAR); 00447 this->SetComponentType(MapPixelType<TPixel>::CType); 00448 } 00449 template <typename TPixel> 00450 void SetPixelTypeInfo(const RGBPixel< TPixel > *) 00451 { 00452 this->SetNumberOfComponents(3); 00453 this->SetPixelType(RGB); 00454 this->SetComponentType(MapPixelType<TPixel>::CType); 00455 } 00456 template <typename TPixel> 00457 void SetPixelTypeInfo(const RGBAPixel< TPixel > *) 00458 { 00459 this->SetNumberOfComponents(4); 00460 this->SetPixelType(RGBA); 00461 this->SetComponentType(MapPixelType<TPixel>::CType); 00462 } 00463 template <typename TPixel, unsigned VLength> 00464 void SetPixelTypeInfo(const Vector< TPixel , VLength > *) 00465 { 00466 this->SetNumberOfComponents(VLength); 00467 this->SetPixelType(VECTOR); 00468 this->SetComponentType(MapPixelType<TPixel>::CType); 00469 } 00470 template <typename TPixel> 00471 void SetPixelTypeInfo(const VariableLengthVector< TPixel > *) 00472 { 00473 this->SetNumberOfComponents(1); 00474 this->SetPixelType(VECTOR); 00475 this->SetComponentType(MapPixelType<TPixel>::CType); 00476 } 00477 template <typename TPixel, unsigned VLength> 00478 void SetPixelTypeInfo(const CovariantVector< TPixel,VLength > *) 00479 { 00480 this->SetNumberOfComponents(VLength); 00481 this->SetPixelType(COVARIANTVECTOR); 00482 this->SetComponentType(MapPixelType<TPixel>::CType); 00483 } 00484 template <typename TPixel,unsigned VLength> 00485 void SetPixelTypeInfo(const FixedArray< TPixel,VLength > *) 00486 { 00487 this->SetNumberOfComponents(VLength); 00488 this->SetPixelType(COVARIANTVECTOR); 00489 this->SetComponentType(MapPixelType<TPixel>::CType); 00490 } 00492 00493 template <class TPixel, unsigned VLength> 00494 void SetPixelTypeInfo(const SymmetricSecondRankTensor<TPixel,VLength> *) 00495 { 00496 this->SetNumberOfComponents(VLength * (VLength + 1) / 2 ); 00497 this->SetPixelType(SYMMETRICSECONDRANKTENSOR); 00498 this->SetComponentType(MapPixelType<TPixel>::CType); 00499 } 00500 00501 template <typename TPixel> 00502 inline void SetPixelTypeInfo(const DiffusionTensor3D< TPixel > *) 00503 { 00504 this->SetNumberOfComponents(6); 00505 this->SetPixelType(DIFFUSIONTENSOR3D); 00506 this->SetComponentType(MapPixelType<TPixel>::CType); 00507 } 00508 00509 template <typename TPixel, unsigned VLength> 00510 void SetPixelTypeInfo(const Matrix< TPixel,VLength, VLength > *) 00511 { 00512 this->SetNumberOfComponents(VLength * VLength); 00513 this->SetPixelType(MATRIX); 00514 this->SetComponentType(MapPixelType<TPixel>::CType); 00515 } 00516 00517 template <typename TPixel> 00518 void SetPixelTypeInfo(const std::complex< TPixel > *) 00519 { 00520 this->SetNumberOfComponents(2); 00521 this->SetPixelType(COMPLEX); 00522 this->SetComponentType(MapPixelType<TPixel>::CType); 00523 } 00524 00525 template <unsigned VLength> 00526 void SetPixelTypeInfo(const Offset< VLength > *) 00527 { 00528 this->SetNumberOfComponents(VLength); 00529 this->SetPixelType(ImageIOBase::OFFSET); 00530 this->SetComponentType(ImageIOBase::LONG); 00531 } 00532 protected: 00533 ImageIOBase(); 00534 ~ImageIOBase(); 00535 void PrintSelf(std::ostream & os, Indent indent) const; 00536 00538 IOPixelType m_PixelType; 00539 00542 IOComponentType m_ComponentType; 00543 00545 ByteOrder m_ByteOrder; 00546 00547 FileType m_FileType; 00548 00550 bool m_Initialized; 00551 00553 std::string m_FileName; 00554 00557 unsigned int m_NumberOfComponents; 00558 00560 unsigned int m_NumberOfDimensions; 00561 00563 bool m_UseCompression; 00564 00566 bool m_UseStreamedReading; 00567 00569 bool m_UseStreamedWriting; 00570 00573 ImageIORegion m_IORegion; 00574 00576 std::vector< SizeValueType > m_Dimensions; 00577 00580 std::vector< double > m_Spacing; 00581 00583 std::vector< double > m_Origin; 00584 00586 std::vector< std::vector< double > > m_Direction; 00587 00590 std::vector< SizeType > m_Strides; 00591 00593 virtual void Reset(const bool freeDynamic = true); 00594 00596 void Resize(const unsigned int numDimensions, 00597 const unsigned int *dimensions); 00598 00601 virtual unsigned int GetPixelSize() const; 00602 00609 void ComputeStrides(); 00610 00613 SizeType GetComponentStride() const; 00614 00617 SizeType GetRowStride() const; 00618 00621 SizeType GetSliceStride() const; 00622 00624 virtual void WriteBufferAsASCII(std::ostream & os, const void *buffer, 00625 IOComponentType ctype, 00626 SizeType numberOfBytesToWrite); 00627 00629 virtual void ReadBufferAsASCII(std::istream & os, void *buffer, 00630 IOComponentType ctype, 00631 SizeType numberOfBytesToBeRead); 00632 00634 bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeType numberOfBytesToBeRead); 00635 00637 void AddSupportedReadExtension(const char *extension); 00638 00640 void AddSupportedWriteExtension(const char *extension); 00641 00644 virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits, 00645 const ImageIORegion & pasteRegion) const; 00646 00649 virtual ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece, 00650 unsigned int numberOfActualSplits, 00651 const ImageIORegion & pasteRegion) const; 00652 00653 private: 00654 ImageIOBase(const Self &); //purposely not implemented 00655 void operator=(const Self &); //purposely not implemented 00656 00657 ArrayOfExtensionsType m_SupportedReadExtensions; 00658 ArrayOfExtensionsType m_SupportedWriteExtensions; 00659 }; 00660 00661 #define IMAGEIOBASE_TYPEMAP(type,ctype) \ 00662 template <> struct ImageIOBase::MapPixelType<type> \ 00663 { \ 00664 static const IOComponentType CType = ctype; \ 00665 } 00666 00667 IMAGEIOBASE_TYPEMAP(char, CHAR); 00668 IMAGEIOBASE_TYPEMAP(unsigned char, UCHAR); 00669 IMAGEIOBASE_TYPEMAP(short, SHORT); 00670 IMAGEIOBASE_TYPEMAP(unsigned short, USHORT); 00671 IMAGEIOBASE_TYPEMAP(int, INT); 00672 IMAGEIOBASE_TYPEMAP(unsigned int, UINT); 00673 IMAGEIOBASE_TYPEMAP(long, LONG); 00674 IMAGEIOBASE_TYPEMAP(unsigned long, ULONG); 00675 IMAGEIOBASE_TYPEMAP(float, FLOAT); 00676 IMAGEIOBASE_TYPEMAP(double, DOUBLE); 00677 #undef IMAGIOBASE_TYPEMAP 00678 00679 00680 } // end namespace itk 00681 00682 #endif // __itkImageIOBase_h 00683