Skip to content

Commit 15098ed

Browse files
dockusandockusan
authored andcommitted
Add filter blend color: Screen, Overlay, Multiply
- Blend photo with custom color without create new solid color bitmap. Ex: float[] mVec4RedColor = new float[]{1f,0f,0f,1f}; GPUImageOverlayColorBlendFilter filter = new GPUImageOverlayColorBlendFilter(mVec4RedColor);
1 parent ceea576 commit 15098ed

3 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2012 CyberAgent
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jp.co.cyberagent.android.gpuimage;
18+
19+
import android.opengl.GLES20;
20+
21+
public class GPUImageMultiplyColorBlendFilter extends GPUImageFilter {
22+
public static final String MULTIPLY_COLOR_BLEND_FRAGMENT_SHADER = "varying highp vec2 textureCoordinate;\n" +
23+
" uniform sampler2D inputImageTexture;\n" +
24+
" uniform vec4 mColor;\n" +
25+
" \n" +
26+
" void main()\n" +
27+
" {\n" +
28+
" lowp vec4 base = texture2D(inputImageTexture, textureCoordinate);\n" +
29+
" \n" +
30+
" gl_FragColor = mColor * base + mColor * (1.0 - base.a) + base * (1.0 - mColor.a);\n" +
31+
" }";
32+
private float[] mColor;
33+
private int mColorLocation;
34+
35+
public GPUImageMultiplyColorBlendFilter(final float[] mColor) {
36+
super(NO_FILTER_VERTEX_SHADER, MULTIPLY_COLOR_BLEND_FRAGMENT_SHADER);
37+
this.mColor = mColor;
38+
}
39+
40+
public GPUImageMultiplyColorBlendFilter() {
41+
this(new float[]{0.0f, 0.0f, 0.0f, 0.0f});
42+
}
43+
44+
@Override
45+
public void onInit() {
46+
super.onInit();
47+
mColorLocation = GLES20.glGetUniformLocation(getProgram(), "mColor");
48+
setBlendColor(mColor);
49+
}
50+
51+
public void setBlendColor(final float[] color) {
52+
mColor = color;
53+
setFloatVec4(mColorLocation, mColor);
54+
}
55+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2012 CyberAgent
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jp.co.cyberagent.android.gpuimage;
18+
19+
import android.opengl.GLES20;
20+
21+
public class GPUImageOverlayColorBlendFilter extends GPUImageFilter {
22+
public static final String OVERLAY_COLOR_BLEND_FRAGMENT_SHADER = "varying highp vec2 textureCoordinate;\n" +
23+
"\n" +
24+
" uniform sampler2D inputImageTexture;\n" +
25+
" uniform vec4 mVec4Color;\n" +
26+
" \n" +
27+
" void main()\n" +
28+
" {\n" +
29+
" mediump vec4 base = texture2D(inputImageTexture, textureCoordinate);\n" +
30+
" \n" +
31+
" mediump float ra;\n" +
32+
" if (2.0 * base.r < base.a) {\n" +
33+
" ra = 2.0 * mVec4Color.r * base.r + mVec4Color.r * (1.0 - base.a) + base.r * (1.0 - mVec4Color.a);\n" +
34+
" } else {\n" +
35+
" ra = mVec4Color.a * base.a - 2.0 * (base.a - base.r) * (mVec4Color.a - mVec4Color.r) + mVec4Color.r * (1.0 - base.a) + base.r * (1.0 - mVec4Color.a);\n" +
36+
" }\n" +
37+
" \n" +
38+
" mediump float ga;\n" +
39+
" if (2.0 * base.g < base.a) {\n" +
40+
" ga = 2.0 * mVec4Color.g * base.g + mVec4Color.g * (1.0 - base.a) + base.g * (1.0 - mVec4Color.a);\n" +
41+
" } else {\n" +
42+
" ga = mVec4Color.a * base.a - 2.0 * (base.a - base.g) * (mVec4Color.a - mVec4Color.g) + mVec4Color.g * (1.0 - base.a) + base.g * (1.0 - mVec4Color.a);\n" +
43+
" }\n" +
44+
" \n" +
45+
" mediump float ba;\n" +
46+
" if (2.0 * base.b < base.a) {\n" +
47+
" ba = 2.0 * mVec4Color.b * base.b + mVec4Color.b * (1.0 - base.a) + base.b * (1.0 - mVec4Color.a);\n" +
48+
" } else {\n" +
49+
" ba = mVec4Color.a * base.a - 2.0 * (base.a - base.b) * (mVec4Color.a - mVec4Color.b) + mVec4Color.b * (1.0 - base.a) + base.b * (1.0 - mVec4Color.a);\n" +
50+
" }\n" +
51+
" \n" +
52+
" gl_FragColor = vec4(ra, ga, ba, 1.0);\n" +
53+
" }";
54+
55+
private float[] mVec4Color;
56+
private int mColorLocation;
57+
58+
public GPUImageOverlayColorBlendFilter(final float[] mColor) {
59+
super(NO_FILTER_VERTEX_SHADER, OVERLAY_COLOR_BLEND_FRAGMENT_SHADER);
60+
this.mVec4Color = mColor;
61+
}
62+
63+
public GPUImageOverlayColorBlendFilter() {
64+
this(new float[]{0.0f, 0.0f, 0.0f, 0.0f});
65+
}
66+
67+
@Override
68+
public void onInit() {
69+
super.onInit();
70+
mColorLocation = GLES20.glGetUniformLocation(getProgram(), "mVec4Color");
71+
setBlendColor(mVec4Color);
72+
}
73+
74+
public void setBlendColor(final float[] color) {
75+
mVec4Color = color;
76+
setFloatVec4(mColorLocation, mVec4Color);
77+
}
78+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2012 CyberAgent
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jp.co.cyberagent.android.gpuimage;
18+
19+
import android.opengl.GLES20;
20+
21+
public class GPUImageScreenColorBlendFilter extends GPUImageFilter {
22+
public static final String SCREEN_COLOR_BLEND_FRAGMENT_SHADER = "varying highp vec2 textureCoordinate;\n" +
23+
" varying highp vec2 textureCoordinate2;\n" +
24+
"\n" +
25+
" uniform sampler2D inputImageTexture;\n" +
26+
" uniform vec4 mVec4Color;\n" +
27+
" \n" +
28+
" void main()\n" +
29+
" {\n" +
30+
" mediump vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n" +
31+
" mediump vec4 whiteColor = vec4(1.0);\n" +
32+
" gl_FragColor = whiteColor - ((whiteColor - mVec4Color) * (whiteColor - textureColor));\n" +
33+
" }";
34+
35+
36+
private float[] mVec4Color;
37+
private int mColorLocation;
38+
39+
public GPUImageScreenColorBlendFilter(final float[] mColor) {
40+
super(NO_FILTER_VERTEX_SHADER, SCREEN_COLOR_BLEND_FRAGMENT_SHADER);
41+
this.mVec4Color = mColor;
42+
}
43+
44+
public GPUImageScreenColorBlendFilter() {
45+
this(new float[]{0.0f, 0.0f, 0.0f, 0.0f});
46+
}
47+
48+
@Override
49+
public void onInit() {
50+
super.onInit();
51+
mColorLocation = GLES20.glGetUniformLocation(getProgram(), "mVec4Color");
52+
setBlendColor(mVec4Color);
53+
}
54+
55+
public void setBlendColor(final float[] color) {
56+
mVec4Color = color;
57+
setFloatVec4(mColorLocation, mVec4Color);
58+
}
59+
}

0 commit comments

Comments
 (0)