14 #ifndef __itkSpatiallyDenseSparseVectorImageFileReader_hxx 15 #define __itkSpatiallyDenseSparseVectorImageFileReader_hxx 18 #include "itkImageRegionIterator.h" 19 #include "itksys/SystemTools.hxx" 25 template <
class TOutputImage>
30 m_UseStreaming =
true;
34 template <
class TOutputImage>
40 template <
class TOutputImage>
44 Superclass::PrintSelf(os, indent);
48 os << indent <<
"ImageIO: \n";
49 m_ImageIO->Print(os, indent.GetNextIndent());
53 os << indent <<
"ImageIO: (null)" <<
"\n";
56 os << indent <<
"m_FileName: " << m_FileName <<
"\n";
57 os << indent <<
"m_UseStreaming: " << m_UseStreaming <<
"\n";
60 template <
class TOutputImage>
64 itkDebugMacro ( <<
"SpatiallyDenseSparseVectorImageFileReader::GenerateData() \n" );
66 if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4,
".spr")!=0)
68 itkExceptionMacro( <<
"the file " << m_FileName <<
" should be a text file ending with '.spr'.");
71 std::string keyFileName;
72 std::string valueFileName;
77 infile.open(m_FileName.c_str(), std::fstream::in);
79 if ( !infile.is_open() )
81 itkExceptionMacro( <<
"Cannot open file: " << m_FileName );
84 std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
85 std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
87 std::string line, extractedLine;
96 unsigned int ndims = 0;
97 unsigned int numberOfComponentsPerPixel = 0;
102 while (!infile.eof())
104 if (infile.getline(tempLine, 256))
106 line = std::string(tempLine);
108 if (line.find(
"NDims") != std::string::npos)
110 extractedLine = line.substr(line.find(
"=") + 1);
111 pch = strtok(const_cast<char*>(extractedLine.c_str()),
" ");
119 if (line.find(
"DimSize") != std::string::npos)
121 extractedLine = line.substr(line.find(
"=") + 1);
123 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
126 if ( idx == ImageDimension )
128 numberOfComponentsPerPixel = atoi(pch);
132 outputSize[idx] = atoi(pch);
135 pch = strtok (NULL,
" ");
139 if (line.find(
"ElementSpacing") != std::string::npos)
141 extractedLine = line.substr(line.find(
"=") + 1);
143 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
146 if ( idx < ImageDimension )
148 outputSpacing[idx] = atof(pch);
151 pch = strtok (NULL,
" ");
155 if (line.find(
"Offset") != std::string::npos)
157 extractedLine = line.substr(line.find(
"=") + 1);
159 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
162 if ( idx < ImageDimension )
164 outputOrigin[idx] = atof(pch);
167 pch = strtok (NULL,
" ");
171 if (line.find(
"TransformMatrix") != std::string::npos)
173 extractedLine = line.substr(line.find(
"=") + 1);
175 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
176 outputDirection.SetIdentity();
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) )
183 outputDirection(row, col ) = atof(pch);
186 pch = strtok (NULL,
" ");
190 if (line.find(
"KeyElementDataFile") != std::string::npos)
192 extractedLine = line.substr(line.find(
"=") + 1);
194 const size_t beginStr = extractedLine.find_first_not_of(
" \t");
195 if (beginStr == std::string::npos)
198 std::cerr <<
"No key file specified!";
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 !=
"" )
205 keyFileName = pathName +
"/" + keyFileName;
209 if (line.find(
"ValueElementDataFile") != std::string::npos)
211 extractedLine = line.substr(line.find(
"=") + 1);
213 const size_t beginStr = extractedLine.find_first_not_of(
" \t");
214 if (beginStr == std::string::npos)
217 std::cerr <<
"No value file specified!";
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 !=
"" )
224 valueFileName = pathName +
"/" + valueFileName;
238 outputStartIndex.Fill(0);
239 outputRegion.SetIndex(outputStartIndex);
240 outputRegion.SetSize(outputSize);
241 output->SetRegions(outputRegion);
242 output->SetSpacing(outputSpacing);
244 output->SetOrigin(outputOrigin);
246 output->SetDirection(outputDirection);
247 output->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel);
251 outputPixel.SetSize(numberOfComponentsPerPixel);
254 output->FillBuffer(outputPixel);
258 m_KeyImageFileReader = KeyImageFileReaderType::New();
259 m_ValueImageFileReader = ValueImageFileReaderType::New();
261 m_KeyImageFileReader->SetFileName(keyFileName);
262 m_ValueImageFileReader->SetFileName(valueFileName);
264 m_KeyImageFileReader->Update();
265 m_ValueImageFileReader->Update();
267 m_KeyImage = m_KeyImageFileReader->GetOutput();
268 m_ValueImage = m_ValueImageFileReader->GetOutput();
270 m_ImageIO = m_ValueImageFileReader->GetImageIO();
273 ImageRegionIterator<KeyImageType>
274 keyImageIterator( m_KeyImage, m_KeyImage->GetRequestedRegion() );
275 ImageRegionIterator<ValueImageType>
276 valueImageIterator( m_ValueImage, m_ValueImage->GetRequestedRegion() );
278 keyImageIterator.GoToBegin();
279 valueImageIterator.GoToBegin();
282 SizeValueType offset;
283 SizeValueType elementIndex;
284 typename KeyImageType::PixelType key;
285 while ( !keyImageIterator.IsAtEnd() )
287 key = keyImageIterator.Get();
288 offset = (int)key / numberOfComponentsPerPixel;
289 elementIndex = key % numberOfComponentsPerPixel;
290 (*container)[offset][elementIndex] = valueImageIterator.Get();
293 ++valueImageIterator;
virtual void GenerateData() ITK_OVERRIDE
OutputImageType::PointType OutputImagePointType
OutputImageType::SpacingType OutputImageSpacingType
OutputImageType::DirectionType OutputImageDirectionType
OutputImageType::RegionType OutputImageRegionType
OutputImageType::PixelContainer OutputImagePixelContainerType
OutputImageType::PixelType OutputImagePixelType
TOutputImage OutputImageType
~SpatiallyDenseSparseVectorImageFileReader()
SpatiallyDenseSparseVectorImageFileReader()
OutputImageType::SizeType OutputImageSizeType
OutputImageType::IndexType OutputImageIndexType
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE