Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkUSImageLoggingFilter.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 
18 #include <mitkIOUtil.h>
19 #include <mitkUIDGenerator.h>
20 #include <Poco/Path.h>
21 
22 #include <algorithm>
23 #include <mitkIOMimeTypes.h>
24 #include <mitkCoreServices.h>
25 #include <mitkIMimeTypeProvider.h>
26 
27 
29  m_ImageExtension(".nrrd")
30 {
31 }
32 
34 {
35 }
36 
38 {
39  mitk::Image::ConstPointer inputImage = this->GetInput();
40  mitk::Image::Pointer outputImage = this->GetOutput();
41 
42  if(inputImage.IsNull() || inputImage->IsEmpty())
43  {
44  MITK_WARN << "Input image is not valid. Cannot save image!";
45  return;
46  }
47 
48  //a clone is needed for a output and to store it.
49  mitk::Image::Pointer inputClone = inputImage->Clone();
50 
51 
52  //simply redirecy the input to the output
53  //this->SetNumberOfRequiredOutputs(1);
54  //this->SetNthOutput(0, inputClone->Clone());
55  //outputImage->Graft(inputImage);
56  //this->SetOutput(this->GetInput());
57  /*if (!this->GetOutput()->IsInitialized())
58  {
59  this->SetNumberOfRequiredOutputs(1);
60  mitk::Image::Pointer newOutput = mitk::Image::New();
61  this->SetNthOutput(0, newOutput);
62  }
63  memcpy(this->GetOutput(),this->GetInput());*/
64 
65  //this->SetNthOutput(0,inputImage.);
66  //this->AllocateOutputs();
67  //this->GraftOutput(inputClone);
68 
69  /*
70  if (!this->GetOutput()->IsInitialized())
71  {
72  mitk::Image::Pointer newOutput = mitk::Image::New();
73  this->SetNthOutput(0, newOutput);
74  }
75  this->GetOutput()Graft(this->GetInput());
76  */
77 
78 
79  m_LoggedImages.push_back(inputClone);
80  m_LoggedMITKSystemTimes.push_back(m_SystemTimeClock->GetCurrentStamp());
81 
82 }
83 
85 {
86  m_LoggedMessages.insert(std::make_pair(static_cast<int>(m_LoggedImages.size()-1),message));
87 }
88 
90 {
91 std::vector<std::string> dummy1;
92 std::string dummy2;
93 this->SaveImages(path,dummy1,dummy2);
94 }
95 
96 void mitk::USImageLoggingFilter::SaveImages(std::string path, std::vector<std::string>& filenames, std::string& csvFileName)
97 {
98  filenames = std::vector<std::string>();
99 
100  //test if path is valid
101  Poco::Path testPath(path);
102  if(!testPath.isDirectory())
103  {
104  mitkThrow() << "Attemting to write to directory " << path << " which is not valid! Aborting!";
105  }
106 
107  //generate a unique ID which is used as part of the filenames, so we avoid to overwrite old files by mistake.
109  std::string uniqueID = myGen.GetUID();
110 
111  //first: write the images
112  for(size_t i=0; i<m_LoggedImages.size(); i++)
113  {
114  std::stringstream name;
115  name << path << uniqueID << "_Image_" << i << m_ImageExtension;
116  mitk::IOUtil::Save(m_LoggedImages.at(i),name.str());
117  filenames.push_back(name.str());
118  }
119 
120  //then: write a csv file which contains comments to all the images
121 
122  //open file
123  std::stringstream csvFilenameStream;
124  csvFilenameStream << path << uniqueID << "_ImageMessages.csv";
125  csvFileName = csvFilenameStream.str();
126  std::filebuf fb;
127  fb.open (csvFileName.c_str(),std::ios::out);
128  std::ostream os(&fb);
129  os.precision(15); //set high precision to avoid loss of digits
130 
131  //write header
132  os << "image filename; MITK system timestamp; message\n";
133 
134  //write data
135  for(size_t i=0; i<m_LoggedImages.size(); i++)
136  {
137  std::map<int, std::string>::iterator it = m_LoggedMessages.find(i);
138  if (m_LoggedMessages.empty() || (it == m_LoggedMessages.end())) os << filenames.at(i) << ";" << m_LoggedMITKSystemTimes.at(i) << ";" << "" << "\n";
139  else os << filenames.at(i) << ";" << m_LoggedMITKSystemTimes.at(i) << ";" << it->second << "\n";
140  }
141 
142  //close file
143  fb.close();
144 }
145 
147  {
148  if(extension.compare(0,1,".") == 0)
149  extension = extension.substr(1,extension.size()-1);
150 
152 
153  std::vector<MimeType> mimeTypes = mimeTypeProvider->GetMimeTypesForCategory(IOMimeTypes::CATEGORY_IMAGES());
154 
155  for(std::vector<MimeType>::size_type i = 0 ; i< mimeTypes.size() ; ++i)
156  {
157  std::vector<std::string> extensions = mimeTypes[i].GetExtensions();
158  if (std::find(extensions.begin(), extensions.end(), extension) != extensions.end())
159  {
160  m_ImageExtension = "."+extension;
161  return true;
162  }
163  }
164  return false;
165  }
void SaveImages(std::string path, std::vector< std::string > &imageFilenames, std::string &csvFileName)
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
Generated unique IDs.
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
virtual void GenerateData() override
static std::string CATEGORY_IMAGES()
RealTimeClock is a superclass to WindowsRealTimeClock, LinuxRealTimeClock, etc.
#define MITK_WARN
Definition: mitkLogMacros.h:23
bool SetImageFilesExtension(std::string extension)
virtual std::vector< MimeType > GetMimeTypesForCategory(const std::string &category) const =0
#define mitkThrow()
A RAII helper class for core service objects.
void AddMessageToCurrentImage(std::string message)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.