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