Skip to content

Commit 5b55ee6

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 cd663a9 commit 5b55ee6

4 files changed

Lines changed: 255 additions & 22 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+
}

sample/src/jp/co/cyberagent/android/gpuimage/sample/GPUImageFilterTools.java

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import android.graphics.PointF;
2424
import android.opengl.Matrix;
2525

26-
import jp.co.cyberagent.android.gpuimage.*;
27-
2826
import java.util.LinkedList;
2927
import java.util.List;
3028

29+
import jp.co.cyberagent.android.gpuimage.*;
30+
3131
public class GPUImageFilterTools {
3232
public static void showDialog(final Context context,
33-
final OnGpuImageFilterChosenListener listener) {
33+
final OnGpuImageFilterChosenListener listener) {
3434
final FilterList filters = new FilterList();
3535
filters.addFilter("Contrast", FilterType.CONTRAST);
3636
filters.addFilter("Invert", FilterType.INVERT);
@@ -68,8 +68,11 @@ public static void showDialog(final Context context,
6868
filters.addFilter("Blend (Add)", FilterType.BLEND_ADD);
6969
filters.addFilter("Blend (Divide)", FilterType.BLEND_DIVIDE);
7070
filters.addFilter("Blend (Multiply)", FilterType.BLEND_MULTIPLY);
71+
filters.addFilter("Blend Color (Multiply)", FilterType.BLEND_COLOR_MULTIPLY);
7172
filters.addFilter("Blend (Overlay)", FilterType.BLEND_OVERLAY);
73+
filters.addFilter("Blend Color (Overlay)", FilterType.BLEND_COLOR_OVERLAY);
7274
filters.addFilter("Blend (Screen)", FilterType.BLEND_SCREEN);
75+
filters.addFilter("Blend Color (Screen)", FilterType.BLEND_COLOR_SCREEN);
7376
filters.addFilter("Blend (Alpha)", FilterType.BLEND_ALPHA);
7477
filters.addFilter("Blend (Color)", FilterType.BLEND_COLOR);
7578
filters.addFilter("Blend (Hue)", FilterType.BLEND_HUE);
@@ -109,7 +112,7 @@ public static void showDialog(final Context context,
109112

110113
filters.addFilter("Levels Min (Mid Adjust)", FilterType.LEVELS_FILTER_MIN);
111114

112-
filters. addFilter("Bilateral Blur", FilterType.BILATERAL_BLUR);
115+
filters.addFilter("Bilateral Blur", FilterType.BILATERAL_BLUR);
113116

114117
filters.addFilter("Transform (2-D)", FilterType.TRANSFORM2D);
115118

@@ -153,7 +156,7 @@ private static GPUImageFilter createFilterForType(final Context context, final F
153156
return new GPUImageSobelEdgeDetection();
154157
case THREE_X_THREE_CONVOLUTION:
155158
GPUImage3x3ConvolutionFilter convolution = new GPUImage3x3ConvolutionFilter();
156-
convolution.setConvolutionKernel(new float[] {
159+
convolution.setConvolutionKernel(new float[]{
157160
-1.0f, 0.0f, 1.0f,
158161
-2.0f, 0.0f, 2.0f,
159162
-1.0f, 0.0f, 1.0f
@@ -174,9 +177,9 @@ private static GPUImageFilter createFilterForType(final Context context, final F
174177
case EXPOSURE:
175178
return new GPUImageExposureFilter(0.0f);
176179
case HIGHLIGHT_SHADOW:
177-
return new GPUImageHighlightShadowFilter(0.0f, 1.0f);
180+
return new GPUImageHighlightShadowFilter(0.0f, 1.0f);
178181
case MONOCHROME:
179-
return new GPUImageMonochromeFilter(1.0f, new float[]{0.6f, 0.45f, 0.3f, 1.0f});
182+
return new GPUImageMonochromeFilter(1.0f, new float[]{0.6f, 0.45f, 0.3f, 1.0f});
180183
case OPACITY:
181184
return new GPUImageOpacityFilter(1.0f);
182185
case RGB:
@@ -187,7 +190,7 @@ private static GPUImageFilter createFilterForType(final Context context, final F
187190
PointF centerPoint = new PointF();
188191
centerPoint.x = 0.5f;
189192
centerPoint.y = 0.5f;
190-
return new GPUImageVignetteFilter(centerPoint, new float[] {0.0f, 0.0f, 0.0f}, 0.3f, 0.75f);
193+
return new GPUImageVignetteFilter(centerPoint, new float[]{0.0f, 0.0f, 0.0f}, 0.3f, 0.75f);
191194
case TONE_CURVE:
192195
GPUImageToneCurveFilter toneCurveFilter = new GPUImageToneCurveFilter();
193196
toneCurveFilter.setFromCurveFileInputStream(
@@ -207,8 +210,6 @@ private static GPUImageFilter createFilterForType(final Context context, final F
207210
return createBlendFilter(context, GPUImageDissolveBlendFilter.class);
208211
case BLEND_EXCLUSION:
209212
return createBlendFilter(context, GPUImageExclusionBlendFilter.class);
210-
211-
212213
case BLEND_HARD_LIGHT:
213214
return createBlendFilter(context, GPUImageHardLightBlendFilter.class);
214215
case BLEND_LIGHTEN:
@@ -219,10 +220,16 @@ private static GPUImageFilter createFilterForType(final Context context, final F
219220
return createBlendFilter(context, GPUImageDivideBlendFilter.class);
220221
case BLEND_MULTIPLY:
221222
return createBlendFilter(context, GPUImageMultiplyBlendFilter.class);
223+
case BLEND_COLOR_MULTIPLY:
224+
return new GPUImageMultiplyColorBlendFilter(new float[]{1.0f, 1.0f, 1.0f, 1.0f});
222225
case BLEND_OVERLAY:
223226
return createBlendFilter(context, GPUImageOverlayBlendFilter.class);
227+
case BLEND_COLOR_OVERLAY:
228+
return new GPUImageOverlayColorBlendFilter(new float[]{1.0f, 1.0f, 1.0f, 1.0f});
224229
case BLEND_SCREEN:
225230
return createBlendFilter(context, GPUImageScreenBlendFilter.class);
231+
case BLEND_COLOR_SCREEN:
232+
return new GPUImageScreenColorBlendFilter(new float[]{1.0f, 1.0f, 1.0f, 1.0f});
226233
case BLEND_ALPHA:
227234
return createBlendFilter(context, GPUImageAlphaBlendFilter.class);
228235
case BLEND_COLOR:
@@ -327,7 +334,7 @@ public interface OnGpuImageFilterChosenListener {
327334
private enum FilterType {
328335
CONTRAST, GRAYSCALE, SHARPEN, SEPIA, SOBEL_EDGE_DETECTION, THREE_X_THREE_CONVOLUTION, FILTER_GROUP, EMBOSS, POSTERIZE, GAMMA, BRIGHTNESS, INVERT, HUE, PIXELATION,
329336
SATURATION, EXPOSURE, HIGHLIGHT_SHADOW, MONOCHROME, OPACITY, RGB, WHITE_BALANCE, VIGNETTE, TONE_CURVE, BLEND_COLOR_BURN, BLEND_COLOR_DODGE, BLEND_DARKEN, BLEND_DIFFERENCE,
330-
BLEND_DISSOLVE, BLEND_EXCLUSION, BLEND_SOURCE_OVER, BLEND_HARD_LIGHT, BLEND_LIGHTEN, BLEND_ADD, BLEND_DIVIDE, BLEND_MULTIPLY, BLEND_OVERLAY, BLEND_SCREEN, BLEND_ALPHA,
337+
BLEND_DISSOLVE, BLEND_EXCLUSION, BLEND_SOURCE_OVER, BLEND_HARD_LIGHT, BLEND_LIGHTEN, BLEND_ADD, BLEND_DIVIDE, BLEND_MULTIPLY, BLEND_COLOR_MULTIPLY, BLEND_OVERLAY, BLEND_COLOR_OVERLAY, BLEND_SCREEN, BLEND_COLOR_SCREEN, BLEND_ALPHA,
331338
BLEND_COLOR, BLEND_HUE, BLEND_SATURATION, BLEND_LUMINOSITY, BLEND_LINEAR_BURN, BLEND_SOFT_LIGHT, BLEND_SUBTRACT, BLEND_CHROMA_KEY, BLEND_NORMAL, LOOKUP_AMATORKA,
332339
GAUSSIAN_BLUR, CROSSHATCH, BOX_BLUR, CGA_COLORSPACE, DILATION, KUWAHARA, RGB_DILATION, SKETCH, TOON, SMOOTH_TOON, BULGE_DISTORTION, GLASS_SPHERE, HAZE, LAPLACIAN, NON_MAXIMUM_SUPPRESSION,
333340
SPHERE_REFRACTION, SWIRL, WEAK_PIXEL_INCLUSION, FALSE_COLOR, COLOR_BALANCE, LEVELS_FILTER_MIN, BILATERAL_BLUR, HALFTONE, TRANSFORM2D
@@ -409,9 +416,13 @@ public FilterAdjuster(final GPUImageFilter filter) {
409416
adjuster = new BilateralAdjuster().filter(filter);
410417
} else if (filter instanceof GPUImageTransformFilter) {
411418
adjuster = new RotateAdjuster().filter(filter);
412-
}
413-
else {
414-
419+
} else if (filter instanceof GPUImageMultiplyColorBlendFilter) {
420+
adjuster = new MultiplyColorAdjuster().filter(filter);
421+
} else if (filter instanceof GPUImageScreenColorBlendFilter) {
422+
adjuster = new ScreenColorAdjuster().filter(filter);
423+
} else if (filter instanceof GPUImageOverlayColorBlendFilter) {
424+
adjuster = new OverlayColorAdjuster().filter(filter);
425+
} else {
415426
adjuster = null;
416427
}
417428
}
@@ -458,17 +469,17 @@ public void adjust(final int percentage) {
458469
}
459470

460471
private class PixelationAdjuster extends Adjuster<GPUImagePixelationFilter> {
461-
@Override
462-
public void adjust(final int percentage) {
463-
getFilter().setPixel(range(percentage, 1.0f, 100.0f));
464-
}
472+
@Override
473+
public void adjust(final int percentage) {
474+
getFilter().setPixel(range(percentage, 1.0f, 100.0f));
475+
}
465476
}
466477

467478
private class HueAdjuster extends Adjuster<GPUImageHueFilter> {
468-
@Override
469-
public void adjust(final int percentage) {
470-
getFilter().setHue(range(percentage, 0.0f, 360.0f));
471-
}
479+
@Override
480+
public void adjust(final int percentage) {
481+
getFilter().setHue(range(percentage, 0.0f, 360.0f));
482+
}
472483
}
473484

474485
private class ContrastAdjuster extends Adjuster<GPUImageContrastFilter> {
@@ -659,6 +670,36 @@ public void adjust(int percentage) {
659670
}
660671
}
661672

673+
private class MultiplyColorAdjuster extends Adjuster<GPUImageMultiplyColorBlendFilter> {
674+
675+
@Override
676+
public void adjust(int percentage) {
677+
getFilter().setBlendColor(new float[]{
678+
range(percentage, 0.0f, 1.0f),
679+
1.0f, 1.0f, 1.0f});
680+
}
681+
}
682+
683+
private class ScreenColorAdjuster extends Adjuster<GPUImageScreenColorBlendFilter> {
684+
685+
@Override
686+
public void adjust(int percentage) {
687+
getFilter().setBlendColor(new float[]{
688+
range(percentage, 0.0f, 1.0f),
689+
1.0f, 1.0f, 1.0f});
690+
}
691+
}
692+
693+
private class OverlayColorAdjuster extends Adjuster<GPUImageOverlayColorBlendFilter> {
694+
695+
@Override
696+
public void adjust(int percentage) {
697+
getFilter().setBlendColor(new float[]{
698+
range(percentage, 0.0f, 1.0f),
699+
1.0f, 1.0f, 1.0f});
700+
}
701+
}
702+
662703
private class LevelsMinMidAdjuster extends Adjuster<GPUImageLevelsFilter> {
663704
@Override
664705
public void adjust(int percentage) {

0 commit comments

Comments
 (0)