Skip to content

Commit 839b95f

Browse files
leonrdjonan
authored andcommitted
Port GPUImageTransformFilter.
1 parent 77b1f10 commit 839b95f

File tree

2 files changed

+167
-15
lines changed

2 files changed

+167
-15
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package jp.co.cyberagent.android.gpuimage;
2+
3+
import android.opengl.GLES20;
4+
import android.opengl.Matrix;
5+
6+
import java.nio.ByteBuffer;
7+
import java.nio.ByteOrder;
8+
import java.nio.FloatBuffer;
9+
10+
public class GPUImageTransformFilter extends GPUImageFilter {
11+
public static final String TRANSFORM_VERTEX_SHADER = "" +
12+
"attribute vec4 position;\n" +
13+
" attribute vec4 inputTextureCoordinate;\n" +
14+
" \n" +
15+
" uniform mat4 transformMatrix;\n" +
16+
" uniform mat4 orthographicMatrix;\n" +
17+
" \n" +
18+
" varying vec2 textureCoordinate;\n" +
19+
" \n" +
20+
" void main()\n" +
21+
" {\n" +
22+
" gl_Position = transformMatrix * vec4(position.xyz, 1.0) * orthographicMatrix;\n" +
23+
" textureCoordinate = inputTextureCoordinate.xy;\n" +
24+
" }";
25+
26+
private int transformMatrixUniform;
27+
private int orthographicMatrixUniform;
28+
private float[] orthographicMatrix;
29+
30+
private float[] transform3D;
31+
32+
// This applies the transform to the raw frame data if set to YES, the default of NO takes the aspect ratio of the image input into account when rotating
33+
private boolean ignoreAspectRatio;
34+
35+
// sets the anchor point to top left corner
36+
private boolean anchorTopLeft;
37+
38+
public GPUImageTransformFilter() {
39+
super(TRANSFORM_VERTEX_SHADER, NO_FILTER_FRAGMENT_SHADER);
40+
41+
orthographicMatrix = new float[16];
42+
Matrix.orthoM(orthographicMatrix, 0, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
43+
44+
transform3D = new float[16];
45+
Matrix.setIdentityM(transform3D, 0);
46+
}
47+
48+
@Override
49+
public void onInit() {
50+
super.onInit();
51+
transformMatrixUniform = GLES20.glGetUniformLocation(getProgram(), "transformMatrix");
52+
orthographicMatrixUniform = GLES20.glGetUniformLocation(getProgram(), "orthographicMatrix");
53+
54+
setUniformMatrix4f(transformMatrixUniform, transform3D);
55+
setUniformMatrix4f(orthographicMatrixUniform, orthographicMatrix);
56+
}
57+
58+
@Override
59+
public void onInitialized() {
60+
super.onInitialized();
61+
}
62+
63+
@Override
64+
public void onOutputSizeChanged(final int width, final int height) {
65+
super.onOutputSizeChanged(width, height);
66+
67+
if (!ignoreAspectRatio) {
68+
Matrix.orthoM(orthographicMatrix, 0, -1.0f, 1.0f, -1.0f * (float) height / (float) width, 1.0f * (float) height / (float) width, -1.0f, 1.0f);
69+
setUniformMatrix4f(orthographicMatrixUniform, orthographicMatrix);
70+
}
71+
}
72+
73+
@Override
74+
public void onDraw(final int textureId, final FloatBuffer cubeBuffer,
75+
final FloatBuffer textureBuffer) {
76+
77+
FloatBuffer vertBuffer = cubeBuffer;
78+
79+
if (!ignoreAspectRatio) {
80+
81+
float[] adjustedVertices = new float[8];
82+
83+
cubeBuffer.position(0);
84+
cubeBuffer.get(adjustedVertices);
85+
86+
float normalizedHeight = (float) getOutputHeight() / (float) getOutputWidth();
87+
adjustedVertices[1] *= normalizedHeight;
88+
adjustedVertices[3] *= normalizedHeight;
89+
adjustedVertices[5] *= normalizedHeight;
90+
adjustedVertices[7] *= normalizedHeight;
91+
92+
vertBuffer = ByteBuffer.allocateDirect(adjustedVertices.length * 4)
93+
.order(ByteOrder.nativeOrder())
94+
.asFloatBuffer();
95+
96+
vertBuffer.put(adjustedVertices).position(0);
97+
}
98+
99+
super.onDraw(textureId, vertBuffer, textureBuffer);
100+
}
101+
102+
public void setTransform3D(float[] transform3D) {
103+
this.transform3D = transform3D;
104+
setUniformMatrix4f(transformMatrixUniform, transform3D);
105+
}
106+
107+
public float[] getTransform3D() {
108+
return transform3D;
109+
}
110+
111+
public void setIgnoreAspectRatio(boolean ignoreAspectRatio) {
112+
this.ignoreAspectRatio = ignoreAspectRatio;
113+
114+
if (ignoreAspectRatio) {
115+
Matrix.orthoM(orthographicMatrix, 0, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
116+
setUniformMatrix4f(orthographicMatrixUniform, orthographicMatrix);
117+
} else {
118+
onOutputSizeChanged(getOutputWidth(), getOutputHeight());
119+
}
120+
}
121+
122+
public boolean ignoreAspectRatio() {
123+
return ignoreAspectRatio;
124+
}
125+
126+
public void setAnchorTopLeft(boolean anchorTopLeft) {
127+
this.anchorTopLeft = anchorTopLeft;
128+
setIgnoreAspectRatio(ignoreAspectRatio);
129+
}
130+
131+
public boolean anchorTopLeft() {
132+
return anchorTopLeft;
133+
}
134+
}

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import android.content.DialogInterface;
2222
import android.graphics.BitmapFactory;
2323
import android.graphics.PointF;
24+
import android.opengl.Matrix;
25+
2426
import jp.co.cyberagent.android.gpuimage.*;
2527

2628
import java.util.LinkedList;
2729
import java.util.List;
2830

29-
public class GPUImageFilterTools {
31+
public class GPUImageFilterTools {
3032
public static void showDialog(final Context context,
3133
final OnGpuImageFilterChosenListener listener) {
3234
final FilterList filters = new FilterList();
@@ -109,6 +111,8 @@ public static void showDialog(final Context context,
109111

110112
filters. addFilter("Bilateral Blur", FilterType.BILATERAL_BLUR);
111113

114+
filters.addFilter("Transform (2-D)", FilterType.TRANSFORM2D);
115+
112116

113117
AlertDialog.Builder builder = new AlertDialog.Builder(context);
114118
builder.setTitle("Choose a filter");
@@ -174,11 +178,11 @@ private static GPUImageFilter createFilterForType(final Context context, final F
174178
case MONOCHROME:
175179
return new GPUImageMonochromeFilter(1.0f, new float[]{0.6f, 0.45f, 0.3f, 1.0f});
176180
case OPACITY:
177-
return new GPUImageOpacityFilter(1.0f);
181+
return new GPUImageOpacityFilter(1.0f);
178182
case RGB:
179-
return new GPUImageRGBFilter(1.0f, 1.0f, 1.0f);
183+
return new GPUImageRGBFilter(1.0f, 1.0f, 1.0f);
180184
case WHITE_BALANCE:
181-
return new GPUImageWhiteBalanceFilter(5000.0f, 0.0f);
185+
return new GPUImageWhiteBalanceFilter(5000.0f, 0.0f);
182186
case VIGNETTE:
183187
PointF centerPoint = new PointF();
184188
centerPoint.x = 0.5f;
@@ -296,6 +300,9 @@ private static GPUImageFilter createFilterForType(final Context context, final F
296300
case BILATERAL_BLUR:
297301
return new GPUImageBilateralFilter();
298302

303+
case TRANSFORM2D:
304+
return new GPUImageTransformFilter();
305+
299306
default:
300307
throw new IllegalStateException("No filter of that type!");
301308
}
@@ -323,7 +330,7 @@ private enum FilterType {
323330
BLEND_DISSOLVE, BLEND_EXCLUSION, BLEND_SOURCE_OVER, BLEND_HARD_LIGHT, BLEND_LIGHTEN, BLEND_ADD, BLEND_DIVIDE, BLEND_MULTIPLY, BLEND_OVERLAY, BLEND_SCREEN, BLEND_ALPHA,
324331
BLEND_COLOR, BLEND_HUE, BLEND_SATURATION, BLEND_LUMINOSITY, BLEND_LINEAR_BURN, BLEND_SOFT_LIGHT, BLEND_SUBTRACT, BLEND_CHROMA_KEY, BLEND_NORMAL, LOOKUP_AMATORKA,
325332
GAUSSIAN_BLUR, CROSSHATCH, BOX_BLUR, CGA_COLORSPACE, DILATION, KUWAHARA, RGB_DILATION, SKETCH, TOON, SMOOTH_TOON, BULGE_DISTORTION, GLASS_SPHERE, HAZE, LAPLACIAN, NON_MAXIMUM_SUPPRESSION,
326-
SPHERE_REFRACTION, SWIRL, WEAK_PIXEL_INCLUSION, FALSE_COLOR, COLOR_BALANCE, LEVELS_FILTER_MIN, BILATERAL_BLUR, HALFTONE
333+
SPHERE_REFRACTION, SWIRL, WEAK_PIXEL_INCLUSION, FALSE_COLOR, COLOR_BALANCE, LEVELS_FILTER_MIN, BILATERAL_BLUR, HALFTONE, TRANSFORM2D
327334
}
328335

329336
private static class FilterList {
@@ -400,6 +407,8 @@ public FilterAdjuster(final GPUImageFilter filter) {
400407
adjuster = new LevelsMinMidAdjuster().filter(filter);
401408
} else if (filter instanceof GPUImageBilateralFilter) {
402409
adjuster = new BilateralAdjuster().filter(filter);
410+
} else if (filter instanceof GPUImageTransformFilter) {
411+
adjuster = new RotateAdjuster().filter(filter);
403412
}
404413
else {
405414

@@ -525,46 +534,46 @@ public void adjust(final int percentage) {
525534
getFilter().setSaturation(range(percentage, 0.0f, 2.0f));
526535
}
527536
}
528-
537+
529538
private class ExposureAdjuster extends Adjuster<GPUImageExposureFilter> {
530539
@Override
531540
public void adjust(final int percentage) {
532541
getFilter().setExposure(range(percentage, -10.0f, 10.0f));
533542
}
534-
}
535-
543+
}
544+
536545
private class HighlightShadowAdjuster extends Adjuster<GPUImageHighlightShadowFilter> {
537546
@Override
538547
public void adjust(final int percentage) {
539548
getFilter().setShadows(range(percentage, 0.0f, 1.0f));
540549
getFilter().setHighlights(range(percentage, 0.0f, 1.0f));
541550
}
542551
}
543-
552+
544553
private class MonochromeAdjuster extends Adjuster<GPUImageMonochromeFilter> {
545554
@Override
546555
public void adjust(final int percentage) {
547556
getFilter().setIntensity(range(percentage, 0.0f, 1.0f));
548557
//getFilter().setColor(new float[]{0.6f, 0.45f, 0.3f, 1.0f});
549558
}
550559
}
551-
560+
552561
private class OpacityAdjuster extends Adjuster<GPUImageOpacityFilter> {
553562
@Override
554563
public void adjust(final int percentage) {
555564
getFilter().setOpacity(range(percentage, 0.0f, 1.0f));
556565
}
557-
}
558-
566+
}
567+
559568
private class RGBAdjuster extends Adjuster<GPUImageRGBFilter> {
560569
@Override
561570
public void adjust(final int percentage) {
562571
getFilter().setRed(range(percentage, 0.0f, 1.0f));
563572
//getFilter().setGreen(range(percentage, 0.0f, 1.0f));
564573
//getFilter().setBlue(range(percentage, 0.0f, 1.0f));
565574
}
566-
}
567-
575+
}
576+
568577
private class WhiteBalanceAdjuster extends Adjuster<GPUImageWhiteBalanceFilter> {
569578
@Override
570579
public void adjust(final int percentage) {
@@ -653,7 +662,7 @@ public void adjust(int percentage) {
653662
private class LevelsMinMidAdjuster extends Adjuster<GPUImageLevelsFilter> {
654663
@Override
655664
public void adjust(int percentage) {
656-
getFilter().setMin(0.0f, range(percentage, 0.0f, 1.0f) , 1.0f);
665+
getFilter().setMin(0.0f, range(percentage, 0.0f, 1.0f), 1.0f);
657666
}
658667
}
659668

@@ -664,5 +673,14 @@ public void adjust(final int percentage) {
664673
}
665674
}
666675

676+
private class RotateAdjuster extends Adjuster<GPUImageTransformFilter> {
677+
@Override
678+
public void adjust(final int percentage) {
679+
float[] transform = new float[16];
680+
Matrix.setRotateM(transform, 0, 360 * percentage / 100, 0, 0, 1.0f);
681+
getFilter().setTransform3D(transform);
682+
}
683+
}
684+
667685
}
668686
}

0 commit comments

Comments
 (0)