23 mitk::OclImage::OclImage() : m_gpuImage(nullptr), m_context(nullptr), m_bufferSize(0), m_gpuModified(false), m_cpuModified(false),
24 m_Image(nullptr), m_dim(0), m_Dims(nullptr), m_BpE(1), m_formatSupported(false)
40 MITK_INFO <<
"CreateGPUImage call with: BPP=" << _bpp;
42 this->m_Dims =
new unsigned int[
MAX_DIMS];
48 for (
unsigned int i=3; i<
MAX_DIMS; i++)
58 cl_context gpuContext = resources->
GetContext();
70 this->m_Image = _image;
71 this->m_cpuModified =
true;
72 this->m_gpuModified =
false;
77 this->m_dim = this->m_Image->GetDimension();
78 this->m_Dims = this->m_Image->GetDimensions();
79 MITK_INFO <<
"Image: " << this->m_Dims[0] <<
"x"<< this->m_Dims[1] <<
"x"<< this->m_Dims[2];
83 for (
unsigned int i=0; i<this->m_dim; i++)
89 this->m_BpE = ( this->m_Image->GetPixelType().GetBpe() / 8);
94 if (_type)
return m_cpuModified;
95 else return m_gpuModified;
101 m_cpuModified = _type;
102 m_gpuModified = !_type;
110 if (!m_Image->IsInitialized()){
111 MITK_ERROR(
"ocl.Image") <<
"(mitk) Image not initialized!\n";
121 clErr = this->AllocateGPUImage();
124 if (m_Image->IsInitialized() &&
125 (clErr == CL_SUCCESS))
127 const size_t origin[3] = {0, 0, 0};
128 const size_t region[3] = {m_Dims[0], m_Dims[1], m_Dims[2]};
130 if( this->m_formatSupported )
133 clErr = clEnqueueWriteImage( gpuComQueue,
m_gpuImage, CL_TRUE, origin, region, 0, 0, accessor.
GetData(), 0,
nullptr,
nullptr);
137 MITK_ERROR <<
"Selected image format currently not supported...";
142 m_gpuModified =
true;
148 cl_int mitk::OclImage::AllocateGPUImage()
155 cl_context gpuContext = resources->
GetContext();
158 this->m_proposedFormat = this->ConvertPixelTypeToOCLFormat();
159 this->m_supportedFormat = this->m_proposedFormat;
162 this->m_formatSupported = resources->GetIsFormatSupported( &(this->m_supportedFormat) );
165 if( !(this->m_formatSupported) )
171 _cl_image_desc imageDescriptor;
173 imageDescriptor.image_width = *(m_Dims);
174 imageDescriptor.image_height = *(m_Dims + 1);
175 imageDescriptor.image_depth = *(m_Dims + 2);
176 imageDescriptor.image_array_size = 0;
177 imageDescriptor.image_row_pitch = 0;
178 imageDescriptor.image_slice_pitch = 0;
179 imageDescriptor.num_mip_levels = 0;
180 imageDescriptor.num_samples = 0;
181 imageDescriptor.buffer =
nullptr;
186 imageDescriptor.image_type = CL_MEM_OBJECT_IMAGE3D;
191 imageDescriptor.image_type = CL_MEM_OBJECT_IMAGE2D;
193 m_gpuImage = clCreateImage(gpuContext, CL_MEM_READ_ONLY, &m_supportedFormat, &imageDescriptor,
nullptr, &clErr);
203 cl_mem_object_type memInfo;
209 clErr = clGetMemObjectInfo(this->
m_gpuImage, CL_MEM_TYPE,
sizeof(cl_mem_object_type), &memInfo,
nullptr );
213 MITK_INFO <<
"Querying info for object, recieving: " << memInfo;
217 if (memInfo == CL_MEM_OBJECT_BUFFER)
219 MITK_WARN <<
" Passed oclImage is a buffer-object, creating image";
224 const size_t origin[3] = {0, 0, 0};
225 size_t region[3] = {this->m_Dims[0], this->m_Dims[1], 1};
227 clErr = this->AllocateGPUImage();
232 clErr = clEnqueueCopyBufferToImage( gpuComQueue, tempBuffer,
m_gpuImage, 0, origin, region, 0,
nullptr,
nullptr);
236 clReleaseMemObject(tempBuffer);
243 this->m_proposedFormat.image_channel_data_type = _image->image_channel_data_type;
244 this->m_proposedFormat.image_channel_order = _image->image_channel_order;
252 if( m_Image.IsNull() ){
253 MITK_INFO <<
"Image not initialized, creating new one.";
263 clErr = clEnqueueReadBuffer( gpuComQueue,
m_gpuImage, CL_FALSE, 0,
m_bufferSize * m_BpE, data ,0,
nullptr,
nullptr);
266 clFlush( gpuComQueue );
268 this->m_gpuModified =
false;
273 cl_image_format mitk::OclImage::ConvertPixelTypeToOCLFormat()
275 cl_image_format texFormat;
277 texFormat.image_channel_order = CL_R;
279 MITK_INFO <<
"Class own value: " << this->m_BpE;
281 switch ( this->m_BpE )
284 texFormat.image_channel_data_type = CL_UNSIGNED_INT8;
285 MITK_INFO<<
"PixelType: UCHAR => CLFormat: [CL_UNORM_INT8, CL_R]";
288 texFormat.image_channel_data_type = CL_SIGNED_INT16;
290 MITK_INFO<<
"PixelType: SHORT => CLFormat: [CL_SIGNED_INT16, CL_R]";
293 texFormat.image_channel_data_type = CL_FLOAT;
297 texFormat.image_channel_data_type = CL_UNORM_INT8;
298 texFormat.image_channel_order = CL_RG;
299 MITK_INFO<<
"Choosing (default) short: 2-Channel UCHAR";
308 if (this->m_dim > idx)
314 MITK_WARN <<
"Trying to access non-existing dimension.";
331 if (this->m_dim > idx)
333 const mitk::Vector3D imSpacing = m_Image->GetSlicedGeometry()->GetSpacing();
335 return imSpacing[idx];
339 MITK_WARN <<
"Trying to access non-existing dimension.";
351 itk::Vector<float, 3> result2;
354 result2 = this->m_Image->GetGeometry()->GetIndexToWorldTransform()->GetOffset();
356 _imOffset[0] = result2[0];
357 _imOffset[1] = result2[1];
358 _imOffset[2] = result2[2];
#define CHECK_OCL_ERR(_er)
virtual ~OclImage()
Destructor.
ServiceReferenceU GetServiceReference(const std::string &clazz)
int GetDimension() const
returns the dimensionality of the image
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.
void GetOffset(float *) const
Returns the image offset (needed for WorldToIndex Transform.
The ImageTypeIsNotSupportedByGPU class specialized exception class for unsupported image formats...
void * GetService(const ServiceReferenceBase &reference)
void SetDimensions(unsigned int *Dims)
Set the image dimensions through an unsigned int array.
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.
unsigned int m_bufferSize
virtual cl_context GetContext() const =0
Returns a valid OpenCL Context (if applicable) or nullptr 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.
const void * GetData() const
Gives const access to the data.
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.