PICCANTE  0.4
The hottest HDR imaging library!
filter_op.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_GL_FILTERING_FILTER_OP_HPP
19 #define PIC_GL_FILTERING_FILTER_OP_HPP
20 
21 #include "../../base.hpp"
22 
23 #include "../../gl/filtering/filter.hpp"
24 #include "../../util/string.hpp"
25 
26 namespace pic {
27 
28 class FilterGLOp: public FilterGL
29 {
30 protected:
31  std::string op;
32  float c0[4], c1[4];
34 
35  void initShaders();
36 
37 public:
38 
46  FilterGLOp(std::string op, bool bTexelFetch, float *c0, float *c1);
47 
53  void update(float *c0, float *c1);
54 
60  {
61  float val[4] = {0.0f, 0.0f, 0.0f, 1.0f};
62  FilterGLOp *filter = new FilterGLOp("C0", true, val, NULL);
63  return filter;
64  }
65 
71  {
72  float val[4] = {1.0f, 1.0f, 1.0f, 1.0f};
73  FilterGLOp *filter = new FilterGLOp("C0", true, val, NULL);
74  return filter;
75  }
76 
82  static FilterGLOp *CreateOpIdentity(bool bType)
83  {
84  FilterGLOp *filter = new FilterGLOp("I0", bType, NULL, NULL);
85  return filter;
86  }
87 
94  static FilterGLOp *CreateOpSegmentation(bool bType, float minVal)
95  {
96  float tmp[4];
97  tmp[0] = tmp[1] = tmp[2] = tmp[3] = minVal;
98  FilterGLOp *filter = new FilterGLOp("(I0.x > 0.0) ? floor(log(I0) / 2.3026) : C0 ",
99  bType, tmp, NULL);
100  return filter;
101  }
102 
108  static FilterGLOp *CreateOpAdd(bool bType)
109  {
110  FilterGLOp *filter = new FilterGLOp("I0 + I1", bType, NULL, NULL);
111  return filter;
112  }
113 
119  static FilterGLOp *CreateOpMulNeg(bool bType)
120  {
121  FilterGLOp *filter = new FilterGLOp("(vec4(1.0) - I1) * I0", bType, NULL, NULL);
122  return filter;
123  }
124 
130  static FilterGLOp *CreateOpMul(bool bType)
131  {
132  FilterGLOp *filter = new FilterGLOp("I0 * I1", bType, NULL, NULL);
133  return filter;
134  }
135 
141  static FilterGLOp *CreateOpSub(bool bType)
142  {
143  FilterGLOp *filter = new FilterGLOp("I0 - I1", bType, NULL, NULL);
144  return filter;
145  }
146 
152  static FilterGLOp *CreateOpDiv(bool bType)
153  {
154  FilterGLOp *filter = new FilterGLOp("I0 / I1", bType, NULL, NULL);
155  return filter;
156  }
157 
163  static FilterGLOp *CreateOpDivConst(bool bType)
164  {
165  FilterGLOp *filter = new FilterGLOp("I0 / C0", bType, NULL, NULL);
166  return filter;
167  }
168 };
169 
170 PIC_INLINE FilterGLOp::FilterGLOp(std::string op, bool bTexelFetch = false,
171  float *c0 = NULL, float *c1 = NULL): FilterGL()
172 {
173  if(c0 != NULL) {
174  memcpy(this->c0, c0, 4 * sizeof(float));
175  } else {
176  Arrayf::assign(1.0f, this->c0, 4);
177  }
178 
179  if(c1 != NULL) {
180  memcpy(this->c1, c1, 4 * sizeof(float));
181  } else {
182  Arrayf::assign(1.0f, this->c1, 4);
183  }
184 
185  this->op = op;
186  this->bTexelFetch = bTexelFetch;
187 
188  if(!bTexelFetch) {
189 
190  if(quad != NULL) {
191  delete quad;
192  }
193 
194  quad = new QuadGL(true);
196  }
197 
198  initShaders();
199 }
200 
202 {
203  std::string strOp = "ret = ";
204  strOp.append(op);
205  strOp.append(";\n");
206  int counter;
207 
208  //I0
209  counter = countSubString(strOp, "I0");
210 
211  if(counter == 1) {
212  size_t I_found = strOp.find("I0");
213 
214  if(I_found != std::string::npos) {
215  if(bTexelFetch) {
216  strOp.replace(I_found, 2, "texelFetch(u_tex_0, coords, 0)");
217  } else {
218  strOp.replace(I_found, 2, "texture(u_tex_0, coords)");
219  }
220  }
221  } else {
222  if(counter > 1) {
223  if(bTexelFetch) {
224  strOp = "vec4 tmp0 = texelFetch(u_tex_0, coords, 0);\n" + strOp;
225  } else {
226  strOp = "vec4 tmp0 = texture(u_tex_0, coords);\n" + strOp;
227  }
228 
229  strOp = stdStringRepAll(strOp, "I0", "tmp0");
230  }
231  }
232 
233  //I1
234  counter = countSubString(strOp, "I1");
235 
236  if(counter == 1) {
237  size_t I_found = strOp.find("I1");
238 
239  if(I_found != std::string::npos) {
240  if(bTexelFetch) {
241  strOp.replace(I_found, 2, "texelFetch(u_tex_1, coords, 0)");
242  } else {
243  strOp.replace(I_found, 2, "texture(u_tex_1, coords)");
244  }
245  }
246  } else {
247  if(counter > 1) {
248  if(bTexelFetch) {
249  strOp = "vec4 tmp1 = texelFetch(u_tex_1, coords, 0);\n" + strOp;
250  } else {
251  strOp = "vec4 tmp1 = texture(u_tex_1, coords);\n" + strOp;
252  }
253 
254  strOp = stdStringRepAll(strOp, "I1", "tmp1");
255  }
256  }
257 
258  //C1 and C2
259  strOp = stdStringRepAll(strOp, "C0", "u_val_0");
260  strOp = stdStringRepAll(strOp, "C1", "u_val_1");
261 
263  (
264  uniform sampler2D u_tex_0; \n
265  uniform sampler2D u_tex_1; \n
266  uniform vec4 u_val_0; \n
267  uniform vec4 u_val_1; \n
268  out vec4 f_color; \n
269  in vec2 v_tex_coord; \n
270  \n
271  void main(void) {
272  \n
273  _COORDINATES_FOR_FETCHING_ \n
274  vec4 ret;
275  \n
276  _PROCESSING_OPERATOR_ \n
277  f_color = ret;
278  \n
279  }
280  );
281 
282  if(bTexelFetch) {
283  size_t processing_found = fragment_source.find("_COORDINATES_FOR_FETCHING_");
284  fragment_source.replace(processing_found, 27,
285  "ivec2 coords = ivec2(gl_FragCoord.xy);\n");
286  } else {
287  size_t processing_found = fragment_source.find("_COORDINATES_FOR_FETCHING_");
288  fragment_source.replace(processing_found, 27,
289  "vec2 coords = v_tex_coord.xy;\n");
290  }
291 
292  size_t processing_found = fragment_source.find("_PROCESSING_OPERATOR_");
293  fragment_source.replace(processing_found, 21, strOp);
294 
296 
297 #ifdef PIC_DEBUG
298  technique.printLog("FilterOp");
299 #endif
300 
301  technique.bind();
302  technique.setAttributeIndex("a_position", 0);
303 
304  if(!bTexelFetch) {
305  technique.setAttributeIndex("a_tex_coord", 1);
306  }
307 
309  technique.link();
310  technique.unbind();
311 
312  technique.bind();
313  technique.setUniform1i("u_tex_0", 0);
314  technique.setUniform1i("u_tex_1", 1);
315  technique.setUniform4fv("u_val_0", c0);
316  technique.setUniform4fv("u_val_1", c1);
317  technique.unbind();
318 }
319 
320 PIC_INLINE void FilterGLOp::update(float *c0, float *c1)
321 {
322  if(c0 != NULL) {
323  for(int i = 0; i < 4; i++) {
324  this->c0[i] = c0[i];
325  }
326  }
327 
328  if(c1 != NULL) {
329  for(int i = 0; i < 4; i++) {
330  this->c1[i] = c1[i];
331  }
332  }
333 
334  technique.bind();
335  technique.setUniform1i("u_tex_0", 0);
336  technique.setUniform1i("u_tex_1", 1);
337  technique.setUniform4fv("u_val_0", this->c0);
338  technique.setUniform4fv("u_val_1", this->c1);
339  technique.unbind();
340 }
341 
342 } // end namespace pic
343 
344 #endif /* PIC_GL_FILTERING_FILTER_OP_HPP */
345 
static FilterGLOp * CreateOpMul(bool bType)
CreateOpMul.
Definition: filter_op.hpp:130
static std::string getVertexProgramWithTexCoordinates()
getVertexProgramWithTexCoordinates creates a simple vertex program with texture coordinates as input...
Definition: quad.hpp:253
static FilterGLOp * CreateOpDivConst(bool bType)
CreateOpDivConst.
Definition: filter_op.hpp:163
TechniqueGL technique
Definition: display.hpp:45
static FilterGLOp * CreateOpDiv(bool bType)
CreateOpDiv.
Definition: filter_op.hpp:152
FilterGLOp(std::string op, bool bTexelFetch, float *c0, float *c1)
FilterGLOp.
int countSubString(std::string str, std::string subStr)
countSubString counts how many subStr are in str.
Definition: string.hpp:240
void printLog(std::string name)
printLog
Definition: display.hpp:176
static FilterGLOp * CreateOpSub(bool bType)
CreateOpSub.
Definition: filter_op.hpp:141
std::string op
Definition: filter_op.hpp:31
void bind()
bind
Definition: display.hpp:189
#define MAKE_STRING(input_string)
Definition: filter_op.hpp:28
void setUniform4fv(const char *name_uniform, const float *value)
setUniform4
Definition: display.hpp:343
static FilterGLOp * CreateOpIdentity(bool bType)
CreateOpIdentity.
Definition: filter_op.hpp:82
static FilterGLOp * CreateOpAdd(bool bType)
CreateOpAdd.
Definition: filter_op.hpp:108
void setAttributeIndex(const char *attribute_name, unsigned int index)
setAttributeIndex
Definition: display.hpp:225
QuadGL * quad
Definition: display.hpp:42
The FilterGL class.
Definition: filter.hpp:35
static FilterGLOp * CreateOpSegmentation(bool bType, float minVal)
CreateOpSegmentation.
Definition: filter_op.hpp:94
The FilterGL class.
Definition: display.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
std::string stdStringRepAll(std::string str, std::string strSub, std::string strRep)
stdStringRepAll replaces all strSub in str with strRep.
Definition: string.hpp:79
void unbind()
unbind
Definition: display.hpp:197
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
void setOutputFragmentShaderIndex(const char *fragment_output_color_name, unsigned int index)
setOutputFragmentShaderIndex
Definition: display.hpp:215
static T * assign(T *data, int size, T *ret)
assign
Definition: array.hpp:464
void update(float *c0, float *c1)
update
The QuadGL class.
Definition: display.hpp:30
void link()
link
Definition: display.hpp:205
bool bTexelFetch
Definition: filter_op.hpp:33
std::string fragment_source
Definition: display.hpp:57
float c1[4]
Definition: filter_op.hpp:32
float c0[4]
Definition: filter_op.hpp:32
bool init(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source)
Definition: display.hpp:67
static FilterGLOp * CreateOpMulNeg(bool bType)
CreateOpMulNeg.
Definition: filter_op.hpp:119
static FilterGLOp * CreateOpSetZero()
CreateOpSetZero.
Definition: filter_op.hpp:59
static FilterGLOp * CreateOpSetOne()
CreateOpSetOne.
Definition: filter_op.hpp:70
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236