YUV420 (YV12, I420) crop, PIP algorithm (notes)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/xjb2006/article/details/78977776

RGB and YUV data on the arrangement no longer tired, Baidu lot.

YV12, I420 belong YUV420P, plus a P mean, I understand the Y, U, V components are arranged in blocks, i.e. so YYYYYYYY UU VV. YV12, I420 difference is that the position of the U and V reversed it.

Directly attached to the bar code:

1, cutting algorithm:

static void Cut_YV12(BYTE* Src,int x,int y,int srcWidth,int srcHeight,BYTE* Dst,int desWidth,int desHeight)//图片按位置裁剪
{
	//得到B图像所在A的坐标
	int nIndex=0;
	int BPosX=x ;//列
	int BPosY=y;//行
	for(int i=0;i<desHeight;i++)//
	{	
		memcpy(Dst+desWidth*i,Src+(srcWidth*BPosY)+BPosX+nIndex,desWidth);
		nIndex+=(srcWidth);
	}

	nIndex=0;
	BYTE *pUSour=Src+srcWidth*srcHeight;
	BYTE *pUDest=Dst+desWidth*desHeight;
	for(int i=0;i<desHeight/2;i++)//
	{	
		memcpy(pUDest+desWidth/2*i,pUSour+(srcWidth/2*BPosY/2)+BPosX/2+nIndex,desWidth/2);
		nIndex+=(srcWidth/2);
	}

	nIndex=0;
	BYTE *pVSour=Src+srcWidth*srcHeight*5/4;
	BYTE *pVDest=Dst+desWidth*desHeight*5/4;
	for(int i=0;i<desHeight/2;i++)//
	{	
		memcpy(pVDest+desWidth/2*i,pVSour+(srcWidth/2*BPosY/2)+BPosX/2+nIndex,desWidth/2);
		nIndex+=(srcWidth/2);
	}

}
The above algorithm is YV12, if I420, U and V components directly to the next exchange.

2, PIP algorithm:

static void PicIn_YV12(BYTE* Src,int x,int y,int srcWidth,int srcHeight,BYTE* Dst,int desWidth,int desHeight)//图片按位置合并
{
	if(x+srcWidth>desWidth)//越界
		return;
	if(y+srcHeight>desHeight)//越界
		return;

	//得到B图像所在A的坐标
	int nIndex=0;
	int BPosX=x ;//列
	int BPosY=y;//行
	for(int i=0;i<srcHeight;i++)//
	{	
		memcpy(Dst+(desWidth*BPosY)+BPosX+nIndex,Src+(srcWidth*i),srcWidth);
		nIndex+=(desWidth);
	}

	nIndex=0;
	BYTE *pUSour=Src+srcWidth*srcHeight;
	BYTE *pUDest=Dst+desWidth*desHeight;
	for(int i=0;i<srcHeight/2;i++)//
	{	
		memcpy(pUDest+(desWidth/2*BPosY/2)+BPosX/2+nIndex,pUSour+(srcWidth/2*i),srcWidth/2);
		nIndex+=(desWidth/2);
	}

	nIndex=0;
	BYTE *pVSour=Src+srcWidth*srcHeight*5/4;
	BYTE *pVDest=Dst+desWidth*desHeight*5/4;
	for(int i=0;i<srcHeight/2;i++)//
	{	
		memcpy(pVDest+(desWidth/2*BPosY/2)+BPosX/2+nIndex,pVSour+(srcWidth/2*i),srcWidth/2);
		nIndex+=(desWidth/2);
	}

}
The above algorithm is YV12, if I420, U and V components directly to the next exchange.

The above two algorithms were not considered odd width and height of the situation, so should the value of the input are even, to ensure full consideration should correct the situation fill the seats, regardless of the first. Record this two algorithms for later use, hope can help to you!

Guess you like

Origin blog.csdn.net/xjb2006/article/details/78977776