Skip to content

Commit 8b5c297

Browse files
殷飞ChillingVan
authored andcommitted
增加高斯模糊滤波器; Add GaussianBlurFilter
# Conflicts: # canvasglsample/src/main/java/com/chillingvan/canvasglsample/filter/FilterActivity.java
1 parent adc0918 commit 8b5c297

File tree

2 files changed

+155
-6
lines changed

2 files changed

+155
-6
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.chillingvan.canvasgl.textureFilter;
2+
3+
import android.opengl.GLES20;
4+
5+
import com.chillingvan.canvasgl.ICanvasGL;
6+
import com.chillingvan.canvasgl.OpenGLUtil;
7+
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
8+
9+
import java.util.ArrayList;
10+
11+
public class GaussianBlurFilter extends FilterGroup {
12+
public static class GaussianFilter extends BasicTextureFilter {
13+
private float blurSize;
14+
private int dir;
15+
public static final String TEXEL_WIDTH_OFFSET = "texelWidthOffset";
16+
public static final String TEXEL_HEIGHT_OFFSET = "texelHeightOffset";
17+
public static final String BLUR_COORDINATES = "blurCoordinates";
18+
19+
20+
public GaussianFilter(int dir, float blurSize) {
21+
this.dir = dir;
22+
this.blurSize = blurSize;
23+
24+
25+
}
26+
27+
public GaussianFilter(int dir) {
28+
this(dir, 1f);
29+
}
30+
31+
public static final String VERTEX_SHADER =
32+
"precision mediump float;\n" +
33+
"uniform mat4 " + MATRIX_UNIFORM + ";\n" +
34+
"uniform mat4 " + TEXTURE_MATRIX_UNIFORM + ";\n" +
35+
"attribute vec2 " + POSITION_ATTRIBUTE + ";\n" +
36+
"const int GAUSSIAN_SAMPLES = 9;\n" +
37+
"\n" +
38+
"uniform float " + TEXEL_WIDTH_OFFSET + ";\n" +
39+
"uniform float " + TEXEL_HEIGHT_OFFSET + ";\n" +
40+
"\n" +
41+
"varying vec2 " + VARYING_TEXTURE_COORD + ";\n" +
42+
"varying vec2 " + BLUR_COORDINATES + "[GAUSSIAN_SAMPLES];\n" +
43+
"\n" +
44+
"void main()\n" +
45+
"{\n" +
46+
" vec4 pos = vec4(" + POSITION_ATTRIBUTE + ", 0.0, 1.0);\n" +
47+
" gl_Position = " + MATRIX_UNIFORM + "* pos;\n" +
48+
VARYING_TEXTURE_COORD + " = (" + TEXTURE_MATRIX_UNIFORM + " * pos).xy;\n" +
49+
" \n" +
50+
" int multiplier = 0;\n" +
51+
" vec2 blurStep;\n" +
52+
" vec2 singleStepOffset = vec2(" + TEXEL_HEIGHT_OFFSET + ", " + TEXEL_WIDTH_OFFSET + ");\n" +
53+
" \n" +
54+
" for (int i = 0; i < GAUSSIAN_SAMPLES; i++)\n" +
55+
" {\n" +
56+
" multiplier = (i - ((GAUSSIAN_SAMPLES - 1) / 2));\n" +
57+
" // Blur in x (horizontal)\n" +
58+
" blurStep = float(multiplier) * singleStepOffset;\n" +
59+
" " + BLUR_COORDINATES + "[i] = " + VARYING_TEXTURE_COORD + ".xy + blurStep;\n" +
60+
" }\n" +
61+
"}\n";
62+
63+
public static final String FRAGMENT_SHADER =
64+
"uniform sampler2D " + TEXTURE_SAMPLER_UNIFORM + ";\n" +
65+
"\n" +
66+
"const lowp int GAUSSIAN_SAMPLES = 9;\n" +
67+
"\n" +
68+
"varying highp vec2 " + VARYING_TEXTURE_COORD + ";\n" +
69+
"varying highp vec2 " + BLUR_COORDINATES + "[GAUSSIAN_SAMPLES];\n" +
70+
"\n" +
71+
"void main()\n" +
72+
"{\n" +
73+
" lowp vec3 sum = vec3(0.0);\n" +
74+
" lowp vec4 fragColor=texture2D(" + TEXTURE_SAMPLER_UNIFORM + "," + VARYING_TEXTURE_COORD + ");\n" +
75+
" \n" +
76+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[0]).rgb * 0.05;\n" +
77+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[1]).rgb * 0.09;\n" +
78+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[2]).rgb * 0.12;\n" +
79+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[3]).rgb * 0.15;\n" +
80+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[4]).rgb * 0.18;\n" +
81+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[5]).rgb * 0.15;\n" +
82+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[6]).rgb * 0.12;\n" +
83+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[7]).rgb * 0.09;\n" +
84+
" sum += texture2D(" + TEXTURE_SAMPLER_UNIFORM + ", " + BLUR_COORDINATES + "[8]).rgb * 0.05;\n" +
85+
"\n" +
86+
" gl_FragColor = vec4(sum,fragColor.a);\n" +
87+
"}";
88+
89+
90+
@Override
91+
public String getVertexShader() {
92+
return VERTEX_SHADER;
93+
}
94+
95+
@Override
96+
public String getFragmentShader() {
97+
return FRAGMENT_SHADER;
98+
}
99+
100+
101+
@Override
102+
public void onPreDraw(int program, BasicTexture texture, ICanvasGL canvas) {
103+
super.onPreDraw(program, texture, canvas);
104+
int widthOffsetLocation = GLES20.glGetUniformLocation(program, TEXEL_WIDTH_OFFSET);
105+
int heightOffsetLocation = GLES20.glGetUniformLocation(program, TEXEL_HEIGHT_OFFSET);
106+
if (dir == 0) {
107+
OpenGLUtil.setFloat(widthOffsetLocation, blurSize / texture.getWidth());
108+
OpenGLUtil.setFloat(heightOffsetLocation, 0);
109+
110+
} else {
111+
OpenGLUtil.setFloat(widthOffsetLocation, 0);
112+
OpenGLUtil.setFloat(heightOffsetLocation, blurSize / texture.getHeight());
113+
}
114+
115+
116+
}
117+
118+
public void setBlurSize(float blurSize) {
119+
this.blurSize = blurSize;
120+
}
121+
122+
123+
}
124+
125+
126+
public GaussianBlurFilter(float blurSize) {
127+
super(new ArrayList<TextureFilter>());
128+
mFilters.add(new GaussianFilter(0, blurSize));
129+
mFilters.add(new GaussianFilter(1, blurSize));
130+
updateMergedFilters();
131+
}
132+
133+
public void setBlurSize(float blurSize) {
134+
for (TextureFilter textureFilter : mFilters) {
135+
if (textureFilter instanceof GaussianFilter) {
136+
GaussianFilter gaussianFilter = (GaussianFilter) textureFilter;
137+
gaussianFilter.setBlurSize(blurSize);
138+
}
139+
140+
}
141+
}
142+
}

