ITK  6.0.0
Insight Toolkit
itkNiftiImageIO.h
Go to the documentation of this file.
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 itkNiftiImageIO_h
20 #define itkNiftiImageIO_h
21 #include "ITKIONIFTIExport.h"
22 
23 
24 #include <fstream>
25 #include <memory>
26 #include "itkImageIOBase.h"
27 
28 namespace itk
29 {
30 
36 {
37 public:
42  enum class Analyze75Flavor : uint8_t
43  {
44 
46  AnalyzeITK4 = 4,
47 
49  AnalyzeFSL = 3,
50 
52  AnalyzeSPM = 2,
53 
56 
58  AnalyzeReject = 0
59  };
60 
65  enum class NiftiFileEnum : int8_t
66  {
68  TwoFileNifti = 2,
69 
71  OneFileNifti = 1,
72 
74  Analyze75 = 0,
75 
77  OtherOrError = -1,
78  };
79 };
80 
82 #if !defined(ITK_LEGACY_REMOVE)
83 using Analyze75Flavor = NiftiImageIOEnums::Analyze75Flavor;
84 #endif
85 
87 extern ITKIONIFTI_EXPORT std::ostream &
88  operator<<(std::ostream & out, const NiftiImageIOEnums::Analyze75Flavor value);
89 extern ITKIONIFTI_EXPORT std::ostream &
90  operator<<(std::ostream & out, const NiftiImageIOEnums::NiftiFileEnum value);
106 class ITKIONIFTI_EXPORT NiftiImageIO : public ImageIOBase
107 {
108 public:
109  ITK_DISALLOW_COPY_AND_MOVE(NiftiImageIO);
110 
115 
117  itkNewMacro(Self);
118 
120  itkOverrideGetNameOfClassMacro(NiftiImageIO);
121 
122  //-------- This part of the interfaces deals with reading data. -----
123 
124 #if !defined(ITK_LEGACY_REMOVE)
125 
126  using FileType = NiftiImageIOEnums::NiftiFileEnum;
127  // We need to expose the enum values at the class level
128  // for backwards compatibility
129  static constexpr FileType TwoFileNifti = NiftiImageIOEnums::NiftiFileEnum::TwoFileNifti;
130  static constexpr FileType OneFileNifti = NiftiImageIOEnums::NiftiFileEnum::OneFileNifti;
131  static constexpr FileType Analyze75 = NiftiImageIOEnums::NiftiFileEnum::Analyze75;
132  static constexpr FileType OtherOrError = NiftiImageIOEnums::NiftiFileEnum::OtherOrError;
133 #endif
134 
142  DetermineFileType(const char * FileNameToRead);
143 
150  bool
151  CanReadFile(const char * FileNameToRead) override;
152 
154  void
155  ReadImageInformation() override;
156 
158  void
159  Read(void * buffer) override;
160 
161  //-------- This part of the interfaces deals with writing data. -----
162 
168  bool
169  CanWriteFile(const char * FileNameToWrite) override;
170 
176  void
177  WriteImageInformation() override;
178 
181  void
182  Write(const void * buffer) override;
183 
187  GenerateStreamableReadRegionFromRequestedRegion(const ImageIORegion & requestedRegion) const override;
188 
190  itkSetMacro(RescaleSlope, double);
191  itkSetMacro(RescaleIntercept, double);
198  itkSetMacro(LegacyAnalyze75Mode, NiftiImageIOEnums::Analyze75Flavor);
199  itkGetConstMacro(LegacyAnalyze75Mode, NiftiImageIOEnums::Analyze75Flavor);
210  itkSetMacro(ConvertRASVectors, bool);
211  itkGetConstMacro(ConvertRASVectors, bool);
212  itkBooleanMacro(ConvertRASVectors);
230  itkSetMacro(ConvertRASDisplacementVectors, bool);
231  itkGetConstMacro(ConvertRASDisplacementVectors, bool);
232  itkBooleanMacro(ConvertRASDisplacementVectors);
236  itkSetMacro(SFORM_Permissive, bool);
237  itkGetConstMacro(SFORM_Permissive, bool);
238  itkBooleanMacro(SFORM_Permissive);
241 protected:
242  NiftiImageIO();
243  ~NiftiImageIO() override;
244  void
245  PrintSelf(std::ostream & os, Indent indent) const override;
246 
247  virtual bool
249  {
250  return false;
251  }
252 
253 private:
254  // Try to use the Q and S form codes from MetaDataDictionary if they are specified
255  // there, otherwise default to the backwards compatible values from earlier
256  // versions of ITK. The qform guess would probably been better to have
257  // been guessed as NIFTI_XFORM_SCANNER_ANAT
258  unsigned int
259  getSFormCodeFromDictionary() const;
260  unsigned int
261  getQFormCodeFromDictionary() const;
262 
263  bool
264  MustRescale() const;
265 
266  void
267  DefineHeaderObjectDataType();
268 
269  void
270  SetNIfTIOrientationFromImageIO(unsigned short origdims, unsigned short dims);
271 
272  void
273  SetImageIOOrientationFromNIfTI(unsigned short dims, double spacingscale, double timingscale);
274 
275  void
276  SetImageIOMetadataFromNIfTI();
277 
278  // This proxy class provides a nifti_image pointer interface to the internal implementation
279  // of itk::NiftiImageIO, while hiding the niftilib interface from the external ITK interface.
280  class NiftiImageProxy;
281 
282  // Note that it is essential that m_NiftiImageHolder is defined before m_NiftiImage, to ensure that
283  // m_NiftiImage can directly get a proxy from m_NiftiImageHolder during NiftiImageIO construction.
284  const std::unique_ptr<NiftiImageProxy> m_NiftiImageHolder;
285 
286  NiftiImageProxy & m_NiftiImage;
287 
288  double m_RescaleSlope{ 1.0 };
289  double m_RescaleIntercept{ 0.0 };
290 
291  bool m_ConvertRAS{ false };
292  bool m_ConvertRASVectors{ false };
293  bool m_ConvertRASDisplacementVectors{ true };
294 
296 
297  NiftiImageIOEnums::Analyze75Flavor m_LegacyAnalyze75Mode{};
298 
300  bool m_SFORM_Corrected{ false };
301 };
302 
303 
304 } // end namespace itk
305 
306 #endif // itkNiftiImageIO_h
itk::CommonEnums::IOComponent::UNKNOWNCOMPONENTTYPE
itk::ImageIOBase
Abstract superclass defines image IO interface.
Definition: itkImageIOBase.h:77
itk::NiftiImageIOEnums::NiftiFileEnum::OneFileNifti
itk::NiftiImageIOEnums::NiftiFileEnum::TwoFileNifti
itk::NiftiImageIOEnums::Analyze75Flavor::AnalyzeITK4Warning
itk::NiftiImageIOEnums::Analyze75Flavor::AnalyzeReject
itk::NiftiImageIO::m_NiftiImage
NiftiImageProxy & m_NiftiImage
Definition: itkNiftiImageIO.h:286
itk::NiftiImageIOEnums::Analyze75Flavor::AnalyzeITK4
itk::NiftiImageIOEnums::NiftiFileEnum
NiftiFileEnum
Definition: itkNiftiImageIO.h:65
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::NiftiImageIO::m_NiftiImageHolder
const std::unique_ptr< NiftiImageProxy > m_NiftiImageHolder
Definition: itkNiftiImageIO.h:280
itk::ImageIORegion
An ImageIORegion represents a structured region of data.
Definition: itkImageIORegion.h:52
itk::CommonEnums::IOComponent
IOComponent
Definition: itkCommonEnums.h:74
itk::NiftiImageIO::GetUseLegacyModeForTwoFileWriting
virtual bool GetUseLegacyModeForTwoFileWriting() const
Definition: itkNiftiImageIO.h:248
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::NiftiImageIOEnums::Analyze75Flavor
Analyze75Flavor
Definition: itkNiftiImageIO.h:42
itk::NiftiImageIOEnums::Analyze75Flavor::AnalyzeFSL
itk::NiftiImageIOEnums::NiftiFileEnum::OtherOrError
itk::NiftiImageIOEnums::NiftiFileEnum::Analyze75
itkImageIOBase.h
NiftiFileEnum
itk::NiftiImageIO::m_SFORM_Permissive
bool m_SFORM_Permissive
Definition: itkNiftiImageIO.h:299
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::NiftiImageIOEnums::Analyze75Flavor::AnalyzeSPM
itk::NiftiImageIO
Class that defines how to read Nifti file format. Nifti IMAGE FILE FORMAT - As much information as I ...
Definition: itkNiftiImageIO.h:106
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::NiftiImageIOEnums
Definition: itkNiftiImageIO.h:35