星星博客 »  > 

C语言裁剪YUV指定区域

/*-----------------------------------------------------------------------------------------------------------
    @brief : YUV420数据的存储方式有两类:planar和packed。

    	 该函数裁剪一个对于planar的YUV420格式,读取指定区域的数据,
     	 并保存为一个新的YUV420格式RAW。

    	 首先指定的新的宽高必须是4的整倍数,不然新图像会有底边会花,
    	 这是由YUV420这个数据存储格式决定的
    	 
     @note : 该函数会对传入的pOutBuf指针分配内存,调用完毕后需要自行释放
 ------------------------------------------------------------------------------------------------------------*/
int crop_yuv420_area(uint8_t *pInBuf, uint32_t width, uint32_t height, uint8_t **pOutBuf, int new_x, int new_y, int new_width, int new_height)
{
	if(NULL == pInBuf || 0 == width || 0 == height)
		return -1;

	int length = new_width* new_height* 3 / 2;
	*pOutBuf = (uint8_t *)malloc(length);
	if (NULL == *pOutBuf)
	{
		stderr("malloc new size memory failed! size=%d\n", length);
		return -1;
	}
	memset(*pOutBuf , 0, length);

	uint8_t *pUBuf = *pOutBuf + new_width * new_height;
	uint8_t *pVBuf = *pOutBuf +new_width * new_height * 5 / 4;
	int x = 0,y = 0;
	for(x = 0; x < new_width; x++)
	{
		for (y = 0; y < new_height; y++)	//每个循环写一列
		{
			*(*pOutBuf + y * new_width + x) = *(pInBuf + (x + new_x) + width * (y + new_y));	//cope Y
			int ret = (y + new_y)%2;
			if (1 == (x + new_x)%2 && 1 == (y + new_y)%2)
			{
				long pix = width * height + (width>>1) * ((y + new_y)>>1) + (((x + new_x))>>1);
				*(pUBuf + (new_width/2)*(y/2) + x/2) = *(pInBuf + pix);	//cope U

				pix += width * height / 4;
				*(pVBuf + (new_width/2)*(y/2) + x/2) = *(pInBuf + pix);	//cope V
			}
		}
	}	
	return 0;
}


相关文章