Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added GPUImageTiltShiftFilter #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package jp.co.cyberagent.android.gpuimage;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLES20;

/**
* A simulated tilt shift lens effect
*/
public class GPUImageTiltShiftFilter extends GPUImageTwoInputFilter {
public static final String SHADER = "" +
"varying vec2 textureCoordinate;\n" +
" varying vec2 textureCoordinate2;\n" +
" \n" +
" uniform sampler2D inputImageTexture;\n" +
" uniform sampler2D inputImageTexture2;\n" +
" \n" +
" uniform float topFocusLevel;\n" +
" uniform float bottomFocusLevel;\n" +
" uniform float mFocusFallOffRate;\n" +
" \n" +
" void main()\n" +
" {\n" +
" vec4 sharpImageColor = texture2D(inputImageTexture, textureCoordinate);\n" +
" vec4 blurredImageColor = texture2D(inputImageTexture2, textureCoordinate2);\n" +
" \n" +
" float blurIntensity = 1.0 - smoothstep(topFocusLevel - mFocusFallOffRate, topFocusLevel, textureCoordinate2.y);\n" +
" blurIntensity += smoothstep(bottomFocusLevel, bottomFocusLevel + mFocusFallOffRate, textureCoordinate2.y);\n" +
" \n" +
" gl_FragColor = mix(sharpImageColor, blurredImageColor, blurIntensity);\n" +
" }\n";

private int mTopFocusLevelLocation;
private int mBottomFocusLevelLocation;
private int mFocusFallOffRate;

private GPUImageGaussianBlurFilter mBlurFilter;

private float mTilt;

public GPUImageTiltShiftFilter(){
this(0.5f);
}

public GPUImageTiltShiftFilter(float tilt){
super(SHADER);

mTilt = tilt;

mBlurFilter = new GPUImageGaussianBlurFilter();
mBlurFilter.setBlurSize(2);
}

@Override
public void onInit() {
super.onInit();

mTopFocusLevelLocation = GLES20.glGetUniformLocation(getProgram(), "topFocusLevel");
mBottomFocusLevelLocation = GLES20.glGetUniformLocation(getProgram(), "bottomFocusLevel");
mFocusFallOffRate = GLES20.glGetUniformLocation(getProgram(), "mFocusFallOffRate");
}

@Override
public void onInitialized() {
super.onInitialized();
setTilt(mTilt);
setFocusFallOffRate(0.3f);
}

public void setTilt(final float tilt){
mTilt = tilt;

setFloat(mTopFocusLevelLocation, tilt);
setFloat(mBottomFocusLevelLocation, tilt);
}

public void setFocusFallOffRate(final float rate){
setFloat(mFocusFallOffRate, rate);
}

public void setBitmap(Context context, final Bitmap bitmap){
GPUImage gpuImage = new GPUImage(context);
gpuImage.setImage(bitmap);
gpuImage.setFilter(mBlurFilter);

super.setBitmap(gpuImage.getBitmapWithFilterApplied());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.opengl.Matrix;
import android.util.Log;

import jp.co.cyberagent.android.gpuimage.*;

Expand Down Expand Up @@ -113,6 +114,8 @@ public static void showDialog(final Context context,

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

filters.addFilter("TiltShift", FilterType.TILT_SHIFT);


AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose a filter");
Expand Down Expand Up @@ -303,6 +306,9 @@ private static GPUImageFilter createFilterForType(final Context context, final F
case TRANSFORM2D:
return new GPUImageTransformFilter();

case TILT_SHIFT:
return new GPUImageTiltShiftFilter();

default:
throw new IllegalStateException("No filter of that type!");
}
Expand Down Expand Up @@ -330,7 +336,7 @@ private enum FilterType {
BLEND_DISSOLVE, BLEND_EXCLUSION, BLEND_SOURCE_OVER, BLEND_HARD_LIGHT, BLEND_LIGHTEN, BLEND_ADD, BLEND_DIVIDE, BLEND_MULTIPLY, BLEND_OVERLAY, BLEND_SCREEN, BLEND_ALPHA,
BLEND_COLOR, BLEND_HUE, BLEND_SATURATION, BLEND_LUMINOSITY, BLEND_LINEAR_BURN, BLEND_SOFT_LIGHT, BLEND_SUBTRACT, BLEND_CHROMA_KEY, BLEND_NORMAL, LOOKUP_AMATORKA,
GAUSSIAN_BLUR, CROSSHATCH, BOX_BLUR, CGA_COLORSPACE, DILATION, KUWAHARA, RGB_DILATION, SKETCH, TOON, SMOOTH_TOON, BULGE_DISTORTION, GLASS_SPHERE, HAZE, LAPLACIAN, NON_MAXIMUM_SUPPRESSION,
SPHERE_REFRACTION, SWIRL, WEAK_PIXEL_INCLUSION, FALSE_COLOR, COLOR_BALANCE, LEVELS_FILTER_MIN, BILATERAL_BLUR, HALFTONE, TRANSFORM2D
SPHERE_REFRACTION, SWIRL, WEAK_PIXEL_INCLUSION, FALSE_COLOR, COLOR_BALANCE, LEVELS_FILTER_MIN, BILATERAL_BLUR, HALFTONE, TRANSFORM2D, TILT_SHIFT
}

private static class FilterList {
Expand Down Expand Up @@ -409,6 +415,8 @@ public FilterAdjuster(final GPUImageFilter filter) {
adjuster = new BilateralAdjuster().filter(filter);
} else if (filter instanceof GPUImageTransformFilter) {
adjuster = new RotateAdjuster().filter(filter);
} else if (filter instanceof GPUImageTiltShiftFilter){
adjuster = new TiltShiftAdjuster().filter(filter);
}
else {

Expand Down Expand Up @@ -458,17 +466,17 @@ public void adjust(final int percentage) {
}

private class PixelationAdjuster extends Adjuster<GPUImagePixelationFilter> {
@Override
public void adjust(final int percentage) {
getFilter().setPixel(range(percentage, 1.0f, 100.0f));
}
@Override
public void adjust(final int percentage) {
getFilter().setPixel(range(percentage, 1.0f, 100.0f));
}
}

private class HueAdjuster extends Adjuster<GPUImageHueFilter> {
@Override
public void adjust(final int percentage) {
getFilter().setHue(range(percentage, 0.0f, 360.0f));
}
@Override
public void adjust(final int percentage) {
getFilter().setHue(range(percentage, 0.0f, 360.0f));
}
}

private class ContrastAdjuster extends Adjuster<GPUImageContrastFilter> {
Expand Down Expand Up @@ -682,5 +690,13 @@ public void adjust(final int percentage) {
}
}

private class TiltShiftAdjuster extends Adjuster<GPUImageTiltShiftFilter>{
@Override
public void adjust(final int percentage) {
Log.d("hogehoge",percentage+"");
getFilter().setTilt(range(percentage,0.0f,1.0f));
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package jp.co.cyberagent.android.gpuimage.sample.activity;

import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageTiltShiftFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageView;
import jp.co.cyberagent.android.gpuimage.GPUImageView.OnPictureSavedListener;
import jp.co.cyberagent.android.gpuimage.sample.GPUImageFilterTools;
Expand All @@ -25,6 +27,7 @@
import jp.co.cyberagent.android.gpuimage.sample.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
Expand Down Expand Up @@ -111,7 +114,11 @@ private void saveImage() {
private void switchFilterTo(final GPUImageFilter filter) {
if (mFilter == null
|| (filter != null && !mFilter.getClass().equals(filter.getClass()))) {



mFilter = filter;
setUpTiltShiftFilterIfNeeded(mFilter);
mGPUImageView.setFilter(mFilter);
mFilterAdjuster = new FilterAdjuster(mFilter);

Expand All @@ -120,6 +127,17 @@ private void switchFilterTo(final GPUImageFilter filter) {
}
}

private void setUpTiltShiftFilterIfNeeded(final GPUImageFilter filter){
if (filter instanceof GPUImageTiltShiftFilter){
final GPUImage gpuImage = new GPUImage(this);
gpuImage.setImage(mGPUImageView.getGPUImage().getBitmapWithFilterApplied());


final Bitmap bitmap = gpuImage.getBitmapWithFilterApplied();
((GPUImageTiltShiftFilter) filter).setBitmap(this,bitmap);
}
}

@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) {
if (mFilterAdjuster != null) {
Expand Down