Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNrrdTbssImageReader.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef __mitkNrrdTbssImageReader_cpp
18 #define __mitkNrrdTbssImageReader_cpp
19 
21 
22 #include "itkImageFileReader.h"
23 #include "itkMetaDataObject.h"
24 #include "itkNrrdImageIO.h"
25 #include "itkNiftiImageIO.h"
26 
27 #include <itkImageFileWriter.h>
28 
29 #include <iostream>
30 #include <fstream>
31 
32 #include "itksys/SystemTools.hxx"
33 
34 
35 namespace mitk
36 {
39  {
40 
41  // Since everything is completely read in GenerateOutputInformation() it is stored
42  // in a cache variable. A timestamp is associated.
43  // If the timestamp of the cache variable is newer than the MTime, we only need to
44  // assign the cache variable to the DataObject.
45  // Otherwise, the tree must be read again from the file and OuputInformation must
46  // be updated!
47  if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) )
48  {
49  this->GenerateOutputInformation();
50  itkWarningMacro("Cache regenerated!");
51  }
52 
53  if (!m_OutputCache)
54  {
55  itkWarningMacro("Tree cache is empty!")
56  }
57 
58 
59  static_cast<OutputType*>(this->GetPrimaryOutput())
60  ->SetImage(m_OutputCache->GetImage());
61  static_cast<OutputType*>(this->GetPrimaryOutput())
62  ->SetGroupInfo(m_OutputCache->GetGroupInfo());
63 
64  static_cast<OutputType*>(this->GetPrimaryOutput())
65  ->InitializeFromVectorImage();
66 
67  }
68 
71  {
72  OutputType::Pointer outputForCache = OutputType::New();
73 
74  if ( m_FileName == "")
75  {
76  throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!");
77  }
78  else
79  {
80  try
81  {
82  const std::string& locale = "C";
83  const std::string& currLocale = setlocale( LC_ALL, nullptr );
84 
85  if ( locale.compare(currLocale)!=0 )
86  {
87  try
88  {
89  MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, nullptr) << " to '" << locale << "'";
90  setlocale(LC_ALL, locale.c_str());
91  }
92  catch(...)
93  {
94  MITK_INFO << "Could not set locale " << locale;
95  }
96  }
97 
98 
99  MITK_INFO << "NrrdTbssImageReader READING IMAGE INFORMATION";
100  ImageType::Pointer img;
101 
102  std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
103  ext = itksys::SystemTools::LowerCase(ext);
104  if (ext == ".tbss")
105  {
106  typedef itk::ImageFileReader<ImageType> FileReaderType;
108  reader->SetFileName(this->m_FileName);
110  reader->SetImageIO(io);
111 
112  reader->Update();
113  img = reader->GetOutput();
114 
115 
116  MITK_INFO << "NrrdTbssImageReader READING HEADER INFORMATION";
117 
118 
119  itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
120  std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
121  std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
122  std::string metaString;
123 
124 
125 
126  std::string measurementInfo;
127 
128 
129  std::vector< std::pair<std::string, int> > groups;
130 
131  for (; itKey != imgMetaKeys.end(); itKey ++)
132  {
133  itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);
134 
135  MITK_INFO << *itKey << " ---> " << metaString;
136 
137  if (itKey->find("Group_index") != std::string::npos)
138  {
139 
140  std::vector<std::string> tokens;
141  this->Tokenize(metaString, tokens, " ");
142 
143 
144  std::pair< std::string, int > p;
145 
146  p.first="";
147  for (unsigned int i=0; i<tokens.size()-1;i++)
148  {
149  p.first.append(" ");
150  p.first.append(tokens.at(i));
151  }
152 
153  std::cout << p.first << std::endl;
154 
155  p.second = atoi(tokens.at(tokens.size()-1 ).c_str());
156  groups.push_back(p);
157 
158 
159 
160  }
161 
162  else if(itKey->find("Measurement info") != std::string::npos)
163  {
164  measurementInfo = metaString;
165  }
166 
167 
168 
169 
170  }
171 
172 
173  outputForCache->SetGroupInfo(groups);
174  outputForCache->SetMeasurementInfo(measurementInfo);
175 
176 
177  }
178 
179 
180 
181  // This call updates the output information of the associated VesselTreeData
182  outputForCache->SetImage(img);
183 
184 
185  // Since we have already read the tree, we can store it in a cache variable
186  // so that it can be assigned to the DataObject in GenerateData();
187  m_OutputCache = outputForCache;
188  m_CacheTime.Modified();
189 
190  try
191  {
192  MITK_INFO << " ** Changing locale back from " << setlocale(LC_ALL, nullptr) << " to '" << currLocale << "'";
193  setlocale(LC_ALL, currLocale.c_str());
194  }
195  catch(...)
196  {
197  MITK_INFO << "Could not reset locale " << currLocale;
198  }
199  }
200  catch(std::exception& e)
201  {
202  MITK_INFO << "Std::Exception while reading file!!";
203  MITK_INFO << e.what();
204  throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
205  }
206  catch(...)
207  {
208  MITK_INFO << "Exception while reading file!!";
209  throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!");
210  }
211  }
212  }
213 
214 
215 
216  const char* NrrdTbssImageReader
218  {
219  return m_FileName.c_str();
220  }
221 
223  ::SetFileName(const char* aFileName)
224  {
225  m_FileName = aFileName;
226  }
227 
228  const char* NrrdTbssImageReader
230  {
231  return m_FilePrefix.c_str();
232  }
233 
235  ::SetFilePrefix(const char* aFilePrefix)
236  {
237  m_FilePrefix = aFilePrefix;
238  }
239 
240  const char* NrrdTbssImageReader
242  {
243  return m_FilePattern.c_str();
244  }
245 
247  ::SetFilePattern(const char* aFilePattern)
248  {
249  m_FilePattern = aFilePattern;
250  }
251 
253  ::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
254  {
255 
256  // First check the extension
257  if( filename == "" )
258  return false;
259 
260  // check if image is serie
261  if( filePattern != "" && filePrefix != "" )
262  return false;
263 
264 
265  std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename);
266  ext = itksys::SystemTools::LowerCase(ext);
267 
268  if (ext == ".tbss")
269  {
271 
272  typedef itk::ImageFileReader<ImageType> FileReaderType;
274  reader->SetImageIO(io);
275  reader->SetFileName(filename);
276 
277  try
278  {
279  reader->Update();
280  }
281  catch(itk::ExceptionObject e)
282  {
283  MITK_INFO << e.GetDescription();
284  return false;
285  }
286 
287 
288  return true;
289  }
290  return false;
291  }
292 
293 } //namespace MITK
294 
295 #endif
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
Base of all data objects.
Definition: mitkBaseData.h:39
void SetFilePrefix(const char *aFilePrefix) override
Specify file prefix for the file(s) to load.
DataCollection - Class to facilitate loading/accessing structured data.
virtual void GenerateOutputInformation() override
const char * GetFileName() const override
const char * GetFilePrefix() const override
Get the specified file prefix for the file(s) to load.
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
void SetFilePattern(const char *aFilePattern) override
Specified file pattern for the file(s) to load. The sprintf format used to build filename from FilePr...
const char * GetFilePattern() const override
Get the specified file pattern for the file(s) to load. The sprintf format used to build filename fro...
static const std::string filename
void SetFileName(const char *aFileName) override
Specify the file to load.
virtual void GenerateData() override
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.