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