Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkUSTelemedScanConverterPlugin.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "mitkImageWriteAccessor.h"
15 
17  : m_Plugin(nullptr), m_OutputImage(nullptr), m_OutputImageMutex(nullptr)
18 {
19 }
20 
22 {
23  ReleasePlugin();
24 }
25 
26 // -- internal Telemed API function
27 HRESULT __stdcall USTelemedScanConverterPlugin::QueryInterface(const IID& iid, void** ppv)
28 {
29  reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
30  return S_OK ;
31 }
32 
33 // -- internal Telemed API function
35 {
36  return InterlockedIncrement(&m_cRef) ;
37 }
38 
39 // -- internal Telemed API function
41 {
42  if (InterlockedDecrement(&m_cRef) == 0)
43  {
44  delete this ;
45  return 0 ;
46  }
47  return m_cRef ;
48 }
49 
51  PBYTE pBufferInterim,
52  int nInterimBufferLen,
53  PBYTE pBufferOut,
54  int nOutBufferLen,
55  int nOutX1,
56  int nOutY1,
57  int nOutX2,
58  int nOutY2
59  )
60 {
61  if ( m_OutputImage.IsNull() ) { return S_FALSE; };
62 
63  if ( m_OutputImageMutex.IsNotNull() ) { m_OutputImageMutex->Lock(); }
64 
65  // initialize mitk::Image with given image size on the first time
66  if ( ! m_OutputImage->IsInitialized() )
67  {
68  unsigned int dim[]={static_cast<unsigned int>(abs(nOutX2 - nOutX1)), static_cast<unsigned int>(abs(nOutY2 - nOutY1))}; // image dimensions
69 
70  m_OutputImage->Initialize(mitk::MakeScalarPixelType<BYTE>(), 2, dim);
71  }
72 
73  // lock the image for writing an copy the given buffer into the image then
74  m_OutputImage->SetSlice(pBufferOut);
75 
76  if ( m_OutputImageMutex.IsNotNull() ) { m_OutputImageMutex->Unlock(); }
77 
78  return S_OK;
79 }
80 
82 {
83  if (m_Plugin != nullptr)
84  {
85  // remove this callback from Telemed API plugin
86  m_Plugin->SetCallback(nullptr,USPC_BUFFER_INTERIM_OUTPUT);
87  }
88 }
89 
90 void USTelemedScanConverterPlugin::SetOutputImage(mitk::Image::Pointer outputImage, itk::FastMutexLock::Pointer outputImageMutex)
91 {
92  m_OutputImage = outputImage;
93  m_OutputImageMutex = outputImageMutex;
94 }
95 
97 {
98  // make sure that there is no scan converter plugin registered already
99  this->ReleasePlugin();
100 
101  HRESULT hr;
102 
103  // it is ok to call this method with a nullptr plugin to remove
104  // a previous callback
105  if (plugin == nullptr)
106  {
107  MITK_INFO("IUsgfwScanConverterPluginCB")("ScanConverterPlugin")
108  << "nullptr plugin set to the scan converter. The callback for the previous plugin is removed now.";
109  return S_OK;
110  }
111 
112  // get Telemed API plugin from the COM library
113  Usgfw2Lib::IUsgScanConverterPlugin* tmp_plugin;
114  hr = plugin->QueryInterface(__uuidof(Usgfw2Lib::IUsgScanConverterPlugin), (void**)&tmp_plugin);
115 
116  if (FAILED(hr))
117  {
118  MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin")
119  << "Could not query com interface for IUsgScanConverterPlugin (" << hr << ").";
120  return hr;
121  }
122 
123  // get the converter for scan lines from the COM library and
124  // save it as a member attribute
125  hr = tmp_plugin->get_ScanConverter((IUnknown**)&m_Plugin);
126 
127  if (FAILED(hr))
128  {
129  MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin")
130  << "Could not get ScanConverter from plugin (" << hr << ").";
131  return hr;
132  }
133 
134  SAFE_RELEASE(tmp_plugin);
135 
136  // now the callback can be set -> interface functions of this
137  // object will be called from now on when new image data is
138  // available
139  hr = m_Plugin->SetCallback(this,USPC_BUFFER_INTERIM_OUTPUT);
140 
141  if (FAILED(hr))
142  {
143  MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin")
144  << "Could not set callback for plugin (" << hr << ").";
145  return hr;
146  }
147 
148  return S_OK;
149 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
STDMETHOD() SetScanConverterPlugin(IDispatch *plugin)
itk::FastMutexLock::Pointer m_OutputImageMutex
#define MITK_WARN
Definition: mitkLogMacros.h:19
STDMETHOD() InterimOutBufferCB(PBYTE pBufferInterim, int nInterimBufferLen, PBYTE pBufferOut, int nOutBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2)
virtual HRESULT __stdcall QueryInterface(const IID &iid, void **ppv)
#define SAFE_RELEASE(x)
void SetOutputImage(mitk::Image::Pointer outputImage, itk::FastMutexLock::Pointer outputImageMutex=0)