8000 Fix: Rotation of texture in GPUImageTwoInputFilter is wrong (closes #14) · metacortex/android-gpuimage@f33c62d · GitHub
[go: up one dir, main page]

Skip to content

Commit f33c62d

Browse files
author
Patrick Boos
committed
Fix: Rotation of texture in GPUImageTwoInputFilter is wrong (closes cats-oss#14)
1 parent 8cf8bd9 commit f33c62d

File tree

6 files changed

+135
-163
lines changed

6 files changed

+135
-163
lines changed

library/src/jp/co/cyberagent/android/gpuimage/GPUImage.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import android.provider.MediaStore;
3838
import android.view.Display;
3939
import android.view.WindowManager;
40-
import jp.co.cyberagent.android.gpuimage.GPUImageRenderer.Rotation;
4140

4241
import java.io.File;
4342
import java.io.FileNotFoundException;
@@ -425,8 +424,7 @@ private Bitmap loadResizedImage(final File imageFile) {
425424
while (options.outWidth / scale > mMaxWidth || options.outHeight / scale > mMaxHeight) {
426425
scale++;
427426
}
428-
Bitmap bitmap = null;
429-
Bitmap scaledBitmap = null;
427+
Bitmap bitmap;
430428
if (scale > 1) {
431429
scale--;
432430
options = new BitmapFactory.Options();
@@ -452,7 +450,7 @@ private Bitmap loadResizedImage(final File imageFile) {
452450
newHeight = (newWidth / width) * height;
453451
}
454452

455-
scaledBitmap = Bitmap.createScaledBitmap(bitmap, Math.round((float) newWidth),
453+
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, Math.round((float) newWidth),
456454
Math.round((float) newHeight), true);
457455
bitmap.recycle();
458456
bitmap = scaledBitmap;
@@ -470,9 +468,8 @@ private Bitmap rotateImage(final Bitmap bitmap, final File fileWithExifInfo) {
470468
return null;
471469
}
472470
Bitmap rotatedBitmap = bitmap;
473-
int orientation = 0;
474471
try {
475-
orientation = getImageOrientation(fileWithExifInfo.getAbsolutePath());
472+
int orientation = getImageOrientation(fileWithExifInfo.getAbsolutePath());
476473
if (orientation != 0) {
477474
Matrix matrix = new Matrix();
478475
matrix.postRotate(orientation);

library/src/jp/co/cyberagent/android/gpuimage/GPUImageFilterGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.List;
2525

2626
import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.CUBE;
27-
import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.TEXTURE_NO_ROTATION;
27+
import static jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils.TEXTURE_NO_ROTATION;
2828

2929
/**
3030
* Resembles a filter that consists of multiple filters applied after each

library/src/jp/co/cyberagent/android/gpuimage/GPUImageRenderer.java

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.hardware.Camera.Size;
2626
import android.opengl.GLES20;
2727
import android.opengl.GLSurfaceView.Renderer;
28+
import jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils;
2829

2930
import javax.microedition.khronos.egl.EGLConfig;
3031
import javax.microedition.khronos.opengles.GL10;
@@ -36,6 +37,8 @@
3637
import java.util.LinkedList;
3738
import java.util.Queue;
3839

40+
import static jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils.TEXTURE_NO_ROTATION;
41+
3942
@TargetApi(11)
4043
public class GPUImageRenderer implements Renderer, PreviewCallback {
4144
public static final int NO_IMAGE = -1;
@@ -46,32 +49,6 @@ public class GPUImageRenderer implements Renderer, PreviewCallback {
4649
1.0f, -1.0f,
4750
};
4851

49-
static final float TEXTURE_NO_ROTATION[] = {
50-
0.0f, 1.0f,
51-
1.0f, 1.0f,
52-
0.0f, 0.0f,
53-
1.0f, 0.0f,
54-
};
55-
56-
private static final float TEXTURE_ROTATED_90[] = {
57-
1.0f, 1.0f,
58-
1.0f, 0.0f,
59-
0.0f, 1.0f,
60-
0.0f, 0.0f,
61-
};
62-
private static final float TEXTURE_ROTATED_180[] = {
63-
1.0f, 0.0f,
64-
0.0f, 0.0f,
65-
1.0f, 1.0f,
66-
0.0f, 1.0f,
67-
};
68-
private static final float TEXTURE_ROTATED_270[] = {
69-
0.0f, 0.0f,
70-
0.0f, 1.0f,
71-
1.0f, 0.0f,
72-
1.0f, 1.0f,
73-
};
74-
7552
private GPUImageFilter mFilter;
7653

7754
private int mGLTextureId = NO_IMAGE;
@@ -201,7 +178,7 @@ public void deleteImage() {
201178

202179
@Override
203180
public void run() {
204-
GLES20.glDeleteTextures(1, new int[] {
181+
GLES20.glDeleteTextures(1, new int[]{
205182
mGLTextureId
206183
}, 0);
207184
mGLTextureId = NO_IMAGE;
@@ -284,48 +261,13 @@ private void adjustImageScaling() {
284261
mGLCubeBuffer.put(cube).position(0);
285262
}
286263

287-
public enum Rotation {
288-
NORMAL, ROTATION_90, ROTATION_180, ROTATION_270
289-
}
290-
291264
public void setRotation(final Rotation rotation, final boolean flipHorizontal,
292265
final boolean flipVertical) {
293266
mRotation = rotation;
294267
mFlipHorizontal = flipHorizontal;
295268
mFlipVertical = flipVertical;
296269

297-
float[] rotatedTex = null;
298-
switch (rotation) {
299-
case ROTATION_90:
300-
rotatedTex = TEXTURE_ROTATED_90;
301-
break;
302-
case ROTATION_180:
303-
rotatedTex = TEXTURE_ROTATED_180;
304-
break;
305-
case ROTATION_270:
306-
rotatedTex = TEXTURE_ROTATED_270;
307-
break;
308-
case NORMAL:
309-
default:
310-
rotatedTex = TEXTURE_NO_ROTATION;
311-
break;
312-
}
313-
if (flipHorizontal) {
314-
rotatedTex = new float[] {
315-
rotatedTex[0], flip(rotatedTex[1]),
316-
rotatedTex[2], flip(rotatedTex[3]),
317-
rotatedTex[4], flip(rotatedTex[5]),
318-
rotatedTex[6], flip(rotatedTex[7]),
319-
};
320-
}
321-
if (flipVertical) {
322-
rotatedTex = new float[] {
323-
flip(rotatedTex[0]), rotatedTex[1],
324-
flip(rotatedTex[2]), rotatedTex[3],
325-
flip(rotatedTex[4]), rotatedTex[5],
326-
flip(rotatedTex[6]), rotatedTex[7],
327-
};
328-
}
270+
float[] rotatedTex = TextureRotationUtils.getRotationCamera(rotation, flipHorizontal, flipVertical);
329271

330272
mGLTextureBuffer.clear();
331273
mGLTextureBuffer.put(rotatedTex).position(0);
@@ -343,13 +285,6 @@ public boolean isFlippedVertically() {
343285
return mFlipVertical;
344286
}
345287

346-
private float flip(final float i) {
347-
if (i == 0.0f) {
348-
return 1.0f;
349-
}
350-
return 0.0f;
351-
}
352-
353288
protected void runOnDraw(final Runnable runnable) {
354289
synchronized (mRunOnDraw) {
355290
mRunOnDraw.add(runnable);

library/src/jp/co/cyberagent/android/gpuimage/GPUImageTwoInputFilter.java

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.graphics.Bitmap;
2020
import android.opengl.GLES20;
21+
import jp.co.cyberagent.android.gpuimage.utils.TextureRotationUtils;
2122

2223
import java.nio.ByteBuffer;
2324
import java.nio.ByteOrder;
@@ -50,7 +51,7 @@ public GPUImageTwoInputFilter(String fragmentShader) {
5051

5152
public GPUImageTwoInputFilter(String vertexShader, String fragmentShader) {
5253
super(vertexShader, fragmentShader);
53-
setRotation(ROTATION_NONE);
54+
setRotation(Rotation.NORMAL, false, false);
5455
}
5556

5657
@Override
@@ -97,91 +98,8 @@ protected void onDrawArraysPre() {
9798
GLES20.glVertexAttribPointer(filterSecondTextureCoordinateAttribute, 2, GLES20.GL_FLOAT, false, 0, mTexture2CoordinatesBuffer);
9899
}
99100

100-
public static float[] noRotationTextureCoordinates = {
101-
0.0f, 0.0f,
102-
1.0f, 0.0f,
103-
0.0f, 1.0f,
104-
1.0f, 1.0f,
105-
};
106-
107-
static float[] rotateLeftTextureCoordinates = {
108-
1.0f, 0.0f,
109-
1.0f, 1.0f,
110-
0.0f, 0.0f,
111-
0.0f, 1.0f,
112-
};
113-
114-
static float[] rotateRightTextureCoordinates = {
115-
0.0f, 1.0f,
116-
0.0f, 0.0f,
117-
1.0f, 1.0f,
118-
1.0f, 0.0f,
119-
};
120-
121-
static float[] verticalFlipTextureCoordinates = {
122-
0.0f, 1.0f,
123-
1.0f, 1.0f,
124-
0.0f, 0.0f,
125-
1.0f, 0.0f,
126-
};
127-
128-
static float[] horizontalFlipTextureCoordinates = {
129-
1.0f, 0.0f,
130-
0.0f, 0.0f,
131-
1.0f, 1.0f,
132-
0.0f, 1.0f,
133-
};
134-
135-
static float[] rotateRightVerticalFlipTextureCoordinates = {
136-
0.0f, 0.0f,
137-
0.0f, 1.0f,
138-
1.0f, 0.0f,
139-
1.0f, 1.0f,
140-
};
141-
142-
static float[] rotate180TextureCoordinates = {
143-
1.0f, 1.0f,
144-
0.0f, 1.0f,
145-
1.0f, 0.0f,
146-
0.0f, 0.0f,
147-
};
148-
149-
public static final int ROTATION_NONE = 1;
150-
public static final int ROTATION_LEFT = 2;
151-
public static final int ROTATION_RIGHT = 3;
152-
public static final int ROTATION_FLIP_VERTICAL = 4;
153-
public static final int ROTATION_FLIP_HORIZONTAL = 5;
154-
public static final int ROTATION_RIGHT_FLIP_VERTICAL = 6;
155-
public static final int ROTATION_180 = 7;
156-
157-
public void setRotation(int rotationMode) {
158-
float[] buffer;
159-
switch (rotationMode) {
160-
case ROTATION_NONE:
161-
buffer = noRotationTextureCoordinates;
162-
break;
163-
case ROTATION_LEFT:
164-
buffer = rotateLeftTextureCoordinates;
165-
break;
166-
case ROTATION_RIGHT:
167-
buffer = rotateRightTextureCoordinates;
168-
break;
169-
case ROTATION_FLIP_VERTICAL:
170-
buffer = verticalFlipTextureCoordinates;
171-
break;
172-
case ROTATION_FLIP_HORIZONTAL:
173-
buffer = horizontalFlipTextureCoordinates;
174-
break;
175-
case ROTATION_RIGHT_FLIP_VERTICAL:
176-
buffer = rotateRightVerticalFlipTextureCoordinates;
177-
break;
178-
case ROTATION_180:
179-
buffer = rotate180TextureCoordinates;
180-
break;
181-
default:
182-
buffer = noRotationTextureCoordinates;
183-
break;
184-
}
101+
public void setRotation(final Rotation rotation, final boolean flipHorizontal, final boolean flipVertical) {
102+
float[] buffer = TextureRotationUtils.getRotation(rotation, flipHorizontal, flipVertical);
185103

186104
ByteBuffer bBuffer = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder());
187105
FloatBuffer fBuffer = bBuffer.asFloatBuffer();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2012 CyberAgent
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jp.co.cyberagent.android.gpuimage;
18+
19+
public enum Rotation {
20+
NORMAL, ROTATION_90, ROTATION_180, ROTATION_270
21+
}

0 commit comments

Comments
 (0)
0