Skip to content

Commit 4347804

Browse files
committed
added GPUImageLevelsFilter
1 parent e83250b commit 4347804

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package jp.co.cyberagent.android.gpuimage;
2+
3+
import android.opengl.GLES20;
4+
import android.util.Log;
5+
6+
/**
7+
* Created by vashisthg 30/05/14.
8+
*/
9+
public class GPUImageLevelsFilter extends GPUImageFilter{
10+
11+
private static final String LOGTAG = GPUImageLevelsFilter.class.getSimpleName();
12+
13+
public static final String LEVELS_FRAGMET_SHADER =
14+
15+
" varying highp vec2 textureCoordinate;\n" +
16+
" \n" +
17+
" uniform sampler2D inputImageTexture;\n" +
18+
" uniform mediump vec3 levelMinimum;\n" +
19+
" uniform mediump vec3 levelMiddle;\n" +
20+
" uniform mediump vec3 levelMaximum;\n" +
21+
" uniform mediump vec3 minOutput;\n" +
22+
" uniform mediump vec3 maxOutput;\n" +
23+
" \n" +
24+
" void main()\n" +
25+
" {\n" +
26+
" mediump vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n" +
27+
" \n" +
28+
" gl_FragColor = vec4( mix(minOutput, maxOutput, pow(min(max(textureColor.rgb -levelMinimum, vec3(0.0)) / (levelMaximum - levelMinimum ), vec3(1.0)), 1.0 /levelMiddle)) , textureColor.a);\n" +
29+
" }\n";
30+
31+
private int mMinLocation;
32+
private float[] mMin;
33+
private int mMidLocation;
34+
private float[] mMid;
35+
private int mMaxLocation;
36+
private float[] mMax;
37+
private int mMinOutputLocation;
38+
private float[] mMinOutput;
39+
private int mMaxOutputLocation;
40+
private float[] mMaxOutput;
41+
42+
public GPUImageLevelsFilter() {
43+
this(new float[] {0.0f,0.0f,0.0f}, new float[] {1.0f, 1.0f, 1.0f }, new float[] {1.0f, 1.0f ,1.0f}, new float[] {0.0f, 0.0f, 0.0f}, new float[] {1.0f,1.0f,1.0f});
44+
}
45+
46+
private GPUImageLevelsFilter(final float[] min, final float[] mid, final float[] max, final float[] minOUt, final float[] maxOut) {
47+
super(NO_FILTER_VERTEX_SHADER, LEVELS_FRAGMET_SHADER);
48+
49+
mMin = min;
50+
mMid = mid;
51+
mMax = max;
52+
mMinOutput = minOUt;
53+
mMaxOutput = maxOut;
54+
setMin(0.0f, 1.0f, 1.0f, 0.0f, 1.0f);
55+
}
56+
57+
@Override
58+
public void onInit() {
59+
super.onInit();
60+
mMinLocation = GLES20.glGetUniformLocation(getProgram(), "levelMinimum");
61+
mMidLocation = GLES20.glGetUniformLocation(getProgram(), "levelMiddle");
62+
mMaxLocation = GLES20.glGetUniformLocation(getProgram(), "levelMaximum");
63+
mMinOutputLocation = GLES20.glGetUniformLocation(getProgram(), "minOutput");
64+
mMaxOutputLocation = GLES20.glGetUniformLocation(getProgram(), "maxOutput");
65+
}
66+
67+
@Override
68+
public void onInitialized() {
69+
super.onInitialized();
70+
updateUniforms();
71+
}
72+
73+
74+
public void updateUniforms () {
75+
setFloatVec3(mMinLocation, mMin);
76+
setFloatVec3(mMidLocation, mMid);
77+
setFloatVec3(mMaxLocation, mMax);
78+
setFloatVec3(mMinOutputLocation, mMinOutput);
79+
setFloatVec3(mMaxOutputLocation, mMaxOutput);
80+
}
81+
82+
public void setMin(float min, float mid , float max ,float minOut , float maxOut) {
83+
setRedMin(min, mid, max, minOut, maxOut);
84+
setGreenMin(min, mid, max, minOut, maxOut);
85+
setBlueMin(min, mid, max, minOut, maxOut);
86+
}
87+
88+
public void setMin(float min, float mid , float max ) {
89+
setMin(min, mid, max, 0.0f, 1.0f);
90+
}
91+
92+
public void setRedMin(float min, float mid , float max ,float minOut , float maxOut) {
93+
mMin[0] = min;
94+
mMid[0] = mid;
95+
mMax[0] = max;
96+
mMinOutput[0] = minOut;
97+
mMaxOutput[0] = maxOut;
98+
updateUniforms();
99+
}
100+
101+
public void setRedMin(float min, float mid , float max ){
102+
setRedMin(min, mid, max, 0, 1);
103+
}
104+
105+
public void setGreenMin(float min, float mid , float max ,float minOut , float maxOut) {
106+
mMin[1] = min;
107+
mMid[1] = mid;
108+
mMax[1] = max;
109+
mMinOutput[1] = minOut;
110+
mMaxOutput[1] = maxOut;
111+
updateUniforms();
112+
}
113+
114+
public void setGreenMin(float min, float mid , float max ){
115+
setGreenMin(min, mid, max, 0, 1);
116+
}
117+
118+
public void setBlueMin(float min, float mid , float max ,float minOut , float maxOut) {
119+
mMin[2] = min;
120+
mMid[2] = mid;
121+
mMax[2] = max;
122+
mMinOutput[2] = minOut;
123+
mMaxOutput[2] = maxOut;
124+
updateUniforms();
125+
}
126+
127+
public void setBlueMin(float min, float mid , float max ){
128+
setBlueMin(min, mid, max, 0, 1);
129+
}
130+
}

0 commit comments

Comments
 (0)