ITK  5.4.0
Insight Toolkit
itkImageFileWriter.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 #ifndef itkImageFileWriter_h
19 #define itkImageFileWriter_h
20 #include "ITKIOImageBaseExport.h"
21 
22 #include "itkProcessObject.h"
23 #include "itkImageIOBase.h"
24 #include "itkMacro.h"
26 
27 namespace itk
28 {
34 class ITKIOImageBase_EXPORT ImageFileWriterException : public ExceptionObject
35 {
36 public:
38  itkOverrideGetNameOfClassMacro(ImageFileWriterException);
39 
41  ImageFileWriterException(const char * file,
42  unsigned int line,
43  const char * message = "Error in IO",
44  const char * loc = "Unknown")
45  : ExceptionObject(file, line, message, loc)
46  {}
47 
49  ImageFileWriterException(const std::string & file,
50  unsigned int line,
51  const char * message = "Error in IO",
52  const char * loc = "Unknown")
53  : ExceptionObject(file, line, message, loc)
54  {}
55 
57  ~ImageFileWriterException() noexcept override;
58 };
87 template <typename TInputImage>
88 class ITK_TEMPLATE_EXPORT ImageFileWriter : public ProcessObject
89 {
90 public:
91  ITK_DISALLOW_COPY_AND_MOVE(ImageFileWriter);
92 
98 
100  itkNewMacro(Self);
101 
103  itkOverrideGetNameOfClassMacro(ImageFileWriter);
104 
106  using InputImageType = TInputImage;
109  using InputImagePixelType = typename InputImageType::PixelType;
110 
112  using Superclass::SetInput;
113  void
114  SetInput(const InputImageType * input);
115 
116  const InputImageType *
117  GetInput();
118 
119  const InputImageType *
120  GetInput(unsigned int idx);
121 
123  itkSetStringMacro(FileName);
124  itkGetStringMacro(FileName);
137  void
139  {
140  if (this->m_ImageIO != io)
141  {
142  this->Modified();
143  this->m_ImageIO = io;
144  }
145  m_FactorySpecifiedImageIO = false;
146  }
147  itkGetModifiableObjectMacro(ImageIO, ImageIOBase);
157  virtual void
158  Write();
159 
162  void
163  SetIORegion(const ImageIORegion & region);
164 
165  const ImageIORegion &
166  GetIORegion() const
167  {
168  return m_PasteIORegion;
169  }
170 
173  itkSetMacro(NumberOfStreamDivisions, unsigned int);
174  itkGetConstReferenceMacro(NumberOfStreamDivisions, unsigned int);
179  void
180  Update() override
181  {
182  this->Write();
183  }
184 
190  void
192  {
193  m_PasteIORegion = ImageIORegion(TInputImage::ImageDimension);
194  m_UserSpecifiedIORegion = false;
195  this->Write();
196  }
197 
199  itkSetMacro(UseCompression, bool);
200  itkGetConstReferenceMacro(UseCompression, bool);
201  itkBooleanMacro(UseCompression);
206  itkSetMacro(CompressionLevel, int);
207  itkGetConstReferenceMacro(CompressionLevel, int);
216  itkSetMacro(UseInputMetaDataDictionary, bool);
217  itkGetConstReferenceMacro(UseInputMetaDataDictionary, bool);
218  itkBooleanMacro(UseInputMetaDataDictionary);
221 protected:
222  ImageFileWriter() = default;
223  ~ImageFileWriter() override = default;
224  void
225  PrintSelf(std::ostream & os, Indent indent) const override;
226 
228  void
229  GenerateData() override;
230 
231 private:
232  std::string m_FileName{};
233 
234  ImageIOBase::Pointer m_ImageIO{};
235  bool m_UserSpecifiedImageIO{ false };
236 
237  ImageIORegion m_PasteIORegion{ TInputImage::ImageDimension };
238  unsigned int m_NumberOfStreamDivisions{ 1 };
239  bool m_UserSpecifiedIORegion{ false };
240 
241  bool m_FactorySpecifiedImageIO{ false }; // did factory mechanism set the ImageIO?
242  bool m_UseCompression{ false };
243  int m_CompressionLevel{ -1 };
244  bool m_UseInputMetaDataDictionary{ true };
245 };
246 
247 
252 template <typename TImagePointer>
253 ITK_TEMPLATE_EXPORT void
254 WriteImage(TImagePointer && image, const std::string & filename, bool compress = false)
255 {
256  using NonReferenceImagePointer = std::remove_reference_t<TImagePointer>;
257  static_assert(std::is_pointer_v<NonReferenceImagePointer> || mpl::IsSmartPointer<NonReferenceImagePointer>::Value,
258  "WriteImage requires a raw pointer or SmartPointer.");
261  using ImageType = std::remove_const_t<std::remove_reference_t<decltype(*image)>>;
262  auto writer = ImageFileWriter<ImageType>::New();
263  writer->SetInput(image);
264  writer->SetFileName(filename);
265  writer->SetUseCompression(compress);
266  writer->Update();
267 }
268 
269 } // end namespace itk
270 
271 #ifndef ITK_MANUAL_INSTANTIATION
272 # include "itkImageFileWriter.hxx"
273 #endif
274 
275 #if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
276 # include "itkImageIOFactoryRegisterManager.h"
277 #endif
278 
279 #endif // itkImageFileWriter_h
itk::ImageFileWriter::SetImageIO
void SetImageIO(ImageIOBase *io)
Definition: itkImageFileWriter.h:138
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::ImageIOBase
Abstract superclass defines image IO interface.
Definition: itkImageIOBase.h:77
itk::ImageFileWriter::Update
void Update() override
Definition: itkImageFileWriter.h:180
itk::ImageFileWriter::InputImagePointer
typename InputImageType::Pointer InputImagePointer
Definition: itkImageFileWriter.h:107
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ImageIORegion
An ImageIORegion represents a structured region of data.
Definition: itkImageIORegion.h:52
itk::ImageFileWriterException::ImageFileWriterException
ImageFileWriterException(const std::string &file, unsigned int line, const char *message="Error in IO", const char *loc="Unknown")
Definition: itkImageFileWriter.h:49
itkMetaProgrammingLibrary.h
itk::ImageFileWriter::GetIORegion
const ImageIORegion & GetIORegion() const
Definition: itkImageFileWriter.h:166
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::ImageFileWriter::InputImageType
TInputImage InputImageType
Definition: itkImageFileWriter.h:106
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itkMacro.h
itkProcessObject.h
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::ImageFileWriter::InputImagePixelType
typename InputImageType::PixelType InputImagePixelType
Definition: itkImageFileWriter.h:109
itk::ImageFileWriterException::ImageFileWriterException
ImageFileWriterException(const char *file, unsigned int line, const char *message="Error in IO", const char *loc="Unknown")
Definition: itkImageFileWriter.h:41
itkImageIOBase.h
itk::ImageFileWriterException
Base exception class for IO problems during writing.
Definition: itkImageFileWriter.h:34
itk::ImageFileWriter::UpdateLargestPossibleRegion
void UpdateLargestPossibleRegion() override
Writes the entire image to file.
Definition: itkImageFileWriter.h:191
itk::ImageFileWriter::InputImageRegionType
typename InputImageType::RegionType InputImageRegionType
Definition: itkImageFileWriter.h:108
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
itk::ImageFileWriter::New
static Pointer New()
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:254