/**
* 简单的颜色空间转换
* YUV Transformation
*
* 梁启东 qidong.liang
* 18088708700@163.com
* https://blog.csdn.net/u011645307
*
*
* 本程序实现了简单的YUV数据之间的转换和YUV与RGB的转换。
* This program realizes the simple conversion between
* YUV data and the conversion between YUV and RGB.
*
*/
#include <iostream>
#define YV12_To_I420 0
#define I420_To_YV12 0
#define NV12_To_I420 0
#define I420_To_NV12 0
#define NV21_To_YV12 0
#define YV12_To_NV21 0
#define I420_To_RGB32 0
#define RGB32_To_I420 0
#define I420_To_RGB24 0
#define RGB24_To_I420 1
int YV12_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int I420_2_YV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int NV12_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int I420_2_NV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int NV21_2_YV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int YV12_2_NV21(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int I420_2_rgb32(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int rgb32_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int I420_2_rgb24(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
int rgb24_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height);
typedef struct RGB32 {
uint8_t rgbBlue; // 蓝色分量
uint8_t rgbGreen; // 绿色分量
uint8_t rgbRed; // 红色分量
uint8_t rgbReserved; // 保留字节(用作Alpha通道或忽略)
RGB32()
{
rgbBlue = 0;
rgbGreen = 0;
rgbRed = 0;
rgbReserved = 0;
}
} RGB32;
typedef struct RGB24 {
uint8_t rgbBlue; // 蓝色分量
uint8_t rgbGreen; // 绿色分量
uint8_t rgbRed; // 红色分量
RGB24()
{
rgbBlue = 0;
rgbGreen = 0;
rgbRed = 0;
}
} RGB24;
int main()
{
FILE* file_in = nullptr;
FILE* file_out = nullptr;
char* input_name = nullptr;
char* output_name = nullptr;
int w = 0, h = 0;
float flotScale = 0;
int out_w = 0, out_h = 0;
float out_flotScale = 0;
int (*pfun)(uint8_t * src, uint8_t * dst, uint32_t width, uint32_t height);
pfun = nullptr;
#if YV12_To_I420
input_name = const_cast<char*>("yv12_480x272.yuv");
output_name = const_cast<char*>("out/i420_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = YV12_2_I420;
#endif
#if I420_To_YV12
input_name = const_cast<char*>("I420_480x272.yuv");
output_name = const_cast<char*>("out/yv12_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = I420_2_YV12;
#endif
#if NV12_To_I420
input_name = const_cast<char*>("nv12_480x272.yuv");
output_name = const_cast<char*>("out/I420_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = NV12_2_I420;
#endif
#if I420_To_NV12
input_name = const_cast<char*>("I420_480x272.yuv");
output_name = const_cast<char*>("out/nv12_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = I420_2_NV12;
#endif
#if NV21_To_YV12
input_name = const_cast<char*>("nv21_480x272.yuv");
output_name = const_cast<char*>("out/yv12_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = NV21_2_YV12;
#endif
#if YV12_To_NV21
input_name = const_cast<char*>("yv12_480x272.yuv");
output_name = const_cast<char*>("out/nv21_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = YV12_2_NV21;
#endif
#if I420_To_RGB32
input_name = const_cast<char*>("I420_480x272.yuv");
output_name = const_cast<char*>("out/rgb32_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 4;
pfun = I420_2_rgb32;
#endif
#if RGB32_To_I420
input_name = const_cast<char*>("rgb32_480x272.yuv");
output_name = const_cast<char*>("out/i420_480x272.yuv");
w = 480;
h = 272;
flotScale = 4;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = rgb32_2_I420;
#endif
#if I420_To_RGB24
input_name = const_cast<char*>("I420_480x272.yuv");
output_name = const_cast<char*>("out/rgb24_480x272.yuv");
w = 480;
h = 272;
flotScale = 1.5;
out_w = 480;
out_h = 272;
out_flotScale = 3;
pfun = I420_2_rgb24;
#endif
#if RGB24_To_I420
input_name = const_cast<char*>("rgb24_480x272.yuv");
output_name = const_cast<char*>("out/i420_480x272.yuv");
w = 480;
h = 272;
flotScale = 3;
out_w = 480;
out_h = 272;
out_flotScale = 1.5;
pfun = rgb24_2_I420;
#endif
int in_buff_len = w * h * flotScale;
int out_buff_len = out_w * out_h * out_flotScale;
char* inbuff = new char[in_buff_len];
char* outbuff = new char[out_buff_len];
fopen_s(&file_in, input_name, "rb+");
fopen_s(&file_out, output_name, "wb+");
while (true)
{
if (fread(inbuff, 1, in_buff_len, file_in) != in_buff_len)
{
break;
}
pfun((uint8_t*)inbuff, (uint8_t*)outbuff, w,h);
fwrite(outbuff, 1,out_buff_len, file_out);
}
fclose(file_in);
fclose(file_out);
file_in = nullptr;
file_out = nullptr;
std::cout << "Hello World!\n";
return 0;
}
int YV12_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height)
{
uint8_t* src_y = src;
uint8_t* src_u = src + width * height;
uint8_t* src_v = src + width * height * 5 / 4;
uint8_t* dst_y = dst;
uint8_t* dst_v = dst + width * height;
uint8_t* dst_u = dst + width * height * 5 / 4;
memcpy(dst_y, src_y, width * height);
memcpy(dst_v, src_v, width * height / 4);
memcpy(dst_u, src_u, width * height / 4);
return 0;
}
int I420_2_YV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height)
{
uint8_t* src_y = src;
uint8_t* src_u = src + width* height;
uint8_t* src_v = src + width * height*5/4;
uint8_t* dst_y = dst;
uint8_t* dst_v = dst + width * height;
uint8_t* dst_u = dst + width * height * 5 / 4;
memcpy(dst_y, src_y, width * height);
memcpy(dst_v, src_v, width * height/4);
memcpy(dst_u, src_u, width * height/4);
return 0;
}
int NV12_2_I420(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height)
{
uint8_t* src_y = src;
uint8_t* src_uv = src + width * height;
uint8_t* dst_y = dst;
uint8_t* dst_u = dst + width * height;
uint8_t* dst_v = dst + width * height * 5 / 4;
memcpy(dst_y, src_y, width * height);
for (int i = 0; i < width/2 * height / 2; i++)
{
dst_u[i] = src_uv[2 * i];
dst_v[i] = src_uv[2 * i + 1];
}
return 0;
}
int I420_2_NV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height)
{
uint8_t* src_y = src;
uint8_t* src_u = src + width * height;
uint8_t* src_v = src + width * height * 5 / 4;
uint8_t* dst_y = dst;
uint8_t* dst_uv = dst + width * height;
memcpy(dst_y, src_y, width * height);
for (int i = 0; i < width / 2 * height / 2; i++)
{
dst_uv[2*i] = src_u[i];
dst_uv[2 * i+1] = src_v[i];
}
return 0;
}
int NV21_2_YV12(uint8_t* src, uint8_t* dst, uint32_t width, uint32_t height)
{
uint8_t* src_y = src;
uint8_t* src_uv = src + width * height;
uint8_t* dst_y = dst;
uint8_t* dst_v = dst + width * height;
uint8_t* dst_u = dst + width * height * 5 / 4;
memcpy(dst_y, src_y, width * height);
for (int i = 0; i < width / 2 * height / 2; i++)
{
dst_v[i] = src_uv[2 * i];
dst_u[i] = src_uv[2 * i + 1];
}
return 0;
}
int YV12_2_NV21(uint8_t* src, uint8_t* dst, uint32_t width, uint3
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本程序实现了简单的YUV数据之间的转换和YUV与RGB的转换。 注:1、本程序只是简单YUV之间的颜色空间转换和YUV与RGB的转换。 2、本程序使用的vs2019创建的,需使用2019打开工程。 3、包含功能: 1)YV12_2_I420; 2)I420_2_YV12; 3)NV12_2_I420; 4)I420_2_NV12; 5)NV21_2_YV12; 6)YV12_2_NV21; 7)I420_2_rgb32; 8)rgb32_2_I420; 9)I420_2_rgb24; 10)rgb24_2_I420。
资源推荐
资源详情
资源评论
收起资源包目录
YUV_Transformation.7z (16个子文件)
YUV_Transformation
.vs
YUV_Transformation
v16
Browse.VC.db 6.07MB
.suo 32KB
ipch
AutoPCH
ffbf01bb6aaded64
YUV_TRANSFORMATION.ipch 33.63MB
v15
Browse.VC.db 3.5MB
.suo 21KB
ipch
AutoPCH
9cc67c800ad227eb
YUV_TRANSFORMATION.ipch 24.81MB
YUV_Transformation.vcxproj 7KB
YUV_Transformation.cpp 13KB
rgb32_480x272.yuv 124.01MB
I420_480x272.yuv 46.51MB
nv12_480x272.yuv 46.51MB
YUV_Transformation.vcxproj.user 168B
out
rgb24_480x272.yuv 93.01MB
YUV_Transformation.sln 1KB
YUV_Transformation.vcxproj.filters 980B
yv12_480x272.yuv 46.51MB
共 16 条
- 1
资源评论
- 张盛锋2023-07-26我觉得这个文件对于初学者来说有点难度,但是读者只要耐心阅读,一定会有收获的。
- 小小二-yan2023-07-26这个文件对于理解YUV颜色空间转换非常有帮助。
- daidaiyijiu2023-07-26文件的结构清晰,内容详细,不仅介绍了转换的原理,还给出了具体的使用方法,非常有指导性。
- SLHJ-Translator2023-07-26非常实用的文件,里面包含了许多实例和代码,帮助读者更好地理解转换的过程。
- 一曲歌长安2023-07-26文件将YV12、NV12、NV21、I420、RGB等多种颜色空间进行了清晰的解释。
Dreamt灬
- 粉丝: 9
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功