YUV420旋转和裁切样例代码

YUV420sp

#define BYTE	unsigned char

int width = 1280;
int height = 720;
int wxh = width * height;
int yuvsize = (wxh * 3) >> 1;

void rotateYuv(BYTE* yuv, BYTE* src, int rotation)
{
	int count = 0;
	switch(rotation)
	{
		case 90:
		//rotate Y 90
		for(int i = 0; i < width; i++){
			for(int j = height - 1; j >= 0; j--){
				yuv[count++] = src[width * j + i];
			}
		}
		//rotate UV 90
		for(int i = 0; i < width; i += 2){
			for(int j = (height - 1) >> 1; j >= 0; j--){
				yuv[count++] = src[wxh + widht * j + i];
				yuv[count++] = src[wxh + widht * j + i + 1];
			}
		}
		break;

		case 270:
		//rotate Y 270
		for(int j = width - 1; j >= 0; j--){
			for(int i = 0; i < height; i++){
				yuv[count++] = src[width * i + j];
			}
		}
		//rotate UV 270
		for(int j = width - 1; j >= 0; j--){
			for(int i = 0; i < height; i++){
				yuv[count++] = src[wxh + widht * i + j -1];
				yuv[count++] = src[wxh + widht * i + j];
			}
		}
		break;
	}
}

//保留YUV原始分辨率
void cropYuv(BYTE* yuv, BYTE src, int cropw, int croph)
{
	int count = 0;
	int offsetw = (width - cropw) >> 1;
	int offseth = (height - croph) >> 1;

	//crop Y
	for(int i = 0; i < height; i++){
		for(int j = 0; j < width; j++){
			if(offsetw -1 < j && j < cropw + offsetw && offseth - 1 < i && i < croph + offseth)
				yuv[count++] = src[width * i + j];
			else
				yuv[count++] = 0;
		}
	}
	//crop UV
	for(int i = 0; i < height >> 1; i++){
		for(int j = 0; j < width; j++){
			if(offsetw -1 < j && j < cropw + offsetw && offseth - 1 < i && i < croph + offseth)
				yuv[count++] = src[wxh + width * i + j];
			else
				yuv[count++] = 128;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值