canvasglsample/src/main/java/com/chillingvan/canvasglsample/filter/FilterActivity.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.chillingvan.canvasgl.textureFilter.DirectionalSobelEdgeDetectionFilter;
3737
import com.chillingvan.canvasgl.textureFilter.FilterGroup;
3838
import com.chillingvan.canvasgl.textureFilter.GammaFilter;
39+
import com.chillingvan.canvasgl.textureFilter.GaussianBlurFilter;
3940
import com.chillingvan.canvasgl.textureFilter.HueFilter;
4041
import com.chillingvan.canvasgl.textureFilter.LightenBlendFilter;
4142
import com.chillingvan.canvasgl.textureFilter.LookupFilter;
@@ -62,6 +63,7 @@
6263
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
6364
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilterGroup;
6465
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGammaFilter;
66+
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGaussianBlurFilter;
6567
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHueFilter;
6668
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLightenBlendFilter;
6769
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLookupFilter;
@@ -103,7 +105,6 @@ protected void onCreate(Bundle savedInstanceState) {
103105
initSeekBar();
104106

105107

106-
107108
adapter = new CommonBaseAdapter<>(renderEntityList);
108109
listView.setAdapter(adapter);
109110
refreshDataList();
@@ -145,10 +146,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
145146
}
146147

147148
@Override
148-
public void onStartTrackingTouch(SeekBar seekBar) { }
149+
public void onStartTrackingTouch(SeekBar seekBar) {
150+
}
149151

150152
@Override
151-
public void onStopTrackingTouch(SeekBar seekBar) { }
153+
public void onStopTrackingTouch(SeekBar seekBar) {
154+
}
152155
});
153156
}
154157

@@ -162,7 +165,7 @@ private List<CaseEntity> createRenderEntities() {
162165

163166
int width = firstBitmap.getWidth();
164167
int height = firstBitmap.getHeight();
165-
CropFilter cropFilter = new CropFilter(CropFilter.calc(width/2, width), 0, 1, CropFilter.calc(height/2, height));
168+
CropFilter cropFilter = new CropFilter(CropFilter.calc(width / 2, width), 0, 1, CropFilter.calc(height / 2, height));
166169
renderEntityList.add(new CaseEntity(cropFilter, new GPUImageFilter(), firstBitmap));
167170

168171
Bitmap lookupAmatorka = BitmapFactory.decodeResource(getResources(), R.drawable.lookup_amatorka);
@@ -233,6 +236,10 @@ private List<CaseEntity> createRenderEntities() {
233236
GPUImageColorMatrixFilter gpuImageColorMatrixFilter = new GPUImageColorMatrixFilter(0.3f, matrix4);
234237
renderEntityList.add(new CaseEntity(colorMatrixFilter, gpuImageColorMatrixFilter, firstBitmap));
235238

239+
GaussianBlurFilter gaussianBlurFilter = new GaussianBlurFilter(5f);
240+
GPUImageGaussianBlurFilter gpuImageGaussianBlurFilter = new GPUImageGaussianBlurFilter(5f);
241+
renderEntityList.add(new CaseEntity(gaussianBlurFilter, gpuImageGaussianBlurFilter, firstBitmap));
242+
236243
return renderEntityList;
237244
}
238245

@@ -287,7 +294,7 @@ private void startPicActivity(int requestPickImage) {
287294
}
288295

289296
public static class Range {
290-
float min ;
297+
float min;
291298
float max;
292299

293300
public Range(float min, float max) {
@@ -296,7 +303,7 @@ public Range(float min, float max) {
296303
}
297304

298305
public float value(float percentage) {
299-
return min + (max - min) * (percentage /100);
306+
return min + (max - min) * (percentage / 100);
300307
}
301308
}
302309
}

0 commit comments

Comments
 (0)