Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImgObjGL.cpp
1 // Jan Woetzel
2 
3 #include "ImgObjGL.hh"
4 
5 // DBG:
6 #include <Base/Image/ImageIO.hh>
7 #include <iostream>
8 
9 // -----------------------------
10 using namespace BIAS;
11 using namespace std;
12 
13 
14 // destructor
16 {
17  Release();
18 }
19 
20 
21 // ctor
23 : p_valueImage(NULL)
24 {
25  InitMembers();
26 }
27 
28 
29 // ctor
31  const GLuint & id,
32  const unsigned int & widthBrutto,
33  const unsigned int & heightBrutto,
34  const GLenum & target )
35 {
36  InitMembers();
37  this->id = id;
38  this->widthBrutto = widthBrutto;
39  this->heightBrutto = heightBrutto;
40  this->target = target;
41 
42  // assume netto=brutto size
43  this->widthOrig = widthBrutto;
44  this->heightOrig = heightBrutto;
45 
46 }
47 
48 
49 // static
51 {
52 #ifdef BIAS_HAVE_GLEW
53  if (!GLEW_VERSION_1_1){
54  // maybe glewInit was missing
55  static int err = -42;
56  if (GLEW_OK != err){
57  BIASWARN("initializing glewInit.");
58  err=glewInit(); // once
59  if (GLEW_OK != err) {
60  // problem: glewInit failed, something is seriously wrong
61  if (glewGetErrorString(err)==NULL) {
62  BIASERR("could not init GLEW. glewGetErrorString(err) is NULL");
63  } else {
64  if (err==-42){ /* catceh arr that glewgetErorstring would crash */
65  BIASERR("could not init GLEW. Error -42 ");
66  } else {
67  BIASERR("could not init GLEW. Error: "<<glewGetErrorString(err));
68  BIASBREAK;
69  }
70  }
71  };
72  }
73  }
74 #endif // BIAS_HAVE_GLEW
75 }
76 
77 
79 {
80  id=0;
81  p_id_SelfAllocated=false;
82 
83  target=GL_TEXTURE_2D;
85  GL_NEAREST /* prefer blocks for zoom in */
86  //GL_LINEAR
87  ;
89  GL_NEAREST /* accept aliasing */
90  //GL_LINEAR
91  /* avoid aliasing */
92  //GL_NEAREST_MIPMAP_NEAREST
93  //GL_LINEAR_MIPMAP_NEAREST // linear pix. interpol. within nearest mipmap
94  //GL_NEAREST_MIPMAP_LINEAR
95  //GL_LINEAR_MIPMAP_LINEAR
96  ;
97  widthOrig=0;
98  heightOrig=0;
99  widthBrutto=0;
100  heightBrutto=0;
101  p_valueImage=NULL;
103 }
104 
105 
106 
108 {
109  if (p_id_SelfAllocated){
110  this->DeleteGLTexture();
111  }
113  delete p_valueImage;
114  }
115  // always set NULL, no matter if we freed or another will do.
116  p_valueImage=NULL;
117 }
118 
119 
121 {
122  glEnable( target );
123  CHECK_GL_ERROR;
124  // free gl texture memory and id:
125  glDeleteTextures(1, (GLuint*)(&id) );
126  id=0;
127  CHECK_GL_ERROR;
128  glDisable( target );
129  CHECK_GL_ERROR;
130 
131  //this->InitMembers();
132 }
133 
134 
136 {
137  return target == GL_TEXTURE_2D;
138 }
139 
141 {
142 #if defined(BIAS_HAVE_GLEW)
143  return target == GL_TEXTURE_RECTANGLE_NV;
144 #else
145  return false;
146 #endif
147 }
148 
150 {
151 #if defined(BIAS_HAVE_GLEW)
152  return target == GL_TEXTURE_CUBE_MAP_EXT;
153 #else
154  return false;
155 #endif
156 }
157 
159  SetTarget2D();
160 }
162  target=GL_TEXTURE_2D;
163 }
165 #if defined(BIAS_HAVE_GLEW)
166  target=GL_TEXTURE_RECTANGLE_NV;
167 #else
168  BIASEXIT(1, "you need glew to use GL_TEXTURE_RECTANGLE_NV");
169 #endif
170 }
172 #if defined(BIAS_HAVE_GLEW)
173  target=GL_TEXTURE_CUBE_MAP_EXT;
174 #else
175  BIASEXIT(1, "you need glew to use GL_TEXTURE_RECTANGLE_NV");
176 #endif
177 }
178 
179 
180 
181 //JW
183  const bool & forcePow2tex,
184  const bool & flipY,
185  const bool ImmediateCopyData )
186 {
187  InitGlew();
188  Release(); // release old texture (if there is one)
189 
190  if (forcePow2tex)
191  target=GL_TEXTURE_2D;
192 
193  // do not change the texture target *TO* GL_TEXTURE_RECTANGLE_NV, user should do it by hand.
194  // This way you can put a pow2 texture into a RECT target - which makes sense for shaders.
195  // assume target is correct and fixed...
196 
197 
198  // TODO: use a pointer to avoid copy if it is not required.
199  // create a non const copy:
200  BIAS::ImageBase img(c_img);
201  //BIAS::Image<unsigned char> img(c_img);
202 
203  // save Orig size
204  this->widthOrig=img.GetWidth();
205  this->heightOrig=img.GetHeight();
206  this->widthBrutto=img.GetWidth();
207  this->heightBrutto=img.GetHeight();
208 
209  // determine new size (after padding)
210  if (!IsRectangleTexture() && !img.IsPowerOfTwoSize())
211  { /* padding required */
212  img.PowerOfTwoSize(this->widthBrutto, this->heightBrutto);
213  }
214 
215  // create a copy because we usually need to flip
216  if (ImmediateCopyData)
217  {
218  if (IsRectangleTexture()){
219  // RECT
220  if (flipY) // just flip, RECT dont need padding.
221  img.Flip();
222 
223  } else {
224  // POT 2D always needs padding
225  if (flipY) {
227  } else {
228  img.PadToPowerOfTwo();
229  }
230  };
231  BIASASSERT(img.GetWidth()==this->widthBrutto);
232  BIASASSERT(img.GetHeight()==this->heightBrutto);
233  }
234  BIASASSERT(this->widthBrutto>0);
235  BIASASSERT(this->heightBrutto>0);
236 
237  // HACK
238  glEnable( target );
239  CHECK_GL_ERROR;
240  glGenTextures(1, &id);
241  glBindTexture(target, id);
242  CHECK_GL_ERROR;
243 
244  // set up GL texture env an params
245  glPixelStorei(GL_PACK_ALIGNMENT, 1);
246  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
247  // border handling - repeat or clamp?
248  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP);
249  glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP);
250 
251  if (IsRectangleTexture()){
252  // no mipmapping for rectangle non-power of two texture
253  // minification
254  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255  // magnification
256  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
257  } else {
258  // build mipmaps on texture load?
259  switch (minificationMode){
260  case GL_NEAREST_MIPMAP_NEAREST:
261  case GL_LINEAR_MIPMAP_NEAREST:
262  case GL_NEAREST_MIPMAP_LINEAR:
263  case GL_LINEAR_MIPMAP_LINEAR:
264 #ifdef BIAS_HAVE_GLEW
265  // warning: mipmaping takes performance
266  glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
267  glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
268  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 10);
269 #endif //BIAS_HAVE_GLEW
270 #ifdef BIAS_HAVE_GLEW
271  glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 0.0);
272 #endif // BIAS_HAVE_GLEW
273  break;
274  case GL_NEAREST:
275  case GL_LINEAR:
276  default: {
277 #ifdef BIAS_HAVE_GLEW
278  glTexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
279  glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
280  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
281  glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 0.0);
282 #endif // BIAS_HAVE_GLEW
283  }
284  }; // end of switch
285  // minification
286  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minificationMode);
287  // magnification
288  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magnificationMode);
289  };
290  CHECK_GL_ERROR;
291 
292  //color RGB888 interleaved format: RGB RGB RGB
293  GLvoid * p_pixels=NULL;
294  // copy or just allocate?
295  if (ImmediateCopyData)
296  p_pixels = img.GetImageData();
297 
298  //
299  // determine flags for conversion between CPU and GPU storage
300  //
301 
302  GLenum pixSrc_fmt = GetGLPixelFormat(img.GetColorModel(), img.GetChannelCount() );
303  GLenum pixSrc_type = GetGLStorageType(img.GetStorageType(), img.GetDepth() );
304  // contains channelcount + precision, 1,2,3,4, GL_RGB, GL_RGBA is NOT enough for >8 bit
305  GLint gpu_internalFmt = GetGLInternalFormat(img);
306  // check if the GPU internal format (bits, channels)
307  // is compatible with texture target (pow2, RECT)
308  if (!IsRectangleTexture()) {
309  switch (gpu_internalFmt)
310  {
311  case 1:
312  case 2:
313  case 3:
314  case 4:
315  case GL_RGB:
316  case GL_RGBA:
317  case GL_BGR_EXT:
318  case GL_BGRA_EXT:
319  // OK, standard fmt that works with pow2 and rect.
320  break;
321  default:
322  // the other formats *may* not work with pow2 textures!
323  BIASERR("Your requested GPU internal format (>8 bit) and texture target (pow2 GL_TEXTURE_2D) are possibly not compatible!");
324  BIASBREAK;
325  };
326  };
327 
328  //
329  // now really create the GL texture4 (object)
330  // if p_pixels != NULL then the data will be copied, too.
331  //
332  glTexImage2D( target , // target
333  0, // level
334  gpu_internalFmt, // internalformat on GPU, typically GL_RGB
335  this->widthBrutto, // width, typically 1024
336  this->heightBrutto, // height, typically 512
337  GLint(0), // no border, BORDER does not work anyway!
338  pixSrc_fmt, // format of src pixel data, typically GL_RGB
339  pixSrc_type, // type, typcially GL_UNSIGNED_BYTE
340  p_pixels ); // datptr. NULL means just allocate (without copy)
341 
342  CHECK_GL_ERROR;
343  BIASASSERT( GL_TRUE == glIsTexture( id ) ); // success ?
344 
345  p_id_SelfAllocated=true; // we want to release id on destructor
346 
347  // replace, no light (often forgotten...)
348  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
349  glDisable( target );
350 
351  CHECK_GL_ERROR;
352 }
353 
354 
355 void ImgObjGL::CreateGLCubemap(const std::vector< BIAS::ImageBase * > cm,
356  const bool ImmediateCopyData,
357  const bool mipmapped )
358 {
359  InitGlew();
360  Release(); // release old texture (if there is one)
361 #if defined(BIAS_HAVE_GLEW)
362  target=GL_TEXTURE_CUBE_MAP_EXT;
363 #else
364  BIASEXIT(1,"You nee dglew to use GL_TEXTURE_CUBE_MAP_EXT")
365 #endif
366 
367  // cube maps must have
368  // -exactly 6 faces
369  // -pow2 dim
370  // -same width and height
371  // -same dim for all faces
372  BIASASSERT(cm.size()==6);
373  BIASASSERT(cm[0]!=NULL);
374  const unsigned int w=cm[0]->GetWidth();
375  const unsigned int h=cm[0]->GetHeight();
376  BIASASSERT(w==h);
377  BIASASSERT(w>0);
378  BIASASSERT(h>0);
379  BIASASSERT(BIAS::ImageBase::IsPowerOfTwoSize(w,h));
380 
381  // allocate
382  this->CreateGLCubemapEmpty(w, h,
383  this->GetGLInternalFormat(*cm[0]),
384  mipmapped);
385 
386  // fill with data
387  unsigned int face=0;
388  for (face=0; face<cm.size(); face++) {
389  BIASASSERT(cm[face]!=NULL);
390  // cube map faces must be equal size and equal w and h
391  BIASASSERT(w==cm[face]->GetWidth() );
392  BIASASSERT(h==cm[face]->GetHeight());
393 
394  // build mipmaps on texture load?
395  switch (minificationMode){
396  case GL_NEAREST_MIPMAP_NEAREST:
397  case GL_LINEAR_MIPMAP_NEAREST:
398  case GL_NEAREST_MIPMAP_LINEAR:
399  case GL_LINEAR_MIPMAP_LINEAR:
400 #ifdef BIAS_HAVE_GLEW
401  // warning: mipmaping takes performance
402  glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
403  glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
404  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 10);
405  glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 0.0);
406 #endif // BIAS_HAVE_GLEW
407  break;
408  case GL_NEAREST:
409  case GL_LINEAR:
410  default:
411  {
412 #ifdef BIAS_HAVE_GLEW
413  glTexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
414  glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
415  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
416  glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 0.0);
417 #endif // BIAS_HAVE_GLEW
418  }
419  }; // end of switch
420 
421  // minification
422  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minificationMode);
423  // magnification
424  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magnificationMode);
425 
426  if (ImmediateCopyData){
427 #if defined(BIAS_HAVE_GLEW)
428  // copy pixel data
429  GLvoid * p_pixels = cm[face]->GetImageData();
430  GLenum pixSrc_fmt = GetGLPixelFormat(cm[face]->GetColorModel(), cm[face]->GetChannelCount() );
431  GLenum pixSrc_type = GetGLStorageType(cm[face]->GetStorageType(), cm[face]->GetDepth() );
432  GLint gpu_internalFmt = GetGLInternalFormat(*cm[face]);
433  glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT +face , // target
434  0, // level
435  gpu_internalFmt, // internalformat on GPU, typically GL_RGB
436  this->widthBrutto, // width, typically 512
437  this->heightBrutto, // height, typically 512
438  GLint(0), // no border, BORDER does not work anyway!
439  pixSrc_fmt, // format of src pixel data, typically GL_RGB
440  pixSrc_type, // type, typcially GL_UNSIGNED_BYTE
441  p_pixels ); // datptr. NULL means just allocate (without copy)
442 #else
443  BIASEXIT(1, "you need glew to use GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT");
444 #endif
445  //if (mipmapped)
446  //gluBuild2DMipmaps
447  CHECK_GL_ERROR;
448  BIASASSERT( GL_TRUE == glIsTexture( id ) ); // success ?
449  }
450  } // for all faces
451  p_id_SelfAllocated=true; // we want to release id on destructor
452  glDisable( target );
453  CHECK_GL_ERROR;
454 }
455 
456 
457 // create an empty cubemap including allocating all mipmap texture levels
458 // thus the complete mipmap chain for all faces and levels is created on request
459 void ImgObjGL::CreateGLCubemapEmpty(const unsigned int w, const unsigned int h,
460  GLint gpu_internalFmt,
461  const bool & mipmapped)
462 {
463 #if !defined(BIAS_HAVE_GLEW)
464  BIASEXIT(1,"you need glew to use GL_TEXTURE_CUBE_MAP_EXT");
465 #else
466 
467  InitGlew();
468  Release(); // release old texture (if there is one)
469 
470  target=GL_TEXTURE_CUBE_MAP_EXT;
471 
472  // cube maps must have
473  // -exactly 6 faces
474  // -pow2 dim
475  // -same width and height
476  // -same dim for all faces
477  BIASASSERT(target==GL_TEXTURE_CUBE_MAP_EXT);
478  BIASASSERT(w==h);
479  BIASASSERT(w>0);
480  BIASASSERT(h>0);
481  BIASASSERT(BIAS::ImageBase::IsPowerOfTwoSize(w,h));
482 
483  // save Orig size
484  this->widthOrig = w;
485  this->heightOrig = h;
486  this->widthBrutto = w;
487  this->heightBrutto = h;
488 
489  int maxLevel = 0;
490  if (mipmapped) {
491  maxLevel = (int)(logf((float)w)/logf(2.0f));
492  BIASASSERT(maxLevel>=0);
493  BIASASSERT(maxLevel<=12);// 12==ld(4096), the usual max.
494  }
495 
496  glEnable( target );
497  CHECK_GL_ERROR;
498  glGenTextures(1, &id);
499  glBindTexture(target, id);
500  CHECK_GL_ERROR;
501 
502  // set up GL texture env an params
503  glPixelStorei(GL_PACK_ALIGNMENT, 1);
504  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
505 
506  // border handling - should be clamp to avoid cube border artefacts
507  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP);
508  glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP);
509  glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
510  // replace, no light (often forgotten...)
511  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
512 
513 #ifdef BIAS_HAVE_GLEW
514  glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 0.0);
515 #endif // BIAS_HAVE_GLEW
516 
517  // build mipmaps on texture load?
518  switch (minificationMode){
519  case GL_NEAREST_MIPMAP_NEAREST:
520  case GL_LINEAR_MIPMAP_NEAREST:
521  case GL_NEAREST_MIPMAP_LINEAR:
522  case GL_LINEAR_MIPMAP_LINEAR:
523  {
524  BIASASSERT(mipmapped);
525  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, (GLint)maxLevel );
526  break;
527  }
528  case GL_NEAREST:
529  case GL_LINEAR:
530  default:
531  BIASASSERT(!mipmapped);
532  glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
533  }; // end of switch
534  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minificationMode);
535  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magnificationMode);
536  CHECK_GL_ERROR;
537 
538  GLvoid * p_pixels_dummy=NULL; // dummy pixel data ptr
539  unsigned int face=0;
540  int level=0;
541  // allocate for all faces amd all levels
542  for (face=0; face<6; face++)
543  {
544  for (level=0; level<=maxLevel; level++)
545  {
546  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT+face, // do NOT use target here
547  (GLint)level,
548  (GLint)gpu_internalFmt, // internalformat on GPU, typically GL_RGB/GL_RGBA
549  w,
550  h,
551  GLint(0), // no border, BORDER does not work anyway!
552  GL_RGB, // format ,unsued, ptr NULL, of src pixel data, typically GL_RGB
553  GL_UNSIGNED_BYTE, // type, unsued, ptr NULL,, typcially GL_UNSIGNED_BYTE
554  p_pixels_dummy ); // datptr. NULL means just allocate (without copy)
555  CHECK_GL_ERROR;
556  }
557  }
558  CHECK_GL_ERROR;
559  BIASASSERT( GL_TRUE == glIsTexture( id ) ); // success ?
560  p_id_SelfAllocated=true; // we want to release id on destructor
561  glDisable( target );
562 
563 #endif
564  CHECK_GL_ERROR;
565 }
566 
567 
568 int ImgObjGL::CreateGLCubemap(const std::string & cm0PosX,
569  const std::string & cm1NegX,
570  const std::string & cm2PosY,
571  const std::string & cm3NegY,
572  const std::string & cm4PosZ,
573  const std::string & cm5NegZ,
574  const bool ImmediateCopyData,
575  const bool mipmapped)
576 {
577  std::vector< std::string > filenames;
578  filenames.push_back(cm0PosX);
579  filenames.push_back(cm1NegX);
580  filenames.push_back(cm2PosY);
581  filenames.push_back(cm3NegY);
582  filenames.push_back(cm4PosZ);
583  filenames.push_back(cm5NegZ);
584  BIASASSERT(filenames.size()==6);
585  return CreateGLCubemap(filenames, ImmediateCopyData, mipmapped);
586 }
587 
588 
589 int ImgObjGL::CreateGLCubemap(const std::vector< std::string > cmFilenames,
590  const bool ImmediateCopyData,
591  const bool mipmapped)
592 {
593  int result=0;
594  BIASASSERT(cmFilenames.size()==6);
595  std::vector< BIAS::ImageBase > img;
596  img.resize(cmFilenames.size());
597  int loadres=0;
598  for (unsigned int i=0; i<img.size(); i++)
599  {
600  loadres=BIAS::ImageIO::Load(cmFilenames[i], img[i]);
601  if (loadres!=0) {
602  BIASERR("could not load "<<cmFilenames[i]);
603  BIASBREAK;
604  result=-1*i;
605  } else {
606  // OpenGL image coordiate system has origin on lower left while BIAS has upper left corner
607  img[i].Flip();
608  }
609  }
610  if (result==0)
611  {
612  // convert to image pointers
613  std::vector<BIAS::ImageBase * > v_ptr;
614  for (unsigned int i=0; i<img.size(); i++)
615  v_ptr.push_back( &img[i] );
616 
617  BIASASSERT(v_ptr.size()==6);
618  CreateGLCubemap(v_ptr, ImmediateCopyData, mipmapped );
619  }
620  return result;
621 }
622 
623 
624 //static
626  const unsigned int & ByteDepth )
627 {
628  switch (biasST)
629  {
631  BIASASSERT(ByteDepth==1);
632  return GL_UNSIGNED_BYTE;
633  break;
635  BIASASSERT(ByteDepth==1);
636  return GL_BYTE;
637  break;
639  BIASASSERT(ByteDepth==2);
640  return GL_UNSIGNED_SHORT;
641  break;
643  BIASASSERT(ByteDepth==2);
644  return GL_SHORT;
645  break;
647  BIASASSERT(ByteDepth==4);
648  return GL_UNSIGNED_INT;
649  break;
651  BIASASSERT(ByteDepth==4);
652  return GL_INT;
653  break;
655  BIASASSERT(ByteDepth==4);
656  return GL_FLOAT;
657  break;
658  default:
659  BIASERR( "unsupported StorageType:"<<biasST<<" "
660  <<"(with Byte depth="<<ByteDepth<<")" );
661  BIASBREAK;
662  };
663  return 0;
664 }
665 
666 
667 //static
669  const unsigned int & channelcount )
670 {
671  switch (biasCM)
672  {
673  // these may need ifs tbhrough glew for > GL 1.1
674  // DIB
676  BIASASSERT(channelcount==3);
677 #ifdef BIAS_HAVE_GLEW
678  if (!GLEW_VERSION_1_1)
679  BIASERR("You need a BGR capable GPU!");
680 #endif // BIAS_HAVE_GLEW
681  return GL_BGR_EXT;
682  break;
683  // DIB
685  BIASASSERT(channelcount==3);
686 #ifdef BIAS_HAVE_GLEW
687  if (!GLEW_VERSION_1_1)
688  BIASERR("You need a BGRA_EXT capable GPU!");
689 #endif // BIAS_HAVE_GLEW
690  return GL_BGRA_EXT;
691  break;
692  // one channel grey, replicated to 4 channel RGBA (grey,grey,grey, 1.0)
694  BIASASSERT(channelcount==1);
695  return GL_LUMINANCE;
696  break;
697  // one channel grey+Alpha, replicated to 4 channel RGBA (grey,grey,grey, alpha)
699  BIASASSERT(channelcount==2);
700  return GL_LUMINANCE_ALPHA;
701  break;
702  // one channel grey+Alpha, replicated to 4 channel RGBA (grey,grey,grey, alpha)
703 
708  BIASASSERT(channelcount==3);
709  BIASWARN("you are loading raw Bayer image as RGBA.");
710  return GL_RGB;
711  break;
712 
716  BIASWARN("you are loading HSV/HSL/hsL as RGB.");
718  BIASASSERT(channelcount==3);
719  return GL_RGB;
720  break;
721 
722  // the two simple default cases, used for unkonw formats:
724  BIASASSERT(channelcount==4);
725  return GL_RGBA;
726  break;
727 
728  default:
729  BIASERR( "unsupported ColroModel:"<<biasCM<<" "
730  <<"(with channelcount ="<<channelcount<<")" );
731  BIASBREAK;
732  };
733  return 0;
734 }
735 
736 
737 //static
739 {
740  return GetGLInternalFormat(
741  img.GetStorageType(),
742  img.GetColorModel(),
743  img.GetChannelCount(),
744  img.GetDepth(),
745  img.GetBitDepth()
746  );
747 }
748 
749 
750 //static
752  const BIAS::ImageBase::EColorModel & /*biasCM*/,
753  const unsigned int & channelcount,
754  const unsigned int & ByteDepth,
755  const unsigned int & BitDepth )
756 {
757  // bind the GPU storagetype to the number of Byte and channels for now
758  // TODO: more general assignment to nr. of bits, colormodel etc.
759 
760  // 1,2,3,4,
761  // GL_RGB, GL_RGBA, GL_LUMINANCE
762  // GL_FLOAT_R32_NV
763  // GL_RGBA32F_ARB
764  // GL_FLOAT_RGB16_NV
765 
766  // do ByteDepth and storagtype depth match?
767  BIASASSERT( ByteDepth == (unsigned int) ImageBase::GetSizeByte(biasST));
768 
769  // more fractional bits than capacity?
770  BIASASSERT( (unsigned int)(ImageBase::GetSizeByte(biasST)*8) <= BitDepth);
771 
772  switch (ByteDepth)
773  {
774  case 1: // 8 bit
775  {
776  if (channelcount==4)
777  return GL_RGBA;
778  if (channelcount==3)
779  return GL_RGB;
780  //if (channelcount==1)
781  // return GL_LUMINANCE; /// TODO
782  BIASERR("unsupported channel count for 8 bit");
783  BIASBREAK;
784  break;
785  }
786  case 2: // 16 bit
787  {
788 #if defined(BIAS_DEBUG) && defined(BIAS_HAVE_GLEW)
789  if (!GLEW_VERSION_1_1)
790  BIASERR("glew said not even OpenGL 1.1 is supported - did you call glewInit() ?");
791 #endif
792 
793 #ifdef BIAS_HAVE_GLEW
794  if (GLEW_ARB_texture_float){ // requires NV GF FX 6,7
795 
796  if (channelcount==4)
797  return GL_RGBA16F_ARB;
798  else if (channelcount==3)
799  return GL_RGB16F_ARB;
800 
801  } else if (GLEW_NV_float_buffer){ // for older NV GF 5xxx
802 
803  if (channelcount==4)
804  return GL_FLOAT_RGBA16_NV;
805  else if (channelcount==3)
806  return GL_FLOAT_RGB16_NV;
807  else if (channelcount==1)
808  return GL_FLOAT_RGBA16_NV; // TODO
809  }
810 #endif // BIAS_HAVE_GLEW
811  BIASERR("unsupported channelcount="<<channelcount<<" for 16 bit or missing extension (e.g. ARB_texture_float,NV_float_buffer)");
812  BIASBREAK;
813  break;
814  }
815  case 4: // 32 bit
816  {
817 #if defined(BIAS_DEBUG) && defined(BIAS_HAVE_GLEW)
818  if (!GLEW_VERSION_1_1)
819  BIASERR("glew said not even OpenGL 1.1 is supported - did you call glewInit() ?");
820 #endif //BIAS_DEBUG + BIAS_HAVE_GLEW
821 
822 #ifdef BIAS_HAVE_GLEW
823 
824  if (GLEW_ARB_texture_float){ // newer extension, requires Nvidia GF 6,7
825 
826  if (channelcount==4)
827  return GL_RGBA32F_ARB;
828  else if (channelcount==3)
829  return GL_RGB32F_ARB;
830  else if (channelcount==2)
831  return GL_LUMINANCE_ALPHA32F_ARB; // untested
832  else if (channelcount==1) {
833  BIASWARNONCE("One channel (e.g.LUMINANCE) image is probably not GPU Hardware accelerated! "
834  "You may want to use RGB or RGBA instead!");
835  //return GL_LUMINANCE32F_ARB; // TODO TESTME. use RGB/RGBA instaed?
836  return GL_RGB16F_ARB; // required for Oliver Niemann, (JW)
837  }
838 
839  } else if (GLEW_NV_float_buffer){ // works (only?) with Geforce FX 5xxx
840 
841  if (channelcount==4)
842  return GL_FLOAT_RGBA32_NV;
843  else if (channelcount==3)
844  return GL_FLOAT_RGB32_NV;
845  else if (channelcount==1) {
846  BIASWARNONCE("One channel (e.g. LUMINANCE) images are probably not GPU Hardware accelerated! "
847  "Using RGBA for one channel image.");
848  return GL_FLOAT_RGBA32_NV; // TESTME. Only RGB, RGBA are currently HW accelerated!
849  }
850 
851  } // ext.
852 #endif // BIAS_HAVE_GLEW
853 
854  BIASERR("unsupported channelcount="<<channelcount<<" for 32 bit or missing extension (e.g. ARB_texture_float,NV_float_buffer)");
855  BIASBREAK;
856  break;
857  }
858  default:
859  BIASERR("unsupported ByteDepth.");
860  BIASBREAK;
861  }
862  return 0;
863 }
864 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
Bayer_GRBG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:145
static GLenum GetGLStorageType(const BIAS::ImageBase::EStorageType &biasST, const unsigned int &ByteDepth)
get the GLenum pixel storage type for the corresponding BIAS enum
Definition: ImgObjGL.cpp:625
bool IsPowerOfTwoSize() const
Definition: ImageBase.cpp:940
hsl, similar to HSL but euclidean (h,s) for CNCC
Definition: ImageBase.hh:148
unsigned int heightOrig
Definition: ImgObjGL.hh:148
void Release()
frees the pointer members if we allocated them
Definition: ImgObjGL.cpp:107
static unsigned int PowerOfTwoSize(const unsigned int &val)
Definition: ImageBase.cpp:971
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
BIAS::ImageBase * p_valueImage
Definition: ImgObjGL.hh:155
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
HSL, similar to HSV but space is a double tipped cone.
Definition: ImageBase.hh:147
gray values, 1 channel
Definition: ImageBase.hh:130
GLenum minificationMode
Definition: ImgObjGL.hh:143
(8bit) signed char image storage type
Definition: ImageBase.hh:113
unsigned int GetSizeByte() const
returns the nr.
Definition: ImageBase.hh:352
void SetTargetRECT()
Definition: ImgObjGL.cpp:164
Bayer_RGGB, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:143
float image storage type
Definition: ImageBase.hh:118
bool IsTexture2D() const
Definition: ImgObjGL.cpp:135
bool IsRectangleTexture() const
Definition: ImgObjGL.cpp:140
unsigned int heightBrutto
Definition: ImgObjGL.hh:152
unsigned int GetWidth() const
Definition: ImageBase.hh:312
unsigned int GetBitDepth() const
returns the bits per channel Is not necessairily 8*sizeof(StorageType), could be fewer bits...
Definition: ImageBase.hh:344
bool p_valueImage_SelfAllocated
Definition: ImgObjGL.hh:159
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
color values, 3 channels, order: blue,green,red
Definition: ImageBase.hh:132
Bayer_BGGR, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:146
const void * GetImageData() const
Definition: ImageBase.hh:280
Bayer_GBRG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:144
GLenum magnificationMode
Definition: ImgObjGL.hh:144
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
static GLenum GetGLPixelFormat(const BIAS::ImageBase::EColorModel &biasCM, const unsigned int &channelcount)
get the GLenum pixel src format for the corresponding BIAS enum ColroModel
Definition: ImgObjGL.cpp:668
static GLint GetGLInternalFormat(const BIAS::ImageBase::EStorageType &biasST, const BIAS::ImageBase::EColorModel &biasCM, const unsigned int &channelcount, const unsigned int &ByteDepth, const unsigned int &BitDepth)
JW determin the GPU internal format the src data should be mapped to.
Definition: ImgObjGL.cpp:751
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void SetTargetPOW2()
set the textur retarget to rectangular or pow 2 texture JW
Definition: ImgObjGL.cpp:158
unsigned int widthOrig
Definition: ImgObjGL.hh:147
void InitMembers()
set members to defaults
Definition: ImgObjGL.cpp:78
RGBA, 4 channels, order: red,green,blue,alpha.
Definition: ImageBase.hh:141
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
void CreateGLTexture(const BIAS::ImageBase &c_img, const bool &forcePow2tex=true, const bool &flipY=true, const bool ImmediateCopyData=IMGOBJ_IMMEDIATE_COPY_DEFAULT)
creates an OpenGL texture from p_valueImage JW if ImmediateCopyData is false the memory is just all...
Definition: ImgObjGL.cpp:182
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
void SetTarget2D()
Definition: ImgObjGL.cpp:161
void CreateGLCubemap(const std::vector< BIAS::ImageBase * > cm, const bool ImmediateCopyData=IMGOBJ_IMMEDIATE_COPY_DEFAULT, const bool mipmapped=IMGOBJ_DEFAULT_MIPMAPPED)
Definition: ImgObjGL.cpp:355
bool IsCubemap() const
Definition: ImgObjGL.cpp:149
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
HSV, 3 channels, order: hue, sat , value.
Definition: ImageBase.hh:138
static void InitGlew()
convenience interace for glewInit() c´with checks and ifdefs
Definition: ImgObjGL.cpp:50
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
Definition: ImageBase.cpp:834
GLenum target
Definition: ImgObjGL.hh:138
void SetTargetCUBE()
Definition: ImgObjGL.cpp:171
void DeleteGLTexture()
deletes the texture memory and frees texture in GL
Definition: ImgObjGL.cpp:120
int PadToPowerOfTwoAndFlip(const int &padVal=0)
first pad, then flip.
Definition: ImageBase.cpp:1282
GreyA, 2 channels, grey plus Alpha.
Definition: ImageBase.hh:142
bool p_id_SelfAllocated
Definition: ImgObjGL.hh:141
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
unsigned int widthBrutto
Definition: ImgObjGL.hh:151
int PadToPowerOfTwo(BIAS::ImageBase &dest, const int &padVal=0) const
increase the size of this image to next power of two (e.g.
Definition: ImageBase.cpp:1154
void CreateGLCubemapEmpty(const unsigned int w, const unsigned int h, GLint gpu_internalFmt=GL_RGBA, const bool &mipmapped=false)
Definition: ImgObjGL.cpp:459
BGRA color values, 4 channels, order: blue,green,red,alpha.
Definition: ImageBase.hh:150
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116