ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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(std::string file,
44 unsigned int line,
45 std::string message = "Error in IO",
46 std::string location = {})
47 : ExceptionObject(std::move(file), line, std::move(message), std::move(location))
48 {}
49
51 ~ImageFileWriterException() noexcept override;
52};
53
80template <typename TInputImage>
81class ITK_TEMPLATE_EXPORT ImageFileWriter : public ProcessObject
82{
83public:
84 ITK_DISALLOW_COPY_AND_MOVE(ImageFileWriter);
85
91
93 itkNewMacro(Self);
94
96 itkOverrideGetNameOfClassMacro(ImageFileWriter);
97
99 using InputImageType = TInputImage;
100 using InputImagePointer = typename InputImageType::Pointer;
101 using InputImageRegionType = typename InputImageType::RegionType;
102 using InputImagePixelType = typename InputImageType::PixelType;
103
106 void
107 SetInput(const InputImageType * input);
108
109 const InputImageType *
111
112 const InputImageType *
113 GetInput(unsigned int idx);
114
117 itkSetStringMacro(FileName);
118 itkGetStringMacro(FileName);
131 void
133 {
134 if (this->m_ImageIO != io)
135 {
136 this->Modified();
137 this->m_ImageIO = io;
138 }
140 }
141 itkGetModifiableObjectMacro(ImageIO, ImageIOBase);
150 virtual void
152
155 void
156 SetIORegion(const ImageIORegion & region);
157
158 const ImageIORegion &
160 {
161 return m_PasteIORegion;
162 }
163
167 itkSetMacro(NumberOfStreamDivisions, unsigned int);
168 itkGetConstReferenceMacro(NumberOfStreamDivisions, unsigned int);
172 void
173 Update() override
174 {
175 this->Write();
176 }
177
183 void
185 {
186 m_PasteIORegion = ImageIORegion(TInputImage::ImageDimension);
188 this->Write();
189 }
190
193 itkSetMacro(UseCompression, bool);
194 itkGetConstReferenceMacro(UseCompression, bool);
195 itkBooleanMacro(UseCompression);
200 itkSetMacro(CompressionLevel, int);
201 itkGetConstReferenceMacro(CompressionLevel, int);
210 itkSetMacro(UseInputMetaDataDictionary, bool);
211 itkGetConstReferenceMacro(UseInputMetaDataDictionary, bool);
212 itkBooleanMacro(UseInputMetaDataDictionary);
214protected:
215 ImageFileWriter() = default;
216 ~ImageFileWriter() override = default;
217 void
218 PrintSelf(std::ostream & os, Indent indent) const override;
219
221 void
222 GenerateData() override;
223
224private:
225 std::string m_FileName{};
226
229
230 ImageIORegion m_PasteIORegion{ TInputImage::ImageDimension };
231 unsigned int m_NumberOfStreamDivisions{ 1 };
233
234 bool m_FactorySpecifiedImageIO{ false }; // did factory mechanism set the ImageIO?
235 bool m_UseCompression{ false };
238};
239
240
245template <typename TImagePointer>
246ITK_TEMPLATE_EXPORT void
247WriteImage(TImagePointer && image, const std::string & filename, bool compress = false)
248{
249 using NonReferenceImagePointer = std::remove_reference_t<TImagePointer>;
250 static_assert(std::is_pointer_v<NonReferenceImagePointer> || mpl::IsSmartPointer<NonReferenceImagePointer>::Value,
251 "WriteImage requires a raw pointer or SmartPointer.");
252
253 using ImageType = std::remove_const_t<std::remove_reference_t<decltype(*image)>>;
254 auto writer = ImageFileWriter<ImageType>::New();
255 writer->SetInput(image);
256 writer->SetFileName(filename);
257 writer->SetUseCompression(compress);
258 writer->Update();
259}
260
261} // end namespace itk
262
263#ifndef ITK_MANUAL_INSTANTIATION
264# include "itkImageFileWriter.hxx"
265#endif
266
267#if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
268# include "itkImageIOFactoryRegisterManager.h"
269#endif
270
271#endif // itkImageFileWriter_h
Standard exception handling object.
ExceptionObject() noexcept=default
~ImageFileWriterException() noexcept override
ImageFileWriterException(std::string file, unsigned int line, std::string message="Error in IO", std::string location={})
ITK_DEFAULT_COPY_AND_MOVE(ImageFileWriterException)
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)
SmartPointer< const Self > ConstPointer
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()
SmartPointer< Self > Pointer
typename InputImageType::Pointer InputImagePointer
typename InputImageType::RegionType InputImageRegionType
void UpdateLargestPossibleRegion() override
Writes the entire image to file.
Abstract superclass defines image IO interface.
SmartPointer< Self > Pointer
An ImageIORegion represents a structured region of data.
Control indentation during Print() invocation.
Definition itkIndent.h:50
virtual void Modified() const
virtual void SetInput(const DataObjectIdentifierType &key, DataObject *input)
Protected method for setting indexed and named inputs.
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
STL namespace.