ITK  6.0.0
Insight Toolkit
itkHDF5ImageIO.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  *=========================================================================*/
25 #ifndef itkHDF5ImageIO_h
26 #define itkHDF5ImageIO_h
27 #include "ITKIOHDF5Export.h"
28 #include "itkMetaDataObjectBase.h"
29 #include "itkMetaDataDictionary.h"
30 #include <memory> // For unique_ptr.
31 
32 // itk namespace first suppresses
33 // kwstyle error for the H5 namespace below
34 namespace itk
35 {}
36 namespace H5
37 {
38 class H5File;
39 class DataSpace;
40 class DataSet;
41 } // namespace H5
42 
44 
45 namespace itk
46 {
86 class ITKIOHDF5_EXPORT HDF5ImageIO : public StreamingImageIOBase
87 {
88 public:
89  ITK_DISALLOW_COPY_AND_MOVE(HDF5ImageIO);
90 
92  using Self = HDF5ImageIO;
95 
97  itkNewMacro(Self);
98 
100  itkOverrideGetNameOfClassMacro(HDF5ImageIO);
101 
102  /*-------- This part of the interfaces deals with reading data. ----- */
103 
110  bool
111  CanReadFile(const char * FileNameToRead) override;
112 
114  void
115  ReadImageInformation() override;
116 
118  void
119  Read(void * buffer) override;
120 
121  /*-------- This part of the interfaces deals with writing data. ----- */
122 
129  bool
130  CanWriteFile(const char * name) override;
131 
133  void
134  WriteImageInformation() override;
135 
138  void
139  Write(const void * buffer) override;
140 
141 protected:
142  HDF5ImageIO();
143  ~HDF5ImageIO() override;
144 
145  SizeType
146  GetHeaderSize() const override;
147 
148  void
149  PrintSelf(std::ostream & os, Indent indent) const override;
150 
151 private:
152  void
153  WriteString(const std::string & path, const std::string & value);
154  void
155  WriteString(const std::string & path, const char * s);
156  std::string
157  ReadString(const std::string & path);
158 
159  void
160  WriteScalar(const std::string & path, const bool value);
161  void
162  WriteScalar(const std::string & path, const long value);
163  void
164  WriteScalar(const std::string & path, const unsigned long value);
165  void
166  WriteScalar(const std::string & path, const long long value);
167  void
168  WriteScalar(const std::string & path, const unsigned long long value);
169 
170  template <typename TScalar>
171  void
172  WriteScalar(const std::string & path, const TScalar & value);
173 
174  template <typename TScalar>
175  TScalar
176  ReadScalar(const std::string & DataSetName);
177 
178  template <typename TScalar>
179  void
180  WriteVector(const std::string & path, const std::vector<TScalar> & vec);
181 
182  template <typename TScalar>
183  std::vector<TScalar>
184  ReadVector(const std::string & DataSetName);
185 
186  void
187  WriteDirections(const std::string & path, const std::vector<std::vector<double>> & dir);
188 
189  std::vector<std::vector<double>>
190  ReadDirections(const std::string & path);
191 
192  template <typename TType>
193  bool
194  WriteMeta(const std::string & name, MetaDataObjectBase * metaObjBase);
195  template <typename TType>
196  bool
197  WriteMetaArray(const std::string & name, MetaDataObjectBase * metaObjBase);
198  template <typename TType>
199  void
200  StoreMetaData(MetaDataDictionary * metaDict,
201  const std::string & HDFPath,
202  const std::string & name,
203  unsigned long numElements);
204  void
205  SetupStreaming(H5::DataSpace * imageSpace, H5::DataSpace * slabSpace);
206 
207  /* A convenience function to ensure that the
208  * state of the HDF5ImageIO object is returned
209  * to a state similar to constructing a new
210  * object. This is needed to ensure that
211  * an HDF5ImageIO object can be used multiple
212  * times to read/write many images.
213  */
214  void
215  ResetToInitialState();
216 
217  std::unique_ptr<H5::H5File> m_H5File;
218  std::unique_ptr<H5::DataSet> m_VoxelDataSet;
219  bool m_ImageInformationWritten{ false };
220 };
221 } // end namespace itk
222 
223 #endif // itkHDF5ImageIO_h
itkStreamingImageIOBase.h
itk::MetaDataObjectBase
The common interface for MetaDataObject's.
Definition: itkMetaDataObjectBase.h:47
itkMetaDataObjectBase.h
itk::StreamingImageIOBase
A base class for specific ImageIO file formats which support streaming.
Definition: itkStreamingImageIOBase.h:52
itk::HDF5ImageIO
Class that defines how to read HDF5 file format. HDF5 IMAGE FILE FORMAT - As much information as I ca...
Definition: itkHDF5ImageIO.h:86
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::MetaDataDictionary
Provides a mechanism for storing a collection of arbitrary data types.
Definition: itkMetaDataDictionary.h:54
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
H5
Definition: itkHDF5ImageIO.h:36
itkMetaDataDictionary.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::HDF5ImageIO::m_H5File
std::unique_ptr< H5::H5File > m_H5File
Definition: itkHDF5ImageIO.h:217
itk::ImageIOBase::SizeType
itk::intmax_t SizeType
Definition: itkImageIOBase.h:395
itk::HDF5ImageIO::m_VoxelDataSet
std::unique_ptr< H5::DataSet > m_VoxelDataSet
Definition: itkHDF5ImageIO.h:218
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61