ITK  5.2.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  * http://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  itkTypeMacro(ImageFileWriterException, ExceptionObject);
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 };
60 
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  itkTypeMacro(ImageFileWriter, ProcessObject);
104 
106  using InputImageType = TInputImage;
107  using InputImagePointer = typename InputImageType::Pointer;
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);
126 
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);
149 
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);
176 
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);
203 
206  itkSetMacro(CompressionLevel, int);
207  itkGetConstReferenceMacro(CompressionLevel, int);
209 
216  itkSetMacro(UseInputMetaDataDictionary, bool);
217  itkGetConstReferenceMacro(UseInputMetaDataDictionary, bool);
218  itkBooleanMacro(UseInputMetaDataDictionary);
220 
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 
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 = typename std::remove_reference<TImagePointer>::type;
257  static_assert(std::is_pointer<NonReferenceImagePointer>::value ||
258  mpl::IsSmartPointer<NonReferenceImagePointer>::Value,
259  "WriteImage requires a raw pointer or SmartPointer.");
261 
262  using ImageType = typename std::remove_const<typename std::remove_reference<decltype(*image)>::type>::type;
263  auto writer = ImageFileWriter<ImageType>::New();
264  writer->SetInput(image);
265  writer->SetFileName(filename);
266  writer->SetUseCompression(compress);
267  writer->Update();
268 }
269 
270 } // end namespace itk
271 
272 #ifndef ITK_MANUAL_INSTANTIATION
273 # include "itkImageFileWriter.hxx"
274 #endif
275 
276 #ifdef ITK_IO_FACTORY_REGISTER_MANAGER
277 # include "itkImageIOFactoryRegisterManager.h"
278 #endif
279 
280 #endif // itkImageFileWriter_h
itk::ImageFileWriter::SetImageIO
void SetImageIO(ImageIOBase *io)
Definition: itkImageFileWriter.h:138
itk::ImageIOBase
Abstract superclass defines image IO interface.
Definition: itkImageIOBase.h:77
itk::ImageFileWriter::m_FileName
std::string m_FileName
Definition: itkImageFileWriter.h:232
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:59
itk::ImageFileWriter::InputImageType
TInputImage InputImageType
Definition: itkImageFileWriter.h:106
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itkMacro.h
itk::ImageFileWriter::m_ImageIO
ImageIOBase::Pointer m_ImageIO
Definition: itkImageFileWriter.h:234
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:138
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