DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkVariableLengthVectorImageFileWriter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Variable Length 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 __itkVariableLengthVectorImageFileWriter_hxx
15 #define __itkVariableLengthVectorImageFileWriter_hxx
16 
17 #include <fstream>
19 #include "itksys/SystemTools.hxx"
20 #include "itkImageRegionConstIterator.h"
21 #include "itkImageRegionIterator.h"
22 
23 
24 namespace itk
25 {
26 
27 //---------------------------------------------------------
28 template <class TInputImage>
31 {
32  m_FileName = "";
33  m_UseCompression = true;
34 // m_UseInputMetaDataDictionary = true;
35 }
36 
37 //---------------------------------------------------------
38 template <class TInputImage>
41 {
42 }
43 
44 //---------------------------------------------------------
45 template <class TInputImage>
46 void
49 {
50  // ProcessObject is not const_correct so this cast is required here.
51  this->ProcessObject::SetNthInput(0,
52  const_cast<TInputImage *>(input ) );
53 }
54 
55 
56 //---------------------------------------------------------
57 template <class TInputImage>
61 {
62  if (this->GetNumberOfInputs() < 1)
63  {
64  return 0;
65  }
66 
67  return static_cast<TInputImage*>
68  (this->ProcessObject::GetInput(0));
69 }
70 
71 //---------------------------------------------------------
72 template <class TInputImage>
75 ::GetInput(unsigned int idx)
76 {
77  return static_cast<TInputImage*> (this->ProcessObject::GetInput(idx));
78 }
79 
80 //---------------------------------------------------------
81 template <class TInputImage>
82 void
85 {
86  const InputImageType * input = this->GetInput();
87 
88  itkDebugMacro( <<"Writing an image file" );
89 
90  // Make sure input is available
91  if ( input == 0 )
92  {
93  itkExceptionMacro( << "No input to writer!" );
94  }
95 
96  if ( m_FileName == "" )
97  {
98  itkExceptionMacro( <<"No filename specified" );
99  }
100 
101  // write the data
102  this->GenerateData();
103 }
104 
105 
106 //---------------------------------------------------------
107 template <class TInputImage>
108 void
111 {
112  itkDebugMacro ( << "VariableLengthVectorImageFileWriter::GenerateData() \n" );
113 
114  if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4, ".vlv")!=0)
115  {
116  itkExceptionMacro( << "the file " << m_FileName << " should be a text file ending with '.vlv'.");
117  }
118 
119  // Setup - Input Image
120  InputImageType * input = const_cast<InputImageType*>(this->GetInput());
121 
122  // Iterator
123  ImageRegionConstIterator<InputImageType>
124  inputIt( input, input->GetLargestPossibleRegion() );
125 
126  // Compute some image statistics
127  inputIt.GoToBegin();
128  unsigned long totalLength = 0;
129 
130  while( !inputIt.IsAtEnd() )
131  {
132  totalLength += inputIt.Get().Size();
133  ++inputIt;
134  }
135 
136  // Setup output Images
137  m_LengthImage = LengthImageType::New();
138  m_LengthImage->CopyInformation(input);
139  m_LengthImage->SetRegions(input->GetLargestPossibleRegion());
140  m_LengthImage->Allocate();
141 
142  typename ValueImageType::IndexType startIndex;
143  typename ValueImageType::RegionType region;
144  typename ValueImageType::SizeType size;
145  typename ValueImageType::SpacingType spacing;
146 
147  startIndex.Fill(0);
148  spacing.Fill(1);
149  size[0] = totalLength;
150  region.SetIndex(startIndex);
151  region.SetSize(size);
152 
153  m_ValueImage = ValueImageType::New();
154  m_ValueImage->SetSpacing(spacing);
155  m_ValueImage->SetRegions(region);
156  m_ValueImage->Allocate();
157 
158  // More iterators
159  ImageRegionIterator<LengthImageType>
160  lengthIt( m_LengthImage, m_LengthImage->GetRequestedRegion() );
161  ImageRegionIterator<ValueImageType>
162  valueIt( m_ValueImage, m_ValueImage->GetRequestedRegion() );
163 
164  lengthIt.GoToBegin();
165  valueIt.GoToBegin();
166 
167  // Populate data
168  inputIt.GoToBegin();
169  while ( !inputIt.IsAtEnd() )
170  {
171  InputImagePixelType inputPixel = inputIt.Get();
172  LengthType length = static_cast<LengthType>(inputPixel.Size());
173  lengthIt.Set(length);
174  for (LengthType k=0; k<length; k++)
175  {
176  valueIt.Set(inputPixel[k]);
177  ++valueIt;
178  }
179  ++inputIt;
180  ++lengthIt;
181  }
182 
183  // Process file names
184  std::string baseFileName = "";
185  std::string fileNameExtension;
186  std::string dataFileNameExtension = "nrrd";
187 
188  std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
189  std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
190 
191  std::string::size_type idx;
192  idx = fileName.find_last_of('.');
193 
194  if (idx != std::string::npos)
195  {
196  fileNameExtension = fileName.substr(idx + 1);
197  if (fileNameExtension != "vlv")
198  {
199  std::cout << "Renaming extension to .vlv" << std::endl;
200  fileNameExtension = "vlv";
201  }
202  baseFileName = fileName.substr(0, idx);
203  }
204  std::string lengthFileName = baseFileName + "_length." + dataFileNameExtension;
205  std::string valueFileName = baseFileName + "_value." + dataFileNameExtension;
206  std::string headerFileName = baseFileName + "." + fileNameExtension;
207 
208  // Write files
209  m_LengthImageFileWriter = LengthImageFileWriterType::New();
210  m_ValueImageFileWriter = ValueImageFileWriterType::New();
211 
212  std::string lengthPathName = lengthFileName;
213  std::string valuePathName = valueFileName;
214  std::string headerPathName = headerFileName;
215 
216  if ( pathName != "" )
217  {
218  lengthPathName = pathName + "/" + lengthFileName;
219  valuePathName = pathName + "/" + valueFileName;
220  headerPathName = pathName + "/" + headerFileName;
221  }
222 
223  m_LengthImageFileWriter->SetFileName(lengthPathName);
224  m_LengthImageFileWriter->SetInput(m_LengthImage);
225  m_ValueImageFileWriter->SetFileName(valuePathName);
226  m_ValueImageFileWriter->SetInput(m_ValueImage);
227 
228  m_LengthImageFileWriter->SetUseCompression(this->m_UseCompression);
229  m_ValueImageFileWriter->SetUseCompression(this->m_UseCompression);
230 // m_LengthImageFileWriter->SetUseInputMetaDataDictionary(this->m_UseInputMetaDataDictionary);
231 // m_ValueImageFileWriter->SetUseInputMetaDataDictionary(this->m_UseInputMetaDataDictionary);
232 
233  m_LengthImageFileWriter->Update();
234  m_ValueImageFileWriter->Update();
235 
236  // Write header
237  std::ofstream outfile;
238 // std::string HeaderFileName = GetHeaderFileName();
239 // std::cout << HeaderFileName << std::endl;
240 
241  outfile.open(headerPathName.c_str(), std::fstream::out);
242 
243  InputImageRegionType outputRegion = input->GetLargestPossibleRegion();
244  InputImageSizeType outputSize = outputRegion.GetSize();
245  InputImageSpacingType outputSpacing = input->GetSpacing();
246  InputImagePointType outputOrigin = input->GetOrigin();
247  InputImageDirectionType outputDirection = input->GetDirection();
248 
249  outfile << "NDims = " << input->GetImageDimension() << std::endl;
250  outfile << "DimSize = ";
251 // outfile << input->GetNumberOfComponentsPerPixel() << " ";
252  for (unsigned int d=0; d<input->GetImageDimension(); d++)
253  {
254  outfile << outputSize[d] << " ";
255  }
256  outfile << std::endl;
257 
258  outfile << "ElementSpacing = ";
259 // outfile << "1" << " ";
260  for (unsigned int d=0; d<input->GetImageDimension(); d++)
261  {
262  outfile << outputSpacing[d] << " ";
263  }
264  outfile << std::endl;
265 
266  outfile << "Offset = ";
267 // outfile << "0" << " ";
268  for (unsigned int d=0; d<input->GetImageDimension(); d++)
269  {
270  outfile << outputOrigin[d] << " ";
271  }
272  outfile << std::endl;
273 
274  outfile << "TransformMatrix = ";
275  for (unsigned int d1=0; d1<input->GetImageDimension(); d1++)
276  {
277  for (unsigned int d2=0; d2<input->GetImageDimension(); d2++)
278  outfile << outputDirection(d1,d2) << " ";
279  }
280  outfile << std::endl;
281 
282  outfile << "LengthElementDataFile = " << lengthFileName << std::endl;
283  outfile << "ValueElementDataFile = " << valueFileName << std::endl;
284 
285  outfile.close();
286 }
287 
288 
289 //---------------------------------------------------------
290 template <class TInputImage>
291 void
293 ::PrintSelf(std::ostream& os, Indent indent) const
294 {
295  Superclass::PrintSelf(os,indent);
296 
297  os << indent << "File Name: "
298  << (m_FileName.data() ? m_FileName.data() : "(none)") << std::endl;
299 
300  if (m_UseCompression)
301  {
302  os << indent << "Compression: On\n";
303  }
304  else
305  {
306  os << indent << "Compression: Off\n";
307  }
308 
309 // if (m_UseInputMetaDataDictionary)
310 // {
311 // os << indent << "UseInputMetaDataDictionary: On\n";
312 // }
313 // else
314 // {
315 // os << indent << "UseInputMetaDataDictionary: Off\n";
316 // }
317 
318 }
319 
320 } // end namespace itk
321 
322 #endif
void PrintSelf(std::ostream &os, Indent indent) const