Medical Imaging Interaction Toolkit  2018.4.99-c7ee88da
Medical Imaging Interaction Toolkit
mitkOclImageToImageFilter.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 "mitkOclImage.h"
15 
16 #include "mitkException.h"
17 
19 {
21 }
22 
23 
25 {
26 }
27 
29 {
30  // initialize some variables
31  m_Output->SetPixelType(m_Input->GetPixelType());
32 
33  // create new image, for passing the essential information to the output
34  m_Output->InitializeMITKImage();
35 
36  const unsigned int dimension = m_Input->GetDimension();
37  unsigned int* dimensions = m_Input->GetDimensions();
38 
39  m_Output->SetDimensions( dimensions );
40  m_Output->SetDimension( (unsigned short)dimension );
41 
42  m_Output->GetMITKImage()->Initialize( this->GetOutputType(), dimension, dimensions);
43  const mitk::SlicedGeometry3D::Pointer p_slg = m_Input->GetMITKImage()->GetSlicedGeometry(0);
44  m_Output->GetMITKImage()->SetSpacing( p_slg->GetSpacing() );
45  m_Output->GetMITKImage()->SetGeometry( m_Input->GetMITKImage()->GetGeometry() );
46 
47  return this->m_Output;
48 
49 }
50 
52 {
53  if (m_Output->IsModified(GPU_DATA))
54  {
55  void* pData = m_Output->TransferDataToCPU(m_CommandQue);
56 
57  const unsigned int dimension = m_Input->GetDimension();
58  unsigned int* dimensions = m_Input->GetDimensions();
59 
60  const mitk::SlicedGeometry3D::Pointer p_slg = m_Input->GetMITKImage()->GetSlicedGeometry();
61 
62  MITK_DEBUG << "Creating new MITK Image.";
63 
64  m_Output->GetMITKImage()->Initialize( this->GetOutputType(), dimension, dimensions);
65  m_Output->GetMITKImage()->SetSpacing( p_slg->GetSpacing());
66  m_Output->GetMITKImage()->SetGeometry( m_Input->GetMITKImage()->GetGeometry() );
67  m_Output->GetMITKImage()->SetImportVolume( pData, 0, 0, mitk::Image::ReferenceMemory);
68  }
69 
70  MITK_DEBUG << "Image Initialized.";
71 
72  return m_Output->GetMITKImage();
73 }
74 
76 {
77  // get the current image format from the input image
78  const cl_image_format* currentImFormat = this->m_Input->GetPixelType();
79 
80  // return the value according to the current channel type
81  switch( currentImFormat->image_channel_data_type )
82  {
83  case CL_UNORM_INT8:
84  return mitk::MakeScalarPixelType<unsigned char>();
85  case CL_UNSIGNED_INT8:
86  return mitk::MakeScalarPixelType<unsigned char>();
87  case CL_UNORM_INT16:
88  return mitk::MakeScalarPixelType<short>();
89  default:
90  return mitk::MakeScalarPixelType<short>();
91  }
92 }
93 
95 {
96  return (this->m_CurrentType + 1);
97 }
98 
99 bool mitk::OclImageToImageFilter::InitExec(cl_kernel ckKernel)
100 {
101  cl_int clErr = 0;
102 
103  if( m_Input.IsNull() )
104  mitkThrow() << "Input image is null.";
105 
106  // get image size once
107  const unsigned int uiImageWidth = m_Input->GetDimension(0);
108  const unsigned int uiImageHeight = m_Input->GetDimension(1);
109  const unsigned int uiImageDepth = m_Input->GetDimension(2);
110 
111  // compute work sizes
112  this->SetWorkingSize( 8, uiImageWidth, 8, uiImageHeight , 8, uiImageDepth );
113 
114  cl_mem clBuffIn = m_Input->GetGPUImage(this->m_CommandQue);
115  cl_mem clBuffOut = m_Output->GetGPUBuffer();
116 
117  if (!clBuffIn)
118  {
119  if ( m_Input->TransferDataToGPU(m_CommandQue) != CL_SUCCESS )
120  {
121  mitkThrow()<< "Could not create / initialize gpu image.";
122  }
123 
124  clBuffIn = m_Input->GetGPUImage(m_CommandQue);
125  }
126 
127  // output image not initialized
128  if (!clBuffOut)
129  {
130  //TODO bpp, or SetImageWidth/Height/...
131  MITK_DEBUG << "Create GPU Image call " << uiImageWidth<< "x"<<uiImageHeight<< "x"<<uiImageDepth;
132  clBuffOut = m_Output->CreateGPUImage(uiImageWidth, uiImageHeight, uiImageDepth, this->m_CurrentType + 1);
133  }
134 
135  clErr = 0;
136  clErr = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), &clBuffIn);
137  clErr |= clSetKernelArg(ckKernel, 1, sizeof(cl_mem), &clBuffOut);
138  CHECK_OCL_ERR( clErr );
139 
140  if( clErr != CL_SUCCESS )
141  mitkThrow() << "OpenCL Part initialization failed with " << GetOclErrorAsString(clErr);
142 
143  return( clErr == CL_SUCCESS );
144 }
145 
146  bool mitk::OclImageToImageFilter::InitExec(cl_kernel ckKernel, unsigned int* dimensions)
147  {
148  cl_int clErr = 0;
149 
150  if( m_Input.IsNull() )
151  mitkThrow() << "Input image is null.";
152 
153  // get image size once
154  const unsigned int uiImageWidth = dimensions[0];
155  const unsigned int uiImageHeight = dimensions[1];
156  const unsigned int uiImageDepth = dimensions[2]+1;
157 
158  // compute work sizes
159  this->SetWorkingSize( 8, uiImageWidth, 8, uiImageHeight , 8, uiImageDepth );
160 
161  cl_mem clBuffIn = m_Input->GetGPUImage(this->m_CommandQue);
162  cl_mem clBuffOut = m_Output->GetGPUBuffer();
163 
164  if (!clBuffIn)
165  {
166  if ( m_Input->TransferDataToGPU(m_CommandQue) != CL_SUCCESS )
167  {
168  mitkThrow()<< "Could not create / initialize gpu image.";
169  }
170 
171  clBuffIn = m_Input->GetGPUImage(m_CommandQue);
172  }
173 
174  // output image not initialized
175  //TODO bpp, or SetImageWidth/Height/...
176  MITK_INFO << "Create GPU Image call " << uiImageWidth<< "x"<<uiImageHeight<< "x"<<uiImageDepth;
177  clBuffOut = m_Output->CreateGPUImage(uiImageWidth, uiImageHeight, uiImageDepth, this->m_CurrentType + 1);
178 
179 
180  clErr = 0;
181  clErr = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), &clBuffIn);
182  clErr |= clSetKernelArg(ckKernel, 1, sizeof(cl_mem), &clBuffOut);
183  CHECK_OCL_ERR( clErr );
184 
185  if( clErr != CL_SUCCESS )
186  mitkThrow() << "OpenCL Part initialization failed with " << GetOclErrorAsString(clErr);
187 
188  return( clErr == CL_SUCCESS );
189 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define GPU_DATA
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
static Pointer New()
std::string GetOclErrorAsString(int _clErr)
Returns the name of an OpenCL Error as a string.
virtual ~OclImageToImageFilter()
Destructor.
#define mitkThrow()
virtual mitk::PixelType GetOutputType()
(Virtual) method returning the format in which the output image will be returned
mitk::OclImage::Pointer m_Input
virtual int GetBytesPerElem()
Get the memory size needed for each element.
OclImageToImageFilter()
OclImageToImageFilter Default constructor.
cl_command_queue m_CommandQue
Command queue for the filter.
Definition: mitkOclFilter.h:83
mitk::OclImage::Pointer GetGPUOutput()
Returns a pointer to the graphics memory.
mitk::Image::Pointer GetOutput()
Returns an mitk::Image::Pointer containing the filtered data.
void SetWorkingSize(unsigned int locx, unsigned int dimx, unsigned int locy=1, unsigned int dimy=1, unsigned int locz=1, unsigned int dimz=1)
Set the working size for the following OpenCL kernel call.
bool InitExec(cl_kernel ckKernel)
InitExec Initialize the execution.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51