go home Home | Main Page | Topics | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
Loading...
Searching...
No Matches
itkImageFileCastWriter.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
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 itkImageFileCastWriter_h
19#define itkImageFileCastWriter_h
20
21#include "itkImageFileWriter.h"
22#include "itkImageIOBase.h"
23#include "itkMacro.h"
24#include "itkSize.h"
25#include "itkImageIORegion.h"
26#include "itkCastImageFilter.h"
27#include "elxDefaultConstruct.h"
29
30namespace itk
31{
32
41template <typename TInputImage>
42class ITK_TEMPLATE_EXPORT ImageFileCastWriter : public ImageFileWriter<TInputImage>
43{
44public:
46
50 using Pointer = SmartPointer<Self>;
51 using ConstPointer = SmartPointer<const Self>;
52
54 itkNewMacro(Self);
55
58
60 using typename Superclass::InputImageType;
61 using typename Superclass::InputImagePointer;
62 using typename Superclass::InputImageRegionType;
63 using typename Superclass::InputImagePixelType;
64
65 itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
66
70 itkSetStringMacro(OutputComponentType);
71 itkGetStringMacro(OutputComponentType);
72
73protected:
75 ~ImageFileCastWriter() override = default;
76
78 void
79 GenerateData() override;
80
81private:
83 static std::string
85
90 template <typename OutputComponentType>
91 const void *
92 ConvertScalarImage(const DataObject & inputImage)
93 {
94 using DiskImageType = Image<OutputComponentType, InputImageDimension>;
95 using InputImageComponentType = typename PixelTraits<InputImagePixelType>::ValueType;
96 using ScalarInputImageType = Image<InputImageComponentType, InputImageDimension>;
97
99 this->GetModifiableImageIO()->SetPixelTypeInfo(static_cast<const OutputComponentType *>(nullptr));
100
102 const auto caster = CastImageFilter<ScalarInputImageType, DiskImageType>::New();
103 m_Caster = caster;
104 const auto localInputImage = ScalarInputImageType::New();
105
106 localInputImage->Graft(static_cast<const ScalarInputImageType *>(&inputImage));
107
108 caster->SetInput(localInputImage);
109 caster->Update();
110
112 return caster->GetOutput()->GetBufferPointer();
113 }
114
115
120 template <typename TResultPixel>
121 bool
122 ConvertScalarImageIfPixelTypesMatch(const DataObject & inputImage, const void *& outputDataBuffer)
123 {
125 {
126 outputDataBuffer = ConvertScalarImage<TResultPixel>(inputImage);
127 return true;
128 }
129 return false;
130 }
131
133 template <typename... TResultPixel>
134 const void *
136 {
137 if (const void * outputDataBuffer = nullptr;
138 (ConvertScalarImageIfPixelTypesMatch<TResultPixel>(inputImage, outputDataBuffer) || ...))
139 {
140 return outputDataBuffer;
141 }
142 return nullptr;
143 }
144
145 ProcessObject::Pointer m_Caster{ nullptr };
146
148};
149
151template <typename TImage>
152void
153WriteCastedImage(const TImage & image,
154 const std::string & filename,
155 const std::string & outputComponentType,
156 bool compress)
157{
159 writer.SetInput(&image);
160 writer.SetFileName(filename);
161 writer.SetOutputComponentType(outputComponentType);
162 writer.SetUseCompression(compress);
163 writer.Update();
164}
165
166} // end namespace itk
167
168#ifndef ITK_MANUAL_INSTANTIATION
169# include "itkImageFileCastWriter.hxx"
170#endif
171
172#endif // itkImageFileCastWriter_h
ITK_DISALLOW_COPY_AND_MOVE(ImageFileCastWriter)
ImageFileWriter< TInputImage > Superclass
ProcessObject::Pointer m_Caster
const void * ConvertScalarImage(const DataObject &inputImage)
~ImageFileCastWriter() override=default
bool ConvertScalarImageIfPixelTypesMatch(const DataObject &inputImage, const void *&outputDataBuffer)
SmartPointer< const Self > ConstPointer
const void * ConvertScalarImageAsSpecifiedByFixedWidthPixelType(const DataObject &inputImage)
itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension)
void GenerateData() override
itkOverrideGetNameOfClassMacro(ImageFileCastWriter)
static std::string GetDefaultOutputComponentType()
std::string PixelTypeToFixedWidthString()
void WriteCastedImage(const TImage &image, const std::string &filename, const std::string &outputComponentType, bool compress)


Generated on 1774142652 for elastix by doxygen 1.15.0 elastix logo