|
| 1 | +package com.daasuu.epf.filter; |
| 2 | + |
| 3 | +import android.content.res.Resources; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.BitmapFactory; |
| 6 | +import android.opengl.GLES20; |
| 7 | + |
| 8 | +import com.daasuu.epf.EglUtil; |
| 9 | + |
| 10 | +/** |
| 11 | + * This will posterize the resulting video a bit since it does not interpolate between 2 LUT values |
| 12 | + */ |
| 13 | +public class GlFastLookUpTableFilter extends GlFilter { |
| 14 | + |
| 15 | + private final static String FRAGMENT_SHADER = |
| 16 | + "precision lowp float;" + |
| 17 | + "uniform sampler2D lutTexture; \n" + |
| 18 | + "uniform sampler2D sTexture; \n" + |
| 19 | + "varying vec2 vTextureCoord; \n" + |
| 20 | + "void main() {\n" + |
| 21 | + " vec4 pixel = texture2D(sTexture, vTextureCoord);\n" + |
| 22 | + " float width = 16.;\n" + |
| 23 | + " float sliceSize = 1.0 / width;\n" + |
| 24 | + " float slicePixelSize = sliceSize / width;\n" + |
| 25 | + " float sliceInnerSize = slicePixelSize * (width - 1.0);\n" + |
| 26 | + " float zSlice0 = min(floor(pixel.b * width), width - 1.0);\n" + |
| 27 | + " float xOffset = slicePixelSize * 0.5 + pixel.r * sliceInnerSize;\n" + |
| 28 | + " float s0 = xOffset + (zSlice0 * sliceSize);\n" + |
| 29 | + " gl_FragColor = texture2D(lutTexture, vec2(s0, pixel.g));\n" + |
| 30 | + "}"; |
| 31 | + |
| 32 | + public GlFastLookUpTableFilter(Bitmap bitmap) { |
| 33 | + super(DEFAULT_VERTEX_SHADER, FRAGMENT_SHADER); |
| 34 | + this.lutTexture = bitmap; |
| 35 | + hTex = EglUtil.NO_TEXTURE; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public GlFastLookUpTableFilter(Resources resources, int fxID) { |
| 40 | + super(DEFAULT_VERTEX_SHADER, FRAGMENT_SHADER); |
| 41 | + this.lutTexture = BitmapFactory.decodeResource(resources, fxID); |
| 42 | + hTex = EglUtil.NO_TEXTURE; |
| 43 | + } |
| 44 | + |
| 45 | + private int hTex; |
| 46 | + |
| 47 | + private Bitmap lutTexture; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onDraw() { |
| 51 | + int offsetDepthMapTextureUniform = getHandle("lutTexture"); |
| 52 | + GLES20.glActiveTexture(GLES20.GL_TEXTURE3); |
| 53 | + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, hTex); |
| 54 | + GLES20.glUniform1i(offsetDepthMapTextureUniform, 3); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void setup() { |
| 59 | + super.setup(); |
| 60 | + loadTexture(); |
| 61 | + } |
| 62 | + |
| 63 | + private void loadTexture() { |
| 64 | + if (hTex == EglUtil.NO_TEXTURE) { |
| 65 | + hTex = EglUtil.loadTexture(lutTexture, EglUtil.NO_TEXTURE, false); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void releaseLutBitmap() { |
| 70 | + if (lutTexture != null && !lutTexture.isRecycled()) { |
| 71 | + lutTexture.recycle(); |
| 72 | + lutTexture = null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void reset() { |
| 77 | + hTex = EglUtil.NO_TEXTURE; |
| 78 | + hTex = EglUtil.loadTexture(lutTexture, EglUtil.NO_TEXTURE, false); |
| 79 | + } |
| 80 | +} |
0 commit comments