Create Filter interface and BaseFilter abstract class

pull/535/head
Mattia Iavarone 6 years ago
parent ba1846bbcb
commit 1439461f15
  1. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/AutoFixFilter.java
  2. 120
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/BaseFilter.java
  3. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/BlackAndWhiteFilter.java
  4. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/BrightnessFilter.java
  5. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/ContrastFilter.java
  6. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/CrossProcessFilter.java
  7. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/CustomFilter.java
  8. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/DocumentaryFilter.java
  9. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/DuotoneFilter.java
  10. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/FillLightFilter.java
  11. 120
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/Filter.java
  12. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/GammaFilter.java
  13. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/GrainFilter.java
  14. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/GrayscaleFilter.java
  15. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/HueFilter.java
  16. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/InvertColorsFilter.java
  17. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/LamoishFilter.java
  18. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/NoFilter.java
  19. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/PosterizeFilter.java
  20. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/SaturationFilter.java
  21. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/SepiaFilter.java
  22. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/SharpnessFilter.java
  23. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/TemperatureFilter.java
  24. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/TintFilter.java
  25. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/filters/VignetteFilter.java

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Attempts to auto-fix the preview based on histogram equalization. * Attempts to auto-fix the preview based on histogram equalization.
*/ */
public class AutoFixFilter extends Filter { public class AutoFixFilter extends BaseFilter {
private float scale = 1.0f; private float scale = 1.0f;

@ -0,0 +1,120 @@
package com.otaliastudios.cameraview.filters;
import androidx.annotation.NonNull;
/**
* A Base abstract class that every effect must extend so that there is a common getShader method.
* <p>
* This class has a default Vertex Shader implementation which in most cases not required to touch.
* In Effects like sepia, B/W any many, only pixel color changes which can be managed by only fragment shader.
* If there is some other effect which requires vertex shader also change, you can override it.
* <p>
* If your provide your own vertex and fragment shader,
* please set the {@link #mPositionVariableName}, {@link #mTextureCoordinateVariableName},
* {@link #mMVPMatrixVariableName}, {@link #mTextureMatrixVariableName}
* according to your shader code.
* <p>
* Please note that these shader applies on live preview as well as pictureSnapshot and videoSnapshot,
* we only support GLES11Ext.GL_TEXTURE_EXTERNAL_OES
* check EglViewport()
* <p>
* The default implementation of this class is NoEffect.
*/
public abstract class BaseFilter implements Filter {
/**
* Vertex shader code written in Shader Language (C) and stored as String.
* This wil be used by GL to apply any effect.
*/
@NonNull
String mVertexShader =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
"}\n";
/**
* Fragment shader code written in Shader Language (C) and stored as String.
* This wil be used by GL to apply any effect.
*/
@NonNull
String mFragmentShader =
"#extension GL_OES_EGL_image_external : require\n"
+ "precision mediump float;\n"
+ "varying vec2 vTextureCoord;\n"
+ "uniform samplerExternalOES sTexture;\n"
+ "void main() {\n"
+ " gl_FragColor = texture2D(sTexture, vTextureCoord);\n"
+ "}\n";
/**
* Width and height of previewing GlSurfaceview.
* This will be used by a few effects.
*/
int mPreviewingViewWidth = 0;
int mPreviewingViewHeight = 0;
public void setOutputSize(int width, int height) {
mPreviewingViewWidth = width;
mPreviewingViewHeight = height;
}
/**
* Local variable name which were used in the shader code.
* These will be used by openGL program to render these vertex and fragment shader
*/
private String mPositionVariableName = "aPosition";
private String mTextureCoordinateVariableName = "aTextureCoord";
private String mMVPMatrixVariableName = "uMVPMatrix";
private String mTextureMatrixVariableName = "uTexMatrix";
public String getPositionVariableName() {
return mPositionVariableName;
}
public void setPositionVariableName(String positionVariableName) {
this.mPositionVariableName = positionVariableName;
}
public String getTexttureCoordinateVariableName() {
return mTextureCoordinateVariableName;
}
public void setTexttureCoordinateVariableName(String texttureCoordinateVariableName) {
this.mTextureCoordinateVariableName = texttureCoordinateVariableName;
}
public String getMVPMatrixVariableName() {
return mMVPMatrixVariableName;
}
public void setMVPMatrixVariableName(String mvpMatrixVariableName) {
this.mMVPMatrixVariableName = mvpMatrixVariableName;
}
public String getTextureMatrixVariableName() {
return mTextureMatrixVariableName;
}
public void setTextureMatrixVariableName(String textureMatrixVariableName) {
this.mTextureMatrixVariableName = textureMatrixVariableName;
}
/**
* Get vertex Shader code
*
* @return complete shader code in C
*/
@Override
@NonNull
public String getVertexShader() {
return mVertexShader;
}
}

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Converts the preview into black and white colors * Converts the preview into black and white colors
*/ */
public class BlackAndWhiteFilter extends Filter { public class BlackAndWhiteFilter extends BaseFilter {
/** /**

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Adjusts the brightness of the preview. * Adjusts the brightness of the preview.
*/ */
public class BrightnessFilter extends Filter { public class BrightnessFilter extends BaseFilter {
private float brightnessValue = 2.0f; private float brightnessValue = 2.0f;
/** /**

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Adjusts the contrast of the preview. * Adjusts the contrast of the preview.
*/ */
public class ContrastFilter extends Filter { public class ContrastFilter extends BaseFilter {
private float contrast = 2.0f; private float contrast = 2.0f;
/** /**

@ -6,7 +6,7 @@ import androidx.annotation.NonNull;
* Applies a cross process effect on preview, in which the red and green channels * Applies a cross process effect on preview, in which the red and green channels
* are enhanced while the blue channel is restricted. * are enhanced while the blue channel is restricted.
*/ */
public class CrossProcessFilter extends Filter { public class CrossProcessFilter extends BaseFilter {
/** /**
* Initialize Effect * Initialize Effect

@ -6,7 +6,7 @@ import androidx.annotation.NonNull;
/** /**
* This class is to implement any custom effect. * This class is to implement any custom effect.
*/ */
public class CustomFilter extends Filter { public class CustomFilter extends BaseFilter {
/** /**
* Parameterized constructor with vertex and fragment shader as parameter * Parameterized constructor with vertex and fragment shader as parameter

@ -8,7 +8,7 @@ import java.util.Random;
/** /**
* Applies black and white documentary style effect on preview. * Applies black and white documentary style effect on preview.
*/ */
public class DocumentaryFilter extends Filter { public class DocumentaryFilter extends BaseFilter {
private Random mRandom; private Random mRandom;
public DocumentaryFilter() { public DocumentaryFilter() {

@ -7,7 +7,7 @@ import androidx.annotation.NonNull;
/** /**
* Representation of preview using only two color tones. * Representation of preview using only two color tones.
*/ */
public class DuotoneFilter extends Filter { public class DuotoneFilter extends BaseFilter {
// Default values // Default values
private int mFirstColor = Color.MAGENTA; private int mFirstColor = Color.MAGENTA;
private int mSecondColor = Color.YELLOW; private int mSecondColor = Color.YELLOW;

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Applies back-light filling to the preview. * Applies back-light filling to the preview.
*/ */
public class FillLightFilter extends Filter { public class FillLightFilter extends BaseFilter {
private float strength = 0.5f; private float strength = 0.5f;
/** /**

@ -2,127 +2,15 @@ package com.otaliastudios.cameraview.filters;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
/**
* A Base abstract class that every effect must extend so that there is a common getShader method.
* <p>
* This class has a default Vertex Shader implementation which in most cases not required to touch.
* In Effects like sepia, B/W any many, only pixel color changes which can be managed by only fragment shader.
* If there is some other effect which requires vertex shader also change, you can override it.
* <p>
* If your provide your own vertex and fragment shader,
* please set the {@link #mPositionVariableName}, {@link #mTextureCoordinateVariableName},
* {@link #mMVPMatrixVariableName}, {@link #mTextureMatrixVariableName}
* according to your shader code.
* <p>
* Please note that these shader applies on live preview as well as pictureSnapshot and videoSnapshot,
* we only support GLES11Ext.GL_TEXTURE_EXTERNAL_OES
* check EglViewport()
* <p>
* The default implementation of this class is NoEffect.
*/
public abstract class Filter {
/** public interface Filter {
* Vertex shader code written in Shader Language (C) and stored as String.
* This wil be used by GL to apply any effect.
*/
@NonNull
String mVertexShader =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
"}\n";
void setOutputSize(int width, int height);
/**
* Fragment shader code written in Shader Language (C) and stored as String.
* This wil be used by GL to apply any effect.
*/
@NonNull @NonNull
String mFragmentShader = String getVertexShader();
"#extension GL_OES_EGL_image_external : require\n"
+ "precision mediump float;\n"
+ "varying vec2 vTextureCoord;\n"
+ "uniform samplerExternalOES sTexture;\n"
+ "void main() {\n"
+ " gl_FragColor = texture2D(sTexture, vTextureCoord);\n"
+ "}\n";
/**
* Width and height of previewing GlSurfaceview.
* This will be used by a few effects.
*/
int mPreviewingViewWidth = 0;
int mPreviewingViewHeight = 0;
public void setOutputSize(int width, int height) {
mPreviewingViewWidth = width;
mPreviewingViewHeight = height;
}
/**
* Local variable name which were used in the shader code.
* These will be used by openGL program to render these vertex and fragment shader
*/
private String mPositionVariableName = "aPosition";
private String mTextureCoordinateVariableName = "aTextureCoord";
private String mMVPMatrixVariableName = "uMVPMatrix";
private String mTextureMatrixVariableName = "uTexMatrix";
public String getPositionVariableName() {
return mPositionVariableName;
}
public void setPositionVariableName(String positionVariableName) {
this.mPositionVariableName = positionVariableName;
}
public String getTexttureCoordinateVariableName() {
return mTextureCoordinateVariableName;
}
public void setTexttureCoordinateVariableName(String texttureCoordinateVariableName) {
this.mTextureCoordinateVariableName = texttureCoordinateVariableName;
}
public String getMVPMatrixVariableName() {
return mMVPMatrixVariableName;
}
public void setMVPMatrixVariableName(String mvpMatrixVariableName) {
this.mMVPMatrixVariableName = mvpMatrixVariableName;
}
public String getTextureMatrixVariableName() {
return mTextureMatrixVariableName;
}
public void setTextureMatrixVariableName(String textureMatrixVariableName) {
this.mTextureMatrixVariableName = textureMatrixVariableName;
}
/**
* Get vertex Shader code
*
* @return complete shader code in C
*/
@NonNull
public String getVertexShader() {
return mVertexShader;
}
/**
* Get fragment Shader code
*
* @return complete shader code in C
*/
@NonNull @NonNull
public abstract String getFragmentShader(); String getFragmentShader();
} }

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Apply Gamma Effect on preview being played * Apply Gamma Effect on preview being played
*/ */
public class GammaFilter extends Filter { public class GammaFilter extends BaseFilter {
private float gammaValue = 2.0f; private float gammaValue = 2.0f;
/** /**

@ -8,7 +8,7 @@ import java.util.Random;
/** /**
* Applies film grain effect to preview. * Applies film grain effect to preview.
*/ */
public class GrainFilter extends Filter { public class GrainFilter extends BaseFilter {
private float strength = 0.5f; private float strength = 0.5f;
private Random mRandom; private Random mRandom;

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Converts preview to GreyScale. * Converts preview to GreyScale.
*/ */
public class GrayscaleFilter extends Filter { public class GrayscaleFilter extends BaseFilter {
/** /**
* Initialize Effect * Initialize Effect
*/ */

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Apply Hue effect on the preview * Apply Hue effect on the preview
*/ */
public class HueFilter extends Filter { public class HueFilter extends BaseFilter {
float hueValue = 0.0f; float hueValue = 0.0f;
/** /**

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Inverts the preview colors. This can also be known as negative Effect. * Inverts the preview colors. This can also be known as negative Effect.
*/ */
public class InvertColorsFilter extends Filter { public class InvertColorsFilter extends BaseFilter {
/** /**
* Initialize Effect * Initialize Effect
*/ */

@ -8,7 +8,7 @@ import java.util.Random;
/** /**
* Applies lomo-camera style effect to preview. * Applies lomo-camera style effect to preview.
*/ */
public class LamoishFilter extends Filter { public class LamoishFilter extends BaseFilter {
private Random mRandom; private Random mRandom;
/** /**

@ -2,7 +2,7 @@ package com.otaliastudios.cameraview.filters;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
public class NoFilter extends Filter { public class NoFilter extends BaseFilter {
@NonNull @NonNull
@Override @Override

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Applies Posterization effect to Preview. * Applies Posterization effect to Preview.
*/ */
public class PosterizeFilter extends Filter { public class PosterizeFilter extends BaseFilter {
/** /**
* Initialize Effect * Initialize Effect
*/ */

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Adjusts color saturation of preview. * Adjusts color saturation of preview.
*/ */
public class SaturationFilter extends Filter { public class SaturationFilter extends BaseFilter {
private float scale = 1.0f; private float scale = 1.0f;
/** /**

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Converts preview to Sepia tone. * Converts preview to Sepia tone.
*/ */
public class SepiaFilter extends Filter { public class SepiaFilter extends BaseFilter {
/** /**
* Initialize Effect * Initialize Effect
*/ */

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Sharpens the preview. * Sharpens the preview.
*/ */
public class SharpnessFilter extends Filter { public class SharpnessFilter extends BaseFilter {
private float scale = 0.5f; private float scale = 0.5f;
/** /**

@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
/** /**
* Adjusts color temperature of the preview. * Adjusts color temperature of the preview.
*/ */
public class TemperatureFilter extends Filter { public class TemperatureFilter extends BaseFilter {
private float scale = 0f; private float scale = 0f;
/** /**

@ -8,7 +8,7 @@ import androidx.annotation.NonNull;
/** /**
* Tints the preview with specified color.. * Tints the preview with specified color..
*/ */
public class TintFilter extends Filter { public class TintFilter extends BaseFilter {
private int mTint = 0xFFFF0000; private int mTint = 0xFFFF0000;
/** /**

@ -6,7 +6,7 @@ import androidx.annotation.NonNull;
/** /**
* Applies lomo-camera style effect to your preview. * Applies lomo-camera style effect to your preview.
*/ */
public class VignetteFilter extends Filter { public class VignetteFilter extends BaseFilter {
private float mScale = 0.85f; private float mScale = 0.85f;
private float mShade = 0.5f; private float mShade = 0.5f;

Loading…
Cancel
Save