Orfeo Toolbox  3.16
itkGDCMSeriesFileNames.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkGDCMSeriesFileNames.cxx,v $
5  Language: C++
6  Date: $Date: 2010-02-25 00:55:49 $
7  Version: $Revision: 1.37 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef _itkGDCMSeriesFileNames_h
18 #define _itkGDCMSeriesFileNames_h
19 
20 #include "itkGDCMSeriesFileNames.h"
21 #include <itksys/SystemTools.hxx>
22 
23 #include "gdcmSerieHelper.h"
24 #include "gdcmFile.h"
25 
26 #include <vector>
27 #include <string>
28 
29 namespace itk
30 {
31 
33 {
34  m_SerieHelper = new gdcm::SerieHelper();
35  m_InputDirectory = "";
36  m_OutputDirectory = "";
37  m_UseSeriesDetails = true;
38  m_Recursive = false;
39  m_LoadSequences = false;
40  m_LoadPrivateTags = false;
41 }
42 
44 {
45  delete m_SerieHelper;
46 }
47 
49 {
50  if ( !name )
51  {
52  itkExceptionMacro(<<"SetInputDirectory() received a NULL string");
53  }
54  std::string fname = name;
55  this->SetInputDirectory( fname );
56 }
57 
58 void GDCMSeriesFileNames::SetInputDirectory (std::string const &name)
59 {
60  if ( name == "" )
61  {
62  itkWarningMacro( << "You need to specify a directory where "
63  "the DICOM files are located");
64  return;
65  }
66  if( m_InputDirectory == name )
67  {
68  return;
69  }
70  if( !itksys::SystemTools::FileIsDirectory(name.c_str()) )
71  {
72  itkWarningMacro( << name << " is not a directory" );
73  return;
74  }
75  m_InputDirectory = name;
76  m_SerieHelper->Clear();
77  m_SerieHelper->SetUseSeriesDetails( m_UseSeriesDetails );
78  m_SerieHelper->SetLoadMode( (m_LoadSequences ? 0 : gdcm::LD_NOSEQ)
79  | (m_LoadPrivateTags ? 0: gdcm::LD_NOSHADOW));
80  m_SerieHelper->SetDirectory( name, m_Recursive );
81  //as a side effect it also execute
82  this->Modified();
83 }
84 
86 {
87  m_SeriesUIDs.clear();
88  // Accessing the first serie found (assume there is at least one)
89  gdcm::FileList *flist = m_SerieHelper->GetFirstSingleSerieUIDFileSet();
90  while(flist)
91  {
92  if( flist->size() ) //make sure we have at leat one serie
93  {
94  gdcm::File *file = (*flist)[0]; //for example take the first one
95 
96  // Create its unique series ID
97  std::string id = m_SerieHelper->
98  CreateUniqueSeriesIdentifier( file ).c_str();
99 
100  m_SeriesUIDs.push_back( id.c_str() );
101  }
102  flist = m_SerieHelper->GetNextSingleSerieUIDFileSet();
103  }
104  if( !m_SeriesUIDs.size() )
105  {
106  itkWarningMacro(<<"No Series were found");
107  }
108  return m_SeriesUIDs;
109 }
110 
112 {
113  m_InputFileNames.clear();
114  // Accessing the first serie found (assume there is at least one)
115  gdcm::FileList *flist = m_SerieHelper->GetFirstSingleSerieUIDFileSet();
116  if( !flist )
117  {
118  itkWarningMacro(
119  << "No Series can be found, make sure your restrictions are not too strong");
120  return m_InputFileNames;
121  }
122  if( serie != "" ) // user did not specify any sub selection based on UID
123  {
124  bool found = false;
125  while(flist && !found)
126  {
127  if( flist->size() ) //make sure we have at leat one serie
128  {
129  gdcm::File *file = (*flist)[0]; //for example take the first one
130  std::string id = m_SerieHelper->
131  CreateUniqueSeriesIdentifier( file ).c_str();
132 
133  if( id == serie )
134  {
135  found = true; // we found a match
136  break;
137  }
138  }
139  flist = m_SerieHelper->GetNextSingleSerieUIDFileSet();
140  }
141  if( !found)
142  {
143  itkWarningMacro(<<"No Series were found");
144  return m_InputFileNames;
145  }
146  }
147  m_SerieHelper->OrderFileList(flist);
148 
149  gdcm::FileList::iterator it;
150  if( flist->size() )
151  {
152  for(it = flist->begin();
153  it != flist->end(); ++it )
154  {
155 #if GDCM_MAJOR_VERSION < 2
156  gdcm::File * header = *it;
157  if( !header )
158  {
159  itkWarningMacro( << "GDCMSeriesFileNames got NULL header, "
160  "this is a serious bug" );
161  continue;
162  }
163  if( !header->IsReadable() )
164  {
165  itkWarningMacro( << "GDCMSeriesFileNames got a non DICOM file:"
166  << header->GetFileName() );
167  continue;
168  }
169  m_InputFileNames.push_back( header->GetFileName() );
170 #else
171  gdcm::FileWithName * header = *it;
172  m_InputFileNames.push_back( header->filename );
173 #endif
174  }
175  }
176  else
177  {
178  itkDebugMacro(<<"No files were found");
179  }
180 
181  return m_InputFileNames;
182 }
183 
185 {
186  // Do not specify any UID
187  return GetFileNames("");
188 }
189 
191 {
192  // We are trying to extract the original filename and compose it with a path:
193 
194  //There are two different approachs if directory does not exist:
195  // 1. Exit
196  // 2. Mkdir
197  //bool SystemTools::FileExists(const char* filename)
198  //bool SystemTools::FileIsDirectory(const char* name)
199  m_OutputFileNames.clear();
200 
201  if( m_OutputDirectory.empty() )
202  {
203  itkDebugMacro(<<"No output directory was specified");
204  return m_OutputFileNames;
205  }
206 
207  itksys::SystemTools::ConvertToUnixSlashes( m_OutputDirectory );
208  if(m_OutputDirectory[m_OutputDirectory.size()-1] != '/')
209  {
210  m_OutputDirectory += '/';
211  }
212 
213  if( m_InputFileNames.size() )
214  {
215  bool hasExtension = false;
216  for(std::vector<std::string>::const_iterator it = m_InputFileNames.begin();
217  it != m_InputFileNames.end(); ++it )
218  {
219  // look for extension ".dcm" and ".DCM"
220  std::string::size_type dcmPos = (*it).rfind(".dcm");
221  if ( (dcmPos != std::string::npos)
222  && (dcmPos == (*it).length() - 4) )
223  {
224  hasExtension = true;
225  }
226  else
227  {
228  dcmPos = (*it).rfind(".DCM");
229  if ( (dcmPos != std::string::npos)
230  && (dcmPos == (*it).length() - 4) )
231  {
232  hasExtension = true;
233  }
234  }
235 
236  // look for extension ".dicom" and ".DICOM"
237  std::string::size_type dicomPos = (*it).rfind(".dicom");
238  if ( (dicomPos != std::string::npos)
239  && (dicomPos == (*it).length() - 6) )
240  {
241  hasExtension = true;
242  }
243  else
244  {
245  dicomPos = (*it).rfind(".DICOM");
246  if ( (dicomPos != std::string::npos)
247  && (dicomPos == (*it).length() - 6) )
248  {
249  hasExtension = true;
250  }
251  }
252 
253  // construct a filename, adding an extension if necessary
254  std::string filename =
255  m_OutputDirectory + itksys::SystemTools::GetFilenameName( *it );
256  if (!hasExtension)
257  {
258  // input filename has no extension, add a ".dcm"
259  filename += ".dcm";
260  }
261 
262  // Add the file name to the output list
263  m_OutputFileNames.push_back( filename );
264  }
265  }
266  else
267  {
268  itkDebugMacro(<<"No files were found.");
269  }
270 
271  return m_OutputFileNames;
272 }
273 
274 void GDCMSeriesFileNames::PrintSelf(std::ostream& os, Indent indent) const
275 {
276  Superclass::PrintSelf(os, indent);
277 
278  unsigned int i;
279  os << indent << "InputDirectory: " << m_InputDirectory << std::endl;
280  os << indent << "LoadSequences:" << m_LoadSequences << std::endl;
281  os << indent << "LoadPrivateTags:" << m_LoadPrivateTags << std::endl;
282  if(m_Recursive)
283  {
284  os << indent << "Recursive: True" << std::endl;
285  }
286  else
287  {
288  os << indent << "Recursive: False" << std::endl;
289  }
290 
291  for (i = 0; i < m_InputFileNames.size(); i++)
292  {
293  os << indent << "InputFilenames[" << i << "]: " << m_InputFileNames[i] << std::endl;
294  }
295 
296  os << indent << "OutputDirectory: " << m_OutputDirectory << std::endl;
297  for (i = 0; i < m_OutputFileNames.size(); i++)
298  {
299  os << indent << "OutputFilenames[" << i << "]: " << m_OutputFileNames[i] << std::endl;
300  }
301 }
302 
303 void GDCMSeriesFileNames::SetUseSeriesDetails( bool useSeriesDetails)
304 {
305  m_UseSeriesDetails = useSeriesDetails;
306  m_SerieHelper->SetUseSeriesDetails( m_UseSeriesDetails );
307  m_SerieHelper->CreateDefaultUniqueSeriesIdentifier();
308 }
309 
310 } //namespace ITK
311 
312 #endif

Generated at Sat Feb 2 2013 23:38:57 for Orfeo Toolbox with doxygen 1.8.1.1