RGB转NV21/YUYV转NV21/UYVY转NV21

RGB_2_NV21

static void rgb8888_2_nv21(unsigned char *s, unsigned char *iYuv, int width, int height) {
    int ui = width * height;
    int i = 0;
    int j, k;
	unsigned char sR,sG,sB;
    for (j = 0; j < height; j++) {
       for (k = 0; k < width; k++) {
            // Y value is generated for each pixel
			sR = *s;
			sG = *(s+1);
			sB = *(s+2);
            iYuv[i] = (unsigned char) ((66 * sR + 129 * sG + 25 * sB + 128) >> 8) + 16;
            if (0 == j % 2 && 0 == k % 2) {
                iYuv[ui++] = (unsigned char) ((112 * sR - 94 * sG - 18 * sB + 128) >> 8) + 128;
                iYuv[ui++] = (unsigned char) ((-38 * sR - 74 * sG + 112  sB + 128) >> 8) + 128;
            }
            i++;
            s += 3;
        }
    }
    return;
}

YUYV_2_NV21

void YUYV2NV21(unsigned char*src, unsigned char*dst, int width, int height)
{
    for (int i = 0; i < width * height * 2; i += 2) {
        *dst++ = *(src + i);
    }

    for (int y = 0; y < height - 1; y +=2) {
        for (int j = 0; j < width * 2; j += 4) {
            *dst++ = (*(src + 3 + j) + *(src + 3 + j + width * 2) + 1) >> 1;
            *dst++ = (*(src + 1 + j) + *(src + 1 + j + width * 2) + 1) >> 1;
        }
        src += width * 2 * 2;
    }
}

UYVY_2_NV21

void UYVY_2_NV21(unsigned char *src, unsigned char *dst, int width, int height)
{
	int i = 0;
	int j = 0;
	int y = 0;
	//int k = 0;
    for (i = 0; i < width * height * 2; i += 2) {
        *dst++ = *(src + i + 1);
    }

    for (y = 0; y < height - 1; y +=2) {
        for (j = 0; j < width * 2; j += 4) {
            *dst++ = (*(src + 2 + j) + *(src + 2 + j + width * 2) + 1) >> 1;
            *dst++ = (*(src + j) + *(src + j + width * 2) + 1) >> 1;
        }
        src += width * 2 * 2;
    }
}

yuv_to_rgb32

void yuv_to_rgb32(char y,char u,char v,char *rgb)
{
    int r,g,b;

    r = (1192 * (y - 16) + 1634 * (v - 128) ) >> 10;
    g = (1192 * (y - 16) - 833 * (v - 128) - 400 * (u -128) ) >> 10;
    b = (1192 * (y - 16) + 2066 * (u - 128) ) >> 10;

    r = r > 255 ? 255 : r < 0 ? 0 : r;
    g = g > 255 ? 255 : g < 0 ? 0 : g;
    b = b > 255 ? 255 : b < 0 ? 0 : b;

    /*ARGB*/
    *rgb = (unsigned char)r;
    rgb++;
    *rgb = (unsigned char)g;
    rgb++;
    *rgb = (unsigned char)b;
    rgb++;
    *rgb = 0xff;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值