GPUimage中马赛克滤镜的自定义着色器(马赛克拼贴纹理效果)

时间:2015-04-22 02:17:02

标签: android gpuimage

我想将马赛克滤镜从iOS中的GPUImage转换为GPUImage Android。

这在我想要的iOS中有效:

enter image description here

默认图片:

enter image description here

图片模式:

lh3.googleusercontent.com/-MRl8oLNKLYY/VTb3tPP61mI/AAAAAAABucc/x5X2JUQPDDE/s0/tile_box.png http://lh3.googleusercontent.com/-MRl8oLNKLYY/VTb3tPP61mI/AAAAAAABucc/x5X2JUQPDDE/s0/tile_box.png

这是iOS和Android中的代码。我在Android中使用GPUImage库进行转换,但效果并不像GPUImage iOS样本中那样。 GPUImage iOS中的代码: github.com/BradLarson/GPUImage/blob/master/framework/Source/GPUImageMosaicFilter.m

Android中的代码:

 public class GPUImageMosaicFilter extends GPUImageTwoInputFilter {
        public static final String MOSAIC_BLEND_FRAGMENT_SHADER = "varying highp vec2 textureCoordinate;\n"
                        + " varying highp vec2 textureCoordinate2;\n"
                        + "\n"
                        + " uniform sampler2D inputImageTexture;\n"
                        + " uniform sampler2D inputImageTexture2;\n"
                        + " uniform vec2 inputTileSize;\n"
                        + " uniform vec2 displayTileSize;\n"
                        + " uniform float numTiles;\n"
                        + " uniform int colorOn;\n"
                        + " \n"
                        + " void main()\n"
                        + " {\n"
                        + " highp vec2 xy = textureCoordinate;\n"
                        + "xy = xy - mod(xy, displayTileSize);\n"
                        + " vec4 lumcoeff = vec4(0.299,0.587,0.114,0.0);\n"
                        + " vec4 inputColor = texture2D(inputImageTexture2, xy);\n"
                        + "float lum = dot(inputColor,lumcoeff);\n"
                        + "lum = 1.0 - lum;\n"
                        + " float stepsize = 1.0 / numTiles;\n"
                        + " float lumStep = (lum - mod(lum, stepsize)) / stepsize;\n"
                        + " float rowStep = 1.0 / inputTileSize.x;\n"
                        + " float x = mod(lumStep, rowStep);\n"
                        + " float y = floor(lumStep / rowStep);\n"
                        + "  vec2 startCoord = vec2(x *  inputTileSize.x, y * inputTileSize.y);\n"
                        + " vec2 finalCoord = startCoord + ((textureCoordinate - xy) * (inputTileSize / displayTileSize));\n"
                        + "lowp  vec4 color = texture2D(inputImageTexture, finalCoord);\n"
                        + "   if (colorOn == 1) {\n"
                        + "     color = color * inputColor;\n"
                        + "   }\n" + "\n" + " gl_FragColor = color;\n" + " }";
        private PointF mTileSize;
        private int mTileSizeLocation;
        private PointF mDisplayTileSize;
        private int mDisplayTileSizeLocation;
        private float mNumTiles;
        private int mNumTilesLocation;
        private int mIsColorOn;
        private int mIsColorOnLocation;

        public GPUImageMosaicFilter(PointF tileSize, PointF displaytileSize,
                        float numtiles, int ColorOn) {
                super(MOSAIC_BLEND_FRAGMENT_SHADER);
                mTileSize = tileSize;
                mDisplayTileSize = displaytileSize;
                mNumTiles = numtiles;
                mIsColorOn = ColorOn;
        }

        public GPUImageMosaicFilter() {
                this(new PointF(0.0125f, 0.0125f), new PointF(0.025f, 0.025f), 64.0f, 1);
        }

        @Override
        public void onInit() {
                // TODO Auto-generated method stub
                super.onInit();
                mTileSizeLocation = GLES20.glGetUniformLocation(getProgram(),
                                "inputTileSize");
                mDisplayTileSizeLocation = GLES20.glGetUniformLocation(getProgram(),
                                "displayTileSize");
                mNumTilesLocation = GLES20.glGetUniformLocation(getProgram(),
                                "numTiles");
                mIsColorOnLocation = GLES20.glGetUniformLocation(getProgram(),
                                "colorOn");
        }

        @Override
        public void onInitialized() {
                // TODO Auto-generated method stub
                super.onInitialized();
                setTileSize(mTileSize);
                setDisplayTileSize(mDisplayTileSize);
                setNumtiles(mNumTiles);
                setColorOn(mIsColorOn);
        }

        public void setTileSize(PointF tilesize) {
                mTileSize = tilesize;
                setPoint(mTileSizeLocation, tilesize);
        }

        public void setDisplayTileSize(PointF display) {
                mDisplayTileSize = display;
                setPoint(mDisplayTileSizeLocation, display);
        }

        public void setNumtiles(float numTiles) {
                mNumTiles = numTiles;
                setFloat(mNumTilesLocation, numTiles);
        }

        public void setColorOn(int colorOn) {
                mIsColorOn = colorOn;
                setFloat(mIsColorOnLocation, colorOn);
        }
}

结果图片我在应用过滤器后进入Android: lh3.googleusercontent.com/-1dFBc95tm-Q/VTcAa6KCK1I/AAAAAAABuf0/yynb8q4ImFs/s0/result.png http://lh3.googleusercontent.com/-1dFBc95tm-Q/VTcAa6KCK1I/AAAAAAABuf0/yynb8q4ImFs/s0/result.png

请帮我修复字符串着色器。谢谢。 (对不起,我的声誉不足以发布链接和图片)

0 个答案:

没有答案