1717package com .pedro .encoder .input .gl .render .filters ;
1818
1919import android .content .Context ;
20+ import android .content .res .Resources ;
2021import android .opengl .GLES20 ;
2122import android .opengl .Matrix ;
2223import android .os .Build ;
2829
2930import java .nio .ByteBuffer ;
3031import 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}
0 commit comments