Skip to content

Commit

Permalink
add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
MasayukiSuda committed Jul 2, 2019
1 parent 61e0911 commit 05fd8a3
Show file tree
Hide file tree
Showing 23 changed files with 1,284 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

public class GlCrosshatchFilter extends GlFilter {

private static final String CROSSHATCH_FRAGMENT_SHADER = "" +
"precision mediump float;" +
" varying vec2 vTextureCoord;\n" +
" uniform lowp sampler2D sTexture;\n" +
"uniform highp float crossHatchSpacing;\n" +
"uniform highp float lineWidth;\n" +
"const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);\n" +
"void main()\n" +
"{\n" +
"highp float luminance = dot(texture2D(sTexture, vTextureCoord).rgb, W);\n" +
"lowp vec4 colorToDisplay = vec4(1.0, 1.0, 1.0, 1.0);\n" +
"if (luminance < 1.00)\n" +
"{\n" +
"if (mod(vTextureCoord.x + vTextureCoord.y, crossHatchSpacing) <= lineWidth)\n" +
"{\n" +
"colorToDisplay = vec4(0.0, 0.0, 0.0, 1.0);\n" +
"}\n" +
"}\n" +
"if (luminance < 0.75)\n" +
"{\n" +
"if (mod(vTextureCoord.x - vTextureCoord.y, crossHatchSpacing) <= lineWidth)\n" +
"{\n" +
"colorToDisplay = vec4(0.0, 0.0, 0.0, 1.0);\n" +
"}\n" +
"}\n" +
"if (luminance < 0.50)\n" +
"{\n" +
"if (mod(vTextureCoord.x + vTextureCoord.y - (crossHatchSpacing / 2.0), crossHatchSpacing) <= lineWidth)\n" +
"{\n" +
"colorToDisplay = vec4(0.0, 0.0, 0.0, 1.0);\n" +
"}\n" +
"}\n" +
"if (luminance < 0.3)\n" +
"{\n" +
"if (mod(vTextureCoord.x - vTextureCoord.y - (crossHatchSpacing / 2.0), crossHatchSpacing) <= lineWidth)\n" +
"{\n" +
"colorToDisplay = vec4(0.0, 0.0, 0.0, 1.0);\n" +
"}\n" +
"}\n" +
"gl_FragColor = colorToDisplay;\n" +
"}\n";

public GlCrosshatchFilter() {
super(DEFAULT_VERTEX_SHADER, CROSSHATCH_FRAGMENT_SHADER);
}

private float crossHatchSpacing = 0.03f;
private float lineWidth = 0.003f;

@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("crossHatchSpacing"), crossHatchSpacing);
GLES20.glUniform1f(getHandle("lineWidth"), lineWidth);
}

public void setCrossHatchSpacing(float crossHatchSpacing) {
this.crossHatchSpacing = crossHatchSpacing;
}

public void setLineWidth(float lineWidth) {
this.lineWidth = lineWidth;
}

