From 9d0cb83334040b1fabcaec4056a4d752cecd4cc3 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Wed, 19 Oct 2022 14:58:45 +0800 Subject: [PATCH] Feature: add filter of white balance --- .../factory/BeautyFilterFactory.java | 3 ++ .../factory/BeautyFilterType.java | 1 + .../advance/BeautyWhiteBalanceFilter.java | 36 +++++++++++++++++++ .../src/main/res/raw/whitebalance.glsl | 26 ++++++++++++++ .../ffmpeg/activity/CameraFilterActivity.kt | 3 +- .../com/frank/ffmpeg/util/FilterTypeUtil.kt | 3 +- app/src/main/res/values-en/strings.xml | 3 +- app/src/main/res/values/strings.xml | 3 +- 8 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyWhiteBalanceFilter.java create mode 100644 CameraFilter/src/main/res/raw/whitebalance.glsl diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java index be5ceb6..7fe4973 100644 --- a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java +++ b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterFactory.java @@ -5,6 +5,7 @@ import android.content.Context; import com.frank.camerafilter.filter.advance.BeautyHueFilter; import com.frank.camerafilter.filter.advance.BeautySketchFilter; import com.frank.camerafilter.filter.BaseFilter; +import com.frank.camerafilter.filter.advance.BeautyWhiteBalanceFilter; public class BeautyFilterFactory { @@ -17,6 +18,8 @@ public class BeautyFilterFactory { return new BeautySketchFilter(context); case HUE: return new BeautyHueFilter(context); + case WHITE_BALANCE: + return new BeautyWhiteBalanceFilter(context); default: return null; } diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java index 47f9040..6bf3785 100644 --- a/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java +++ b/CameraFilter/src/main/java/com/frank/camerafilter/factory/BeautyFilterType.java @@ -3,5 +3,6 @@ package com.frank.camerafilter.factory; public enum BeautyFilterType { NONE, HUE, + WHITE_BALANCE, SKETCH } diff --git a/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyWhiteBalanceFilter.java b/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyWhiteBalanceFilter.java new file mode 100644 index 0000000..0f4ac60 --- /dev/null +++ b/CameraFilter/src/main/java/com/frank/camerafilter/filter/advance/BeautyWhiteBalanceFilter.java @@ -0,0 +1,36 @@ +package com.frank.camerafilter.filter.advance; + +import android.content.Context; +import android.opengl.GLES30; + +import com.frank.camerafilter.R; +import com.frank.camerafilter.filter.BaseFilter; +import com.frank.camerafilter.util.OpenGLUtil; + +public class BeautyWhiteBalanceFilter extends BaseFilter { + + private int tint; + private int temperature; + + public BeautyWhiteBalanceFilter(Context context) { + super(NORMAL_VERTEX_SHADER, OpenGLUtil.readShaderFromSource(context, R.raw.whitebalance)); + } + + protected void onInit() { + super.onInit(); + tint = GLES30.glGetUniformLocation(getProgramId(), "tint"); + temperature = GLES30.glGetUniformLocation(getProgramId(), "temperature"); + } + + protected void onInitialized() { + super.onInitialized(); + setFloat(tint, 0.5f); + setFloat(temperature, 0.3f); + } + + @Override + public void onInputSizeChanged(int width, int height) { + super.onInputSizeChanged(width, height); + } + +} diff --git a/CameraFilter/src/main/res/raw/whitebalance.glsl b/CameraFilter/src/main/res/raw/whitebalance.glsl new file mode 100644 index 0000000..5a6557c --- /dev/null +++ b/CameraFilter/src/main/res/raw/whitebalance.glsl @@ -0,0 +1,26 @@ +uniform sampler2D inputImageTexture; +varying highp vec2 textureCoordinate; + +uniform lowp float temperature; +uniform lowp float tint; + +const lowp vec3 warmFilter = vec3(0.93, 0.54, 0.0); + +const mediump mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.596, -0.274, -0.322, 0.212, -0.523, 0.311); +const mediump mat3 YIQtoRGB = mat3(1.0, 0.956, 0.621, 1.0, -0.272, -0.647, 1.0, -1.105, 1.702); + +void main() +{ + lowp vec4 source = texture2D(inputImageTexture, textureCoordinate); + + mediump vec3 yiq = RGBtoYIQ * source.rgb; //adjusting tint + yiq.b = clamp(yiq.b + tint*0.5226*0.1, -0.5226, 0.5226); + lowp vec3 rgb = YIQtoRGB * yiq; + + lowp vec3 processed = vec3( + (rgb.r < 0.5 ? (2.0 * rgb.r * warmFilter.r) : (1.0 - 2.0 * (1.0 - rgb.r) * (1.0 - warmFilter.r))), //adjusting temperature + (rgb.g < 0.5 ? (2.0 * rgb.g * warmFilter.g) : (1.0 - 2.0 * (1.0 - rgb.g) * (1.0 - warmFilter.g))), + (rgb.b < 0.5 ? (2.0 * rgb.b * warmFilter.b) : (1.0 - 2.0 * (1.0 - rgb.b) * (1.0 - warmFilter.b)))); + + gl_FragColor = vec4(mix(rgb, processed, temperature), source.a); +} \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt index 8c75ed1..8110316 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/CameraFilterActivity.kt @@ -15,7 +15,8 @@ class CameraFilterActivity : BaseActivity() { private val filterType: Array = arrayOf( BeautyFilterType.NONE, - BeautyFilterType.CRAYON, + BeautyFilterType.HUE, + BeautyFilterType.WHITE_BALANCE, BeautyFilterType.SKETCH ) diff --git a/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt index eab6ae7..d5317f3 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt +++ b/app/src/main/java/com/frank/ffmpeg/util/FilterTypeUtil.kt @@ -13,7 +13,8 @@ object FilterTypeUtil { fun filterTypeToNameId(type: BeautyFilterType): Int { return when (type) { BeautyFilterType.NONE -> R.string.camera_filter_none - BeautyFilterType.CRAYON -> R.string.camera_filter_crayon + BeautyFilterType.HUE -> R.string.camera_filter_hue + BeautyFilterType.WHITE_BALANCE -> R.string.camera_filter_balance BeautyFilterType.SKETCH -> R.string.camera_filter_sketch else -> R.string.camera_filter_none } diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index 2793304..d31c30f 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -113,7 +113,8 @@ sharpen None - Crayon + Hue + White balance Sketch diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c2b47d..3b7ca55 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -113,7 +113,8 @@ 锐化 正常 - 蜡笔 + Hue + 白平衡 素描