• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "../gstgleffects.h"
25 #include "gstgleffectscurves.h"
26 #include "gstgleffectlumatocurve.h"
27 
28 static gboolean kernel_ready = FALSE;
29 static float gauss_kernel[7];
30 
31 void
gst_gl_effects_xray(GstGLEffects * effects)32 gst_gl_effects_xray (GstGLEffects * effects)
33 {
34   const GstGLFuncs *gl = GST_GL_BASE_FILTER (effects)->context->gl_vtable;
35   GstGLFilter *filter = GST_GL_FILTER (effects);
36   GstGLShader *shader;
37 
38   if (!kernel_ready) {
39     fill_gaussian_kernel (gauss_kernel, 7, 1.5);
40     kernel_ready = TRUE;
41   }
42 
43   /* map luma to xray curve */
44   gst_gl_effects_luma_to_curve (effects, &xray_curve, GST_GL_EFFECTS_CURVE_XRAY,
45       effects->intexture, effects->midtexture[0]);
46 
47   /* horizontal blur */
48   shader = gst_gl_effects_get_fragment_shader (effects, "hconv7",
49       hconv7_fragment_source_gles2);
50   gst_gl_shader_use (shader);
51   gst_gl_shader_set_uniform_1fv (shader, "kernel", 9, gauss_kernel);
52   gst_gl_shader_set_uniform_1f (shader, "gauss_width",
53       GST_VIDEO_INFO_WIDTH (&filter->in_info));
54   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[0],
55       effects->midtexture[1], shader);
56 
57   /* vertical blur */
58   shader = gst_gl_effects_get_fragment_shader (effects, "vconv7",
59       vconv7_fragment_source_gles2);
60   gst_gl_shader_use (shader);
61   gst_gl_shader_set_uniform_1fv (shader, "kernel", 9, gauss_kernel);
62   gst_gl_shader_set_uniform_1f (shader, "gauss_height",
63       GST_VIDEO_INFO_HEIGHT (&filter->out_info));
64   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[1],
65       effects->midtexture[2], shader);
66 
67   /* detect edges with Sobel */
68   /* the old version used edges from the blurred texture, this uses
69    * the ones from original texture, still not sure what I like
70    * more. This one gives better edges obviously but behaves badly
71    * with noise */
72   /* desaturate */
73   shader = gst_gl_effects_get_fragment_shader (effects, "desaturate",
74       desaturate_fragment_source_gles2);
75   gst_gl_filter_render_to_target_with_shader (filter, effects->intexture,
76       effects->midtexture[3], shader);
77 
78   /* horizontal convolution */
79   shader = gst_gl_effects_get_fragment_shader (effects, "sobel_hconv3",
80       sep_sobel_hconv3_fragment_source_gles2);
81   gst_gl_shader_use (shader);
82   gst_gl_shader_set_uniform_1f (shader, "width",
83       GST_VIDEO_INFO_WIDTH (&filter->out_info));
84   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[3],
85       effects->midtexture[4], shader);
86 
87   /* vertical convolution */
88   shader = gst_gl_effects_get_fragment_shader (effects, "sobel_vconv3",
89       sep_sobel_vconv3_fragment_source_gles2);
90   gst_gl_shader_use (shader);
91   gst_gl_shader_set_uniform_1f (shader, "height",
92       GST_VIDEO_INFO_HEIGHT (&filter->out_info));
93   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[4],
94       effects->midtexture[3], shader);
95 
96   /* gradient length */
97   shader = gst_gl_effects_get_fragment_shader (effects, "sobel_length",
98       sep_sobel_length_fragment_source_gles2);
99   gst_gl_shader_use (shader);
100   gst_gl_shader_set_uniform_1i (shader, "invert", TRUE);
101   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[3],
102       effects->midtexture[4], shader);
103 
104   /* multiply edges with the blurred image */
105   shader = gst_gl_effects_get_fragment_shader (effects, "multiply",
106       multiply_fragment_source_gles2);
107   gst_gl_shader_use (shader);
108 
109   gl->ActiveTexture (GL_TEXTURE2);
110   gl->BindTexture (GL_TEXTURE_2D,
111       gst_gl_memory_get_texture_id (effects->midtexture[2]));
112 
113   gst_gl_shader_set_uniform_1i (shader, "base", 2);
114 
115   gl->ActiveTexture (GL_TEXTURE1);
116   gl->BindTexture (GL_TEXTURE_2D,
117       gst_gl_memory_get_texture_id (effects->midtexture[4]));
118 
119   gst_gl_shader_set_uniform_1f (shader, "alpha", (gfloat) 0.5f);
120   gst_gl_shader_set_uniform_1i (shader, "blend", 1);
121 
122   gst_gl_filter_render_to_target_with_shader (filter, effects->midtexture[4],
123       effects->outtexture, shader);
124 }
125