Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "usServiceReference.h"
19 
21 : m_ckBinaryThreshold( NULL )
22 {
23  this->AddSourceFile("BinaryThresholdFilter.cl");
24  this->m_FilterID = "BinaryThreshold";
25 
26  this->m_LowerThreshold = 10;
27  this->m_UpperThreshold = 200;
28 
29  this->m_InsideValue = 100;
30  this->m_OutsideValue = 0;
31 }
32 
34 {
35  if ( this->m_ckBinaryThreshold )
36  {
37  clReleaseKernel( m_ckBinaryThreshold );
38  }
39 }
40 
42 {
43  //Check if context & program available
44  if (!this->Initialize())
45  {
48 
49  // clean-up also the resources
50  resources->InvalidateStorage();
51  mitkThrow() <<"Filter is not initialized. Cannot update.";
52  }
53  else{
54  // Execute
55  this->Execute();
56  }
57 }
58 
60 {
61  cl_int clErr = 0;
62 
63  try
64  {
65  this->InitExec( this->m_ckBinaryThreshold );
66  }
67  catch( const mitk::Exception& e)
68  {
69  MITK_ERROR << "Catched exception while initializing filter: " << e.what();
70  return;
71  }
72 
73  // set kernel arguments
74  clErr = clSetKernelArg( this->m_ckBinaryThreshold, 2, sizeof(cl_int), &(this->m_LowerThreshold) );
75  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 3, sizeof(cl_int), &(this->m_UpperThreshold) );
76  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 4, sizeof(cl_int), &(this->m_OutsideValue) );
77  clErr |= clSetKernelArg( this->m_ckBinaryThreshold, 5, sizeof(cl_int), &(this->m_InsideValue) );
78  CHECK_OCL_ERR( clErr );
79 
80  // execute the filter on a 3D NDRange
81  this->ExecuteKernel( m_ckBinaryThreshold, 3);
82 
83  // signalize the GPU-side data changed
84  m_Output->Modified( GPU_DATA );
85 }
86 
88 {
89  return us::GetModuleContext()->GetModule();
90 }
91 
93 {
94  bool buildErr = true;
95  cl_int clErr = 0;
96 
97  if ( OclFilter::Initialize() )
98  {
99  this->m_ckBinaryThreshold = clCreateKernel( this->m_ClProgram, "ckBinaryThreshold", &clErr);
100  buildErr |= CHECK_OCL_ERR( clErr );
101  }
102 
103  return (OclFilter::IsInitialized() && buildErr );
104 }
105 
107 {
108  if(image->GetDimension() != 3)
109  {
110  mitkThrowException(mitk::Exception) << "Input for " << this->GetNameOfClass() <<
111  " is not 3D. The filter only supports 3D. Please change your input.";
112  }
114 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:25
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:85
void SetInput(Image::Pointer image)
SetInput Set the input image. Only 3D images are supported for now.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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't instantiate exceptions manually...
Definition: mitkException.h:49
Module * GetModule() const
#define mitkThrow()
#define mitkThrowException(classname)
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.
#define GPU_DATA
Definition: mitkOclImage.h:20