Skip to content

Instantly share code, notes, and snippets.

@spiritedRunning
Last active December 1, 2021 03:30
Show Gist options
  • Save spiritedRunning/0e9fa42eb3c178dee450e94058e2275d to your computer and use it in GitHub Desktop.
Save spiritedRunning/0e9fa42eb3c178dee450e94058e2275d to your computer and use it in GitHub Desktop.
YUV操作工具类
bool UYVYRotate90(unsigned char *src, unsigned char *dst, int width, int height) {
const int copyBytes = 4;
const int bytesPerLine = width << 1;
const int step = height << 2;
const int offset = (height - 1) * bytesPerLine;
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;
}
return true;
}
bool UYVYRotate270(unsigned char *src, unsigned char *dst, 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 = (unsigned char *)malloc(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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment