Medical Imaging Interaction Toolkit  2018.4.99-1bab67a2
Medical Imaging Interaction Toolkit
mitkOclImageFormats.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 
13 #include "mitkOclImageFormats.h"
14 
16  :m_Image2DSupport( nullptr ), m_Image3DSupport( nullptr ),
17  m_GpuContext( nullptr )
18 {
19  //todo: what happens here?
20  const unsigned int matrixSize = MAX_FORMATS * MAX_DATA_TYPES;
21 
22  this->m_Image2DSupport = new unsigned char[matrixSize];
23  this->m_Image3DSupport = new unsigned char[matrixSize];
24 
25  for( unsigned int i = 0; i<matrixSize; i++ )
26  {
27  this->m_Image2DSupport[i] = 0;
28  this->m_Image3DSupport[i] = 0;
29  }
30 }
31 
33 {
34 }
35 
36 void mitk::OclImageFormats::PrintSelf()
37 {
38  std::stringstream outputstream;
39  outputstream << "Values: Read-Write(1) ReadOnly(2) , WriteOnly(4) \n";
40  outputstream << "ROWS: [CL_A, CL_R, CL_RA, CL_RG, CL_RGB, CL_RGBA, CL_ARGB, CL_BGRA, CL_LUM, CL_INT] \n";
41 
42  const unsigned int matrixSize = MAX_FORMATS * MAX_DATA_TYPES;
43 
44  for( unsigned int i = 0; i<matrixSize; i++ )
45  {
46  outputstream << (int) this->m_Image2DSupport[i] << ", \t";
47  if( (i+1) % MAX_DATA_TYPES == 0 )
48  outputstream << " \n";
49  }
50  outputstream << "========================== \n";
51 
52  for( unsigned int i = 0; i<matrixSize; i++ )
53  {
54  outputstream << (int) this->m_Image3DSupport[i] << ", \t";
55  if( (i+1) % MAX_DATA_TYPES == 0 )
56  outputstream << " \n";
57  }
58  MITK_INFO << outputstream.str();
59 }
60 
61 unsigned int mitk::OclImageFormats::GetOffset(cl_image_format format)
62 {
63  //todo: what happens here?
64  unsigned int offset = 0;
65 
66  switch( format.image_channel_order )
67  {
68  case CL_A:
69  break;
70  case CL_R:
71  offset += 1 * MAX_DATA_TYPES;
72  break;
73  case CL_RA:
74  offset += 2 * MAX_DATA_TYPES;
75  break;
76  case CL_RG:
77  offset += 3 * MAX_DATA_TYPES;
78  break;
79  case CL_RGB:
80  offset += 4 * MAX_DATA_TYPES;
81  break;
82  case CL_RGBA:
83  offset += 5 * MAX_DATA_TYPES;
84  break;
85  case CL_ARGB:
86  offset += 6 * MAX_DATA_TYPES;
87  break;
88  case CL_BGRA:
89  offset += 7 * MAX_DATA_TYPES;
90  break;
91  case CL_LUMINANCE:
92  offset += 8 * MAX_DATA_TYPES;
93  break;
94  case CL_INTENSITY:
95  offset += 9 * MAX_DATA_TYPES;
96  break;
97  }
98 
99  switch ( format.image_channel_data_type )
100  {
101  case CL_SNORM_INT8:
102  break;
103  case CL_SNORM_INT16:
104  offset += 1;
105  break;
106  case CL_UNORM_INT8:
107  offset += 2;
108  break;
109  case CL_UNORM_INT16:
110  offset += 3;
111  break;
112  case CL_SIGNED_INT8:
113  offset += 4;
114  break;
115  case CL_SIGNED_INT16:
116  offset += 5;
117  break;
118  case CL_SIGNED_INT32:
119  offset += 6;
120  break;
121  case CL_UNSIGNED_INT8:
122  offset += 7;
123  break;
124  case CL_UNSIGNED_INT16:
125  offset += 8;
126  break;
127  case CL_UNSIGNED_INT32:
128  offset += 9;
129  break;
130  case CL_HALF_FLOAT:
131  offset += 10;
132  break;
133  case CL_FLOAT:
134  offset += 11;
135  break;
136  }
137 
138  return offset;
139 }
140 
141 bool mitk::OclImageFormats::IsFormatSupported(cl_image_format* format)
142 {
143  bool retVal = false;
144 
145  // FIXME needs finer subdivision...
146  //todo: Comment above???
147  if ( this->m_Image2DSupport[ GetOffset(*format)] > 4 )
148  retVal = true;
149 
150  return retVal;
151 }
152 
153 bool mitk::OclImageFormats::GetNearestSupported(cl_image_format *inputformat, cl_image_format *outputformat)
154 {
155  bool returnValue = false;
156 
157  // init output format
158  outputformat->image_channel_data_type = inputformat->image_channel_data_type;
159  outputformat->image_channel_order = inputformat->image_channel_order;
160 
161  // the input format is supported, just copy the information into out
162  if( this->IsFormatSupported(inputformat) )
163  {
164  returnValue = true;
165  }
166  else
167  {
168  // get the 'nearest' format
169  // try RGBA first
170  //todo: It seems like ONLY RGBA is considered to be near?!? Either code or docu should be adapted.
171  cl_image_format test;
172  test.image_channel_order = CL_RGBA;
173  test.image_channel_data_type = inputformat->image_channel_data_type;
174 
175  if(this->IsFormatSupported( &test) )
176  {
177  outputformat->image_channel_order = CL_RGBA;
178  }
179  }
180  return returnValue;
181 }
182 
183 void mitk::OclImageFormats::SetGPUContext(cl_context context)
184 {
185  this->m_GpuContext = context;
186  //collect available formats can now be called
187  this->CollectAvailableFormats();
188 }
189 
190 void mitk::OclImageFormats::SortFormats(cl_image_format *formats, cl_uint count, int val, int dims)
191 {
192  //todo what happens here?
193  unsigned char *target = this->m_Image2DSupport;
194  if (dims == 3)
195  {
196  target = this->m_Image3DSupport;
197  }
198 
199  for( unsigned int i=0; i<count; i++)
200  {
201  // each supported format => +1
202  target[ this->GetOffset( formats[i] ) ]+=val;
203  }
204 }
205 
207 {
208  if( this->m_GpuContext == nullptr)
209  {
210  mitkThrow() << "No GPU context was set! Use SetGPUContext() before calling this method!";
211  }
212  //todo what happens here?
213  const unsigned int entries = 100;
214  cl_image_format* formats = new cl_image_format[entries];
215 
216  cl_uint written = 0;
217  cl_int clErr = 0;
218 
219  // GET formats for R/W, 2D
220  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, entries, formats, &written);
221  CHECK_OCL_ERR( clErr );
222 
223  this->SortFormats( formats, written, 1 );
224 
225  // GET formats for R/-, 2D
226  written = 0;
227  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE2D, entries, formats, &written);
228  CHECK_OCL_ERR( clErr );
229 
230  this->SortFormats( formats, written, 2 );
231 
232  // GET formats for -/W, 2D
233  written = 0;
234  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_WRITE_ONLY, CL_MEM_OBJECT_IMAGE2D, entries, formats, &written);
235  CHECK_OCL_ERR( clErr );
236 
237  this->SortFormats( formats, written, 4 );
238  //-----------------------
239 
240 
241  // GET formats for R/W, 3D
242  written = 0;
243  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE3D, entries, formats, &written);
244  CHECK_OCL_ERR( clErr );
245 
246  this->SortFormats( formats, written, 1, 3 );
247 
248  // GET formats for R/-, 3D
249  written = 0;
250  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE3D, entries, formats, &written);
251  CHECK_OCL_ERR( clErr );
252 
253  this->SortFormats( formats, written, 2, 3 );
254 
255  // GET formats for -/W, 3D
256  written = 0;
257  clErr = clGetSupportedImageFormats( m_GpuContext, CL_MEM_WRITE_ONLY, CL_MEM_OBJECT_IMAGE3D, entries, formats, &written);
258  CHECK_OCL_ERR( clErr );
259 
260  this->SortFormats( formats, written, 4, 3 );
261 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
#define MITK_INFO
Definition: mitkLogMacros.h:18
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
void SetGPUContext(cl_context context)
SetGPUContext Set the GPU context. Must be called before using this class!
OclImageFormats()
OclImageFormats Constructor.
static Vector3D offset
bool IsFormatSupported(cl_image_format *format)
Checks if format supported.
bool GetNearestSupported(cl_image_format *inputformat, cl_image_format *outputformat)
Finds one supported image format similar to the given format.
#define MAX_FORMATS
#define mitkThrow()
#define MAX_DATA_TYPES
void CollectAvailableFormats()
Get and store all available infos.
virtual ~OclImageFormats()
Destructor (default)