YUV420P 旋转--待整理


// 逆时针90
void YUVRotate90(BYTE *des, BYTE *src, int width, int height)
{
	int i = 0, j = 0, n = 0;
	int hw = width / 2, hh = height / 2;
	for (j = width; j > 0; j--) {
		for (i = 0; i < height; i++)
		{
			des[n++] = src[width*i + j];
		}
	}

	unsigned char *ptmp = src + width*height;
	for (j = hw; j > 0; j--) {
		for (i = 0; i < hh; i++)
		{
			des[n++] = ptmp[hw*i + j];
		}
	}

	ptmp = src + width*height * 5 / 4;
	for (j = hw; j > 0; j--){
		for (i = 0; i < hh; i++)
		{
			des[n++] = ptmp[hw*i + j];
		}
	}
}

//逆时针180
void yuv_rotate_180(const char* yuvfilename, int width, int height){
	FILE* fp = NULL;
	fopen_s(&fp, yuvfilename, "rb");
	uint8_t* yuvbuf = new uint8_t[width*height * 3 / 2];
	fread(yuvbuf, width*height * 3 / 2, 1, fp);
	fclose(fp);
	uint8_t* dstbuf = new uint8_t[width*height * 3 / 2];
	int idx = 0;
	//旋转180:将右下角的点作为第一个点,从右往左,从下往上取点
	//Y 宽:[0,w-1]  高:[0,h-1]
	for (int i = height - 1; i >= 0; i--){
		for (int j = width - 1; j >= 0; j--){
			dstbuf[idx++] = *(yuvbuf + (i*width + j));
		}
	}
	uint8_t* uheader = yuvbuf + width*height;
	//U
	for (int i = height / 2 - 1; i >= 0; i--){
		for (int j = width / 2 - 1; j >= 0; j--){
			dstbuf[idx++] = *(uheader + (i*width / 2 + j));
		}
	}
	uint8_t* vheader = uheader + width*height / 4;
	//V
	for (int i = height / 2 - 1; i >= 0; i--){
		for (int j = width / 2 - 1; j >= 0; j--){
			dstbuf[idx++] = *(vheader + (i*width / 2 + j));
		}
	}
	FILE* fpout = NULL;
	fopen_s(&fpout, "yuv_rotate_180.yuv", "wb");
	fwrite(dstbuf, width*height * 3 / 2, 1, fpout);
	fclose(fpout);
	delete[] yuvbuf;
	delete[] dstbuf;
}

//逆时针旋转180后镜像
void yuv_rotate_180_by_ssz(const char* yuvfilename, int width, int height){
	FILE* fp = NULL;
	fopen_s(&fp, yuvfilename, "rb");
	uint8_t* yuvbuf = new uint8_t[width*height * 3 / 2];
	fread(yuvbuf, width*height * 3 / 2, 1, fp);
	fclose(fp);

	uint8_t* dstbuf = new uint8_t[width*height * 3 / 2];
	int idx = 0;
	//旋转180:将左下角的点作为第一个点,从左往右,从下往上取点
	//Y 宽:[0,w-1]  高:[0,h-1]
	for (int i = height - 1; i >= 0; i--){
		for (int j = 0; j <= width - 1; j++){
			dstbuf[idx++] = *(yuvbuf + (i*width + j));
		}
	}
	uint8_t* uheader = yuvbuf + width*height;
	//U
	for (int i = height / 2 - 1; i >= 0; i--){
		for (int j = 0; j <= width / 2 - 1; j++){
			dstbuf[idx++] = *(uheader + (i*width / 2 + j));
		}
	}
	uint8_t* vheader = uheader + width*height / 4;
	//V
	for (int i = height / 2 - 1; i >= 0; i--){
		for (int j = 0; j <= width / 2 - 1; j++){
			dstbuf[idx++] = *(vheader + (i*width / 2 + j));
		}
	}
	FILE* fpout = NULL;
	fopen_s(&fpout, "yuv_rotate_180_by_ssz2.yuv", "wb");
	fwrite(dstbuf, width*height * 3 / 2, 1, fpout);
	fclose(fpout);
	delete[] yuvbuf;
	delete[] dstbuf;
}

