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