DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkSpatiallyDenseSparseVectorImageFileWriter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Spatially Dense Sparse 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 __itkSpatiallyDenseSparseVectorImageFileWriter_hxx
15 #define __itkSpatiallyDenseSparseVectorImageFileWriter_hxx
16 
17 #include <fstream>
18 #include "itkImageRegionIteratorWithIndex.h"
20 #include "itksys/SystemTools.hxx"
21 
22 namespace itk
23 {
24 
25 //---------------------------------------------------------
26 template <class TInputImage>
29 {
30  m_FileName = "";
31  m_UseCompression = true;
32 // m_UseInputMetaDataDictionary = true;
33 }
34 
35 //---------------------------------------------------------
36 template <class TInputImage>
39 {
40 }
41 
42 //---------------------------------------------------------
43 template <class TInputImage>
44 void
47 {
48  // ProcessObject is not const_correct so this cast is required here.
49  this->ProcessObject::SetNthInput(0,
50  const_cast<TInputImage *>(input ) );
51 }
52 
53 
54 //---------------------------------------------------------
55 template <class TInputImage>
59 {
60  if (this->GetNumberOfInputs() < 1)
61  {
62  return 0;
63  }
64 
65  return static_cast<TInputImage*>
66  (this->ProcessObject::GetInput(0));
67 }
68 
69 //---------------------------------------------------------
70 template <class TInputImage>
73 ::GetInput(unsigned int idx)
74 {
75  return static_cast<TInputImage*> (this->ProcessObject::GetInput(idx));
76 }
77 
78 //---------------------------------------------------------
79 template <class TInputImage>
80 void
83 {
84  const InputImageType * input = this->GetInput();
85 
86  itkDebugMacro( <<"Writing an image file" );
87 
88  // Make sure input is available
89  if ( input == 0 )
90  {
91  itkExceptionMacro(<< "No input to writer!");
92  }
93 
94  if ( m_FileName == "" )
95  {
96  itkExceptionMacro(<<"No filename specified");
97  }
98 
99  // write the data
100  this->GenerateData();
101 }
102 
103 
104 //---------------------------------------------------------
105 template <class TInputImage>
106 void
109 {
110  itkDebugMacro ( << "SpatiallyDenseSparseVectorImageFileWriter::GenerateData() \n" );
111 
112  if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4, ".spr")!=0)
113  {
114  itkExceptionMacro( << "the file " << m_FileName << " should be a text file ending with '.spr'.");
115  }
116 
117  // Setup - Input Image
118  InputImageType * input = const_cast<InputImageType*>(this->GetInput());
119 
120  // Total number of elements
121  ImageRegionIteratorWithIndex<InputImageType>
122  inputImageIterator( input, input->GetRequestedRegion() );
123 
124  inputImageIterator.GoToBegin();
125 
126  SizeValueType totalElements = 0;
127  while ( !inputImageIterator.IsAtEnd() )
128  {
129  totalElements += (input->GetInternalPixel(inputImageIterator.GetIndex())).GetSize();
130  ++inputImageIterator;
131  }
132 
133  // Vector length
134  unsigned int numberOfComponentsPerPixel = input->GetNumberOfComponentsPerPixel();
135 
136  // Setup - Output Images
137  m_KeyImage = KeyImageType::New();
138  m_ValueImage = ValueImageType::New();
139 
140  typename KeyImageType::IndexType startIndex;
141  typename KeyImageType::RegionType region;
142  typename KeyImageType::SizeType size;
143  typename KeyImageType::SpacingType spacing;
144 
145  startIndex.Fill(0);
146 
147  if ( totalElements > 0 )
148  {
149  size[0] = totalElements;
150  }
151  else
152  {
153  size[0] = 1; // Output at least one voxel.
154  }
155 
156  spacing.Fill(1);
157 
158  region.SetIndex(startIndex);
159  region.SetSize(size);
160 
161  m_KeyImage->SetSpacing(spacing);
162  m_KeyImage->SetRegions(region);
163  m_ValueImage->SetSpacing(spacing);
164  m_ValueImage->SetRegions(region);
165 
166  m_KeyImage->Allocate();
167  m_ValueImage->Allocate();
168  m_KeyImage->FillBuffer(static_cast<InputImageKeyType>(0));
169  m_ValueImage->FillBuffer(static_cast<InputImageValueType>(0));
170 
171  // Iterators
172  ImageRegionIteratorWithIndex<KeyImageType>
173  keyImageIterator( m_KeyImage, m_KeyImage->GetRequestedRegion() );
174  ImageRegionIteratorWithIndex<ValueImageType>
175  valueImageIterator( m_ValueImage, m_ValueImage->GetRequestedRegion() );
176 
177  keyImageIterator.GoToBegin();
178  valueImageIterator.GoToBegin();
179  inputImageIterator.GoToBegin();
180 
181  InputImageIndexType index;
182  SizeValueType offset;
183 
184  if ( totalElements > 0 )
185  {
186  while ( !inputImageIterator.IsAtEnd() )
187  {
188  index = inputImageIterator.GetIndex();
189 
190  const InputImagePixelPixelMapType *internalData =
191  (input->GetInternalPixel(index)).GetDataPointer();
192  InputImagePixelMapConstIteratorType iterator = internalData->begin();
193 
194  offset = input->ComputeOffset(index) * numberOfComponentsPerPixel;
195 
196  while ( iterator != internalData->end() )
197  {
198  keyImageIterator.Set(static_cast<InputImageKeyType>(iterator->first + offset));
199  valueImageIterator.Set(iterator->second);
200  ++iterator;
201 
202  ++keyImageIterator;
203  ++valueImageIterator;
204  }
205  ++inputImageIterator;
206  }
207  }
208  else
209  {
210  keyImageIterator.Set(static_cast<InputImageKeyType>(0));
211  valueImageIterator.Set(0);
212  }
213 
214  // Process File Names
215  std::string baseFileName = "";
216  std::string fileNameExtension;
217  std::string dataFileNameExtension = "nrrd";
218 
219  std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
220  std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
221 
222  std::string::size_type idx;
223  idx = fileName.find_last_of('.');
224 
225  if (idx != std::string::npos)
226  {
227  fileNameExtension = fileName.substr(idx + 1);
228  if (fileNameExtension != "spr")
229  {
230  std::cout << "Renaming extension to .spr" << std::endl;
231  fileNameExtension = "spr";
232  }
233  baseFileName = fileName.substr(0, idx);
234  }
235  std::string keyFileName = baseFileName + "_key." + dataFileNameExtension;
236  std::string valueFileName = baseFileName + "_value." + dataFileNameExtension;
237  std::string headerFileName = baseFileName + "." + fileNameExtension;
238 
239  // Write Files
240  m_KeyImageFileWriter = KeyImageFileWriterType::New();
241  m_ValueImageFileWriter = ValueImageFileWriterType::New();
242 
243  std::string keyPathName = keyFileName;
244  std::string valuePathName = valueFileName;
245  std::string headerPathName = headerFileName;
246 
247  if ( pathName != "" )
248  {
249  keyPathName = pathName + "/" + keyFileName;
250  valuePathName = pathName + "/" + valueFileName;
251  headerPathName = pathName + "/" + headerFileName;
252  }
253 
254  m_KeyImageFileWriter->SetFileName(keyPathName);
255  m_KeyImageFileWriter->SetInput(m_KeyImage);
256  m_ValueImageFileWriter->SetFileName(valuePathName);
257  m_ValueImageFileWriter->SetInput(m_ValueImage);
258 
259  m_KeyImageFileWriter->SetUseCompression(this->m_UseCompression);
260  m_ValueImageFileWriter->SetUseCompression(this->m_UseCompression);
261 // m_KeyImageFileWriter->SetUseInputMetaDataDictionary(this->m_UseInputMetaDataDictionary);
262 // m_ValueImageFileWriter->SetUseInputMetaDataDictionary(this->m_UseInputMetaDataDictionary);
263 
264  m_KeyImageFileWriter->Update();
265  m_ValueImageFileWriter->Update();
266 
267  // Write Header
268  std::ofstream outfile;
269 // std::string HeaderFileName = GetHeaderFileName();
270 // std::cout << HeaderFileName << std::endl;
271 
272  outfile.open(headerPathName.c_str(), std::fstream::out);
273 
274  InputImageRegionType outputRegion = input->GetLargestPossibleRegion();
275  InputImageSizeType outputSize = outputRegion.GetSize();
276  InputImageSpacingType outputSpacing = input->GetSpacing();
277  InputImagePointType outputOrigin = input->GetOrigin();
278  InputImageDirectionType outputDirection = input->GetDirection();
279 
280  unsigned int dim = input->GetImageDimension();
281  outfile << "NDims = " << dim + 1 << std::endl;
282  outfile << "DimSize = ";
283  for (unsigned int d=0; d<dim; d++)
284  {
285  outfile << outputSize[d] << " ";
286  }
287  outfile << input->GetNumberOfComponentsPerPixel() << " ";
288  outfile << std::endl;
289 
290  outfile << "ElementSpacing = ";
291  for (unsigned int d=0; d<dim; d++)
292  {
293  outfile << outputSpacing[d] << " ";
294  }
295  outfile << "1" << " ";
296  outfile << std::endl;
297 
298  outfile << "Offset = ";
299  for (unsigned int d=0; d<dim; d++)
300  {
301  outfile << outputOrigin[d] << " ";
302  }
303  outfile << "0" << " ";
304  outfile << std::endl;
305 
306  outfile << "TransformMatrix = ";
307  for (unsigned int d1=0; d1 < dim + 1; d1++)
308  {
309  for (unsigned int d2=0; d2 < dim + 1; d2++)
310  {
311  if ((d1 == dim) && (d2 == dim))
312  {
313  outfile << "1" << " ";
314  }
315  else if ((d1 == dim) && (d2 != dim))
316  {
317  outfile << "0" << " ";
318  }
319  else if ((d2 == dim) && (d1 != dim))
320  {
321  outfile << "0" << " ";
322  }
323  else
324  {
325  outfile << outputDirection(d1,d2) << " ";
326  }
327  }
328  }
329  outfile << std::endl;
330 
331  outfile << "KeyElementDataFile = " << keyFileName << std::endl;
332  outfile << "ValueElementDataFile = " << valueFileName << std::endl;
333 
334  outfile.close();
335 }
336 
337 
338 //---------------------------------------------------------
339 template <class TInputImage>
340 void
342 ::PrintSelf(std::ostream& os, Indent indent) const
343 {
344  Superclass::PrintSelf(os,indent);
345 
346  os << indent << "File Name: "
347  << (m_FileName.data() ? m_FileName.data() : "(none)") << std::endl;
348 
349  if (m_UseCompression)
350  {
351  os << indent << "Compression: On\n";
352  }
353  else
354  {
355  os << indent << "Compression: Off\n";
356  }
357 
358 // if (m_UseInputMetaDataDictionary)
359 // {
360 // os << indent << "UseInputMetaDataDictionary: On\n";
361 // }
362 // else
363 // {
364 // os << indent << "UseInputMetaDataDictionary: Off\n";
365 // }
366 
367 }
368 
369 } // end namespace itk
370 
371 #endif
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE