Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkDiffusionImageNrrdWriterService.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 __mitkDiffusionImageNrrdWriterService__cpp
18 #define __mitkDiffusionImageNrrdWriterService__cpp
19 
21 #include "itkMetaDataDictionary.h"
22 #include "itkMetaDataObject.h"
23 #include "itkNrrdImageIO.h"
24 #include "itkImageFileWriter.h"
25 #include "itksys/SystemTools.hxx"
27 #include "mitkImageCast.h"
28 #include <mitkLocaleSwitch.h>
29 
30 #include <iostream>
31 #include <fstream>
32 
33 
35  : AbstractFileWriter(mitk::Image::GetStaticNameOfClass(), CustomMimeType( mitk::DiffusionCoreIOMimeTypes::DWI_NRRD_MIMETYPE() ), mitk::DiffusionCoreIOMimeTypes::DWI_NRRD_MIMETYPE_DESCRIPTION())
36 {
38 }
39 
41  : AbstractFileWriter(other)
42 {
43 }
44 
46 {}
47 
49 {
50  mitk::Image::ConstPointer input = dynamic_cast<const mitk::Image *>(this->GetInput());
51 
53  mitk::CastToItkImage(input,itkImg);
54 
55  if (input.IsNull())
56  {
57  MITK_ERROR <<"Sorry, input to DiffusionImageNrrdWriterService is NULL!";
58  return;
59  }
60  if ( this->GetOutputLocation().empty() )
61  {
62  MITK_ERROR << "Sorry, filename has not been set!";
63  return ;
64  }
65  mitk::LocaleSwitch localeSwitch("C");
66 
67  char keybuffer[512];
68  char valbuffer[512];
69 
70  //itk::MetaDataDictionary dic = input->GetImage()->GetMetaDataDictionary();
71 
72  vnl_matrix_fixed<double,3,3> measurementFrame = mitk::DiffusionPropertyHelper::GetMeasurementFrame(input);
73  if (measurementFrame(0,0) || measurementFrame(0,1) || measurementFrame(0,2) ||
74  measurementFrame(1,0) || measurementFrame(1,1) || measurementFrame(1,2) ||
75  measurementFrame(2,0) || measurementFrame(2,1) || measurementFrame(2,2))
76  {
77  sprintf( valbuffer, " (%lf,%lf,%lf) (%lf,%lf,%lf) (%lf,%lf,%lf)", measurementFrame(0,0), measurementFrame(0,1), measurementFrame(0,2), measurementFrame(1,0), measurementFrame(1,1), measurementFrame(1,2), measurementFrame(2,0), measurementFrame(2,1), measurementFrame(2,2));
78  itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("measurement frame"),std::string(valbuffer));
79  }
80 
81  sprintf( valbuffer, "DWMRI");
82  itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("modality"),std::string(valbuffer));
83 
85  {
86  sprintf( valbuffer, "%1f", mitk::DiffusionPropertyHelper::GetReferenceBValue(input) );
87  itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("DWMRI_b-value"),std::string(valbuffer));
88  }
89 
90  for(unsigned int i=0; i<mitk::DiffusionPropertyHelper::GetGradientContainer(input)->Size(); i++)
91  {
92  sprintf( keybuffer, "DWMRI_gradient_%04d", i );
93 
94  /*if(itk::ExposeMetaData<std::string>(input->GetMetaDataDictionary(),
95  std::string(keybuffer),tmp))
96  continue;*/
97 
98  sprintf( valbuffer, "%1f %1f %1f", mitk::DiffusionPropertyHelper::GetGradientContainer(input)->ElementAt(i).get(0),
100 
101  itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer));
102  }
103 
104  typedef itk::VectorImage<short,3> ImageType;
105 
106  std::string ext = this->GetMimeType()->GetExtension(this->GetOutputLocation());
107  ext = itksys::SystemTools::LowerCase(ext);
108 
109  // default extension is .dwi
110  if( ext == "")
111  {
112  ext = ".nrrd";
113  this->SetOutputLocation(this->GetOutputLocation() + ext);
114  }
115 
116  if (ext == ".hdwi" || ext == ".nrrd" || ext == ".dwi")
117  {
118 
119  MITK_INFO << "Extension " << ext;
121  //io->SetNrrdVectorType( nrrdKindList );
122  io->SetFileType( itk::ImageIOBase::Binary );
123  io->UseCompressionOn();
124 
125  typedef itk::ImageFileWriter<ImageType> WriterType;
126  WriterType::Pointer nrrdWriter = WriterType::New();
127  nrrdWriter->UseInputMetaDataDictionaryOn();
128  nrrdWriter->SetInput( itkImg );
129  nrrdWriter->SetImageIO(io);
130  nrrdWriter->SetFileName(this->GetOutputLocation());
131  nrrdWriter->UseCompressionOn();
132  nrrdWriter->SetImageIO(io);
133  try
134  {
135  nrrdWriter->Update();
136  }
137  catch (itk::ExceptionObject e)
138  {
139  std::cout << e << std::endl;
140  throw;
141  }
142 
143  }
144 }
145 
147 {
148  return new DiffusionImageNrrdWriterService(*this);
149 }
150 
152 {
153  mitk::Image::ConstPointer input = dynamic_cast<const mitk::Image*>(this->GetInput());
154  if (input.IsNull() || !mitk::DiffusionPropertyHelper::IsDiffusionWeightedImage( input ) )
155  {
156  return Unsupported;
157  }
158  else
159  {
160  return Supported;
161  }
162 }
163 
164 #endif //__mitkDiffusionImageNrrdWriterService__cpp
const MeasurementFrameType & GetMeasurementFrame() const
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
map::core::discrete::Elements< 3 >::InternalImageType ImageType
Convenience class to temporarily change the current locale.
virtual ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
virtual void Write() override
Write the base data to the specified location or output stream.
Image class for storing images.
Definition: mitkImage.h:76
static const char * GetStaticNameOfClass()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
GradientDirectionsContainerType::Pointer GetGradientContainer() const
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
virtual mitk::DiffusionImageNrrdWriterService * Clone() const override
Base class for writing mitk::BaseData objects to files or streams.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.