RGB to YUV using shader

I want to use fragment shader to convert RGB to YUV420P using shader.

1,I figured out the coordinates of the current texture color.

2,I calculated the 4 Y and put it on the current color.

My code:

    precision mediump float;
    varying vec2 vTextureCoord;
    uniform sampler2D tex;
    uniform float width;
    uniform float height;
    uniform float offset;
    uniform vec3 scal;

    void main(void) {
        vec2 nowTxtPos = vTextureCoord;
        vec2 size = vec2(width, height);
    // y
        if(nowTxtPos.y<0.25){
    // y1 postion
            vec2 now_pos = nowTxtPos * size;
            vec2 basePos = now_pos * vec2(4.0,4.0);
            float addY = float(int(basePos.x / width));
            basePos.x = basePos.x - addY * width;
            basePos.y += addY;

            float y1,y2,y3,y4;
            vec2 samplingPos = basePos / size;
            vec4 texel = texture2D(tex, samplingPos);
            y1 = dot(texel.rgb, scal);
            y1 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y2 = dot(texel.rgb, scal);
            y2 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y3 = dot(texel.rgb, scal);
            y3 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y4 = dot(texel.rgb, scal);
            y4 += offset;

            gl_FragColor = vec4(y1, y2, y3, y4);
        }
    }

The picture(YUV420P):

picture

Enlarged post picture

It has aliasing. I don't know why.

share improve this question
 

1 Answer

up vote 1 down vote accepted

I have solved it. Because the calculation accuracy is not enough.

Edit here:

"precision mediump float;" -> "precision highp float;"

share improve this answer

https://stackoverflow.com/questions/33339874/rgb-to-yuv-using-shader
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值