14 #ifndef __itkSpatiallyDenseSparseVectorImageFileWriter_hxx 15 #define __itkSpatiallyDenseSparseVectorImageFileWriter_hxx 18 #include "itkImageRegionIteratorWithIndex.h" 20 #include "itksys/SystemTools.hxx" 26 template <
class TInputImage>
31 m_UseCompression =
true;
36 template <
class TInputImage>
43 template <
class TInputImage>
49 this->ProcessObject::SetNthInput(0,
50 const_cast<TInputImage *>(input ) );
55 template <
class TInputImage>
60 if (this->GetNumberOfInputs() < 1)
65 return static_cast<TInputImage*
> 66 (this->ProcessObject::GetInput(0));
70 template <
class TInputImage>
75 return static_cast<TInputImage*
> (this->ProcessObject::GetInput(idx));
79 template <
class TInputImage>
86 itkDebugMacro( <<
"Writing an image file" );
91 itkExceptionMacro(<<
"No input to writer!");
94 if ( m_FileName ==
"" )
96 itkExceptionMacro(<<
"No filename specified");
100 this->GenerateData();
105 template <
class TInputImage>
110 itkDebugMacro ( <<
"SpatiallyDenseSparseVectorImageFileWriter::GenerateData() \n" );
112 if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4,
".spr")!=0)
114 itkExceptionMacro( <<
"the file " << m_FileName <<
" should be a text file ending with '.spr'.");
121 ImageRegionIteratorWithIndex<InputImageType>
122 inputImageIterator( input, input->GetRequestedRegion() );
124 inputImageIterator.GoToBegin();
126 SizeValueType totalElements = 0;
127 while ( !inputImageIterator.IsAtEnd() )
129 totalElements += (input->GetInternalPixel(inputImageIterator.GetIndex())).GetSize();
130 ++inputImageIterator;
134 unsigned int numberOfComponentsPerPixel = input->GetNumberOfComponentsPerPixel();
137 m_KeyImage = KeyImageType::New();
138 m_ValueImage = ValueImageType::New();
140 typename KeyImageType::IndexType startIndex;
141 typename KeyImageType::RegionType region;
142 typename KeyImageType::SizeType size;
143 typename KeyImageType::SpacingType spacing;
147 if ( totalElements > 0 )
149 size[0] = totalElements;
158 region.SetIndex(startIndex);
159 region.SetSize(size);
161 m_KeyImage->SetSpacing(spacing);
162 m_KeyImage->SetRegions(region);
163 m_ValueImage->SetSpacing(spacing);
164 m_ValueImage->SetRegions(region);
166 m_KeyImage->Allocate();
167 m_ValueImage->Allocate();
168 m_KeyImage->FillBuffer(static_cast<InputImageKeyType>(0));
169 m_ValueImage->FillBuffer(static_cast<InputImageValueType>(0));
172 ImageRegionIteratorWithIndex<KeyImageType>
173 keyImageIterator( m_KeyImage, m_KeyImage->GetRequestedRegion() );
174 ImageRegionIteratorWithIndex<ValueImageType>
175 valueImageIterator( m_ValueImage, m_ValueImage->GetRequestedRegion() );
177 keyImageIterator.GoToBegin();
178 valueImageIterator.GoToBegin();
179 inputImageIterator.GoToBegin();
182 SizeValueType offset;
184 if ( totalElements > 0 )
186 while ( !inputImageIterator.IsAtEnd() )
188 index = inputImageIterator.GetIndex();
191 (input->GetInternalPixel(index)).GetDataPointer();
194 offset = input->ComputeOffset(index) * numberOfComponentsPerPixel;
196 while ( iterator != internalData->end() )
198 keyImageIterator.Set(static_cast<InputImageKeyType>(iterator->first + offset));
199 valueImageIterator.Set(iterator->second);
203 ++valueImageIterator;
205 ++inputImageIterator;
210 keyImageIterator.Set(static_cast<InputImageKeyType>(0));
211 valueImageIterator.Set(0);
215 std::string baseFileName =
"";
216 std::string fileNameExtension;
217 std::string dataFileNameExtension =
"nrrd";
219 std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
220 std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
222 std::string::size_type idx;
223 idx = fileName.find_last_of(
'.');
225 if (idx != std::string::npos)
227 fileNameExtension = fileName.substr(idx + 1);
228 if (fileNameExtension !=
"spr")
230 std::cout <<
"Renaming extension to .spr" << std::endl;
231 fileNameExtension =
"spr";
233 baseFileName = fileName.substr(0, idx);
235 std::string keyFileName = baseFileName +
"_key." + dataFileNameExtension;
236 std::string valueFileName = baseFileName +
"_value." + dataFileNameExtension;
237 std::string headerFileName = baseFileName +
"." + fileNameExtension;
240 m_KeyImageFileWriter = KeyImageFileWriterType::New();
241 m_ValueImageFileWriter = ValueImageFileWriterType::New();
243 std::string keyPathName = keyFileName;
244 std::string valuePathName = valueFileName;
245 std::string headerPathName = headerFileName;
247 if ( pathName !=
"" )
249 keyPathName = pathName +
"/" + keyFileName;
250 valuePathName = pathName +
"/" + valueFileName;
251 headerPathName = pathName +
"/" + headerFileName;
254 m_KeyImageFileWriter->SetFileName(keyPathName);
255 m_KeyImageFileWriter->SetInput(m_KeyImage);
256 m_ValueImageFileWriter->SetFileName(valuePathName);
257 m_ValueImageFileWriter->SetInput(m_ValueImage);
259 m_KeyImageFileWriter->SetUseCompression(this->m_UseCompression);
260 m_ValueImageFileWriter->SetUseCompression(this->m_UseCompression);
264 m_KeyImageFileWriter->Update();
265 m_ValueImageFileWriter->Update();
268 std::ofstream outfile;
272 outfile.open(headerPathName.c_str(), std::fstream::out);
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++)
285 outfile << outputSize[d] <<
" ";
287 outfile << input->GetNumberOfComponentsPerPixel() <<
" ";
288 outfile << std::endl;
290 outfile <<
"ElementSpacing = ";
291 for (
unsigned int d=0; d<dim; d++)
293 outfile << outputSpacing[d] <<
" ";
295 outfile <<
"1" <<
" ";
296 outfile << std::endl;
298 outfile <<
"Offset = ";
299 for (
unsigned int d=0; d<dim; d++)
301 outfile << outputOrigin[d] <<
" ";
303 outfile <<
"0" <<
" ";
304 outfile << std::endl;
306 outfile <<
"TransformMatrix = ";
307 for (
unsigned int d1=0; d1 < dim + 1; d1++)
309 for (
unsigned int d2=0; d2 < dim + 1; d2++)
311 if ((d1 == dim) && (d2 == dim))
313 outfile <<
"1" <<
" ";
315 else if ((d1 == dim) && (d2 != dim))
317 outfile <<
"0" <<
" ";
319 else if ((d2 == dim) && (d1 != dim))
321 outfile <<
"0" <<
" ";
325 outfile << outputDirection(d1,d2) <<
" ";
329 outfile << std::endl;
331 outfile <<
"KeyElementDataFile = " << keyFileName << std::endl;
332 outfile <<
"ValueElementDataFile = " << valueFileName << std::endl;
339 template <
class TInputImage>
344 Superclass::PrintSelf(os,indent);
346 os << indent <<
"File Name: " 347 << (m_FileName.data() ? m_FileName.data() :
"(none)") << std::endl;
349 if (m_UseCompression)
351 os << indent <<
"Compression: On\n";
355 os << indent <<
"Compression: Off\n";
InputImageType::RegionType InputImageRegionType
void SetInput(const InputImageType *input)
TInputImage InputImageType
SpatiallyDenseSparseVectorImageFileWriter()
InputImageType::SpacingType InputImageSpacingType
const InputImageType * GetInput(void)
InputImageType::DirectionType InputImageDirectionType
InputImageType::PointType InputImagePointType
InputImageType::SizeType InputImageSizeType
void GenerateData(void) ITK_OVERRIDE
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
InputImageType::PixelMapConstIterator InputImagePixelMapConstIteratorType
InputImageType::PixelMapType InputImagePixelPixelMapType
InputImageType::IndexType InputImageIndexType
~SpatiallyDenseSparseVectorImageFileWriter()