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