23 #include <itksys/SystemTools.hxx>
24 #include <itksys/RegularExpression.hxx>
50 itkExceptionMacro(<<
"A FileName must be specified.");
61 itkDebugMacro(<<
"Initialize: opening file " << filename);
64 os.open( filename, std::ios::in | std::ios::binary );
82 itkExceptionMacro(<<
"A FileName must be specified.");
93 itkDebugMacro(<<
"Initialize: opening file " << filename);
97 std::ofstream tFile(filename,std::ios::out);
102 os.open( filename, std::ios::out | std::ios::binary );
106 itkExceptionMacro(<<
"Could not open file for writing: " << filename);
119 std::string fname(filename);
123 itkDebugMacro(<<
"No filename specified.");
127 bool extensionFound =
false;
128 std::string::size_type sprPos = fname.rfind(
".spr");
129 if ((sprPos != std::string::npos)
130 && (sprPos == fname.length() - 4))
132 extensionFound =
true;
135 if( !extensionFound )
137 itkDebugMacro(<<
"The filename extension is not recognized");
147 file.getline(buffer,255);
151 if ( fname.find(
"numDim:") < fname.length() ||
152 fname.find(
"dim:") < fname.length() ||
153 fname.find(
"dataType:") < fname.length() )
172 std::ifstream file_data;
186 file_data.open(
m_DataFileName.c_str(), std::ios::in | std::ios::binary );
187 if ( file_data.fail() )
192 file_data.open(
m_DataFileName.c_str(), std::ios::in | std::ios::binary );
193 if ( file_data.fail() )
195 itkExceptionMacro(<<
"No Data file was specified in header (spr) file and guessing file data name failed." );
203 itkExceptionMacro(
"StimulateImageIO could not open file: "
207 << itksys::SystemTools::GetLastSystemError());
213 itkExceptionMacro(<<
"Read failed: Wanted "
215 <<
" bytes, but read "
216 << file_data.gcount() <<
" bytes."
256 itkExceptionMacro(
"StimulateImageIO could not open sdt file: "
260 << itksys::SystemTools::GetLastSystemError());
265 file.open(
m_FileName.c_str(), std::ios::in );
269 unsigned int dims[4];
292 bool fov_specified =
false;
293 bool origin_specified =
false;
294 bool spacing_specified =
false;
295 while((file.getline(line, 255), file.gcount() > 0))
302 && (text[text.size() - 1] ==
'\r'))
304 text.resize(text.size() - 1);
308 if ( text.find(
"numDim") < text.length())
310 sscanf(line,
"%*s %d", &dim);
313 else if ( text.find(
"dim") < text.length())
315 sscanf(line,
"%*s %d %d %d %d", dims, dims+1, dims+2 , dims+3);
329 else if ( text.find(
"origin") < text.length())
336 sscanf(line,
"%*s %f %f %f %f", origin, origin+1, origin+2 , origin+3);
341 origin_specified =
true;
343 else if ( text.find(
"extent") < text.length())
346 itkDebugMacro(<<
"Extent was specified");
348 else if ( text.find(
"fov") < text.length())
355 sscanf(line,
"%*s %f %f %f %f", fov, fov+1, fov+2 , fov+3);
356 fov_specified =
true;
358 else if ( text.find(
"interval") < text.length())
365 sscanf(line,
"%*s %f %f %f %f", spacing, spacing+1, spacing+2 , spacing+3);
370 spacing_specified =
true;
372 else if ( text.find(
"dataType") < text.length())
374 sscanf(line,
"%*s %s", pixelType);
377 if ( text.find(
"BYTE") < text.length() )
381 else if ( text.find(
"WORD") < text.length() )
385 else if ( text.find(
"LWORD") < text.length() )
389 else if ( text.find(
"REAL") < text.length() )
393 else if ( text.find(
"COMPLEX") < text.length() )
400 itkExceptionMacro(<<
"Unrecognized type");
403 else if ( text.find(
"displayRange") < text.length())
412 sscanf(line,
"%*s %f %f", range, range+1);
416 else if ( text.find(
"fidName") < text.length())
422 pch = strchr(line,
':');
424 itkDebugMacro(<<
"fidName was specified");
426 else if ( text.find(
"sdtOrient") < text.length())
433 pch = strchr(line,
':');
435 itkDebugMacro(<<
"Orientation was specified");
437 else if ( text.find(
"dsplyThres") < text.length())
440 itkDebugMacro(<<
"Display threshold was specified");
442 else if ( text.find(
"endian") < text.length())
445 if ( text.find(
"ieee-le") < text.length())
447 itkExceptionMacro(<<
"Little Endian Stimulate files are not handled.");
450 else if ( text.find(
"mapParmFileName") < text.length())
453 itkDebugMacro(<<
"mapParmFileName was specified");
455 else if ( text.find(
"mapTypeName") < text.length())
458 itkDebugMacro(<<
"mapTypeName was specified");
460 else if ( text.find(
"stimFileName:") < text.length())
463 std::string datafilename;
465 itksys::RegularExpression regexp(
"stimFileName:[ ]*(.*)[ ]*$");
467 datafilename = regexp.match(1);
471 std::string datafilenamePath =
472 ::itksys::SystemTools::GetFilenamePath (datafilename);
473 if (datafilenamePath ==
"")
475 std::string fileNamePath = ::itksys::SystemTools::GetFilenamePath (
m_FileName.c_str());
483 else if ( text.find(
"mapConf") < text.length())
486 itkDebugMacro(<<
"mapConf was specified");
488 else if ( text.find(
"periodStr") < text.length())
491 itkDebugMacro(<<
"periodStr was specified");
497 if( !spacing_specified && fov_specified)
504 if( !origin_specified && fov_specified)
522 std::string filename = name;
526 itkDebugMacro(<<
"No filename specified.");
530 bool extensionFound =
false;
531 std::string::size_type sprPos = filename.rfind(
".spr");
532 if ((sprPos != std::string::npos)
533 && (sprPos == filename.length() - 4))
535 extensionFound =
true;
538 if( !extensionFound )
540 itkDebugMacro(<<
"The filename extension is not recognized");
559 if ( numDims < 2 || numDims > 4 )
561 itkExceptionMacro(<<
"Stimulate Writer can only write 2,3 or 4-dimensional images");
587 file <<
"\ninterval:";
594 const unsigned long numberOfBytes =
static_cast<unsigned long>(this->
GetImageSizeInBytes());
597 file <<
"\ndataType: ";
599 char * tempmemory=
new char[numberOfBytes];
600 memcpy(tempmemory,buffer,numberOfBytes);
637 std::ofstream file_data;
645 file_data.write( static_cast<const char *>(tempmemory) , numberOfBytes );
646 delete [] tempmemory;