Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUSTelemedImageSource.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 "mitkUSTelemedSDKHeader.h"
19 #include "MITKUSTelemedScanConverterPlugin.h"
20 #include "mitkImageReadAccessor.h"
21 
23  : m_Image(mitk::Image::New()),
24  m_ImageMutex(itk::FastMutexLock::New()),
25  m_Plugin(0),
26  m_PluginCallback(0),
27  m_UsgDataView(0),
28  m_ImageProperties(0),
29  m_DepthProperties(0),
30  m_OldnXPelsPerUnit(0),
31  m_OldnYPelsPerUnit(0)
32 
33 {
34 
35 }
36 
38 {
39  SAFE_RELEASE(m_PluginCallback);
40  SAFE_RELEASE(m_Plugin);
41  SAFE_RELEASE(m_ImageProperties);
42  SAFE_RELEASE(m_DepthProperties);
43 }
44 
46 {
47  if ( image.IsNull() ) { image = mitk::Image::New(); }
48 
49  //get the actual resolution to check if it changed. We have to do this every time because the geometry takes a few frames to adapt
50  Usgfw2Lib::tagImageResolution resolutionInMetersActual;
51  m_ImageProperties->GetResolution(&resolutionInMetersActual, 0);
52  if (m_OldnXPelsPerUnit != resolutionInMetersActual.nXPelsPerUnit || m_OldnYPelsPerUnit != resolutionInMetersActual.nYPelsPerUnit)
53  {
54  //we can only update if the image exists and has a geometry
55  if (m_Image.IsNotNull() && m_Image->GetGeometry() != NULL)
56  {
57  m_OldnXPelsPerUnit = resolutionInMetersActual.nXPelsPerUnit;
58  m_OldnYPelsPerUnit = resolutionInMetersActual.nYPelsPerUnit;
59  UpdateImageGeometry();
60  }
61 
62  }
63  //now update image
64  if ( m_Image->IsInitialized() )
65  {
66  m_ImageMutex->Lock();
67 
68  // copy contents of the given image into the member variable
69  image->Initialize(m_Image->GetPixelType(), m_Image->GetDimension(), m_Image->GetDimensions());
70  mitk::ImageReadAccessor inputReadAccessor(m_Image, m_Image->GetSliceData(0,0,0));
71  image->SetSlice(inputReadAccessor.GetData());
72  image->SetGeometry(m_Image->GetGeometry());
73 
74  m_ImageMutex->Unlock();
75  }
76 
77 }
78 
80 {
81  Usgfw2Lib::tagPixelsOrigin origin = Usgfw2Lib::tagPixelsOrigin();
82  Usgfw2Lib::tagImageResolution resolutionInMeters;
83  m_ImageProperties->GetResolution(&resolutionInMeters,0);
84 
85  mitk::Vector3D spacing;
86  spacing[0] = ((double)1 / resolutionInMeters.nXPelsPerUnit) * 1000; //conversion: meters to millimeters
87  spacing[1] = ((double)1 / resolutionInMeters.nXPelsPerUnit) * 1000; //conversion: meters to millimeters
88  spacing[2] = 1;
89 
90  m_ImageMutex->Lock();
91  if(m_Image.IsNotNull() && (m_Image->GetGeometry()!=NULL))
92  {
93  m_Image->GetGeometry()->SetSpacing(spacing);
94  m_Image->GetGeometry()->Modified();
95  }
96  else
97  {MITK_WARN << "image or geometry was NULL, can't adapt geometry";}
98  m_ImageMutex->Unlock();
99 
100  MITK_DEBUG << "UpdateImageGeometry called!";
101  MITK_DEBUG << "depth: " << m_DepthProperties->GetCurrent();
102  MITK_DEBUG << "new spacing: " << spacing;
103 }
104 
105 bool mitk::USTelemedImageSource::CreateAndConnectConverterPlugin(Usgfw2Lib::IUsgDataView* usgDataView, Usgfw2Lib::tagScanMode scanMode)
106 {
107  IUnknown* tmp_obj = NULL;
108 
109  // create control object from Telemed API
110  mitk::telemed::CreateUsgControl( usgDataView, Usgfw2Lib::IID_IUsgScanConverterPlugin, scanMode, 0, (void**)&tmp_obj );
111  if ( ! tmp_obj )
112  {
113  MITK_ERROR("USImageSource")("USTelemedImageSource") << "Could not create scan converter plugin.";
114  return false;
115  }
116 
117  // create the callback object for the scan conversion
118  if ( ! m_PluginCallback )
119  {
120  m_PluginCallback = new USTelemedScanConverterPlugin();
121 
122  // current image buffer should be copied to m_Image at every callback
123  m_PluginCallback->SetOutputImage(m_Image.GetPointer(), m_ImageMutex);
124  }
125  else
126  {
127  // make sure that the scan converter plugin is not set
128  // to the plugin callback any longer
129  m_PluginCallback->SetScanConverterPlugin(0);
130  }
131 
132  // now the ScanConverterPlugin can be created and set as plugin
133  SAFE_RELEASE(m_Plugin);
134  m_Plugin = (Usgfw2Lib::IUsgScanConverterPlugin*)tmp_obj;
135  m_PluginCallback->SetScanConverterPlugin(m_Plugin);
136 
137  //last: create some connections which are needed inside this class for communication with the telemed device
138  m_UsgDataView = usgDataView;
139 
140  // create telemed controls
141  if (!m_DepthProperties) {CREATE_TelemedControl(m_DepthProperties, m_UsgDataView, Usgfw2Lib::IID_IUsgDepth, Usgfw2Lib::IUsgDepth, Usgfw2Lib::SCAN_MODE_B);}
142  if (!m_ImageProperties) {CREATE_TelemedControl(m_ImageProperties, m_UsgDataView, Usgfw2Lib::IID_IUsgImageProperties, Usgfw2Lib::IUsgImageProperties, Usgfw2Lib::SCAN_MODE_B);}
143 
144  return true;
145 }
#define CREATE_TelemedControl(control, dataView, iidType, type, scanMode)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
DataCollection - Class to facilitate loading/accessing structured data.
virtual void GetNextRawImage(mitk::Image::Pointer &)
#define MITK_WARN
Definition: mitkLogMacros.h:23
Image class for storing images.
Definition: mitkImage.h:76
bool CreateUsgControl(Usgfw2Lib::IUsgDataView *dataView, const IID &typeId, ULONG scanMode, ULONG streamId, void **ctrl)
static Pointer New()
#define SAFE_RELEASE(x)
Telemed API plugin for getting images from scan lines. Implements a COM interface whereat only the fu...
bool CreateAndConnectConverterPlugin(Usgfw2Lib::IUsgDataView *, Usgfw2Lib::tagScanMode)
Connect this object to the Telemed API. This method is for being used by mitk::USTelemedDevice.
ImageReadAccessor class to get locked read access for a particular image part.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.