DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkCastImageFileWriter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Cast Image File Writer
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 #ifndef __itkCastImageFileWriter_hxx
14 #define __itkCastImageFileWriter_hxx
15 
16 #include "itkImageFileWriter.h"
17 #include "itkDataObject.h"
18 #include "itkObjectFactoryBase.h"
19 #include "itkImageIOFactory.h"
20 #include "itkCommand.h"
21 #include "itkImage.h"
22 #include "itkCastImageFilter.h"
23 
24 namespace itk
25 {
26 
27 //---------------------------------------------------------
28 template <class TInputImage>
31  m_PasteIORegion(TInputImage::ImageDimension)
32 {
33  m_UseCompression = false;
34  m_UseInputMetaDataDictionary = true;
35  m_FactorySpecifiedImageIO = false;
36  m_UserSpecifiedIORegion = false;
37  m_UserSpecifiedImageIO = false;
38  m_NumberOfStreamDivisions = 1;
39  m_ComponentType = ImageIOBase::DOUBLE;
40 }
41 
42 //---------------------------------------------------------
43 template <class TInputImage>
46 {
47 }
48 
49 //---------------------------------------------------------
50 template <class TInputImage>
51 void
54 {
55  // ProcessObject is not const_correct so this cast is required here.
56  this->ProcessObject::SetNthInput(0,
57  const_cast<TInputImage *>(input ) );
58 }
59 
60 
61 //---------------------------------------------------------
62 template <class TInputImage>
66 {
67  if (this->GetNumberOfInputs() < 1)
68  {
69  return 0;
70  }
71 
72  return static_cast<TInputImage*>
73  (this->ProcessObject::GetInput(0));
74 }
75 
76 //---------------------------------------------------------
77 template <class TInputImage>
80 ::GetInput(unsigned int idx)
81 {
82  return static_cast<TInputImage*> (this->ProcessObject::GetInput(idx));
83 }
84 
85 //---------------------------------------------------------
86 template <class TInputImage>
87 void
89 ::SetIORegion (const ImageIORegion& region)
90 {
91  itkDebugMacro("setting IORegion to " << region );
92  if ( m_PasteIORegion != region)
93  {
94  m_PasteIORegion = region;
95  this->Modified();
96  m_UserSpecifiedIORegion = true;
97  }
98 }
99 
100 //---------------------------------------------------------
101 template <class TInputImage>
102 void
105 {
106  itkDebugMacro( <<"Writing an image file" );
107  this->GenerateData();
108 }
109 
110 
111 //---------------------------------------------------------
112 template <class TInputImage>
113 void
116 {
117  const InputImageType * inputPtr = this->GetInput();
118 
119  itkDebugMacro(<<"Writing file: " << m_FileName);
120 
121 // if( strcmp( input->GetNameOfClass(), "VectorImage" ) == 0 )
122 
123  // Writer typedefs
124  typedef Image<unsigned char, TInputImage::ImageDimension> UCharImageType;
125  typedef Image<char, TInputImage::ImageDimension> CharImageType;
126  typedef Image<unsigned short, TInputImage::ImageDimension> UShortImageType;
127  typedef Image<short, TInputImage::ImageDimension> ShortImageType;
128  typedef Image<unsigned int, TInputImage::ImageDimension> UIntImageType;
129  typedef Image<int, TInputImage::ImageDimension> IntImageType;
130  typedef Image<unsigned long, TInputImage::ImageDimension> ULongImageType;
131  typedef Image<long, TInputImage::ImageDimension> LongImageType;
132  typedef Image<float, TInputImage::ImageDimension> FloatImageType;
133  typedef Image<double, TInputImage::ImageDimension> DoubleImageType;
134 
135  typedef ImageFileWriter<UCharImageType> UCharWriterType;
136  typedef ImageFileWriter<CharImageType> CharWriterType;
137  typedef ImageFileWriter<UShortImageType> UShortWriterType;
138  typedef ImageFileWriter<ShortImageType> ShortWriterType;
139  typedef ImageFileWriter<UIntImageType> UIntWriterType;
140  typedef ImageFileWriter<IntImageType> IntWriterType;
141  typedef ImageFileWriter<ULongImageType> ULongWriterType;
142  typedef ImageFileWriter<LongImageType> LongWriterType;
143  typedef ImageFileWriter<FloatImageType> FloatWriterType;
144  typedef ImageFileWriter<DoubleImageType> DoubleWriterType;
145 
146  typedef CastImageFilter<TInputImage, UCharImageType> UCharCastFilterType;
147  typedef CastImageFilter<TInputImage, CharImageType> CharCastFilterType;
148  typedef CastImageFilter<TInputImage, UShortImageType> UShortCastFilterType;
149  typedef CastImageFilter<TInputImage, ShortImageType> ShortCastFilterType;
150  typedef CastImageFilter<TInputImage, UIntImageType> UIntCastFilterType;
151  typedef CastImageFilter<TInputImage, IntImageType> IntCastFilterType;
152  typedef CastImageFilter<TInputImage, ULongImageType> ULongCastFilterType;
153  typedef CastImageFilter<TInputImage, LongImageType> LongCastFilterType;
154  typedef CastImageFilter<TInputImage, FloatImageType> FloatCastFilterType;
155  typedef CastImageFilter<TInputImage, DoubleImageType> DoubleCastFilterType;
156 
157  typename UCharWriterType::Pointer ucharWriter;
158  typename CharWriterType::Pointer charWriter;
159  typename UShortWriterType::Pointer ushortWriter;
160  typename ShortWriterType::Pointer shortWriter;
161  typename UIntWriterType::Pointer uintWriter;
162  typename IntWriterType::Pointer intWriter;
163  typename ULongWriterType::Pointer ulongWriter;
164  typename LongWriterType::Pointer longWriter;
165  typename FloatWriterType::Pointer floatWriter;
166  typename DoubleWriterType::Pointer doubleWriter;
167 
168  typename UCharCastFilterType::Pointer ucharCastFilter;
169  typename CharCastFilterType::Pointer charCastFilter;
170  typename UShortCastFilterType::Pointer ushortCastFilter;
171  typename ShortCastFilterType::Pointer shortCastFilter;
172  typename UIntCastFilterType::Pointer uintCastFilter;
173  typename IntCastFilterType::Pointer intCastFilter;
174  typename ULongCastFilterType::Pointer ulongCastFilter;
175  typename LongCastFilterType::Pointer longCastFilter;
176  typename FloatCastFilterType::Pointer floatCastFilter;
177  typename DoubleCastFilterType::Pointer doubleCastFilter;
178 
179  switch ( m_ComponentType )
180  {
181  case ImageIOBase::UCHAR:
182  ucharWriter = UCharWriterType::New();
183  ucharCastFilter = UCharCastFilterType::New();
184  ucharWriter->SetFileName( m_FileName );
185  ucharCastFilter->SetInput( inputPtr );
186  ucharWriter->SetInput( ucharCastFilter->GetOutput() );
187  ucharWriter->SetImageIO( this->GetImageIO() );
188  ucharWriter->SetIORegion( this->GetIORegion() );
189  ucharWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
190  ucharWriter->SetUseCompression( this->GetUseCompression() );
191  ucharWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
192  ucharWriter->Update();
193  break;
194  case ImageIOBase::CHAR:
195  charWriter = CharWriterType::New();
196  charCastFilter = CharCastFilterType::New();
197  charWriter->SetFileName( m_FileName );
198  charCastFilter->SetInput( inputPtr );
199  charWriter->SetInput( charCastFilter->GetOutput() );
200  charWriter->SetImageIO( this->GetImageIO() );
201  charWriter->SetIORegion( this->GetIORegion() );
202  charWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
203  charWriter->SetUseCompression( this->GetUseCompression() );
204  charWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
205  charWriter->Update();
206  break;
207  case ImageIOBase::USHORT:
208  ushortWriter = UShortWriterType::New();
209  ushortCastFilter = UShortCastFilterType::New();
210  ushortWriter->SetFileName( m_FileName );
211  ushortCastFilter->SetInput( inputPtr );
212  ushortWriter->SetInput( ushortCastFilter->GetOutput() );
213  ushortWriter->SetImageIO( this->GetImageIO() );
214  ushortWriter->SetIORegion( this->GetIORegion() );
215  ushortWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
216  ushortWriter->SetUseCompression( this->GetUseCompression() );
217  ushortWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
218  ushortWriter->Update();
219  break;
220  case ImageIOBase::SHORT:
221  shortWriter = ShortWriterType::New();
222  shortCastFilter = ShortCastFilterType::New();
223  shortWriter->SetFileName( m_FileName );
224  shortCastFilter->SetInput( inputPtr );
225  shortWriter->SetInput( shortCastFilter->GetOutput() );
226  shortWriter->SetImageIO( this->GetImageIO() );
227  shortWriter->SetIORegion( this->GetIORegion() );
228  shortWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
229  shortWriter->SetUseCompression( this->GetUseCompression() );
230  shortWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
231  shortWriter->Update();
232  break;
233  case ImageIOBase::UINT:
234  uintWriter = UIntWriterType::New();
235  uintCastFilter = UIntCastFilterType::New();
236  uintWriter->SetFileName( m_FileName );
237  uintCastFilter->SetInput( inputPtr );
238  uintWriter->SetInput( uintCastFilter->GetOutput() );
239  uintWriter->SetImageIO( this->GetImageIO() );
240  uintWriter->SetIORegion( this->GetIORegion() );
241  uintWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
242  uintWriter->SetUseCompression( this->GetUseCompression() );
243  uintWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
244  uintWriter->Update();
245  break;
246  case ImageIOBase::INT:
247  intWriter = IntWriterType::New();
248  intCastFilter = IntCastFilterType::New();
249  intWriter->SetFileName( m_FileName );
250  intCastFilter->SetInput( inputPtr );
251  intWriter->SetInput( intCastFilter->GetOutput() );
252  intWriter->SetImageIO( this->GetImageIO() );
253  intWriter->SetIORegion( this->GetIORegion() );
254  intWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
255  intWriter->SetUseCompression( this->GetUseCompression() );
256  intWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
257  intWriter->Update();
258  break;
259  case ImageIOBase::ULONG:
260  ulongWriter = ULongWriterType::New();
261  ulongCastFilter = ULongCastFilterType::New();
262  ulongWriter->SetFileName( m_FileName );
263  ulongCastFilter->SetInput( inputPtr );
264  ulongWriter->SetInput( ulongCastFilter->GetOutput() );
265  ulongWriter->SetImageIO( this->GetImageIO() );
266  ulongWriter->SetIORegion( this->GetIORegion() );
267  ulongWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
268  ulongWriter->SetUseCompression( this->GetUseCompression() );
269  ulongWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
270  ulongWriter->Update();
271  break;
272  case ImageIOBase::LONG:
273  longWriter = LongWriterType::New();
274  longCastFilter = LongCastFilterType::New();
275  longWriter->SetFileName( m_FileName );
276  longCastFilter->SetInput( inputPtr );
277  longWriter->SetInput( longCastFilter->GetOutput() );
278  longWriter->SetImageIO( this->GetImageIO() );
279  longWriter->SetIORegion( this->GetIORegion() );
280  longWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
281  longWriter->SetUseCompression( this->GetUseCompression() );
282  longWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
283  longWriter->Update();
284  break;
285  case ImageIOBase::FLOAT:
286  floatWriter = FloatWriterType::New();
287  floatCastFilter = FloatCastFilterType::New();
288  floatWriter->SetFileName( m_FileName );
289  floatCastFilter->SetInput( inputPtr );
290  floatWriter->SetInput( floatCastFilter->GetOutput() );
291  floatWriter->SetImageIO( this->GetImageIO() );
292  floatWriter->SetIORegion( this->GetIORegion() );
293  floatWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
294  floatWriter->SetUseCompression( this->GetUseCompression() );
295  floatWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
296  floatWriter->Update();
297  break;
298  case ImageIOBase::DOUBLE:
299  doubleWriter = DoubleWriterType::New();
300  doubleCastFilter = DoubleCastFilterType::New();
301  doubleWriter->SetFileName( m_FileName );
302  doubleCastFilter->SetInput( inputPtr );
303  doubleWriter->SetInput( doubleCastFilter->GetOutput() );
304  doubleWriter->SetImageIO( this->GetImageIO() );
305  doubleWriter->SetIORegion( this->GetIORegion() );
306  doubleWriter->SetNumberOfStreamDivisions( this->GetNumberOfStreamDivisions() );
307  doubleWriter->SetUseCompression( this->GetUseCompression() );
308  doubleWriter->SetUseInputMetaDataDictionary( this->GetUseInputMetaDataDictionary() );
309  doubleWriter->Update();
310  break;
311  default:
312  break;
313  }
314 
315 }
316 
317 
318 //---------------------------------------------------------
319 template <class TInputImage>
320 void
322 ::PrintSelf(std::ostream& os, Indent indent) const
323 {
324  Superclass::PrintSelf(os,indent);
325 
326  os << indent << "File Name: "
327  << (m_FileName.data() ? m_FileName.data() : "(none)") << std::endl;
328 
329  os << indent << "Image IO: ";
330  if ( m_ImageIO.IsNull() )
331  {
332  os << "(none)\n";
333  }
334  else
335  {
336  os << m_ImageIO << "\n";
337  }
338 
339  os << indent << "IO Region: " << m_PasteIORegion << "\n";
340  os << indent << "Number of Stream Divisions: " << m_NumberOfStreamDivisions << "\n";
341 
342  if (m_UseCompression)
343  {
344  os << indent << "Compression: On\n";
345  }
346  else
347  {
348  os << indent << "Compression: Off\n";
349  }
350 
351  if (m_UseInputMetaDataDictionary)
352  {
353  os << indent << "UseInputMetaDataDictionary: On\n";
354  }
355  else
356  {
357  os << indent << "UseInputMetaDataDictionary: Off\n";
358  }
359 
360  if (m_FactorySpecifiedImageIO)
361  {
362  os << indent << "FactorySpecifiedmageIO: On\n";
363  }
364  else
365  {
366  os << indent << "FactorySpecifiedmageIO: Off\n";
367  }
368 
369 }
370 
371 } // end namespace itk
372 
373 #endif
void SetIORegion(const ImageIORegion &region)
const InputImageType * GetInput(void)
virtual void SetInput(const InputImageType *input)
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
void GenerateData(void) ITK_OVERRIDE