content: package com.mitakeran.magicfilter.filter.advance.common;
  
  import android.content.Context;
  import android.opengl.GLES20;
  
  import com.mitakeran.magicfilter.R;
  import com.mitakeran.magicfilter.filter.base.gpuimage.GPUImageFilter;
  import com.mitakeran.magicfilter.utils.OpenGLUtils;
  
  public class MagicFreudFilter extends GPUImageFilter{
  	private int mTexelHeightUniformLocation;
      private int mTexelWidthUniformLocation;
  	private int[] inputTextureHandles = {-1};
  	private int[] inputTextureUniformLocations = {-1};
  	private Context mContext;
  	
  	public MagicFreudFilter(Context context){
  		super(NO_FILTER_VERTEX_SHADER,OpenGLUtils.readShaderFromRawResource(context, R.raw.freud));
  		mContext = context;
  	}
  	
  	protected void onDestroy() {
          super.onDestroy();
          GLES20.glDeleteTextures(1, inputTextureHandles, 0);
          for(int i = 0; i < inputTextureHandles.length; i++)
          	inputTextureHandles[i] = -1;
      }
  	
  	protected void onDrawArraysAfter(){
  		for(int i = 0; i < inputTextureHandles.length
  				&& inputTextureHandles[i] != OpenGLUtils.NO_TEXTURE; i++){
  			GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + (i+3));
  			GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
  			GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  		}
  	}
  	  
  	protected void onDrawArraysPre(){
  		for(int i = 0; i < inputTextureHandles.length 
  				&& inputTextureHandles[i] != OpenGLUtils.NO_TEXTURE; i++){
  			GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + (i+3) );
  			GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, inputTextureHandles[i]);
  			GLES20.glUniform1i(inputTextureUniformLocations[i], (i+3));
  		}
  	}
  	
  	protected void onInit(){
  		super.onInit();
  		inputTextureUniformLocations[0] = GLES20.glGetUniformLocation(getProgram(), "inputImageTexture2");
  		
  		mTexelWidthUniformLocation = GLES20.glGetUniformLocation(getProgram(), "inputImageTextureWidth");
          mTexelHeightUniformLocation = GLES20.glGetUniformLocation(getProgram(), "inputImageTextureHeight");
  	}
  	
  	protected void onInitialized(){
  		super.onInitialized();
  	    runOnDraw(new Runnable(){
  		    public void run(){
  		    	inputTextureHandles[0] = OpenGLUtils.loadTexture(mContext, "filter/freud_rand.png");
  		    }
  	    });
  	}
  	
  	public void onOutputSizeChanged(int width, int height) {
          super.onOutputSizeChanged(width, height);
          GLES20.glUniform1f(mTexelWidthUniformLocation, (float)width);
          GLES20.glUniform1f(mTexelHeightUniformLocation, (float)height);
      }
  }
  