14 #ifndef __itkVariableLengthVectorImageFileWriter_hxx 15 #define __itkVariableLengthVectorImageFileWriter_hxx 19 #include "itksys/SystemTools.hxx" 20 #include "itkImageRegionConstIterator.h" 21 #include "itkImageRegionIterator.h" 28 template <
class TInputImage>
33 m_UseCompression =
true;
38 template <
class TInputImage>
45 template <
class TInputImage>
51 this->ProcessObject::SetNthInput(0,
52 const_cast<TInputImage *>(input ) );
57 template <
class TInputImage>
62 if (this->GetNumberOfInputs() < 1)
67 return static_cast<TInputImage*
> 68 (this->ProcessObject::GetInput(0));
72 template <
class TInputImage>
77 return static_cast<TInputImage*
> (this->ProcessObject::GetInput(idx));
81 template <
class TInputImage>
88 itkDebugMacro( <<
"Writing an image file" );
93 itkExceptionMacro( <<
"No input to writer!" );
96 if ( m_FileName ==
"" )
98 itkExceptionMacro( <<
"No filename specified" );
102 this->GenerateData();
107 template <
class TInputImage>
112 itkDebugMacro ( <<
"VariableLengthVectorImageFileWriter::GenerateData() \n" );
114 if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4,
".vlv")!=0)
116 itkExceptionMacro( <<
"the file " << m_FileName <<
" should be a text file ending with '.vlv'.");
123 ImageRegionConstIterator<InputImageType>
124 inputIt( input, input->GetLargestPossibleRegion() );
128 unsigned long totalLength = 0;
130 while( !inputIt.IsAtEnd() )
132 totalLength += inputIt.Get().Size();
137 m_LengthImage = LengthImageType::New();
138 m_LengthImage->CopyInformation(input);
139 m_LengthImage->SetRegions(input->GetLargestPossibleRegion());
140 m_LengthImage->Allocate();
142 typename ValueImageType::IndexType startIndex;
143 typename ValueImageType::RegionType region;
144 typename ValueImageType::SizeType size;
145 typename ValueImageType::SpacingType spacing;
149 size[0] = totalLength;
150 region.SetIndex(startIndex);
151 region.SetSize(size);
153 m_ValueImage = ValueImageType::New();
154 m_ValueImage->SetSpacing(spacing);
155 m_ValueImage->SetRegions(region);
156 m_ValueImage->Allocate();
159 ImageRegionIterator<LengthImageType>
160 lengthIt( m_LengthImage, m_LengthImage->GetRequestedRegion() );
161 ImageRegionIterator<ValueImageType>
162 valueIt( m_ValueImage, m_ValueImage->GetRequestedRegion() );
164 lengthIt.GoToBegin();
169 while ( !inputIt.IsAtEnd() )
173 lengthIt.Set(length);
176 valueIt.Set(inputPixel[k]);
184 std::string baseFileName =
"";
185 std::string fileNameExtension;
186 std::string dataFileNameExtension =
"nrrd";
188 std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
189 std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
191 std::string::size_type idx;
192 idx = fileName.find_last_of(
'.');
194 if (idx != std::string::npos)
196 fileNameExtension = fileName.substr(idx + 1);
197 if (fileNameExtension !=
"vlv")
199 std::cout <<
"Renaming extension to .vlv" << std::endl;
200 fileNameExtension =
"vlv";
202 baseFileName = fileName.substr(0, idx);
204 std::string lengthFileName = baseFileName +
"_length." + dataFileNameExtension;
205 std::string valueFileName = baseFileName +
"_value." + dataFileNameExtension;
206 std::string headerFileName = baseFileName +
"." + fileNameExtension;
209 m_LengthImageFileWriter = LengthImageFileWriterType::New();
210 m_ValueImageFileWriter = ValueImageFileWriterType::New();
212 std::string lengthPathName = lengthFileName;
213 std::string valuePathName = valueFileName;
214 std::string headerPathName = headerFileName;
216 if ( pathName !=
"" )
218 lengthPathName = pathName +
"/" + lengthFileName;
219 valuePathName = pathName +
"/" + valueFileName;
220 headerPathName = pathName +
"/" + headerFileName;
223 m_LengthImageFileWriter->SetFileName(lengthPathName);
224 m_LengthImageFileWriter->SetInput(m_LengthImage);
225 m_ValueImageFileWriter->SetFileName(valuePathName);
226 m_ValueImageFileWriter->SetInput(m_ValueImage);
228 m_LengthImageFileWriter->SetUseCompression(this->m_UseCompression);
229 m_ValueImageFileWriter->SetUseCompression(this->m_UseCompression);
233 m_LengthImageFileWriter->Update();
234 m_ValueImageFileWriter->Update();
237 std::ofstream outfile;
241 outfile.open(headerPathName.c_str(), std::fstream::out);
249 outfile <<
"NDims = " << input->GetImageDimension() << std::endl;
250 outfile <<
"DimSize = ";
252 for (
unsigned int d=0; d<input->GetImageDimension(); d++)
254 outfile << outputSize[d] <<
" ";
256 outfile << std::endl;
258 outfile <<
"ElementSpacing = ";
260 for (
unsigned int d=0; d<input->GetImageDimension(); d++)
262 outfile << outputSpacing[d] <<
" ";
264 outfile << std::endl;
266 outfile <<
"Offset = ";
268 for (
unsigned int d=0; d<input->GetImageDimension(); d++)
270 outfile << outputOrigin[d] <<
" ";
272 outfile << std::endl;
274 outfile <<
"TransformMatrix = ";
275 for (
unsigned int d1=0; d1<input->GetImageDimension(); d1++)
277 for (
unsigned int d2=0; d2<input->GetImageDimension(); d2++)
278 outfile << outputDirection(d1,d2) <<
" ";
280 outfile << std::endl;
282 outfile <<
"LengthElementDataFile = " << lengthFileName << std::endl;
283 outfile <<
"ValueElementDataFile = " << valueFileName << std::endl;
290 template <
class TInputImage>
295 Superclass::PrintSelf(os,indent);
297 os << indent <<
"File Name: " 298 << (m_FileName.data() ? m_FileName.data() :
"(none)") << std::endl;
300 if (m_UseCompression)
302 os << indent <<
"Compression: On\n";
306 os << indent <<
"Compression: Off\n";
InputImageType::RegionType InputImageRegionType
void PrintSelf(std::ostream &os, Indent indent) const
InputImageType::SizeType InputImageSizeType
const InputImageType * GetInput(void)
VariableLengthVectorImageFileWriter()
InputImageType::SpacingType InputImageSpacingType
InputImageType::PixelType InputImagePixelType
~VariableLengthVectorImageFileWriter()
InputImageType::PointType InputImagePointType
void SetInput(const InputImageType *input)
InputImageType::DirectionType InputImageDirectionType
TInputImage InputImageType