Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfFormatDB.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfFormatDB.hh"
26 #include "glfException.hh"
27 
28 using namespace BIAS;
29 
30 
31 void glfFormatDB::
32  GetFormatForInternalFormat(GLenum internalFormat,
33  GLenum& format,
34  int& numChannels)
35 {
36  switch (internalFormat)
37  {
38 #ifdef GL_VERSION_3_0
39  case GL_R32F:
40  format = GL_RED;
41  numChannels = 1;
42  break;
43 #endif
44 
45  case GL_ALPHA:
46  case GL_ALPHA4:
47  case GL_ALPHA8:
48  case GL_ALPHA12:
49  case GL_ALPHA16:
50  case GL_COMPRESSED_ALPHA:
51  case GL_INTENSITY:
52  case GL_INTENSITY4:
53  case GL_INTENSITY8:
54  case GL_INTENSITY12:
55  case GL_INTENSITY16:
56  case GL_COMPRESSED_INTENSITY:
57  format = GL_ALPHA;
58  numChannels = 1;
59  break;
60 
61  case GL_LUMINANCE:
62  case GL_LUMINANCE4:
63  case GL_LUMINANCE8:
64  case GL_LUMINANCE12:
65  case GL_LUMINANCE16:
66  //case GL_SLUMINANCE:
67  //case GL_SLUMINANCE8:
68  case GL_COMPRESSED_LUMINANCE:
69  case GL_LUMINANCE16F_ARB:
70  case GL_LUMINANCE32F_ARB:
71  //format = GL_LUMINANCE;
72  format = GL_RED;
73  numChannels = 1;
74  break;
75 
76  case GL_LUMINANCE_ALPHA:
77  case GL_LUMINANCE4_ALPHA4:
78  case GL_LUMINANCE6_ALPHA2:
79  case GL_LUMINANCE8_ALPHA8:
80  case GL_LUMINANCE12_ALPHA4:
81  case GL_LUMINANCE12_ALPHA12:
82  case GL_LUMINANCE16_ALPHA16:
83  //case GL_SLUMINANCE_ALPHA:
84  //case GL_SLUMINANCE8_ALPHA8:
85  case GL_COMPRESSED_LUMINANCE_ALPHA:
86  case GL_LUMINANCE_ALPHA16F_ARB:
87  case GL_LUMINANCE_ALPHA32F_ARB:
88  format = GL_LUMINANCE_ALPHA;
89  numChannels = 2;
90  break;
91 
92  case GL_R3_G3_B2:
93  case GL_RGB:
94  case GL_RGB4:
95  case GL_RGB5:
96  case GL_RGB8:
97  case GL_RGB10:
98  case GL_RGB12:
99  case GL_RGB16:
100  //case GL_SRGB:
101  //case GL_SRGB8:
102  case GL_COMPRESSED_RGB:
103  case GL_RGB16F_ARB:
104  case GL_RGB32F_ARB:
105  format = GL_RGB;
106  numChannels = 3;
107  break;
108 
109  case GL_RGBA:
110  case GL_RGBA2:
111  case GL_RGBA4:
112  case GL_RGB5_A1:
113  case GL_RGBA8:
114  case GL_RGB10_A2:
115  case GL_RGBA12:
116  case GL_RGBA16:
117  //case GL_SRGB_ALPHA:
118  //case GL_SRGB8_ALPHA8:
119  case GL_COMPRESSED_RGBA:
120  case GL_RGBA16F_ARB:
121  case GL_RGBA32F_ARB:
122  format = GL_RGBA;
123  numChannels = 4;
124  break;
125 
126  case GL_RGBA8UI:
127  format = GL_RGBA_INTEGER;
128  numChannels = 4;
129  break;
130 
131  case GL_DEPTH_COMPONENT:
132  case GL_DEPTH_COMPONENT16:
133  case GL_DEPTH_COMPONENT24:
134  case GL_DEPTH_COMPONENT32:
135  format = GL_DEPTH_COMPONENT;
136  numChannels = 1;
137  break;
138  case GL_DEPTH24_STENCIL8_EXT:
139  format = GL_DEPTH_STENCIL_EXT;
140  numChannels = 2;
141  break;
142 
143  default:
144  GLF_THROW_EXCEPTION("Unknown or invalid internal texture format")
145  ;
146  break;
147  }
148 }
149 
150 GLenum glfFormatDB::
152 {
153  switch (storageType)
154  {
156  GLF_THROW_EXCEPTION("Invalid storage type")
157  ;
158  break;
160  return GL_UNSIGNED_BYTE;
161  case ImageBase::ST_char:
162  return GL_BYTE;
164  return GL_UNSIGNED_SHORT;
166  return GL_SHORT;
168  return GL_UNSIGNED_INT;
169  case ImageBase::ST_int:
170  return GL_INT;
171  case ImageBase::ST_float:
172  return GL_FLOAT;
174  GLF_THROW_EXCEPTION("Storage type 'double' is not supported by OpenGL")
175  ;
176  break;
177  default:
178  GLF_THROW_EXCEPTION("Unknown storage type")
179  ;
180  break;
181  }
182 }
183 
184 GLenum glfFormatDB::
186 {
187  switch (colorModel)
188  {
189  case ImageBase::CM_Grey:
190  case ImageBase::CM_Depth:
192  return GL_LUMINANCE;
193  case ImageBase::CM_GreyA:
194  return GL_LUMINANCE_ALPHA;
195  case ImageBase::CM_RGB:
196  return GL_RGB;
197  case ImageBase::CM_RGBA:
198  return GL_RGBA;
199  case ImageBase::CM_BGR:
200  return GL_BGR;
201  case ImageBase::CM_BGRA:
202  return GL_BGRA;
203  case ImageBase::CM_LUV:
204  case ImageBase::CM_LAB:
205  return GL_RGB;
206  default:
207  GLF_THROW_EXCEPTION("Color model " << colorModel << " is not supported by OpenGL or unknown")
208  ;
209  }
210 }
211 
212 GLenum glfFormatDB::
214  ImageBase::EColorModel colorModel)
215 {
216 
217  if (storageType == ImageBase::ST_float)
218  {
219  switch (colorModel)
220  {
221  case ImageBase::CM_Grey:
222  case ImageBase::CM_Depth:
224  return GL_LUMINANCE32F_ARB;
225  case ImageBase::CM_GreyA:
226  return GL_LUMINANCE_ALPHA32F_ARB;
227  case ImageBase::CM_RGB:
228  case ImageBase::CM_LUV:
229  case ImageBase::CM_LAB:
230  case ImageBase::CM_BGR:
231  return GL_RGB32F_ARB;
232  case ImageBase::CM_RGBA:
233  case ImageBase::CM_BGRA:
234  return GL_RGBA32F_ARB;
235 
236  default:
237  break; // suppress warning
238  }
239  }
240 
241  if (storageType == ImageBase::ST_unsignedchar || storageType
242  == ImageBase::ST_unsignedshortint || storageType
244  {
245  switch (colorModel)
246  {
247  case ImageBase::CM_Grey:
248  return GL_LUMINANCE;
249  case ImageBase::CM_GreyA:
250  return GL_LUMINANCE_ALPHA;
251  case ImageBase::CM_RGB:
252  case ImageBase::CM_BGR:
253  return GL_RGB;
254  case ImageBase::CM_RGBA:
255  case ImageBase::CM_BGRA:
256  return GL_RGBA;
257  default:
258  break; // suppress warning
259  }
260  }
261 
262  GLF_THROW_EXCEPTION("Could not find appropriate OpenGL pixel format for "
263  "storage type " << storageType << " and color model "
264  << colorModel);
265 
266 }
267 
268 GLenum glfFormatDB::
269  GLenumFromString(const std::string& enumString)
270 {
271 
272  if(enumString == "GL_RED") return GL_RED;
273  if(enumString == "GL_GREEN") return GL_GREEN;
274  if(enumString == "GL_BLUE") return GL_BLUE;
275 
276  if(enumString == "GL_RGB") return GL_RGB;
277  if(enumString == "GL_RGBA") return GL_RGBA;
278 
279  if(enumString == "GL_LUMINANCE_ALPHA32F_ARB") return GL_LUMINANCE_ALPHA32F_ARB;
280  if(enumString == "GL_RGB32F" || enumString == "GL_RGB32F_ARB") return GL_RGB32F_ARB;
281  if(enumString == "GL_RGBA32F" || enumString == "GL_RGBA32F_ARB") return GL_RGBA32F_ARB;
282 
283 
284  if(enumString == "GL_CLAMP") return GL_CLAMP;
285  if(enumString == "GL_CLAMP_TO_BORDER") return GL_CLAMP_TO_BORDER;
286  if(enumString == "GL_REPEAT") return GL_REPEAT;
287 
288  if(enumString == "GL_NEAREST") return GL_NEAREST;
289  if(enumString == "GL_LINEAR") return GL_LINEAR;
290  if(enumString == "GL_NEAREST_MIPMAP_NEAREST") return GL_NEAREST_MIPMAP_NEAREST;
291  if(enumString == "GL_LINEAR_MIPMAP_NEAREST") return GL_LINEAR_MIPMAP_NEAREST;
292  if(enumString == "GL_LINEAR_MIPMAP_LINEAR") return GL_LINEAR_MIPMAP_LINEAR;
293 
294  if(enumString == "GL_LESS") return GL_LESS;
295  if(enumString == "GL_LEQUAL") return GL_LEQUAL;
296  if(enumString == "GL_GREATER") return GL_GREATER;
297  if(enumString == "GL_GEQUAL") return GL_GEQUAL;
298 
299 
300  BIASERR("enumString could not be matched!");
301  return GL_INVALID_ENUM;
302 }
303 
304 int glfFormatDB::ChannelsFromGLFormat(GLenum textureFormat)
305 {
306  int numChannels = 0;
307  switch (textureFormat)
308  {
309  case GL_RED:
310  case GL_GREEN:
311  case GL_BLUE:
312  case GL_ALPHA:
313  case GL_LUMINANCE:
314  numChannels = 1;
315  break;
316  case GL_LUMINANCE_ALPHA:
317  numChannels = 2;
318  break;
319  case GL_RGB:
320  case GL_BGR:
321  numChannels = 3;
322  break;
323  case GL_RGBA:
324  case GL_BGRA:
325  numChannels = 4;
326  break;
327  default:
328  GLF_THROW_EXCEPTION("Invalid or unknown format");
329  ;
330  break;
331  }
332  return numChannels;
333 }
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
LAB, 3 channels, http://en.wikipedia.org/wiki/Lab_color_space.
Definition: ImageBase.hh:157
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
gray values, 1 channel
Definition: ImageBase.hh:130
(8bit) signed char image storage type
Definition: ImageBase.hh:113
static int ChannelsFromGLFormat(GLenum textureFormat)
Returns the number of channels required to store an image of specified texture format, e.g.
float image storage type
Definition: ImageBase.hh:118
static void GetFormatForInternalFormat(GLenum internalFormat, GLenum &format, int &numChannels)
Computes a pixel format that can be used with glGetTexImage and glReadPixels for an internal format o...
Definition: glfFormatDB.cpp:32
double image storage type
Definition: ImageBase.hh:119
invalid not set image storage type
Definition: ImageBase.hh:111
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
color values, 3 channels, order: blue,green,red
Definition: ImageBase.hh:132
Disparity images Q: should disp and depth be treated separately, if not what would be a good name to ...
Definition: ImageBase.hh:158
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
CIELUV color space, 3 channels, http://en.wikipedia.org/wiki/CIELUV_color_space.
Definition: ImageBase.hh:155
static GLenum ProposeInternalFormat(ImageBase::EStorageType storageType, ImageBase::EColorModel colorModel)
Returns an OpenGL internal pixel format for a BIAS storage type and color model.
RGBA, 4 channels, order: red,green,blue,alpha.
Definition: ImageBase.hh:141
static GLenum GetFormatForColorModel(ImageBase::EColorModel colorModel)
Returns the OpenGL pixel format (GL_RGB etc.) for a BIAS color model.
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
static GLenum GetPixelTypeForStorageType(ImageBase::EStorageType storageType)
Returns the OpenGL pixel type (GL_UNSIGNED_BYTE etc.) for a BIAS storage type.
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
GreyA, 2 channels, grey plus Alpha.
Definition: ImageBase.hh:142
Depth images A: separated for now.
Definition: ImageBase.hh:159
BGRA color values, 4 channels, order: blue,green,red,alpha.
Definition: ImageBase.hh:150
static GLenum GLenumFromString(const std::string &enumString)
Taking the enum as string and converting to the appropriate numerical constant.
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116