DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkSpatiallyDenseSparseVectorImageFileReader.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Spatially Dense Sparse Vector Image File Reader
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 __itkSpatiallyDenseSparseVectorImageFileReader_hxx
15 #define __itkSpatiallyDenseSparseVectorImageFileReader_hxx
16 
18 #include "itkImageRegionIterator.h"
19 #include "itksys/SystemTools.hxx"
20 
21 
22 namespace itk
23 {
24 
25 template <class TOutputImage>
28 {
29  m_FileName = "";
30  m_UseStreaming = true;
31  m_ImageIO = 0;
32 }
33 
34 template <class TOutputImage>
37 {
38 }
39 
40 template <class TOutputImage>
42 ::PrintSelf(std::ostream& os, Indent indent) const
43 {
44  Superclass::PrintSelf(os, indent);
45 
46  if (m_ImageIO)
47  {
48  os << indent << "ImageIO: \n";
49  m_ImageIO->Print(os, indent.GetNextIndent());
50  }
51  else
52  {
53  os << indent << "ImageIO: (null)" << "\n";
54  }
55 
56  os << indent << "m_FileName: " << m_FileName << "\n";
57  os << indent << "m_UseStreaming: " << m_UseStreaming << "\n";
58 }
59 
60 template <class TOutputImage>
63 {
64  itkDebugMacro ( << "SpatiallyDenseSparseVectorImageFileReader::GenerateData() \n" );
65 
66  if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4, ".spr")!=0)
67  {
68  itkExceptionMacro( << "the file " << m_FileName << " should be a text file ending with '.spr'.");
69  }
70 
71  std::string keyFileName;
72  std::string valueFileName;
73  char tempLine[256];
74 
75  // Read Header
76  std::ifstream infile;
77  infile.open(m_FileName.c_str(), std::fstream::in);
78 
79  if ( !infile.is_open() )
80  {
81  itkExceptionMacro( << "Cannot open file: " << m_FileName );
82  }
83 
84  std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
85  std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
86 
87  std::string line, extractedLine;
88 
89  OutputImageRegionType outputRegion;
90  OutputImageSizeType outputSize;
91  OutputImageSpacingType outputSpacing;
92  OutputImageIndexType outputStartIndex;
93  OutputImagePointType outputOrigin;
94  OutputImageDirectionType outputDirection;
95 
96  unsigned int ndims = 0;
97  unsigned int numberOfComponentsPerPixel = 0;
98 
99  char * pch;
100  unsigned int idx;
101 
102  while (!infile.eof())
103  {
104  if (infile.getline(tempLine, 256))
105  {
106  line = std::string(tempLine);
107 
108  if (line.find("NDims") != std::string::npos)
109  {
110  extractedLine = line.substr(line.find("=") + 1);
111  pch = strtok(const_cast<char*>(extractedLine.c_str())," ");
112  if (pch != NULL)
113  {
114  ndims = atoi(pch);
115  ndims--;
116  }
117  }
118 
119  if (line.find("DimSize") != std::string::npos)
120  {
121  extractedLine = line.substr(line.find("=") + 1);
122  idx = 0;
123  pch = strtok (const_cast<char*>(extractedLine.c_str())," ");
124  while (pch != NULL)
125  {
126  if ( idx == ImageDimension )
127  {
128  numberOfComponentsPerPixel = atoi(pch);
129  }
130  else
131  {
132  outputSize[idx] = atoi(pch);
133  }
134  idx++;
135  pch = strtok (NULL, " ");
136  }
137  }
138 
139  if (line.find("ElementSpacing") != std::string::npos)
140  {
141  extractedLine = line.substr(line.find("=") + 1);
142  idx = 0;
143  pch = strtok (const_cast<char*>(extractedLine.c_str())," ");
144  while (pch != NULL)
145  {
146  if ( idx < ImageDimension )
147  {
148  outputSpacing[idx] = atof(pch);
149  }
150  idx++;
151  pch = strtok (NULL, " ");
152  }
153  }
154 
155  if (line.find("Offset") != std::string::npos)
156  {
157  extractedLine = line.substr(line.find("=") + 1);
158  idx = 0;
159  pch = strtok (const_cast<char*>(extractedLine.c_str())," ");
160  while (pch != NULL)
161  {
162  if ( idx < ImageDimension )
163  {
164  outputOrigin[idx] = atof(pch);
165  }
166  idx++;
167  pch = strtok (NULL, " ");
168  }
169  }
170 
171  if (line.find("TransformMatrix") != std::string::npos)
172  {
173  extractedLine = line.substr(line.find("=") + 1);
174  idx = 0;
175  pch = strtok (const_cast<char*>(extractedLine.c_str())," ");
176  outputDirection.SetIdentity();
177  while (pch != NULL)
178  {
179  unsigned int row = vcl_floor(static_cast<double>(idx)/static_cast<double>(ImageDimension + 1));
180  unsigned int col = idx % (ImageDimension + 1);
181  if ( (row != ImageDimension) && (col != ImageDimension) )
182  {
183  outputDirection(row, col ) = atof(pch);
184  }
185  idx++;
186  pch = strtok (NULL, " ");
187  }
188  }
189 
190  if (line.find("KeyElementDataFile") != std::string::npos)
191  {
192  extractedLine = line.substr(line.find("=") + 1);
193 
194  const size_t beginStr = extractedLine.find_first_not_of(" \t");
195  if (beginStr == std::string::npos)
196  {
197  // no content
198  std::cerr << "No key file specified!";
199  }
200  const size_t endStr = extractedLine.find_last_not_of(" \t");
201  const size_t range = endStr - beginStr + 1;
202  keyFileName = extractedLine.substr(beginStr, range);
203  if ( pathName != "" )
204  {
205  keyFileName = pathName + "/" + keyFileName;
206  }
207  }
208 
209  if (line.find("ValueElementDataFile") != std::string::npos)
210  {
211  extractedLine = line.substr(line.find("=") + 1);
212 
213  const size_t beginStr = extractedLine.find_first_not_of(" \t");
214  if (beginStr == std::string::npos)
215  {
216  // no content
217  std::cerr << "No value file specified!";
218  }
219  const size_t endStr = extractedLine.find_last_not_of(" \t");
220  const size_t range = endStr - beginStr + 1;
221  valueFileName = extractedLine.substr(beginStr, range);
222  if ( pathName != "" )
223  {
224  valueFileName = pathName + "/" + valueFileName;
225  }
226  }
227  }
228  else
229  {
230  break;
231  }
232 
233  }
234  infile.close();
235 
236  // Setup - Output Image
237  OutputImageType * output = this->GetOutput();
238  outputStartIndex.Fill(0);
239  outputRegion.SetIndex(outputStartIndex);
240  outputRegion.SetSize(outputSize);
241  output->SetRegions(outputRegion);
242  output->SetSpacing(outputSpacing);
243  // outputOrigin.Fill(0);
244  output->SetOrigin(outputOrigin);
245  // outputDirection.SetIdentity();
246  output->SetDirection(outputDirection);
247  output->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel);
248  output->Allocate();
249 
250  OutputImagePixelType outputPixel;
251  outputPixel.SetSize(numberOfComponentsPerPixel);
252  outputPixel.Fill(0);
253 
254  output->FillBuffer(outputPixel);
255 
256  OutputImagePixelContainerType * container = output->GetPixelContainer();
257 
258  m_KeyImageFileReader = KeyImageFileReaderType::New();
259  m_ValueImageFileReader = ValueImageFileReaderType::New();
260 
261  m_KeyImageFileReader->SetFileName(keyFileName);
262  m_ValueImageFileReader->SetFileName(valueFileName);
263 
264  m_KeyImageFileReader->Update();
265  m_ValueImageFileReader->Update();
266 
267  m_KeyImage = m_KeyImageFileReader->GetOutput();
268  m_ValueImage = m_ValueImageFileReader->GetOutput();
269 
270  m_ImageIO = m_ValueImageFileReader->GetImageIO();
271 
272  // Iterators
273  ImageRegionIterator<KeyImageType>
274  keyImageIterator( m_KeyImage, m_KeyImage->GetRequestedRegion() );
275  ImageRegionIterator<ValueImageType>
276  valueImageIterator( m_ValueImage, m_ValueImage->GetRequestedRegion() );
277 
278  keyImageIterator.GoToBegin();
279  valueImageIterator.GoToBegin();
280 
281  // Populate Data
282  SizeValueType offset;
283  SizeValueType elementIndex;
284  typename KeyImageType::PixelType key;
285  while ( !keyImageIterator.IsAtEnd() )
286  {
287  key = keyImageIterator.Get();
288  offset = (int)key / numberOfComponentsPerPixel;
289  elementIndex = key % numberOfComponentsPerPixel;
290  (*container)[offset][elementIndex] = valueImageIterator.Get();
291 
292  ++keyImageIterator;
293  ++valueImageIterator;
294  }
295 
296 }
297 
298 
299 } //namespace ITK
300 
301 #endif
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE