|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 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 | + * https://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 | + |
| 19 | +#ifndef itkMGHImageIO_h |
| 20 | +#define itkMGHImageIO_h |
| 21 | + |
| 22 | +#include "itkMatrix.h" |
| 23 | +#include "itkImageIOBase.h" |
| 24 | +#include <fstream> |
| 25 | +#include "itk_zlib.h" |
| 26 | + |
| 27 | +#include "MGHIOExport.h" |
| 28 | + |
| 29 | +namespace itk |
| 30 | +{ |
| 31 | +/** \class MGHImageIO |
| 32 | + * |
| 33 | + * \author Hans J. Johnson |
| 34 | + * \brief Class that defines how to read MGH file format. |
| 35 | + * Originally developed as part of the Slicer software |
| 36 | + * package under grants XXXX |
| 37 | + * |
| 38 | + * \ingroup IOFilters |
| 39 | + * \ingroup MGHIO |
| 40 | + */ |
| 41 | +class MGHIO_EXPORT MGHImageIO : public ImageIOBase |
| 42 | +{ |
| 43 | +public: |
| 44 | + /** Standard class type alias. */ |
| 45 | + using Self = MGHImageIO; |
| 46 | + using Superclass = ImageIOBase; |
| 47 | + using Pointer = SmartPointer<Self>; |
| 48 | + |
| 49 | + /** Method for creation through the object factory. */ |
| 50 | + itkNewMacro(Self); |
| 51 | + |
| 52 | + /** Run-time type information (and related methods). */ |
| 53 | + itkOverrideGetNameOfClassMacro(MGHImageIO); |
| 54 | + |
| 55 | + /*-------- This part of the interfaces deals with reading data. ----- */ |
| 56 | + |
| 57 | + /** Determine if the file can be read with this ImageIO implementation. |
| 58 | + * \param FileNameToRead The name of the file to test for reading. |
| 59 | + * \post Sets classes ImageIOBase::m_FileName variable to be FileNameToWrite |
| 60 | + * \return Returns true if this ImageIO can read the file specified. |
| 61 | + */ |
| 62 | + bool |
| 63 | + CanReadFile(const char * FileNameToRead) override; |
| 64 | + |
| 65 | + /** Set the spacing and dimension information for the set filename. */ |
| 66 | + void |
| 67 | + ReadImageInformation() override; |
| 68 | + |
| 69 | + /** Reads the data from disk into the memory buffer provided. */ |
| 70 | + void |
| 71 | + Read(void * pData) override; |
| 72 | + |
| 73 | + /*-------- This part of the interfaces deals with writing data. ----- */ |
| 74 | + |
| 75 | + /** Determine if the file can be written with this ImageIO implementation. |
| 76 | + * \param FileNameToWrite The name of the file to test for writing. |
| 77 | + * \post Sets classes ImageIOBase::m_FileName variable to be FileNameToWrite |
| 78 | + * \return Returns true if this ImageIO can write the file specified. |
| 79 | + */ |
| 80 | + bool |
| 81 | + CanWriteFile(const char * name) override; |
| 82 | + |
| 83 | + /** Set the spacing and dimension information for the set filename. */ |
| 84 | + void |
| 85 | + WriteImageInformation() override; |
| 86 | + |
| 87 | + /** Writes the data to disk from the memory buffer provided. Make sure |
| 88 | + * that the IORegions has been set properly. */ |
| 89 | + void |
| 90 | + Write(const void * buffer) override; |
| 91 | + |
| 92 | +protected: |
| 93 | + MGHImageIO(); |
| 94 | + ~MGHImageIO() override; |
| 95 | + void |
| 96 | + PrintSelf(std::ostream & os, Indent indent) const override; |
| 97 | + |
| 98 | + void |
| 99 | + ReadVolumeHeader(); |
| 100 | + |
| 101 | +private: |
| 102 | + static const int MRI_UCHAR = 0; |
| 103 | + static const int MRI_INT = 1; |
| 104 | + static const int MRI_FLOAT = 3; |
| 105 | + static const int MRI_SHORT = 4; |
| 106 | + static const int MRI_TENSOR = 6; |
| 107 | + static const unsigned int FS_DIMENSION_HEADER_SIZE = sizeof(int) * 7; |
| 108 | + static const unsigned int FS_RAS_HEADER_SIZE = (sizeof(float) * 15) + sizeof(short); |
| 109 | + static const unsigned int FS_UNUSED_HEADER_SIZE = 256 - FS_RAS_HEADER_SIZE; |
| 110 | + static const unsigned int FS_WHOLE_HEADER_SIZE = |
| 111 | + FS_RAS_HEADER_SIZE + FS_DIMENSION_HEADER_SIZE + FS_UNUSED_HEADER_SIZE; |
| 112 | + |
| 113 | + /** check if a filename is for a compressed file */ |
| 114 | + bool |
| 115 | + IsCompressedFilename(const std::string fname); |
| 116 | + /// processes the actual data buffer |
| 117 | + void |
| 118 | + SwapBytesIfNecessary(void * const buffer, const unsigned long numberOfPixels); |
| 119 | + |
| 120 | + /// examines the direction cosines and creates encapsulation data |
| 121 | + // void MriDirCos(); |
| 122 | + |
| 123 | + void |
| 124 | + WriteHeader(); |
| 125 | + |
| 126 | + void |
| 127 | + WriteData(const void * buffer); |
| 128 | + |
| 129 | + void |
| 130 | + PermuteFrameValues(const void * buffer, char * tempmemory); |
| 131 | + |
| 132 | + unsigned int |
| 133 | + GetComponentSize() const override; |
| 134 | + |
| 135 | + std::string |
| 136 | + GetOrientation(itk::Matrix<double> directions); |
| 137 | + |
| 138 | + bool m_IsCompressed; |
| 139 | + gzFile m_GZFile; |
| 140 | + std::ofstream m_Output; |
| 141 | + |
| 142 | + // Utility function to assist with writing to disk in the |
| 143 | + // proper format. TInType is static_cast<TDiskType> type. |
| 144 | + template <typename TInType, typename TDiskType> |
| 145 | + int |
| 146 | + TWrite(const TInType inValue); |
| 147 | + template <typename TDiskType, typename TOutType> |
| 148 | + int |
| 149 | + TRead(TOutType & outValue); |
| 150 | + |
| 151 | + int |
| 152 | + TWrite(const char * buf, const unsigned long count); |
| 153 | + void |
| 154 | + OpenFile(); |
| 155 | + void |
| 156 | + CloseFile(); |
| 157 | +}; |
| 158 | +} // end namespace itk |
| 159 | + |
| 160 | +#endif // itkMGHImageIO_h |
0 commit comments