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
27namespace itk
28{
34class ITKIOImageBase_EXPORT ImageFileWriterException : public ExceptionObject
35{
36public:
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};
89template <typename TInputImage>
90class ITK_TEMPLATE_EXPORT ImageFileWriter : public ProcessObject
91{
92public:
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 *
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
161
164 void
165 SetIORegion(const ImageIORegion & region);
166
167 const ImageIORegion &
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);
223protected:
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
233private:
234 std::string m_FileName{};
235
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
254template <typename TImagePointer>
255ITK_TEMPLATE_EXPORT void
256WriteImage(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
Base exception class for IO problems during writing.
~ImageFileWriterException() noexcept override
ImageFileWriterException(const std::string &file, unsigned int line, const char *message="Error in IO", const char *loc="Unknown")
ImageFileWriterException(const char *file, unsigned int line, const char *message="Error in IO", const char *loc="Unknown")
ITK_DEFAULT_COPY_AND_MOVE(ImageFileWriterException)
Writes image data to a single file.
void PrintSelf(std::ostream &os, Indent indent) const override
void SetIORegion(const ImageIORegion &region)
~ImageFileWriter() override=default
void SetInput(const InputImageType *input)
void SetImageIO(ImageIOBase *io)
void GenerateData() override
typename InputImageType::PixelType InputImagePixelType
const InputImageType * GetInput(unsigned int idx)
ImageFileWriter()=default
const ImageIORegion & GetIORegion() const
virtual void Write()
static Pointer New()
const InputImageType * GetInput()
typename InputImageType::Pointer InputImagePointer
typename InputImageType::RegionType InputImageRegionType
void UpdateLargestPossibleRegion() override
Writes the entire image to file.
Abstract superclass defines image IO interface.
An ImageIORegion represents a structured region of data.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)