ITK  6.0.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:
37  ITK_DEFAULT_COPY_AND_MOVE(ImageFileWriterException);
38 
40  itkOverrideGetNameOfClassMacro(ImageFileWriterException);
41 
43  ImageFileWriterException(const char * file,
44  unsigned int line,
45  const char * message = "Error in IO",
46  const char * loc = "Unknown")
47  : ExceptionObject(file, line, message, loc)
48  {}
49 
51  ImageFileWriterException(const std::string & file,
52  unsigned int line,
53  const char * message = "Error in IO",
54  const char * loc = "Unknown")
55  : ExceptionObject(file, line, message, loc)
56  {}
57 
59  ~ImageFileWriterException() noexcept override;
60 };
89 template <typename TInputImage>
90 class ITK_TEMPLATE_EXPORT ImageFileWriter : public ProcessObject
91 {
92 public:
93  ITK_DISALLOW_COPY_AND_MOVE(ImageFileWriter);
94 
100 
102  itkNewMacro(Self);
103 
105  itkOverrideGetNameOfClassMacro(ImageFileWriter);
106 
108  using InputImageType = TInputImage;
111  using InputImagePixelType = typename InputImageType::PixelType;
112 
114  using Superclass::SetInput;
115  void
116  SetInput(const InputImageType * input);
117 
118  const InputImageType *
119  GetInput();
120 
121  const InputImageType *
122  GetInput(unsigned int idx);
123 
125  itkSetStringMacro(FileName);
126  itkGetStringMacro(FileName);
139  void
141  {
142  if (this->m_ImageIO != io)
143  {
144  this->Modified();
145  this->m_ImageIO = io;
146  }
147  m_FactorySpecifiedImageIO = false;
148  }
149  itkGetModifiableObjectMacro(ImageIO, ImageIOBase);
159  virtual void
160  Write();
161 
164  void
165  SetIORegion(const ImageIORegion & region);
166 
167  const ImageIORegion &
168  GetIORegion() const
169  {
170  return m_PasteIORegion;
171  }
172 
175  itkSetMacro(NumberOfStreamDivisions, unsigned int);
176  itkGetConstReferenceMacro(NumberOfStreamDivisions, unsigned int);
181  void
182  Update() override
183  {
184  this->Write();
185  }
186 
192  void
194  {
195  m_PasteIORegion = ImageIORegion(TInputImage::ImageDimension);
196  m_UserSpecifiedIORegion = false;
197  this->Write();
198  }
199 
201  itkSetMacro(UseCompression, bool);
202  itkGetConstReferenceMacro(UseCompression, bool);
203  itkBooleanMacro(UseCompression);
208  itkSetMacro(CompressionLevel, int);
209  itkGetConstReferenceMacro(CompressionLevel, int);
218  itkSetMacro(UseInputMetaDataDictionary, bool);
219  itkGetConstReferenceMacro(UseInputMetaDataDictionary, bool);
220  itkBooleanMacro(UseInputMetaDataDictionary);
223 protected:
224  ImageFileWriter() = default;
225  ~ImageFileWriter() override = default;
226  void
227  PrintSelf(std::ostream & os, Indent indent) const override;
228 
230  void
231  GenerateData() override;
232 
233 private:
234  std::string m_FileName{};
235 
236  ImageIOBase::Pointer m_ImageIO{};
237  bool m_UserSpecifiedImageIO{ false };
238 
239  ImageIORegion m_PasteIORegion{ TInputImage::ImageDimension };
240  unsigned int m_NumberOfStreamDivisions{ 1 };
241  bool m_UserSpecifiedIORegion{ false };
242 
243  bool m_FactorySpecifiedImageIO{ false }; // did factory mechanism set the ImageIO?
244  bool m_UseCompression{ false };
245  int m_CompressionLevel{ -1 };
246  bool m_UseInputMetaDataDictionary{ true };
247 };
248 
249 
254 template <typename TImagePointer>
255 ITK_TEMPLATE_EXPORT void
256 WriteImage(TImagePointer && image, const std::string & filename, bool compress = false)
257 {
258  using NonReferenceImagePointer = std::remove_reference_t<TImagePointer>;
259  static_assert(std::is_pointer_v<NonReferenceImagePointer> || mpl::IsSmartPointer<NonReferenceImagePointer>::Value,
260  "WriteImage requires a raw pointer or SmartPointer.");
263  using ImageType = std::remove_const_t<std::remove_reference_t<decltype(*image)>>;
264  auto writer = ImageFileWriter<ImageType>::New();
265  writer->SetInput(image);
266  writer->SetFileName(filename);
267  writer->SetUseCompression(compress);
268  writer->Update();
269 }
270 
271 } // end namespace itk
272 
273 #ifndef ITK_MANUAL_INSTANTIATION
274 # include "itkImageFileWriter.hxx"
275 #endif
276 
277 #if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
278 # include "itkImageIOFactoryRegisterManager.h"
279 #endif
280 
281 #endif // itkImageFileWriter_h
itk::ImageFileWriter::SetImageIO
void SetImageIO(ImageIOBase *io)
Definition: itkImageFileWriter.h:140
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:182
itk::ImageFileWriter::InputImagePointer
typename InputImageType::Pointer InputImagePointer
Definition: itkImageFileWriter.h:109
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:51
itkMetaProgrammingLibrary.h
itk::ImageFileWriter::GetIORegion
const ImageIORegion & GetIORegion() const
Definition: itkImageFileWriter.h:168
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::ImageFileWriter::InputImageType
TInputImage InputImageType
Definition: itkImageFileWriter.h:108
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:90
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:111
itk::ImageFileWriterException::ImageFileWriterException
ImageFileWriterException(const char *file, unsigned int line, const char *message="Error in IO", const char *loc="Unknown")
Definition: itkImageFileWriter.h:43
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:193
itk::ImageFileWriter::InputImageRegionType
typename InputImageType::RegionType InputImageRegionType
Definition: itkImageFileWriter.h:110
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
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:256