diff --git a/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp b/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp index f92fc78268..52cfe7cc4e 100644 --- a/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp +++ b/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp @@ -1,111 +1,111 @@ /*=================================================================== 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 #include #include #include // itk filter for reference computation #include #include #include using namespace mitk; /** This function is testing the class mitk::OclContextManager. */ int mitkOclBinaryThresholdImageFilterTest( int argc, char* argv[] ) { MITK_TEST_BEGIN("mitkOclBinaryThresholdImageFilterTest"); us::ServiceReference ref = GetModuleContext()->GetServiceReference(); MITK_TEST_CONDITION_REQUIRED( ref != 0, "Got valid ServiceReference" ); OclResourceService* resources = GetModuleContext()->GetService(ref); MITK_TEST_CONDITION_REQUIRED( resources != NULL, "OpenCL Resource service available." ); cl_context gpuContext = resources->GetContext(); MITK_TEST_CONDITION_REQUIRED( gpuContext != NULL, "Got not-null OpenCL context."); cl_device_id gpuDevice = resources->GetCurrentDevice(); MITK_TEST_CONDITION_REQUIRED( gpuDevice != NULL, "Got not-null OpenCL device."); //Create a random reference image mitk::Image::Pointer inputImage = mitk::ImageGenerator::GenerateRandomImage(119, 204, 52, 1, // dimension 1.0f, 1.0f, 1.0f, // spacing 255, 0); // max, min MITK_TEST_CONDITION_REQUIRED( inputImage.IsNotNull(), "Input (random) mitk::Image object instantiated."); // FIXME: could also be random values int upperThr = 255; int lowerThr = 60; int outsideVal = 0; int insideVal = 100; - mitk::OclBinaryThresholdImageFilter* oclFilter = new mitk::OclBinaryThresholdImageFilter; - MITK_TEST_CONDITION_REQUIRED( oclFilter != NULL, "Filter was created. "); + mitk::OclBinaryThresholdImageFilter::Pointer oclFilter = mitk::OclBinaryThresholdImageFilter::New(); + MITK_TEST_CONDITION_REQUIRED( oclFilter.GetPointer() != NULL, "Filter was created. "); oclFilter->SetInput( inputImage ); oclFilter->SetUpperThreshold( upperThr ); oclFilter->SetLowerThreshold( lowerThr ); oclFilter->SetOutsideValue( outsideVal ); oclFilter->SetInsideValue( insideVal ); oclFilter->Update(); mitk::Image::Pointer outputImage = mitk::Image::New(); outputImage = oclFilter->GetOutput(); MITK_TEST_CONDITION_REQUIRED( outputImage.IsNotNull(), "Filter returned an not-NULL image. "); // reference computation typedef itk::Image< unsigned char, 3> ImageType; typedef itk::BinaryThresholdImageFilter< ImageType, ImageType > ThresholdFilterType; ImageType::Pointer itkInputImage = ImageType::New(); CastToItkImage( inputImage, itkInputImage ); ThresholdFilterType::Pointer refThrFilter = ThresholdFilterType::New(); refThrFilter->SetInput( itkInputImage ); refThrFilter->SetLowerThreshold( lowerThr ); refThrFilter->SetUpperThreshold( upperThr ); refThrFilter->SetOutsideValue( outsideVal ); refThrFilter->SetInsideValue( insideVal ); typedef itk::SubtractImageFilter< ImageType, ImageType > SubtractFilterType; SubtractFilterType::Pointer subFilt = SubtractFilterType::New(); ImageType::Pointer gpuReferenceImage = ImageType::New(); CastToItkImage( oclFilter->GetOutput() ,gpuReferenceImage ); subFilt->SetInput1( refThrFilter->GetOutput() ); subFilt->SetInput2( gpuReferenceImage ); typedef itk::StatisticsImageFilter< ImageType > StatFilterType; StatFilterType::Pointer stats = StatFilterType::New(); stats->SetInput( subFilt->GetOutput() ); stats->Update(); MITK_TEST_CONDITION( stats->GetMaximum() == 0, "Maximal value in the difference image is 0."); MITK_TEST_CONDITION( stats->GetMinimum() == 0, "Minimal value in the difference image is 0.") MITK_TEST_END(); -} \ No newline at end of file +} diff --git a/Modules/OpenCL/Testing/mitkOclReferenceCountTest.cpp b/Modules/OpenCL/Testing/mitkOclReferenceCountTest.cpp index 180c76af11..eb02c9dd97 100644 --- a/Modules/OpenCL/Testing/mitkOclReferenceCountTest.cpp +++ b/Modules/OpenCL/Testing/mitkOclReferenceCountTest.cpp @@ -1,92 +1,92 @@ /*=================================================================== 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 #include #include #include // itk filter for reference computation #include #include #include using namespace mitk; /** This function is testing the OclFilter class and the OpenCL resource service. To prevent segmentation faults a mutexed reference counter is implemented in the resource service. It tracks the number of opencl program references for the corresponding filter and delete only the opencl programm if the reference count reaches 0. Every new instance of a filter increases the reference count by 1. This test runs successfull if the 2 filters are initialized, run and deleted without any crash. */ int mitkOclReferenceCountTest( int argc, char* argv[] ) { MITK_TEST_BEGIN("mitkOclReferenceCountTest"); // instancate uService us::ServiceReference ref = GetModuleContext()->GetServiceReference(); OclResourceService* resources = GetModuleContext()->GetService(ref); cl_context gpuContext = resources->GetContext(); cl_device_id gpuDevice = resources->GetCurrentDevice(); //Create a random reference image mitk::Image::Pointer inputImage = mitk::ImageGenerator::GenerateRandomImage(119, 204, 52, 1, // dimension 1.0f, 1.0f, 1.0f, // spacing 255, 0); // max, min int upperThr = 255; int lowerThr = 60; int outsideVal = 0; int insideVal = 100; - mitk::OclBinaryThresholdImageFilter* oclFilter1 = new mitk::OclBinaryThresholdImageFilter; + mitk::OclBinaryThresholdImageFilter::Pointer oclFilter1 = mitk::OclBinaryThresholdImageFilter::New(); oclFilter1->SetInput( inputImage ); oclFilter1->SetUpperThreshold( upperThr ); oclFilter1->SetLowerThreshold( lowerThr ); oclFilter1->SetOutsideValue( outsideVal ); oclFilter1->SetInsideValue( insideVal ); oclFilter1->Update(); mitk::Image::Pointer outputImage1 = mitk::Image::New(); outputImage1 = oclFilter1->GetOutput(); - mitk::OclBinaryThresholdImageFilter* oclFilter2 = new mitk::OclBinaryThresholdImageFilter; + mitk::OclBinaryThresholdImageFilter::Pointer oclFilter2 = mitk::OclBinaryThresholdImageFilter::New(); oclFilter2->SetInput( inputImage ); oclFilter2->SetUpperThreshold( upperThr ); oclFilter2->SetLowerThreshold( lowerThr ); oclFilter2->SetOutsideValue( outsideVal ); oclFilter2->SetInsideValue( insideVal ); oclFilter2->Update(); mitk::Image::Pointer outputImage2 = mitk::Image::New(); outputImage2 = oclFilter2->GetOutput(); // delete filters - delete oclFilter1; - delete oclFilter2; + oclFilter1 = NULL; + oclFilter2 = NULL; // this is only visible if the delete did not cause a segmentation fault // it is always true and successfull if the program reaches this state MITK_TEST_CONDITION_REQUIRED( true, "2 Filters deleted without a crash -> success "); MITK_TEST_END(); -} \ No newline at end of file +} diff --git a/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.cpp b/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.cpp index c451265b37..88f7a7c06e 100644 --- a/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.cpp +++ b/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.cpp @@ -1,104 +1,104 @@ /*=================================================================== 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 "mitkOclBinaryThresholdImageFilter.h" #include "usServiceReference.h" mitk::OclBinaryThresholdImageFilter::OclBinaryThresholdImageFilter() : m_ckBinaryThreshold( NULL ) { this->AddSourceFile("BinaryThresholdFilter.cl"); this->m_FilterID = "BinaryThreshold"; this->m_LowerThreshold = 10; this->m_UpperThreshold = 200; this->m_InsideValue = 100; this->m_OutsideValue = 0; } mitk::OclBinaryThresholdImageFilter::~OclBinaryThresholdImageFilter() { if ( this->m_ckBinaryThreshold ) { clReleaseKernel( m_ckBinaryThreshold ); } } void mitk::OclBinaryThresholdImageFilter::Update() { //Check if context & program available if (!this->Initialize()) { us::ServiceReference ref = GetModuleContext()->GetServiceReference(); OclResourceService* resources = GetModuleContext()->GetService(ref); // clean-up also the resources resources->InvalidateStorage(); mitkThrow() <<"Filter is not initialized. Cannot update."; } else{ // Execute this->Execute(); } } void mitk::OclBinaryThresholdImageFilter::Execute() { cl_int clErr = 0; try { this->InitExec( this->m_ckBinaryThreshold ); } catch( const mitk::Exception& e) { MITK_ERROR << "Catched exception while initializing filter: " << e.what(); return; } // set kernel arguments clErr = clSetKernelArg( this->m_ckBinaryThreshold, 2, sizeof(cl_int), &(this->m_LowerThreshold) ); clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 3, sizeof(cl_int), &(this->m_UpperThreshold) ); clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 4, sizeof(cl_int), &(this->m_OutsideValue) ); clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 5, sizeof(cl_int), &(this->m_InsideValue) ); CHECK_OCL_ERR( clErr ); // execute the filter on a 3D NDRange this->ExecuteKernel( m_ckBinaryThreshold, 3); // signalize the GPU-side data changed m_Output->Modified( GPU_DATA ); } us::Module *mitk::OclBinaryThresholdImageFilter::GetModule() { return us::GetModuleContext()->GetModule(); } bool mitk::OclBinaryThresholdImageFilter::Initialize() { bool buildErr = true; cl_int clErr = 0; if ( OclFilter::Initialize() ) { this->m_ckBinaryThreshold = clCreateKernel( this->m_ClProgram, "ckBinaryThreshold", &clErr); buildErr |= CHECK_OCL_ERR( clErr ); } - return (Superclass::IsInitialized() && buildErr ); + return (OclFilter::IsInitialized() && buildErr ); } diff --git a/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.h b/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.h index 42425c10b3..51b217e49f 100644 --- a/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.h +++ b/Modules/OpenCL/mitkOclBinaryThresholdImageFilter.h @@ -1,114 +1,119 @@ /*=================================================================== 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 _MITKOCLBINARYTHRESHOLDIMAGEFILTER_H_ #define _MITKOCLBINARYTHRESHOLDIMAGEFILTER_H_ #include "mitkOclImageToImageFilter.h" +#include namespace mitk { class OclImageToImageFilter; /** Documentation * * \brief The OclBinaryThresholdImageFilter computes a binary segmentation based on given threshold values. * * The filter requires two threshold values ( the upper and the lower threshold ) and two image values ( inside and outside ). The resulting voxel of the segmentation image is assigned the inside value 1 if the image value is between the given thresholds and the outside value otherwise. */ -class MitkOpenCL_EXPORT OclBinaryThresholdImageFilter : public OclImageToImageFilter +class MitkOpenCL_EXPORT OclBinaryThresholdImageFilter : public OclImageToImageFilter, public itk::Object { - typedef OclFilter Superclass; public: - /** Update the filter */ - void Update(); + mitkClassMacro(OclBinaryThresholdImageFilter, OclImageToImageFilter); + itkNewMacro(Self); - /** Constructor */ - OclBinaryThresholdImageFilter(); - /** Destructor */ - virtual ~OclBinaryThresholdImageFilter(); + /** Update the filter */ + void Update(); /** Set the lower threshold @param thr Threshold value */ void SetLowerThreshold( int lowerThreshold ) { this->m_LowerThreshold = lowerThreshold; } /** Set the upper threshold @param thr Threshold value */ void SetUpperThreshold( int upperThreshold ) { this->m_UpperThreshold = upperThreshold; } /** Set the outside value @param val The outside value */ void SetOutsideValue( int outsideValue ) { this->m_OutsideValue = outsideValue; } /** Set the inside value @param val The inside value */ void SetInsideValue( int insideValue ) { this->m_InsideValue = insideValue; } protected: + + /** Constructor */ + OclBinaryThresholdImageFilter(); + + /** Destructor */ + virtual ~OclBinaryThresholdImageFilter(); + /** Initialize the filter */ bool Initialize(); void Execute(); mitk::PixelType GetOutputType() { return mitk::MakeScalarPixelType(); } int GetBytesPerElem() { return sizeof(unsigned char); } virtual us::Module* GetModule(); private: /** The OpenCL kernel for the filter */ cl_kernel m_ckBinaryThreshold; int m_LowerThreshold; int m_UpperThreshold; int m_InsideValue; int m_OutsideValue; }; } #endif