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
mitkDiffusionCoreIOMimeTypes.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 "mitkIOMimeTypes.h"
19 #include <itksys/SystemTools.hxx>
20 #include <itkNrrdImageIO.h>
21 #include <itkMetaDataDictionary.h>
22 #include <itkMetaDataObject.h>
23 #include <mitkLogMacros.h>
24 
25 namespace mitk
26 {
27 
28 std::vector<CustomMimeType*> DiffusionCoreIOMimeTypes::Get()
29 {
30  std::vector<CustomMimeType*> mimeTypes;
31 
32  // order matters here (descending rank for mime types)
33 
34  mimeTypes.push_back(DWI_NRRD_MIMETYPE().Clone());
35  mimeTypes.push_back(DWI_NIFTI_MIMETYPE().Clone());
36  mimeTypes.push_back(DTI_MIMETYPE().Clone());
37  mimeTypes.push_back(QBI_MIMETYPE().Clone());
38 
39  return mimeTypes;
40 }
41 
42 // Mime Types
43 
45  : CustomMimeType(DWI_NRRD_MIMETYPE_NAME())
46 {
47  std::string category = "Diffusion Weighted Image";
48  this->SetCategory(category);
49  this->SetComment("Diffusion Weighted Images");
50 
51  this->AddExtension("dwi");
52  this->AddExtension("hdwi");
53  this->AddExtension("nrrd");
54 }
55 
57 {
58  bool canRead( CustomMimeType::AppliesTo(path) );
59 
60  // fix for bug 18572
61  // Currently this function is called for writing as well as reading, in that case
62  // the image information can of course not be read
63  // This is a bug, this function should only be called for reading.
64  if( ! itksys::SystemTools::FileExists( path.c_str() ) )
65  {
66  return canRead;
67  }
68  //end fix for bug 18572
69 
70  std::string ext = this->GetExtension( path );
71  ext = itksys::SystemTools::LowerCase( ext );
72 
73  // Simple NRRD files should only be considered for this mime type if they contain
74  // corresponding tags
75  if( ext == ".nrrd" )
76  {
78  io->SetFileName(path);
79  try
80  {
81  io->ReadImageInformation();
82 
83  itk::MetaDataDictionary imgMetaDictionary = io->GetMetaDataDictionary();
84  std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
85  std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
86  std::string metaString;
87 
88  for (; itKey != imgMetaKeys.end(); itKey ++)
89  {
90  itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);
91  if (itKey->find("modality") != std::string::npos)
92  {
93  if (metaString.find("DWMRI") != std::string::npos)
94  {
95  return canRead;
96  }
97  }
98  }
99 
100  }
101  catch( const itk::ExceptionObject &e )
102  {
103  MITK_ERROR << "ITK Exception: " << e.what();
104  }
105  canRead = false;
106  }
107 
108  return canRead;
109 }
110 
112 {
113  return new DiffusionImageNrrdMimeType(*this);
114 }
115 
116 
118 {
120 }
121 
124 {
125  std::string category = "Diffusion Weighted Image";
126  this->SetCategory(category);
127  this->SetComment("Diffusion Weighted Images");
128  this->AddExtension("fsl");
129  this->AddExtension("fslgz");
130  this->AddExtension("nii");
131  this->AddExtension("nii.gz");
132 }
133 
135 {
136  bool canRead(CustomMimeType::AppliesTo(path));
137 
138  // fix for bug 18572
139  // Currently this function is called for writing as well as reading, in that case
140  // the image information can of course not be read
141  // This is a bug, this function should only be called for reading.
142  if (!itksys::SystemTools::FileExists(path.c_str()))
143  {
144  return canRead;
145  }
146  //end fix for bug 18572
147 
148  std::string ext = this->GetExtension(path);
149  ext = itksys::SystemTools::LowerCase(ext);
150 
151  // Nifti files should only be considered for this mime type if they are
152  // accompanied by bvecs and bvals files defining the diffusion information
153  if (ext == ".nii" || ext == ".nii.gz")
154  {
155  std::string base = itksys::SystemTools::GetFilenamePath(path) + "/"
156  + this->GetFilenameWithoutExtension(path);
157 
158  if (itksys::SystemTools::FileExists(std::string(base + ".bvec").c_str())
159  && itksys::SystemTools::FileExists(std::string(base + ".bval").c_str())
160  )
161  {
162  return canRead;
163  }
164 
165  if (itksys::SystemTools::FileExists(std::string(base + ".bvecs").c_str())
166  && itksys::SystemTools::FileExists(std::string(base + ".bvals").c_str())
167  )
168  {
169  return canRead;
170  }
171 
172  canRead = false;
173  }
174 
175  return canRead;
176 }
177 
179 {
180  return new DiffusionImageNiftiMimeType(*this);
181 }
182 
183 
185 {
187 }
188 
190 {
191  CustomMimeType mimeType(DTI_MIMETYPE_NAME());
192  std::string category = "Tensor Images";
193  mimeType.SetComment("Diffusion Tensor Images");
194  mimeType.SetCategory(category);
195  mimeType.AddExtension("dti");
196  mimeType.AddExtension("hdti");
197  return mimeType;
198 }
199 
201 {
202  CustomMimeType mimeType(QBI_MIMETYPE_NAME());
203  std::string category = "Q-Ball Images";
204  mimeType.SetComment("Diffusion Q-Ball Images");
205  mimeType.SetCategory(category);
206  mimeType.AddExtension("qbi");
207  mimeType.AddExtension("hqbi");
208  return mimeType;
209 }
210 
211 // Names
213 {
214  static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".dwi";
215  return name;
216 }
217 
219 {
220  static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".fsl";
221  return name;
222 }
223 
225 {
226  static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".dti";
227  return name;
228 }
229 
231 {
232  static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".qbi";
233  return name;
234 }
235 
236 // Descriptions
237 
239 {
240  static std::string description = "Diffusion Weighted Images";
241  return description;
242 }
243 
245 {
246  static std::string description = "Diffusion Weighted Images";
247  return description;
248 }
249 
251 {
252  static std::string description = "Diffusion Tensor Images";
253  return description;
254 }
255 
257 {
258  static std::string description = "Q-Ball Images";
259  return description;
260 }
261 
262 }
itk::SmartPointer< Self > Pointer
Pointer Clone() const
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static DiffusionImageNrrdMimeType DWI_NRRD_MIMETYPE()
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool AppliesTo(const std::string &path) const
Checks if the MimeType can handle file at the given location.
void SetComment(const std::string &comment)
virtual bool AppliesTo(const std::string &path) const override
Checks if the MimeType can handle file at the given location.
virtual bool AppliesTo(const std::string &path) const override
Checks if the MimeType can handle file at the given location.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
static std::string DEFAULT_BASE_NAME()
virtual DiffusionImageNiftiMimeType * Clone() const override
void AddExtension(const std::string &extension)
void SetCategory(const std::string &category)
static std::vector< CustomMimeType * > Get()
virtual DiffusionImageNrrdMimeType * Clone() const override
static DiffusionImageNiftiMimeType DWI_NIFTI_MIMETYPE()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.