@Override
public void setFrameSize(int width, int height) {
super.setFrameSize(width, height);

float singlePixelSpacing;
if (width != 0) {
singlePixelSpacing = 1.0f / (float) width;
} else {
singlePixelSpacing = 1.0f / 2048.0f;
}
if (crossHatchSpacing < singlePixelSpacing) {
this.crossHatchSpacing = singlePixelSpacing;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

/**
* exposure: The adjusted exposure (-10.0 - 10.0, with 0.0 as the default)
*/
public class GlExposureFilter extends GlFilter {

private static final String EXPOSURE_FRAGMENT_SHADER = "" +
"precision mediump float;" +
" varying vec2 vTextureCoord;\n" +
" \n" +
" uniform lowp sampler2D sTexture;\n" +
" uniform highp float exposure;\n" +
" \n" +
" void main()\n" +
" {\n" +
" highp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
" \n" +
" gl_FragColor = vec4(textureColor.rgb * pow(2.0, exposure), textureColor.w);\n" +
" } ";

public GlExposureFilter() {
super(DEFAULT_VERTEX_SHADER, EXPOSURE_FRAGMENT_SHADER);
}

private float exposure = 1f;

public void setExposure(float exposure) {
this.exposure = exposure;
}

@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("exposure"), exposure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

public class GlGammaFilter extends GlFilter {
private static final String GAMMA_FRAGMENT_SHADER = "" +
"precision mediump float;" +
" varying vec2 vTextureCoord;\n" +
" \n" +
" uniform lowp sampler2D sTexture;\n" +
" uniform lowp float gamma;\n" +
" \n" +
" void main()\n" +
" {\n" +
" lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
" \n" +
" gl_FragColor = vec4(pow(textureColor.rgb, vec3(gamma)), textureColor.w);\n" +
" }";

public GlGammaFilter() {
super(DEFAULT_VERTEX_SHADER, GAMMA_FRAGMENT_SHADER);
}

private float gamma = 1.2f;

public void setGamma(float gamma) {
this.gamma = gamma;
}

@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("gamma"), gamma);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

public class GlHalftoneFilter extends GlFilter {

private static final String HALFTONE_FRAGMENT_SHADER = "" +
"precision mediump float;" +
" varying vec2 vTextureCoord;\n" +

" uniform lowp sampler2D sTexture;\n" +

"uniform highp float fractionalWidthOfPixel;\n" +
"uniform highp float aspectRatio;\n" +

"const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);\n" +

"void main()\n" +
"{\n" +
" highp vec2 sampleDivisor = vec2(fractionalWidthOfPixel, fractionalWidthOfPixel / aspectRatio);\n" +
" highp vec2 samplePos = vTextureCoord - mod(vTextureCoord, sampleDivisor) + 0.5 * sampleDivisor;\n" +
" highp vec2 textureCoordinateToUse = vec2(vTextureCoord.x, (vTextureCoord.y * aspectRatio + 0.5 - 0.5 * aspectRatio));\n" +
" highp vec2 adjustedSamplePos = vec2(samplePos.x, (samplePos.y * aspectRatio + 0.5 - 0.5 * aspectRatio));\n" +
" highp float distanceFromSamplePoint = distance(adjustedSamplePos, textureCoordinateToUse);\n" +
" lowp vec3 sampledColor = texture2D(sTexture, samplePos).rgb;\n" +
" highp float dotScaling = 1.0 - dot(sampledColor, W);\n" +
" lowp float checkForPresenceWithinDot = 1.0 - step(distanceFromSamplePoint, (fractionalWidthOfPixel * 0.5) * dotScaling);\n" +
" gl_FragColor = vec4(vec3(checkForPresenceWithinDot), 1.0);\n" +
"}";

public GlHalftoneFilter() {
super(DEFAULT_VERTEX_SHADER, HALFTONE_FRAGMENT_SHADER);
}

private float fractionalWidthOfPixel = 0.01f;
private float aspectRatio = 1f;

public void setFractionalWidthOfAPixel(float fractionalWidthOfAPixel) {
this.fractionalWidthOfPixel = fractionalWidthOfAPixel;
}

@Override
public void setFrameSize(int width, int height) {
super.setFrameSize(width, height);
aspectRatio = (float) height / (float) width;
}

@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("fractionalWidthOfPixel"), fractionalWidthOfPixel);
GLES20.glUniform1f(getHandle("aspectRatio"), aspectRatio);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

public class GlHighlightShadowFilter extends GlFilter {

private static final String HIGHLIGHT_SHADOW_FRAGMENT_SHADER = "" +
"precision mediump float;" +
" uniform lowp sampler2D sTexture;\n" +
" varying vec2 vTextureCoord;\n" +
" \n" +
" uniform lowp float shadows;\n" +
" uniform lowp float highlights;\n" +
" \n" +
" const mediump vec3 luminanceWeighting = vec3(0.3, 0.3, 0.3);\n" +
" \n" +
" void main()\n" +
" {\n" +
" lowp vec4 source = texture2D(sTexture, vTextureCoord);\n" +
" mediump float luminance = dot(source.rgb, luminanceWeighting);\n" +
" \n" +
" mediump float shadow = clamp((pow(luminance, 1.0/(shadows+1.0)) + (-0.76)*pow(luminance, 2.0/(shadows+1.0))) - luminance, 0.0, 1.0);\n" +
" mediump float highlight = clamp((1.0 - (pow(1.0-luminance, 1.0/(2.0-highlights)) + (-0.8)*pow(1.0-luminance, 2.0/(2.0-highlights)))) - luminance, -1.0, 0.0);\n" +
" lowp vec3 result = vec3(0.0, 0.0, 0.0) + ((luminance + shadow + highlight) - 0.0) * ((source.rgb - vec3(0.0, 0.0, 0.0))/(luminance - 0.0));\n" +
" \n" +
" gl_FragColor = vec4(result.rgb, source.a);\n" +
" }";

public GlHighlightShadowFilter() {
super(DEFAULT_VERTEX_SHADER, HIGHLIGHT_SHADOW_FRAGMENT_SHADER);
}

private float shadows = 1f;
private float highlights = 0f;

public void setShadows(float shadows) {
this.shadows = shadows;
}

public void setHighlights(float highlights) {
this.highlights = highlights;
}

@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("shadows"), shadows);
GLES20.glUniform1f(getHandle("highlights"), highlights);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.daasuu.mp4compose.filter;

import android.opengl.GLES20;

public class GlHueFilter extends GlFilter {

private static final String HUE_FRAGMENT_SHADER = "" +
"precision highp float;\n" +
" varying vec2 vTextureCoord;\n" +
"\n" +
" uniform lowp sampler2D sTexture;\n" +
"uniform mediump float hueAdjust;\n" +
"const highp vec4 kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0);\n" +
"const highp vec4 kRGBToI = vec4 (0.595716, -0.274453, -0.321263, 0.0);\n" +
"const highp vec4 kRGBToQ = vec4 (0.211456, -0.522591, 0.31135, 0.0);\n" +
"\n" +
"const highp vec4 kYIQToR = vec4 (1.0, 0.9563, 0.6210, 0.0);\n" +
"const highp vec4 kYIQToG = vec4 (1.0, -0.2721, -0.6474, 0.0);\n" +
"const highp vec4 kYIQToB = vec4 (1.0, -1.1070, 1.7046, 0.0);\n" +
"\n" +
"void main ()\n" +
"{\n" +
" // Sample the input pixel\n" +
" highp vec4 color = texture2D(sTexture, vTextureCoord);\n" +
"\n" +
" // Convert to YIQ\n" +
" highp float YPrime = dot (color, kRGBToYPrime);\n" +
" highp float I = dot (color, kRGBToI);\n" +
" highp float Q = dot (color, kRGBToQ);\n" +
"\n" +
" // Calculate the hue and chroma\n" +
" highp float hue = atan (Q, I);\n" +
" highp float chroma = sqrt (I * I + Q * Q);\n" +
"\n" +
" // Make the user's adjustments\n" +
" hue += (-hueAdjust); //why negative rotation?\n" +
"\n" +
" // Convert back to YIQ\n" +
" Q = chroma * sin (hue);\n" +
" I = chroma * cos (hue);\n" +
"\n" +
" // Convert back to RGB\n" +
" highp vec4 yIQ = vec4 (YPrime, I, Q, 0.0);\n" +
" color.r = dot (yIQ, kYIQToR);\n" +
" color.g = dot (yIQ, kYIQToG);\n" +
" color.b = dot (yIQ, kYIQToB);\n" +
"\n" +
" // Save the result\n" +
" gl_FragColor = color;\n" +
"}\n";

public GlHueFilter() {
super(DEFAULT_VERTEX_SHADER, HUE_FRAGMENT_SHADER);
}

private float hue = 90f;

public void setHue(float hue) {
this.hue = hue;
}


@Override
public void onDraw() {
GLES20.glUniform1f(getHandle("hueAdjust"), hue);
}
}
Loading

0 comments on commit 05fd8a3

Please sign in to comment.