-
Notifications
You must be signed in to change notification settings - Fork 117
/
yuv2rgb.h
204 lines (181 loc) · 6.75 KB
/
yuv2rgb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @file yuv2rgb.h
* @author Late Lee
* @date 2012.01.20
*
* @brief
* YUV422P/YUV420P/YUV422SP转RGB24实现代码
*
* @note
* 1、在Windows编译测试
* 2、代码中使用的YUV420P、YUV422P是平面(planar)格式,不是打包(packed)格式
*
* 3、qcif: 176*144
cif: 352*288
* @todo 确定好RGB排序到底是什么
*
* @log 2013-10-26 参考422p函数,实现422sp转换422p格式函数。将初始化接口隐藏,不对外公开
* 2014-02-10 封装统一一个函数。
* 2014-08-03 更新YUV转RGB函数。添加UYVY格式接口。
* 2014-08-04 完成422打包格式的接口。4种格式测试通过。 完成YUV444格式。 SP、P互转接口。
* 2014-08-05 完成422SP、420SP格式的转换。打包格式使用查表法。
* 2014-08-06 Y转换RGB
*
* 笔记:
每个Y、U、V、R、G、B均占用1个字节
YUV422P平面格式
Y、U、V分开存储,Y占w*h,U、V分别占w*h/2。每行Y有w个,U、V各w/2个。
两个Y共用一个U和V,两个像素有Y分量2个,U、V分量各一个,共4字节,因此一个像素占2字节。
内存分布如下:
w
+--------------------+
|Y0Y1Y2Y3... |
|... | h
|... |
| |
+--------------------+
|U0U1 |
|... | h
|... |
| |
+----------+
|V0V1 |
|... | h
|... |
| |
+----------+
w/2
第一个像素:Y0 U0 V0
第二个像素:Y1 U0 V0
第三个像素:Y2 U1 V1
第四个像素:Y3 U1 V1
每种格式分布见转换函数处
*/
#ifndef _YUV2RGB_H_
#define _YUV2RGB_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
FMT_Y = 0,
FMT_YUV420 = 1,
FMT_YV12 = 2,
FMT_YUV422 = 3,
FMT_YV16 = 4,
FMT_YUV444 = 5,
FMT_YUYV = 6,
FMT_YVYU = 7,
FMT_UYVY = 8,
FMT_VYUY = 9,
FMT_NV12 = 10,
FMT_NV21 = 11,
FMT_NV16 = 12,
FMT_NV61 = 13,
FMT_RGB24 = 14,
FMT_BGR24 = 15,
}YUV_TYPE;
/**
* @brief YUV转RGB24(查表法)
*
* @param type YUV格式类型 支持格式见YUV_TYPE定义
* @param yuvbuffer YUV格式缓冲区
* @param rgbbuffer RGB24格式缓冲区
* @param width 图像宽
* @param height 图像高
*
* @return 0: OK -1: failed
*
* @note
* 1、YUV422 buffer: w * h * 2 YUV420 buffer: w * h * 3 / 2
* 2、rgbbuffer数据排序为RGB,如保存BMP,需要调整为BGR
* 3、总转换入口函数
*/
int yuv_to_rgb24(YUV_TYPE type, unsigned char* yuvbuffer,unsigned char* rgbbuffer, int width, int height);
/**
* @brief YUV422P转换为RGB24(查表法)
*
* @param type[IN] YUV格式类型
* @param yuvbuffer[IN] YUV422P平面格式缓冲区
* @param rgbbuffer[OUT] RGB缓冲区
* @param width[IN] 图像宽
* @param height[IN] 图像高
*
* @return no
*
* @note
* 1.YUV422P格式YUV缓冲区大小为w * h * 2
* 2.rgbbuffer数据排序为RGB,如保存BMP,需要调整为BGR
*/
void yuv422p_to_rgb24(YUV_TYPE type, unsigned char* yuvbuffer, unsigned char* rgbbuffer, int width, int height);
/**
* @brief YUV422P打包格式转换为RGB24(查表法)
*
* @param type[IN] YUV格式类型
* @param yuvbuffer[IN] YUV422P打包格式缓冲区
* @param rgbbuffer[OUT] RGB缓冲区
* @param width[IN] 图像宽
* @param height[IN] 图像高
*
* @return no
*
* @note
* 1.YUV422P格式YUV缓冲区大小为w * h * 2,支持YUYV、YVYU、UYVY、VYUY
* 2.rgbbuffer数据排序为RGB,如保存BMP,需要调整为BGR
*/
void yuv422packed_to_rgb24(YUV_TYPE type, unsigned char *yuv, unsigned char *rgb, int width, int height);
/**
* @brief YUV422SP转RGB24(查表法)
*
* @param type[IN] YUV格式类型
* @param yuvbuffer YUV422SP格式缓冲区
* @param rgbbuffer RGB24格式缓冲区
* @param width 图像宽
* @param height 图像高
*
*
* @note
* 1、YUV422SP格式YUV缓冲区大小为w * h * 2,支持NV16、NV61
* 2、rgbbuffer数据排序为RGB,如保存BMP,需要调整为BGR
*/
void yuv422sp_to_rgb24(YUV_TYPE type, unsigned char* yuvbuffer, unsigned char* rgbbuffer, int width, int height);
/**
* @brief YUV420P转RGB24(查表法)
*
* @param type[IN] YUV格式类型
* @param yuvbuffer YUV420P格式缓冲区
* @param rgbbuffer RGB24格式缓冲区
* @param width 图像宽
* @param height 图像高
*
*
* @note
* 1、YUV420P格式YUV缓冲区大小为w * h * 3 / 2
* 2、rgbbuffer数据排序为RGB,如保存BMP,需要调整为BGR
*/
void yuv420p_to_rgb24(YUV_TYPE type, unsigned char* yuvbuffer,unsigned char* rgbbuffer, int width, int height);
void yuv420sp_to_rgb24(YUV_TYPE type, unsigned char* yuvbuffer,unsigned char* rgbbuffer, int width,int height) ;
//void yuv420sp_to_rgb24(YUV_TYPE type, unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height);
////////////////////////////////////////////////////////////////////////////////////////////////////
void yuv422p_to_yuv422sp(YUV_TYPE type, unsigned char* yuv422p, unsigned char* yuv422sp, int width, int height);
void yuv422sp_to_yuv422p(YUV_TYPE type, unsigned char* yuv422sp, unsigned char* yuv422p, int width, int height);
void yuv420p_to_yuv420sp(YUV_TYPE type, unsigned char* yuv420p, unsigned char* yuv420sp, int width, int height);
void yuv420sp_to_yuv420p(YUV_TYPE type, unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height);
void yuv422p_to_yuv422packed(YUV_TYPE type1, YUV_TYPE type2, unsigned char* yuv422p, unsigned char* yuv, int width, int height);
void yuv422packed_to_yuv422p(YUV_TYPE type1, YUV_TYPE type2, unsigned char* yuv422p, unsigned char* yuv, int width, int height);
void yu_to_yv(YUV_TYPE type, unsigned char* yu, unsigned char* yv, int width, int height);
void yv_to_yu(YUV_TYPE type, unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height);
////////////////////////////////////////////////////////////////////////////////////////////////////
void swaprgb(unsigned char* rgb, int len);
////////////////////////////////////////////////////////////////////////////////////////////////////
void yuv420_to_rgb24_1(unsigned char* yuv420, unsigned char* rgb, int width, int height);
void yuv420_to_rgb24_2(unsigned char *yuv420, unsigned char *rgb24, int width, int height) ;
void yuv420_to_rgb24_3(unsigned char* yuv, unsigned char* rgb, int width, int height);
void yuv4444_to_rgb24(unsigned char *yuv, unsigned char *rgb, int width, int height);
// 高最好是10的整数倍
void save_yuv_file(const char* filename, int width, int height, int type);
void change_yuv_file(const char* filename, const char* file_out, int width, int height, int type);
#ifdef __cplusplus
}
#endif
#endif /* _YUV2RGB_H_ */