PICCANTE  0.4
The hottest HDR imaging library!
mask.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_GL_MASK_HPP
19 #define PIC_UTIL_GL_MASK_HPP
20 
21 #include "../../base.hpp"
22 
23 namespace pic {
24 
35 PIC_INLINE GLuint GenerateMask(int width, int height, bool *buffer = NULL,
36  GLuint tex = 0, unsigned char *tmpBuffer = NULL, bool mipmap = false)
37 {
38  bool bGen = (tex == 0);
39 
40  if(bGen) {
41  glGenTextures(1, &tex);
42  }
43 
44  glBindTexture(GL_TEXTURE_2D, tex);
45 
46  if(bGen) {
47  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
48  }
49 
50  unsigned char *data = NULL;
51 
52  if(buffer != NULL) {
53  int n = width * height;
54 
55  if(tmpBuffer != NULL) {
56  data = tmpBuffer;
57  } else {
58  data = new unsigned char[n * 3];
59  }
60 
61  #pragma omp parallel for
62 
63  for(int i = 0; i < n; i++) {
64  data[i] = buffer[i] ? 255 : 0;
65  }
66  }
67 
68  if(bGen) {
69 
70  if(mipmap) {
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
72  } else {
73  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74  }
75 
76  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
77  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
78  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
79  }
80 
81  /*
82  Note: GL_LUMINANCE is deprecated since OpenGL 3.1
83  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8 , width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
84  */
85 
86  glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
87 
88  if(mipmap && bGen) {
89  glGenerateMipmap(GL_TEXTURE_2D);
90  }
91 
92  glBindTexture(GL_TEXTURE_2D, 0);
93 
94  if(data != NULL && tmpBuffer == NULL) {
95  delete[] data;
96  }
97 
98  return tex;
99 }
100 
101 } // end namespace pic
102 
103 #endif /* PIC_UTIL_GL_ */
PIC_INLINE GLuint GenerateMask(int width, int height, bool *buffer=NULL, GLuint tex=0, unsigned char *tmpBuffer=NULL, bool mipmap=false)
GenerateMask creates an opengl mask (a texture) from a buffer of bool values.
Definition: mask.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
Definition: bilateral_separation.hpp:25