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 
62  operator=(const ImageFileWriterException &) = default;
64  operator=(ImageFileWriterException &&) = default;
65 };
66 
93 template <typename TInputImage>
94 class ITK_TEMPLATE_EXPORT ImageFileWriter : public ProcessObject
95 {
96 public:
97  ITK_DISALLOW_COPY_AND_MOVE(ImageFileWriter);
98 
104 
106  itkNewMacro(Self);
107 
109  itkOverrideGetNameOfClassMacro(ImageFileWriter);
110 
112  using InputImageType = TInputImage;
115  using InputImagePixelType = typename InputImageType::PixelType;
116 
118  using Superclass::SetInput;
119  void
120  SetInput(const InputImageType * input);
121 
122  const InputImageType *
123  GetInput();
124 
125  const InputImageType *
126  GetInput(unsigned int idx);
127 
129  itkSetStringMacro(FileName);
130  itkGetStringMacro(FileName);
143  void
145  {
146  if (this->m_ImageIO != io)
147  {
148  this->Modified();
149  this->m_ImageIO = io;
150  }
151  m_FactorySpecifiedImageIO = false;
152  }
153  itkGetModifiableObjectMacro(ImageIO, ImageIOBase);
163  virtual void
164  Write();
165 
168  void
169  SetIORegion(const ImageIORegion & region);
170 
171  const ImageIORegion &
172  GetIORegion() const
173  {
174  return m_PasteIORegion;
175  }
176 
179  itkSetMacro(NumberOfStreamDivisions, unsigned int);
180  itkGetConstReferenceMacro(NumberOfStreamDivisions, unsigned int);
185  void
186  Update() override
187  {
188  this->Write();
189  }
190 
196  void
198  {
199  m_PasteIORegion = ImageIORegion(TInputImage::ImageDimension);
200  m_UserSpecifiedIORegion = false;
201  this->Write();
202  }
203 
205  itkSetMacro(UseCompression, bool);
206  itkGetConstReferenceMacro(UseCompression, bool);
207  itkBooleanMacro(UseCompression);
212  itkSetMacro(CompressionLevel, int);
213  itkGetConstReferenceMacro(CompressionLevel, int);
222  itkSetMacro(UseInputMetaDataDictionary, bool);
223  itkGetConstReferenceMacro(UseInputMetaDataDictionary, bool);
224  itkBooleanMacro(UseInputMetaDataDictionary);
227 protected:
228  ImageFileWriter() = default;
229  ~ImageFileWriter() override = default;
230  void
231  PrintSelf(std::ostream & os, Indent indent) const override;
232 
234  void
235  GenerateData() override;
236 
237 private:
238  std::string m_FileName{};
239 
240  ImageIOBase::Pointer m_ImageIO{};
241  bool m_UserSpecifiedImageIO{ false };
242 
243  ImageIORegion m_PasteIORegion{ TInputImage::ImageDimension };
244  unsigned int m_NumberOfStreamDivisions{ 1 };
245  bool m_UserSpecifiedIORegion{ false };
246 
247  bool m_FactorySpecifiedImageIO{ false }; // did factory mechanism set the ImageIO?
248  bool m_UseCompression{ false };
249  int m_CompressionLevel{ -1 };
250  bool m_UseInputMetaDataDictionary{ true };
251 };
252 
253 
258 template <typename TImagePointer>
259 ITK_TEMPLATE_EXPORT void
260 WriteImage(TImagePointer && image, const std::string & filename, bool compress = false)
261 {
262  using NonReferenceImagePointer = std::remove_reference_t<TImagePointer>;
263  static_assert(std::is_pointer_v<NonReferenceImagePointer> || mpl::IsSmartPointer<NonReferenceImagePointer>::Value,
264  "WriteImage requires a raw pointer or SmartPointer.");
267  using ImageType = std::remove_const_t<std::remove_reference_t<decltype(*image)>>;
268  auto writer = ImageFileWriter<ImageType>::New();
269  writer->SetInput(image);
270  writer->SetFileName(filename);
271  writer->SetUseCompression(compress);
272  writer->Update();
273 }
274 
275 } // end namespace itk
276 
277 #ifndef ITK_MANUAL_INSTANTIATION
278 # include "itkImageFileWriter.hxx"
279 #endif
280 
281 #if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
282 # include "itkImageIOFactoryRegisterManager.h"
283 #endif
284 
285 #endif // itkImageFileWriter_h
itk::ImageFileWriter::SetImageIO
void SetImageIO(ImageIOBase *io)
Definition: itkImageFileWriter.h:144
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:186
itk::ImageFileWriter::InputImagePointer
typename InputImageType::Pointer InputImagePointer
Definition: itkImageFileWriter.h:113
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:172
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::ImageFileWriter::InputImageType
TInputImage InputImageType
Definition: itkImageFileWriter.h:112
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:94
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:115
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:197
itk::ImageFileWriter::InputImageRegionType
typename InputImageType::RegionType InputImageRegionType
Definition: itkImageFileWriter.h:114
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:260