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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章