YUV 420 p回転コード


ネットで探しためちゃくちゃなことを自分で書いてみましょう.実は簡単です.
 
class yuv420_rotate
{
public:
	yuv420_rotate(void);
	~yuv420_rotate(void);

public:
	//rotate clockwise
	static void yuv_rotate_90(BYTE *des,BYTE *src,int width,int height);
	static void yuv_rotate_180(BYTE *des,BYTE *src,int width,int height);
	static void yuv_rotate_270(BYTE *des,BYTE *src,int width,int height);

	//flip
	static void yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height);
	static void yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height);

};

 
yuv420_rotate::yuv420_rotate(void)
{
}

yuv420_rotate::~yuv420_rotate(void)
{
}

void yuv420_rotate::yuv_rotate_90(BYTE *des,BYTE *src,int width,int height)
{
	int n = 0;
	int hw = width / 2;
	int hh = height / 2;
	//copy y
	for(int j = 0; j < width;j++)
	{
		for(int i = height - 1; i >= 0; i--)
		{
			des[n++] = src[width * i + j];
		}
	}

	//copy u
	BYTE *ptemp = src + width * height;
	for(int j = 0;j < hw;j++)
	{
		for(int i = hh - 1;i >= 0;i--)
		{
			des[n++] = ptemp[ hw*i + j ];
		}
	}

	//copy v
	ptemp += width * height / 4;
	for(int j = 0; j < hw; j++)
	{
		for(int i = hh - 1;i >= 0;i--)
		{
			des[n++] = ptemp[hw*i + j];
		}
	}
}

void yuv420_rotate::yuv_rotate_180(BYTE *des,BYTE *src,int width,int height)
{
	int n = 0;
	int hw = width / 2;
	int hh = height / 2;
	//copy y
	for(int j = height - 1; j >= 0; j--)
	{
		for(int i = width; i > 0; i--)
		{
			des[n++] = src[width*j + i];
		}
	}

	//copy u
	BYTE *ptemp = src + width * height;
	for(int j = hh - 1;j >= 0; j--)
	{
		for(int i = hw; i > 0; i--)
		{
			des[n++] = ptemp[hw * j + i];
		}
	}

	//copy v
	ptemp += width * height / 4;
	for(int j = hh - 1;j >= 0; j--)
	{
		for(int i = hw; i > 0; i--)
		{
			des[n++] = ptemp[hw * j + i];
		}
	}
}

void yuv420_rotate::yuv_rotate_270(BYTE *des,BYTE *src,int width,int height)
{
	int n = 0;
	int hw = width / 2;
	int hh = height / 2;
	//copy y
	for(int j = width; j > 0; j--)
	{
		for(int i = 0; i < height;i++)
		{
			des[n++] = src[width*i + j];
		}
	}

	//copy u
	BYTE *ptemp = src + width * height;
	for(int j = hw; j > 0;j--)
	{
		for(int i = 0; i < hh;i++)
		{
			des[n++] = ptemp[hw * i + j];
		}
	}

	//copy v
	ptemp += width * height / 4;
	for(int j = hw; j > 0;j--)
	{
		for(int i = 0; i < hh;i++)
		{
			des[n++] = ptemp[hw * i + j];
		}
	}
}

void yuv420_rotate::yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height)
{
	int n = 0;
	int hw = width / 2;
	int hh = height / 2;
	//copy y
	for(int j = 0; j < height; j++)
	{
		for(int i = width - 1;i >= 0;i--)
		{
			des[n++] = src[width * j + i];
		}
	}

	//copy u
	BYTE *ptemp = src + width * height;
	for(int j = 0; j < hh; j++)
	{
		for(int i = hw - 1;i >= 0;i--)
		{
			des[n++] = ptemp[hw * j + i];
		}
	}
	
	//copy v
	ptemp += width * height / 4;
	for(int j = 0; j < hh; j++)
	{
		for(int i = hw - 1;i >= 0;i--)
		{
			des[n++] = ptemp[hw * j + i];
		}
	}
}

void yuv420_rotate::yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height)
{
	int n = 0;
	int hw = width / 2;
	int hh = height / 2;
	//copy y
	for(int j = 0; j < width;j++)
	{
		for(int i = height - 1; i >= 0;i--)
		{
			des[n++] = src[width * i + j];
		}
	}

	//copy u
	BYTE *ptemp = src + width * height;
	for(int j = 0; j < hw; j++)
	{
		for(int i = hh - 1; i >= 0;i--)
		{
			des[n++] = ptemp[ hw * i + j];
		}
	}

	//copy v
	ptemp += width * height / 4;
	for(int j = 0; j < hw; j++)
	{
		for(int i = hh - 1; i >= 0; i--)
		{
			des[n++] = ptemp[ hw * i + j];
		}
	}
}