C语言实现YUV420图像旋转90度
以下是一个用C语言实现旋转90度的示例代码,其中假设原始图像是YUV420格式,宽为width,高为height:
void yuv420_rotate_90(unsigned char *src, unsigned char *dst, int width, int height) {
int i, j, k;
// 旋转后的图像宽高互换
int dst_width = height;
int dst_height = width;
// Y分量旋转
for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
dst[j * dst_width + (height - 1 - i)] = src[i * width + j];
}
}
// U、V分量旋转
int src_uv_offset = width * height;
int dst_uv_offset = dst_width * dst_height;
for (k = 0; k < 2; ++k) { // 循环处理U、V分量
for (i = 0; i < height / 2; ++i) {
for (j = 0; j < width / 2; ++j) {
dst[dst_uv_offset + j * dst_width / 2 + (height / 2 - 1 - i)] = src[src_uv_offset + i * width / 2 + j];
}
}
src_uv_offset += width * height / 4; // U、V分量偏移
dst_uv_offset += dst_width * dst_height / 4;
}
}
该代码将原始图像旋转90度,结果保存在dst指向的内存中。函数中使用了三重循环,第一重循环处理Y分量,第二重循环处理U分量,第三重循环处理V分量。在处理U、V分量时,需要注意偏移量的计算。
原文地址: https://www.cveoy.top/t/topic/lENl 著作权归作者所有。请勿转载和采集!