原文:http://blog.youkuaiyun.com/zhangyonggen/article/details/48522593
YUV4:2:2 UYVY 格式在内存中的存储方式为交织类型,内存中的存储方式为:UYVY UYVY UYVY...........,Y为亮度信息,UV为色度信息,YUV4:2:2是每两个像素点共用一对UV分量,每个像素点的Y分量是独立的,UV分量是共享的,所以一幅图像的总的字节数totalBytes = width × height + width * height / 2 + width * height / 2 = width * height * 2;在进行旋转操作是必须满足UYVY的结构不变即4个字节为一组,否则会产生颜色不对、锯齿等,也必须保证Y分量在相应的位置上:
1、旋转90度
bool Rotate90(unsigned char *src, int width, int height)
{
const int copyBytes = 4;
const int bytesPerLine = width << 1;
const int step = height << 2;
const int offset = (height - 1) * bytesPerLine;
unsigned char *dst = new (std::nothrow)BYTE[bytesPerLine * height];
if(NULL == dst)
{
return false;
}
unsigned char * dest = dst;
unsigned char * source = src;
unsigned char * psrc = NULL;
unsigned char * pdst[2] = {NULL, NULL};
for (int i = 0; i < bytesPerLine; i += copyBytes)
{
pdst[0] = dest;
pdst[1] = dest + (height << 1);
psrc = source + offset;
for (int j = 0; j < height; ++j)
{
int k = j % 2;
// 拷贝4个字节
*((unsigned int *)pdst[k]) = *((unsigned int *)psrc);
// Y分量交换,保证每个像素点的亮度不改变否则产生锯齿
if(1 == k)
{
unsigned char temp = *(pdst[0] - 1);
*(pdst[0] - 1) = *(pdst[1] + 1);
*(pdst[1] + 1) = temp;
}
pdst[k] += copyBytes;
psrc -= bytesPerLine;
}
dest += step;
source += copyBytes;
}
memcpy(src, dst, width * height * 2);
delete[] dst;
return true;
}
2、旋转180度
旋转180度相对来说较为简单,从左上角开始与右下角相互交换,由于每两个像素点共享一对UV分量,所以交换的字节数为4字节,但是还需要保证Y分量的位置正确,所以还需要交换Y分量。
bool Rotate180(unsigned char *src, int width, int height)
{
const int copyBytes = 4;
const int totalBytes = height * width << 1;
const int end = totalBytes >> 1;
unsigned char *dst = src + totalBytes - copyBytes;
for (int i = 0; i < end; i += copyBytes)
{
unsigned int tmp = *((unsigned int *)dst);
*((unsigned int *)dst) = *((unsigned int *)src);
*((unsigned int *)src) = tmp;
// 交换Y分量
unsigned char temp = dst[1];
dst[1] = dst[3];
dst[3] = temp;
temp = src[1];
src[1] = src[3];
src[3] = temp;
dst -= copyBytes;
src += copyBytes;
}
return true;
}
3、旋转270度
与旋转90度相类似
bool Rotate270(unsigned char *src, int width, int height)
{
const int copyBytes = 4;
const int bytesPerLine = width << 1;
const int step = height << 2;
const int offset = bytesPerLine - copyBytes;
unsigned char *dst = new (std::nothrow)BYTE[bytesPerLine * height];
if(NULL == dst)
{
return false;
}
unsigned char *dest = dst;
unsigned char *source = src;
unsigned char *psrc = NULL;
unsigned char *pdst[2] = {NULL, NULL};
for (int i = 0; i < bytesPerLine; i += copyBytes)
{
pdst[1] = dest;
pdst[0] = dest + (height << 1);
psrc = source + offset;
for (int j = 0; j < height; ++j)
{
int k = j % 2;
*((unsigned int *)pdst[k]) = *((unsigned int *)psrc);
if(1 == k)
{
unsigned char temp = *(pdst[1] + 1);
*(pdst[1] + 1) = *(pdst[0] - 1);
*(pdst[0] - 1) = temp;
}
pdst[k] += copyBytes;
psrc += bytesPerLine;
}
dest += step;
source -= copyBytes;
}
memcpy(src, dst, width * height * 2);
delete[] dst;
return true;
}