//逆时针旋转90
void rotate90YUV420P(char *yuvfilename, int width, int height)
{
	int i, j, k, p, q;

	FILE* fp = NULL;
	fopen_s(&fp, yuvfilename, "rb");
	uint8_t* yuvbuf = new uint8_t[width*height * 3 / 2];
	fread(yuvbuf, width*height * 3 / 2, 1, fp);
	fclose(fp);
	uint8_t* dstbuf = new uint8_t[width*height * 3 / 2];

	char *dest = (char*)dstbuf;
	char* src = (char*)yuvbuf;

	// rotate Y
	for (j = 0; j < width; j++){
		for (i = 1; i <= height; i++){
			*dest++ = *(src + i*width - j);
		}
	}

	// rotate U
	char *src_u = src + width*height;
	for (p = 0; p < width / 2; p++){
		for (k = 1; k <= height / 2; k++){
			*dest++ = *(src_u + k*width / 2 - p);

		}
	}

	// rotate V
	char *src_v = src + width*height * 5 / 4;
	for (p = 0; p < width / 2; p++){
		for (k = 1; k <= height / 2; k++){
			*dest++ = *(src_v + k*width / 2 - p);
		}
	}

	FILE* fpout = NULL;
	fopen_s(&fpout, "rotate90YUV420P.yuv", "wb");
	fwrite(dstbuf, width*height * 3 / 2, 1, fpout);
	fclose(fpout);
	delete[] yuvbuf;
	delete[] dstbuf;
}

//顺时针旋转90
void yuv_rotate_90_by_ssz(const char* yuvfilename, int width, int height){
	FILE* fp = NULL;
	fopen_s(&fp, yuvfilename, "rb");
	uint8_t* yuvbuf = new uint8_t[width*height * 3 / 2];
	fread(yuvbuf, width*height * 3 / 2, 1, fp);
	fclose(fp);

	uint8_t* dstbuf = new uint8_t[width*height * 3 / 2];
	int idx = 0;
	//旋转180:将左下角的点作为第一个点,从右往左,从下往上取点
	//Y 宽:[0,w-1]  高:[0,h-1]
	for (int i = 0; i <= width - 1; i++){
		for (int j = height - 1; j >= 0; j--){
			dstbuf[idx++] = *(yuvbuf + (j*width + i));
		}
	}
	uint8_t* uheader = yuvbuf + width*height;
	//U
	int hw = width / 2;
	int hh = height / 2;

	for (int i = 0; i <= hw - 1; i++){
		for (int j = hh - 1; j >= 0; j--){
			dstbuf[idx++] = *(uheader + (j*hw + i));
		}
	}

	uint8_t* vheader = uheader + width*height / 4;
	//V
	for (int i = 0; i <= hw - 1; i++){
		for (int j = hh - 1; j >= 0; j--){
			dstbuf[idx++] = *(vheader + (j*hw + i));
		}
	}
	FILE* fpout = NULL;
	fopen_s(&fpout, "zssz-90-4.yuv", "wb");
	fwrite(dstbuf, width*height * 3 / 2, 1, fpout);
	fclose(fpout);
	delete[] yuvbuf;
	delete[] dstbuf;
}

//镜像翻转
void yuv_flip(const char* yuvfilename, int width, int height){

	FILE* fp = NULL;
	fopen_s(&fp, yuvfilename, "rb");
	uint8_t* yuvbuf = new uint8_t[width*height * 3 / 2];
	fread(yuvbuf, width*height * 3 / 2, 1, fp);
	fclose(fp);
	uint8_t* dstbuf = new uint8_t[width*height * 3 / 2];
	int idx = 0;
	//水平翻转:将右上角的点作为第一个点,从右往左,从上往下取点
	//Y 宽:[0,w-1]  高:[0,h-1]
	for (int i = 0; i < height; i++){
		for (int j = width - 1; j >= 0; j--){
			dstbuf[idx++] = *(yuvbuf + (i*width + j));
		}
	}
	uint8_t* uheader = yuvbuf + width*height;
	//U
	for (int i = 0; i < height / 2; i++){
		for (int j = width / 2 - 1; j >= 0; j--){
			dstbuf[idx++] = *(uheader + (i*width / 2 + j));
		}
	}
	uint8_t* vheader = uheader + width*height / 4;
	//V
	for (int i = 0; i < height / 2; i++){
		for (int j = width / 2 - 1; j >= 0; j--){
			dstbuf[idx++] = *(vheader + (i*width / 2 + j));
		}
	}
	FILE* fpout = NULL;
	fopen_s(&fpout, "flip_out.yuv", "wb");
	fwrite(dstbuf, width*height * 3 / 2, 1, fpout);
	fclose(fpout);
	delete[] yuvbuf;
	delete[] dstbuf;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值