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