【图像操作代码】

图像结构体

typedef struct __tag_ASVL_OFFSCREEN
{
	MUInt32	u32PixelArrayFormat; //图像格式
	MInt32	i32Width;      //图像宽
	MInt32	i32Height;     //图像高
	MUInt8*	ppu8Plane[4];  //存储图像数据
	MInt32	pi32Pitch[4];  //图像的步长
}ASVLOFFSCREEN, *LPASVLOFFSCREEN;

1、UYVY转NV12

MRESULT UYVY_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage) 
{
    /* TODO: Write your code here */
	if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
		return MERR_UNSUPPORTED;
	size_t width = pSrcImage->i32Width;
	size_t height = pSrcImage->i32Height;
	size_t y_size = width * height;
	size_t pixels_in_a_row = width * 2;
	char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
	char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
	char *nv12_y_ptr = nv12_content;
    char *nv12_uv_ptr = nv12_content + y_size;
	int lines = 0;
	size_t frame_size = 2 * width * height;
	for (int i = 0;i < frame_size;i += 4) {
		// copy y channel
		*nv12_y_ptr++ = uyvy_content[i + 1];
		*nv12_y_ptr++ = uyvy_content[i + 3];
		if (0 == i % pixels_in_a_row) {
			++lines;
		}
		if (lines % 2) {       // extract the UV value of odd rows
			// copy uv channel
            *nv12_uv_ptr++ = uyvy_content[i + 2];
			*nv12_uv_ptr++ = uyvy_content[i];
		}
	}
	return MOK;
}

2、UYVY转NV21

MRESULT UYVY_NV21_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage) 
{
    /* TODO: Write your code here */
	if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV21)
		return MERR_UNSUPPORTED;
	size_t width = pSrcImage->i32Width;
	size_t height = pSrcImage->i32Height;
	size_t y_size = width * height;
	size_t pixels_in_a_row = width * 2;
	char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
	char *nv21_content = (char *)(pDstImage->ppu8Plane[0]);
	char *nv21_y_ptr = nv21_content;
    char *nv21_uv_ptr = nv21_content + y_size;
	int lines = 0;
	size_t frame_size = 2 * width * height;
	for (int i = 0;i < frame_size;i += 4) {
		// copy y channel
		*nv21_y_ptr++ = uyvy_content[i + 1];
		*nv21_y_ptr++ = uyvy_content[i + 3];
		if (0 == i % pixels_in_a_row) {
			++lines;
		}
		if (lines % 2) {       // extract the UV value of odd rows
			// copy uv channel
            *nv21_uv_ptr++ = uyvy_content[i];
			*nv21_uv_ptr++ = uyvy_content[i + 2];
		}
	}
	return MOK;
}

3、YUYV转NV12

MRESULT YUYV_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage) 
{
    /* TODO: Write your code here */
	if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
		return MERR_UNSUPPORTED;
	size_t width = pSrcImage->i32Width;
	size_t height = pSrcImage->i32Height;
	size_t y_size = width * height;
	size_t pixels_in_a_row = width * 2;
	char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
	char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
	char *nv12_y_ptr = nv12_content;
    char *nv12_uv_ptr = nv12_content + y_size;
	int lines = 0;
	size_t frame_size = 2 * width * height;
	for (int i = 0;i < frame_size;i += 4) {
		// copy y channel
		*nv12_y_ptr++ = uyvy_content[i];
		*nv12_y_ptr++ = uyvy_content[i + 2];
		if (0 == i % pixels_in_a_row) {
			++lines;
		}
		if (lines % 2) {       // extract the UV value of odd rows
			// copy uv channel
            *nv12_uv_ptr++ = uyvy_content[i + 1];
			*nv12_uv_ptr++ = uyvy_content[i + 3];
		}
	}
	return MOK;
}

4、I420转NV12

MRESULT I420_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage) 
{
    /* TODO: Write your code here */
	if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
		return MERR_UNSUPPORTED;
	size_t width = pSrcImage->i32Width;
	size_t height = pSrcImage->i32Height;
	size_t y_size = width * height;
	size_t uv_size = width * height / 4;
	char *i420_content = (char *)(pSrcImage->ppu8Plane[0]);
	char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
	char *i420_u_base = i420_content + y_size;
	char *i420_v_base = i420_u_base + uv_size;
	char *nv12_uv_base = nv12_content + y_size;
	//copy y channel
	memcopy(nv21_content, i420_content, y_size);
	//copy uv channel
    int i = 0, j = 0;
    for (;i < uv_size;i++) {
        nv12_uv_base[j] = i420_u_base[i];
        nv12_uv_base[j + 1] = i420_v_base[i];
        j += 2;
    }
	return MOK;
}

5、UYVY转I420

MRESULT UYVY_I420_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage) 
{
    /* TODO: Write your code here */
	if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_I420)
		return MERR_UNSUPPORTED;
	size_t width = pSrcImage->i32Width;
	size_t height = pSrcImage->i32Height;
	size_t y_size = width * height;
	size_t uv_size = width * height / 4;
	char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
	char *i420_content = (char *)(pDstImage->ppu8Plane[0]);
	char *i420_y_base = i420_content;
	char *i420_u_base = i420_y_base + width * height;
	char *i420_v_base = i420_u_base + width * height / 4;
	int y_index = 0;
	for (int i = 0; i < height; i++) {
		for (int j = 0; j < 2 * width; j += 4) {
			y_index = i * 2 * width + j;
			*i420_y_base++ = uyvy_content[y_index + 1];
			*i420_y_base++ = uyvy_content[y_index + 3];
			if (i % 2) {
				*i420_u_base++ = uyvy_content[y_index];
				*i420_v_base++ = uyvy_content[y_index + 2];
			}
		}
	}
	return MOK;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值