NV12 图像的线性插值

#undef clamp_g
#define clamp_g(x, minValue, maxValue) ((x) < (minValue) ? (minValue) : ((x) > (maxValue) ? (maxValue) : (x)))
/*
输入的宽高和输出的宽高都要是偶数


*/

int NV12Linear(unsigned char * srcImage, unsigned char * destImage, int srcW, int srcH, int destW, int destH)
{

    float timeX = 0.0f;
    float timeY = 0.0f;

    timeX = srcW * 1.0f / destW;
    timeY = srcH * 1.0f / destH;

    int i = 0;
    int j = 0;
    int k = 0;

    unsigned char * srcUV = srcImage + srcW * srcH;
    unsigned char * destUV = destImage + destW * destH;

    for (i = 0; i < destH; i++)
    {
        for (j = 0; j < destW; j++)
        {

            int srcIdX = 0;
            int srcIdY = 0;

            float srcfIdX = 0.0f;
            float srcfIdY = 0.0f;


            float weightX[2] = { 0.0f, 0.0f };
            float weightY[2] = { 0.0f, 0.0f };

            srcfIdX = j * timeX;
            srcfIdY = i * timeY;

            srcIdX = (int)(srcfIdX);
            srcIdY = (int)(srcfIdY);

            weightX[1] = srcfIdX - srcIdX;
            weightX[0] = 1.0f - weightX[1];

            weightY[1] = srcfIdY - srcIdY;
            weightY[0] = 1.0f - weightY[1];

            destImage[(i * destW + j)] = (srcImage[(srcIdY * srcW + srcIdX)] * weightX[0] + srcImage[(srcIdY * srcW + clamp_g(srcIdX + 1, 0, srcW - 1))] * weightX[1]) * weightY[0] +
                (srcImage[(clamp_g(srcIdY + 1, 0, srcH - 1) * srcW + srcIdX)] * weightX[0] + srcImage[(clamp_g(srcIdY + 1, 0, srcH - 1) * srcW + clamp_g(srcIdX + 1, 0, srcW - 1))] * weightX[1]) * weightY[1];


            if (0 == i % 2 && 0 == j % 2)
            {

                int srcVUW = srcW / 2;
                int srcVUH = srcH / 2;

                int destVUy = i / 2;
                int destVUx = j / 2;


                int UVsrcIdX = 0;
                int UVsrcIdY = 0;

                float UVsrcfIdX = 0.0f;
                float UVsrcfIdY = 0.0f;


                float UVweightX[2] = { 0.0f };
                float UVweightY[2] = { 0.0f };

                UVsrcfIdX = destVUx * timeX;
                UVsrcfIdY = destVUy * timeY;


                UVsrcIdX = (int)(UVsrcfIdX);
                UVsrcIdY = (int)(UVsrcfIdY);



                UVweightX[1] = UVsrcfIdX - UVsrcIdX;
                UVweightX[0] = 1.0f - UVweightX[1];

                UVweightY[1] = UVsrcfIdY - UVsrcIdY;
                UVweightY[0] = 1.0f - UVweightY[1];


                destUV[(i / 2 * (destW / 2) + j / 2) * 2 + 0] =
                    (srcUV[(UVsrcIdY * srcVUW + UVsrcIdX) * 2 + 0] * weightX[0] + srcUV[(UVsrcIdY * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 0] * weightX[1]) * weightY[0] +
                    (srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + UVsrcIdX) * 2 + 0] * weightX[0] + srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 0] * weightX[1]) * weightY[1];


                destUV[(i / 2 * (destW / 2) + j / 2) * 2 + 1] =
                    (srcUV[(UVsrcIdY * srcVUW + UVsrcIdX) * 2 + 1] * weightX[0] + srcUV[(UVsrcIdY * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 1] * weightX[1]) * weightY[0] +
                    (srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + UVsrcIdX) * 2 + 1] * weightX[0] + srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 1] * weightX[1]) * weightY[1];


            }

        }
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值