Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkOclBinaryThresholdImageFilter.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 "usServiceReference.h"
15 
17 : m_ckBinaryThreshold( nullptr )
18 {
19  this->AddSourceFile("BinaryThresholdFilter.cl");
20  this->m_FilterID = "BinaryThreshold";
21 
22  this->m_LowerThreshold = 10;
23  this->m_UpperThreshold = 200;
24 
25  this->m_InsideValue = 100;
26  this->m_OutsideValue = 0;
27 }
28 
30 {
31  if ( this->m_ckBinaryThreshold )
32  {
33  clReleaseKernel( m_ckBinaryThreshold );
34  }
35 }
36 
38 {
39  //Check if context & program available
40  if (!this->Initialize())
41  {
44 
45  // clean-up also the resources
46  resources->InvalidateStorage();
47  mitkThrow() <<"Filter is not initialized. Cannot update.";
48  }
49  else{
50  // Execute
51  this->Execute();
52  }
53 }
54 
56 {
57  cl_int clErr = 0;
58 
59  try
60  {
61  this->InitExec( this->m_ckBinaryThreshold );
62  }
63  catch( const mitk::Exception& e)
64  {
65  MITK_ERROR << "Catched exception while initializing filter: " << e.what();
66  return;
67  }
68 
69  // set kernel arguments
70  clErr = clSetKernelArg( this->m_ckBinaryThreshold, 2, sizeof(cl_int), &(this->m_LowerThreshold) );
71  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 3, sizeof(cl_int), &(this->m_UpperThreshold) );
72  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 4, sizeof(cl_int), &(this->m_OutsideValue) );
73  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 5, sizeof(cl_int), &(this->m_InsideValue) );
74  CHECK_OCL_ERR( clErr );
75 
76  // execute the filter on a 3D NDRange
77  this->ExecuteKernel( m_ckBinaryThreshold, 3);
78 
79  // signalize the GPU-side data changed
80  m_Output->Modified( GPU_DATA );
81 }
82 
84 {
85  return us::GetModuleContext()->GetModule();
86 }
87 
89 {
90  bool buildErr = true;
91  cl_int clErr = 0;
92 
93  if ( OclFilter::Initialize() )
94  {
95  this->m_ckBinaryThreshold = clCreateKernel( this->m_ClProgram, "ckBinaryThreshold", &clErr);
96  buildErr |= CHECK_OCL_ERR( clErr );
97  }
98 
99  return (OclFilter::IsInitialized() && buildErr );
100 }
101 
103 {
104  if(image->GetDimension() != 3)
105  {
106  mitkThrowException(mitk::Exception) << "Input for " << this->GetNameOfClass() <<
107  " is not 3D. The filter only supports 3D. Please change your input.";
108  }
110 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
void SetInput(mitk::OclImage::Pointer image)
SetInput SetInput Set the input image (as mitk::OclImage).
virtual void InvalidateStorage()=0
Remove all invalid (=do not compile) programs from the internal storage.
ServiceReferenceU GetServiceReference(const std::string &clazz)
std::string m_FilterID
Unique ID of the filter, needs to be specified in the constructor of the derived class.
Definition: mitkOclFilter.h:86
void SetInput(Image::Pointer image)
SetInput Set the input image. Only 3D images are supported for now.
#define GPU_DATA
cl_program m_ClProgram
The compiled OpenCL program.
Definition: mitkOclFilter.h:80
#define MITK_ERROR
Definition: mitkLogMacros.h:20
bool Initialize()
Initialize all necessary parts of the filter.
virtual bool IsInitialized()
Returns true if the initialization was successfull.
virtual us::Module * GetModule()
Get the Module of the filter. Needs to be implemented by every subclass. The filter will load the Ope...
void * GetService(const ServiceReferenceBase &reference)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
Module * GetModule() const
mitk::Image::Pointer image
#define mitkThrowException(classname)
bool InitExec(cl_kernel ckKernel)
InitExec Initialize the execution.
bool ExecuteKernel(cl_kernel kernel, unsigned int workSizeDim)
Execute the given kernel on the OpenCL Index-Space defined by the local and global work sizes...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void AddSourceFile(const char *filename)
Add a source file from the resource files to the OpenCL shader file list. Multiple files can be added...
Declaration of the OpenCL Resources micro-service.