Skip to content

Commit f294ea6

Browse files
committed
add edge detection customizable using performance mode false in the filter constructor
1 parent 8e26ad6 commit f294ea6

File tree

3 files changed

+165
-2
lines changed

3 files changed

+165
-2
lines changed

encoder/src/main/java/com/pedro/encoder/input/gl/render/filters/EdgeDetectionFilterRender.java

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.pedro.encoder.input.gl.render.filters;
1818

1919
import android.content.Context;
20+
import android.content.res.Resources;
2021
import android.opengl.GLES20;
2122
import android.opengl.Matrix;
2223
import android.os.Build;
@@ -28,6 +29,8 @@
2829

2930
import java.nio.ByteBuffer;
3031
import java.nio.ByteOrder;
32+
import java.util.regex.Matcher;
33+
import java.util.regex.Pattern;
3134

3235
/**
3336
* Created by pedro on 4/02/18.
@@ -51,6 +54,25 @@ public class EdgeDetectionFilterRender extends BaseFilterRender {
5154
private int uMVPMatrixHandle = -1;
5255
private int uSTMatrixHandle = -1;
5356
private int uSamplerHandle = -1;
57+
private int uPixelSizeHandle = -1;
58+
private int uEdgeColorHandle = -1;
59+
private int uBackgroundColorHandle = -1;
60+
61+
private static final String HEX_PATTERN = "^#([A-Fa-f0-9]{6})$";
62+
63+
private float edgeSize = 0.001f;
64+
private float edgeRed = 1f;
65+
private float edgeGreen = 1f;
66+
private float edgeBlue = 1f;
67+
private float backgroundRed = 0f;
68+
private float backgroundGreen = 0f;
69+
private float backgroundBlue = 0f;
70+
private final boolean performanceMode;
71+
72+
public EdgeDetectionFilterRender(boolean performanceMode) {
73+
super();
74+
this.performanceMode = performanceMode;
75+
}
5476

5577
public EdgeDetectionFilterRender() {
5678
squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
@@ -59,19 +81,25 @@ public EdgeDetectionFilterRender() {
5981
squareVertex.put(squareVertexDataFilter).position(0);
6082
Matrix.setIdentityM(MVPMatrix, 0);
6183
Matrix.setIdentityM(STMatrix, 0);
84+
performanceMode = true;
6285
}
6386

6487
@Override
6588
protected void initGlFilter(Context context) {
66-
String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
67-
String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.edge_detection_fragment);
89+
String vertexShader = GlUtil.getStringFromRaw(context, performanceMode ? R.raw.simple_vertex : R.raw.edge_detection_vertex);
90+
String fragmentShader = GlUtil.getStringFromRaw(context, performanceMode ? R.raw.edge_detection_fragment : R.raw.edge_detection_sobel_fragment);
6891

6992
program = GlUtil.createProgram(vertexShader, fragmentShader);
7093
aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
7194
aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
7295
uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
7396
uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
7497
uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
98+
if (!performanceMode) {
99+
uPixelSizeHandle = GLES20.glGetUniformLocation(program, "uPixelSize");
100+
uEdgeColorHandle = GLES20.glGetUniformLocation(program, "uEdgeColor");
101+
uBackgroundColorHandle = GLES20.glGetUniformLocation(program, "uBackgroundColor");
102+
}
75103
}
76104

77105
@Override
@@ -91,6 +119,11 @@ protected void drawFilter() {
91119
GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0);
92120
GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0);
93121

122+
if (!performanceMode) {
123+
GLES20.glUniform3f(uEdgeColorHandle, edgeRed, edgeGreen, edgeBlue);
124+
GLES20.glUniform3f(uBackgroundColorHandle, backgroundRed, backgroundGreen, backgroundBlue);
125+
GLES20.glUniform1f(uPixelSizeHandle, edgeSize);
126+
}
94127
GLES20.glUniform1i(uSamplerHandle, 0);
95128
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
96129
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);
@@ -105,4 +138,81 @@ protected void disableResources() {
105138
public void release() {
106139
GLES20.glDeleteProgram(program);
107140
}
141+
142+
/**
143+
* @param rgbHexColor color represented with 7 characters (1 to start with #, 2 for red, 2 for
144+
* green and 2 for blue)
145+
*/
146+
public void setEdgeRGBColor(String rgbHexColor) {
147+
Pattern pattern = Pattern.compile(HEX_PATTERN);
148+
Matcher matcher = pattern.matcher(rgbHexColor);
149+
if (!matcher.matches()) {
150+
throw new IllegalArgumentException(
151+
"Invalid hexColor pattern (Should be: " + HEX_PATTERN + ")");
152+
}
153+
int r = Integer.valueOf(rgbHexColor.substring(1, 3), 16);
154+
int g = Integer.valueOf(rgbHexColor.substring(3, 5), 16);
155+
int b = Integer.valueOf(rgbHexColor.substring(5, 7), 16);
156+
setEdgeRGBColor(r, g, b);
157+
}
158+
159+
/**
160+
* Values range 0 to 255
161+
*/
162+
public void setEdgeRGBColor(int r, int g, int b) {
163+
if (performanceMode) throw new IllegalStateException("Performance mode no support set values");
164+
edgeRed = (float) r / 255.0f;
165+
edgeGreen = (float) g / 255.0f;
166+
edgeBlue = (float) b / 255.0f;
167+
}
168+
169+
/**
170+
* Get string color from color file resource and strip alpha values (alpha values is always auto
171+
* completed)
172+
*/
173+
public void setEdgeColor(Resources resources, int colorResource) {
174+
String color = resources.getString(colorResource);
175+
setEdgeRGBColor("#" + color.substring(3));
176+
}
177+
178+
/**
179+
* @param rgbHexColor color represented with 7 characters (1 to start with #, 2 for red, 2 for
180+
* green and 2 for blue)
181+
*/
182+
public void setBackgroundRGBColor(String rgbHexColor) {
183+
Pattern pattern = Pattern.compile(HEX_PATTERN);
184+
Matcher matcher = pattern.matcher(rgbHexColor);
185+
if (!matcher.matches()) {
186+
throw new IllegalArgumentException(
187+
"Invalid hexColor pattern (Should be: " + HEX_PATTERN + ")");
188+
}
189+
int r = Integer.valueOf(rgbHexColor.substring(1, 3), 16);
190+
int g = Integer.valueOf(rgbHexColor.substring(3, 5), 16);
191+
int b = Integer.valueOf(rgbHexColor.substring(5, 7), 16);
192+
setBackgroundRGBColor(r, g, b);
193+
}
194+
195+
/**
196+
* Values range 0 to 255
197+
*/
198+
public void setBackgroundRGBColor(int r, int g, int b) {
199+
if (performanceMode) throw new IllegalStateException("Performance mode no support set values");
200+
backgroundRed = (float) r / 255.0f;
201+
backgroundGreen = (float) g / 255.0f;
202+
backgroundBlue = (float) b / 255.0f;
203+
}
204+
205+
/**
206+
* Get string color from color file resource and strip alpha values (alpha values is always auto
207+
* completed)
208+
*/
209+
public void setBackgroundColor(Resources resources, int colorResource) {
210+
String color = resources.getString(colorResource);
211+
setBackgroundRGBColor("#" + color.substring(3));
212+
}
213+
214+
public void setEdgeSize(float edgeSize) {
215+
if (performanceMode) throw new IllegalStateException("Performance mode no support set values");
216+
this.edgeSize = edgeSize;
217+
}
108218
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
precision mediump float;
2+
3+
uniform sampler2D uSampler;
4+
5+
varying vec2 vTextureCoord;
6+
varying vec2 vLeftCoord;
7+
varying vec2 vRightCoord;
8+
varying vec2 vTopCoord;
9+
varying vec2 vBottomCoord;
10+
11+
uniform vec3 uEdgeColor;
12+
uniform vec3 uBackgroundColor;
13+
14+
void main() {
15+
float h0 = length(texture2D(uSampler, vLeftCoord).rgb);
16+
float h1 = length(texture2D(uSampler, vRightCoord).rgb);
17+
float v0 = length(texture2D(uSampler, vTopCoord).rgb);
18+
float v1 = length(texture2D(uSampler, vBottomCoord).rgb);
19+
20+
float edge = length(vec2(h1 - h0, v1 - v0));
21+
22+
vec4 bg;
23+
if (uBackgroundColor.r < 0.0 || uBackgroundColor.g < 0.0 || uBackgroundColor.b < 0.0) {
24+
bg = texture2D(uSampler, vTextureCoord);
25+
} else {
26+
bg = vec4(uBackgroundColor, 1.0);
27+
}
28+
gl_FragColor = mix(bg, vec4(uEdgeColor, 1.0), step(0.06, edge));
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
attribute vec4 aPosition;
2+
attribute vec4 aTextureCoord;
3+
4+
uniform mat4 uMVPMatrix;
5+
uniform mat4 uSTMatrix;
6+
7+
varying vec2 vTextureCoord;
8+
9+
varying vec2 vLeftCoord;
10+
varying vec2 vRightCoord;
11+
varying vec2 vTopCoord;
12+
varying vec2 vBottomCoord;
13+
14+
uniform float uPixelSize;
15+
16+
void main() {
17+
gl_Position = uMVPMatrix * aPosition;
18+
vTextureCoord = (uSTMatrix * aTextureCoord).xy;
19+
20+
vLeftCoord = vTextureCoord + vec2(-uPixelSize, 0.0);
21+
vRightCoord = vTextureCoord + vec2(uPixelSize, 0.0);
22+
vTopCoord = vTextureCoord + vec2(0.0, -uPixelSize);
23+
vBottomCoord = vTextureCoord + vec2(0.0, uPixelSize);
24+
}

0 commit comments

Comments
 (0)