Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkFileSeriesReader.h"
00020 #include <itkImageFileReader.h>
00021 #include <itksys/SystemTools.hxx>
00022 #include <itksys/Directory.hxx>
00023 #include <map>
00024
00025
00026
00027 bool mitk::FileSeriesReader::GenerateFileList()
00028 {
00029 typedef std::vector<std::string> StringContainer;
00030 typedef std::map<unsigned int, std::string> SortedStringContainer;
00031
00032 if ( m_FileName == "" )
00033 {
00034 throw itk::ImageFileReaderException( __FILE__, __LINE__, "FileName must be non-empty" );
00035 }
00036
00037
00038
00039
00040
00041
00042
00043 std::string basename, path;
00044 path = itksys::SystemTools::GetFilenamePath( m_FileName );
00045 basename = itksys::SystemTools::GetFilenameName( m_FileName );
00046
00047 unsigned int digitBegin = 0;
00048 unsigned int digitEnd = 0;
00049 bool digitFound = false;
00050 for ( unsigned int i = basename.length() - 1; ; --i )
00051 {
00052 char character = basename[ i ];
00053 if ( character >= '0' && character <= '9' )
00054 {
00055 if (!digitFound)
00056 {
00057 digitEnd = i;
00058 digitBegin = i;
00059 digitFound = true;
00060 }
00061 else
00062 digitBegin = i;
00063 }
00064 else
00065 {
00066
00067 if (digitFound)
00068 break;
00069 }
00070 if ( i == 0 )
00071 break;
00072 }
00073
00074
00075
00076
00077
00078 if ( !digitFound )
00079 {
00080 itkWarningMacro("Filename contains no digit!");
00081 return false;
00082 }
00083
00084
00085
00086
00087 unsigned int prefixBegin = 0;
00088 unsigned int prefixLength = digitBegin;
00089 unsigned int extensionBegin = digitEnd + 1;
00090 unsigned int extensionLength = (digitEnd == basename.length() -1 ? 0 : basename.length() - 1 - digitEnd);
00091 unsigned int numberLength = digitEnd - digitBegin + 1;
00092
00093
00094
00095
00096 std::string prefix = "";
00097 if (prefixLength != 0)
00098 prefix = basename.substr( prefixBegin, prefixLength );
00099 std::string extension = "";
00100 if (extensionLength != 0)
00101 extension = basename.substr( extensionBegin, extensionLength );
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 if( (prefixLength + extensionLength + numberLength) != basename.length() )
00118 {
00119 throw itk::ImageFileReaderException( __FILE__, __LINE__, "prefixLength + extensionLength + numberLength != basenameLength" );
00120 }
00121
00122
00123
00124
00125
00126 std::string directory = itksys::SystemTools::GetFilenamePath( m_FileName );
00127 itksys::Directory itkDir;
00128 if ( !itkDir.Load ( directory.c_str() ) )
00129 {
00130 itkWarningMacro ( << "Directory " << directory << " cannot be read!" );
00131 return false;
00132 }
00133
00134
00135
00136
00137 StringContainer unmatchedFiles;
00138
00139 for ( unsigned long i = 0; i < itkDir.GetNumberOfFiles(); i++ )
00140 {
00141
00142 std::string filename = directory + "/" + itkDir.GetFile( i );
00143 if ( itksys::SystemTools::FileIsDirectory( filename.c_str() ) )
00144 continue;
00145
00146
00147 unmatchedFiles.push_back( itkDir.GetFile( i ) );
00148 }
00149
00150
00151
00152
00153
00154 StringContainer matchedFiles;
00155 for ( StringContainer::iterator it = unmatchedFiles.begin() ; it != unmatchedFiles.end() ; ++it )
00156 {
00157 bool prefixMatch = false;
00158 bool extensionMatch = false;
00159
00160
00161 if ( prefixLength != 0 )
00162 prefixMatch = ( it->find(prefix) == prefixBegin );
00163 else
00164 prefixMatch = ( ( (*it)[0] >='0' ) && ( (*it)[0] <='9' ) );
00165
00166
00167 if ( extensionLength != 0 )
00168 extensionMatch = ( it->find(extension) == it->length() - extensionLength );
00169 else
00170 extensionMatch = ( ( (*it)[it->length()-1] >='0' ) && ( (*it)[it->length()-1] <='9' ) );
00171
00172 if ( prefixMatch && extensionMatch )
00173 {
00174 matchedFiles.push_back( *it );
00175 }
00176 }
00177 if ( matchedFiles.size() == 0 )
00178 {
00179 itkWarningMacro( << "Sorry, none of the files matched the prefix!" );
00180 return false;
00181 }
00182
00183
00184
00185
00186
00187
00188 SortedStringContainer sortedFiles;
00189 for ( StringContainer::iterator it = matchedFiles.begin() ; it != matchedFiles.end() ; ++it )
00190 {
00191
00192
00193 std::string number = "";
00194 std::string currentFilename(*it);
00195 for ( unsigned int i = digitBegin ; i < currentFilename.length() ; ++i)
00196 {
00197 char character = currentFilename[ i ];
00198
00199 if ( character >= '0' && character <= '9' )
00200 number += character;
00201 else
00202 break;
00203 }
00204 if ( number.length() == 0 )
00205 {
00206
00207
00208 itkWarningMacro( << "The filename " << *it << "does not contain a valid digit sequence but matches prefix and extension. Skipping file!" );
00209 }
00210 else
00211 {
00212 if ( ( number.length() + prefix.length() + extension.length() ) != it->length() )
00213 {
00214 itkWarningMacro("The file "<< *it <<" matches prefix and extension, but the string in beteen is not a single digit-sequence. Skipping file!");
00215 }
00216 else
00217 {
00218
00219
00220 unsigned int num = atoi( number.c_str() );
00221 sortedFiles.insert( std::make_pair( num, directory + "/" + *it ) );
00222 }
00223 }
00224 }
00225 if ( sortedFiles.size() == 0 )
00226 {
00227 itkWarningMacro( << "Sorry, no numbered files found, I can't load anything..." );
00228 return false;
00229 }
00230
00231
00232
00233
00234 m_MatchedFileNames.clear();
00235 m_MatchedFileNames.resize( sortedFiles.size() );
00236 unsigned long index = 0;
00237 for ( SortedStringContainer::iterator it = sortedFiles.begin() ; it != sortedFiles.end() ; ++it, ++index )
00238 {
00239 m_MatchedFileNames[ index ] = it->second ;
00240 MITK_INFO << "Added " << it->second << " to the set of matched files!" << std::endl;
00241 }
00242 return true;
00243 }
00244
00245
00246 mitk::FileSeriesReader::MatchedFileNames mitk::FileSeriesReader::GetMatchedFileNames( )
00247 {
00248 return m_MatchedFileNames;
00249 }
00250
00251 mitk::FileSeriesReader::FileSeriesReader()
00252 : m_FileName( "" ), m_FilePrefix( "" ), m_FilePattern( "" )
00253 {}
00254
00255 mitk::FileSeriesReader::~FileSeriesReader()
00256 {
00257 }