28 m_Image(NULL), m_dim(0), m_Dims(NULL), m_BpE(1), m_formatSupported(false)
37 if (m_gpuImage) clReleaseMemObject(m_gpuImage);
44 MITK_INFO <<
"CreateGPUImage call with: BPP=" << _bpp;
46 this->m_Dims =
new unsigned int[
MAX_DIMS];
52 for (
unsigned int i=3; i<
MAX_DIMS; i++)
55 m_bufferSize = _wi * _he * _de;
62 cl_context gpuContext = resources->
GetContext();
65 m_gpuImage = clCreateBuffer( gpuContext, CL_MEM_READ_WRITE, m_bufferSize * m_BpE, NULL, &clErr);
74 this->m_Image = _image;
75 this->m_cpuModified =
true;
76 this->m_gpuModified =
false;
78 this->m_gpuImage = NULL;
81 this->m_dim = this->m_Image->GetDimension();
82 this->m_Dims = this->m_Image->GetDimensions();
83 MITK_INFO <<
"Image: " << this->m_Dims[0] <<
"x"<< this->m_Dims[1] <<
"x"<< this->m_Dims[2];
86 this->m_bufferSize = 1;
87 for (
unsigned int i=0; i<this->m_dim; i++)
89 this->m_bufferSize *= this->m_Dims[i];
93 this->m_BpE = ( this->m_Image->GetPixelType().GetBpe() / 8);
98 if (_type)
return m_cpuModified;
99 else return m_gpuModified;
105 m_cpuModified = _type;
106 m_gpuModified = !_type;
114 if (!m_Image->IsInitialized()){
115 MITK_ERROR(
"ocl.Image") <<
"(mitk) Image not initialized!\n";
123 if(m_gpuImage == NULL)
125 clErr = this->AllocateGPUImage();
128 if (m_Image->IsInitialized() &&
129 (clErr == CL_SUCCESS))
131 const size_t origin[3] = {0, 0, 0};
132 const size_t region[3] = {m_Dims[0], m_Dims[1], m_Dims[2]};
134 if( this->m_formatSupported )
137 clErr = clEnqueueWriteImage( gpuComQueue, m_gpuImage, CL_TRUE, origin, region, 0, 0, accessor.
GetData(), 0, NULL, NULL);
141 MITK_ERROR <<
"Selected image format currently not supported...";
146 m_gpuModified =
true;
152 cl_int mitk::OclImage::AllocateGPUImage()
159 cl_context gpuContext = resources->
GetContext();
162 this->m_proposedFormat = this->ConvertPixelTypeToOCLFormat();
163 this->m_supportedFormat = this->m_proposedFormat;
166 this->m_formatSupported = resources->GetIsFormatSupported( &(this->m_supportedFormat) );
169 if( !(this->m_formatSupported) )
178 m_gpuImage = clCreateImage3D(gpuContext, CL_MEM_READ_ONLY, &m_supportedFormat, *(m_Dims), *(m_Dims+1), *(m_Dims+2), 0, 0, NULL, &clErr);
183 m_gpuImage = clCreateImage2D(gpuContext, CL_MEM_READ_ONLY, &m_supportedFormat, *(m_Dims), *(m_Dims+1), 0, NULL, &clErr);
193 cl_mem_object_type memInfo;
197 if( this->m_gpuImage )
199 clErr = clGetMemObjectInfo(this->m_gpuImage, CL_MEM_TYPE,
sizeof(cl_mem_object_type), &memInfo, NULL );
203 MITK_INFO <<
"Querying info for object, recieving: " << memInfo;
207 if (memInfo == CL_MEM_OBJECT_BUFFER)
209 MITK_WARN <<
" Passed oclImage is a buffer-object, creating image";
212 cl_mem tempBuffer = this->m_gpuImage;
214 const size_t origin[3] = {0, 0, 0};
215 size_t region[3] = {this->m_Dims[0], this->m_Dims[1], 1};
217 clErr = this->AllocateGPUImage();
222 clErr = clEnqueueCopyBufferToImage( gpuComQueue, tempBuffer, m_gpuImage, 0, origin, region, 0, NULL, NULL);
226 clReleaseMemObject(tempBuffer);
233 this->m_proposedFormat.image_channel_data_type = _image->image_channel_data_type;
234 this->m_proposedFormat.image_channel_order = _image->image_channel_order;
242 if( m_Image.IsNull() ){
243 MITK_INFO <<
"Image not initialized, creating new one.";
248 char* data =
new char[m_bufferSize * m_BpE];
253 clErr = clEnqueueReadBuffer( gpuComQueue, m_gpuImage, CL_FALSE, 0, m_bufferSize * m_BpE, data ,0, NULL, NULL);
256 clFlush( gpuComQueue );
258 this->m_gpuModified =
false;
263 cl_image_format mitk::OclImage::ConvertPixelTypeToOCLFormat()
265 cl_image_format texFormat;
267 texFormat.image_channel_order = CL_R;
269 MITK_INFO <<
"Class own value: " << this->m_BpE;
271 switch ( this->m_BpE )
274 texFormat.image_channel_data_type = CL_UNSIGNED_INT8;
275 MITK_INFO<<
"PixelType: UCHAR => CLFormat: [CL_UNORM_INT8, CL_R]";
278 texFormat.image_channel_data_type = CL_SIGNED_INT16;
280 MITK_INFO<<
"PixelType: SHORT => CLFormat: [CL_SIGNED_INT16, CL_R]";
283 texFormat.image_channel_data_type = CL_FLOAT;
287 texFormat.image_channel_data_type = CL_UNORM_INT8;
288 texFormat.image_channel_order = CL_RG;
289 MITK_INFO<<
"Choosing (default) short: 2-Channel UCHAR";
298 if (this->m_dim > idx)
304 MITK_WARN <<
"Trying to access non-existing dimension.";
321 if (this->m_dim > idx)
323 const mitk::Vector3D imSpacing = m_Image->GetSlicedGeometry()->GetSpacing();
325 return imSpacing[idx];
329 MITK_WARN <<
"Trying to access non-existing dimension.";
341 itk::Vector<float, 3> result2;
344 result2 = this->m_Image->GetGeometry()->GetIndexToWorldTransform()->GetOffset();
346 _imOffset[0] = result2[0];
347 _imOffset[1] = result2[1];
348 _imOffset[2] = result2[2];
#define CHECK_OCL_ERR(_er)
virtual ~OclImage()
Destructor.
ServiceReferenceU GetServiceReference(const std::string &clazz)
cl_mem CreateGPUImage(unsigned int, unsigned int, unsigned int, unsigned int)
cl_mem GetGPUImage(cl_command_queue)
Checks whether gpuImage is a valid clImage object.
const void * GetData() const
Gives const access to the data.
The ImageTypeIsNotSupportedByGPU class specialized exception class for unsupported image formats...
void GetOffset(float *) const
Returns the image offset (needed for WorldToIndex Transform.
void * GetService(const ServiceReferenceBase &reference)
void SetDimensions(unsigned int *Dims)
Set the image dimensions through an unsigned int array.
int GetDimension() const
returns the dimensionality of the image
void SetDimension(unsigned short dim)
Set the dimensionality of the image.
float GetSpacing(int)
returns the spacing of the image for specified dimension
void InitializeMITKImage()
Initialize the internal variable of type mitk::Image.
virtual void * TransferDataToCPU(cl_command_queue)
Copies the in GMEM stored data to RAM.
bool IsModified(int _type)
Returns the status of the image buffer.
virtual int TransferDataToGPU(cl_command_queue)
Copies the RAM-stored data to GMEM.
#define mitkThrowException(classname)
void InitializeByMitkImage(mitk::Image::Pointer _image)
Initialze the OclImage with an mitkImage.
virtual cl_context GetContext() const =0
Returns a valid OpenCL Context (if applicable) or NULL if none present.
ImageReadAccessor class to get locked read access for a particular image part.
void Modified(int _type)
Set the modified flag for one of the buffers.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void oclPrintMemObjectInfo(cl_mem memobj)
Prints the available memory info about the given object to std::cout.
void SetPixelType(const cl_image_format *)
Set the pixel type for the image to be used.
Declaration of the OpenCL Resources micro-service.