diff --git a/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.cpp b/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.cpp index fb7b688b67..cc18476cd7 100644 --- a/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.cpp +++ b/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.cpp @@ -1,149 +1,153 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkUSTelemedScanConverterPlugin.h" #include "mitkImageWriteAccessor.h" USTelemedScanConverterPlugin::USTelemedScanConverterPlugin( ) - : m_Plugin(NULL), m_OutputImage(NULL) + : m_Plugin(NULL), m_OutputImage(NULL), m_OutputImageMutex(NULL) { } USTelemedScanConverterPlugin::~USTelemedScanConverterPlugin( ) { ReleasePlugin(); } // -- internal Telemed API function HRESULT __stdcall USTelemedScanConverterPlugin::QueryInterface(const IID& iid, void** ppv) { reinterpret_cast(*ppv)->AddRef() ; return S_OK ; } // -- internal Telemed API function ULONG __stdcall USTelemedScanConverterPlugin::AddRef() { return InterlockedIncrement(&m_cRef) ; } // -- internal Telemed API function ULONG __stdcall USTelemedScanConverterPlugin::Release() { if (InterlockedDecrement(&m_cRef) == 0) { delete this ; return 0 ; } return m_cRef ; } STDMETHODIMP USTelemedScanConverterPlugin::InterimOutBufferCB ( PBYTE pBufferInterim, int nInterimBufferLen, PBYTE pBufferOut, int nOutBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) { - if ( ! m_OutputImage ) { return S_FALSE; }; + if ( m_OutputImage.IsNull() ) { return S_FALSE; }; + + if ( m_OutputImageMutex.IsNotNull() ) { m_OutputImageMutex->Lock(); } // initialize mitk::Image with given image size on the first time if ( ! m_OutputImage->IsInitialized() ) { unsigned int dim[]={(nOutX2 - nOutX1), (nOutY2 - nOutY1)}; // image dimensions m_OutputImage->Initialize(mitk::MakeScalarPixelType(), 2, dim); } // lock the image for writing an copy the given buffer into the image then - mitk::ImageWriteAccessor imageWriteAccessor(m_OutputImage, m_OutputImage->GetSliceData(0,0,0)); m_OutputImage->SetSlice(pBufferOut); + if ( m_OutputImageMutex.IsNotNull() ) { m_OutputImageMutex->Unlock(); } + return S_OK; } void USTelemedScanConverterPlugin::ReleasePlugin() { if (m_Plugin != NULL) { // remove this callback from Telemed API plugin m_Plugin->SetCallback(NULL,USPC_BUFFER_INTERIM_OUTPUT); } } -void USTelemedScanConverterPlugin::SetOutputImage(mitk::Image::Pointer outputImage) +void USTelemedScanConverterPlugin::SetOutputImage(mitk::Image::Pointer outputImage, itk::FastMutexLock::Pointer outputImageMutex) { m_OutputImage = outputImage; + m_OutputImageMutex = outputImageMutex; } STDMETHODIMP USTelemedScanConverterPlugin::SetScanConverterPlugin(IDispatch* plugin) { // make sure that there is no scan converter plugin registered already this->ReleasePlugin(); HRESULT hr; // it is ok to call this method with a NULL plugin to remove // a previous callback if (plugin == NULL) { MITK_INFO("IUsgfwScanConverterPluginCB")("ScanConverterPlugin") << "NULL plugin set to the scan converter. The callback for the previous plugin is removed now."; return S_OK; } // get Telemed API plugin from the COM library Usgfw2Lib::IUsgScanConverterPlugin* tmp_plugin; hr = plugin->QueryInterface(__uuidof(Usgfw2Lib::IUsgScanConverterPlugin), (void**)&tmp_plugin); if (FAILED(hr)) { MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin") << "Could not query com interface for IUsgScanConverterPlugin (" << hr << ")."; return hr; } // get the converter for scan lines from the COM library and // save it as a member attribute hr = tmp_plugin->get_ScanConverter((IUnknown**)&m_Plugin); if (FAILED(hr)) { MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin") << "Could not get ScanConverter from plugin (" << hr << ")."; return hr; } SAFE_RELEASE(tmp_plugin); // now the callback can be set -> interface functions of this // object will be called from now on when new image data is // available hr = m_Plugin->SetCallback(this,USPC_BUFFER_INTERIM_OUTPUT); if (FAILED(hr)) { MITK_WARN("IUsgfwScanConverterPluginCB")("ScanConverterPlugin") << "Could not set callback for plugin (" << hr << ")."; return hr; } return S_OK; } \ No newline at end of file diff --git a/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.h b/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.h index c30e86e54e..1bd71bcf06 100644 --- a/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.h +++ b/Modules/US/USHardwareTelemed/mitkUSTelemedScanConverterPlugin.h @@ -1,161 +1,170 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKUSTelemedScanConverterPlugin_H_HEADER_INCLUDED_ #define MITKUSTelemedScanConverterPlugin_H_HEADER_INCLUDED_ #include #include #include #include #include #include "mitkUSTelemedSDKHeader.h" - #include "mitkImage.h" +#include "itkFastMutexLock.h" + /** * \brief Telemed API plugin for getting images from scan lines. * Implements a COM interface whereat only the function InterimOutBufferCB * is used for copying given image buffer into a mitk::Image. * * A pointer to this mitk::Image must be set by calling * mitk::USTelemedScanConverterPlugin::SetOutputImage() first. * The image content is then updated every time the Telemed API calls * the implemented callback function of this class. * * For more infomration about the implemented COM interface refer to the * Telemed API documentation. */ class USTelemedScanConverterPlugin : public IUsgfwScanConverterPluginCB { public: USTelemedScanConverterPlugin( ); ~USTelemedScanConverterPlugin( ); // internal functions for Telemed API virtual HRESULT __stdcall QueryInterface(const IID& iid,void** ppv); virtual ULONG __stdcall AddRef(); virtual ULONG __stdcall Release(); /** * Setter for a pointer to a mitk::Image in which the current * image buffer from the Telemed API will be stored at every * API callback. This function must be called before image data * can be got from this class. + * A pointer to a mutex can be set in addition. This mutex will + * be locked on every writing to the given image. */ - void SetOutputImage(mitk::Image::Pointer outputImage); + void SetOutputImage(mitk::Image::Pointer outputImage, itk::FastMutexLock::Pointer outputImageMutex = 0); // receives pointers to input and output media samples STDMETHOD(SampleCB) ( IMediaSample *pSampleIn, IMediaSample *pSampleOut, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) {return S_OK;} // receives pointers to input and output sample buffers STDMETHOD(BufferCB) ( PBYTE pBufferIn, int nInBufferLen, PBYTE pBufferOut, int nOutBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) {return S_OK;} // receives pointers to input and intermediate sample buffers STDMETHOD(InInterimBufferCB) ( PBYTE pBufferIn, int nInBufferLen, PBYTE pBufferInterim, int nInterimBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) {return S_OK;} // receves pointers to input media sample and intermediatesample buffer STDMETHOD(InInterimSampleCB) ( IMediaSample *pSampleIn, PBYTE pBufferInterim, int nInterimBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) {return S_OK;} // receives pointers to output and intermediate sample buffers STDMETHOD(InterimOutBufferCB) ( PBYTE pBufferInterim, int nInterimBufferLen, PBYTE pBufferOut, int nOutBufferLen, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ); // receives pointers to output media sample and intermediate sample buffer STDMETHOD(InterimOutSampleCB) ( PBYTE pBufferInterim, int nInterimBufferLen, IMediaSample *pSampleIn, int nOutX1, int nOutY1, int nOutX2, int nOutY2 ) {return S_OK;} // receives conversion parameter change pin index // if parameter is negative parameter was changed by some filter interface STDMETHOD(ParameterCB) ( int nPin ) { return S_OK; } STDMETHOD(SetScanConverterPlugin)(IDispatch* plugin); //STDMETHOD(getSource)(LONG* plugin); protected: /** * Remove Telemed API callback and release and delete m_Plugin attribute. */ void ReleasePlugin( ); /** * Telemed API object for handling callbacks on new image data. */ IUsgfwScanConverterPlugin* m_Plugin; /** * Pointer to mitk::Image in which the current image buffer * from the Telemed API will be stored at every API callback. */ mitk::Image::Pointer m_OutputImage; + /** + * Mutex for the output image. Has to be set together with the + * output image via SetOutputImage(). + */ + itk::FastMutexLock::Pointer m_OutputImageMutex; + private: long m_cRef ; }; #endif // MITKUSTelemedScanConverterPlugin_H_HEADER_INCLUDED_ \ No newline at end of file