1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
31 * YUV colorspace conversion.
33 * @author Brian Paul <brianp@vmware.com>
34 * @author Michal Krol <michal@vmware.com>
35 * @author Jose Fonseca <jfonseca@vmware.com>
38 * - http://www.fourcc.org/fccyvrgb.php
39 * - http://msdn.microsoft.com/en-us/library/ms893078
40 * - http://en.wikipedia.org/wiki/YUV
44 #ifndef U_FORMAT_YUV_H_
45 #define U_FORMAT_YUV_H_
48 #include "pipe/p_compiler.h"
53 * TODO: Ensure we use consistent and right floating formulas, with enough
54 * precision in the coefficients.
58 util_format_rgb_float_to_yuv(float r
, float g
, float b
,
59 uint8_t *y
, uint8_t *u
, uint8_t *v
)
61 const float _r
= CLAMP(r
, 0.0f
, 1.0f
);
62 const float _g
= CLAMP(g
, 0.0f
, 1.0f
);
63 const float _b
= CLAMP(b
, 0.0f
, 1.0f
);
65 const float scale
= 255.0f
;
67 const int _y
= scale
* ( (0.257f
* _r
) + (0.504f
* _g
) + (0.098f
* _b
));
68 const int _u
= scale
* (-(0.148f
* _r
) - (0.291f
* _g
) + (0.439f
* _b
));
69 const int _v
= scale
* ( (0.439f
* _r
) - (0.368f
* _g
) - (0.071f
* _b
));
78 util_format_yuv_to_rgb_float(uint8_t y
, uint8_t u
, uint8_t v
,
79 float *r
, float *g
, float *b
)
81 const int _y
= y
- 16;
82 const int _u
= u
- 128;
83 const int _v
= v
- 128;
85 const float y_factor
= 255.0f
/ 219.0f
;
87 const float scale
= 1.0f
/ 255.0f
;
89 *r
= scale
* (y_factor
* _y
+ 1.596f
* _v
);
90 *g
= scale
* (y_factor
* _y
- 0.391f
* _u
- 0.813f
* _v
);
91 *b
= scale
* (y_factor
* _y
+ 2.018f
* _u
);
96 util_format_rgb_8unorm_to_yuv(uint8_t r
, uint8_t g
, uint8_t b
,
97 uint8_t *y
, uint8_t *u
, uint8_t *v
)
99 *y
= (( 66 * r
+ 129 * g
+ 25 * b
+ 128) >> 8) + 16;
100 *u
= (( -38 * r
- 74 * g
+ 112 * b
+ 128) >> 8) + 128;
101 *v
= (( 112 * r
- 94 * g
- 18 * b
+ 128) >> 8) + 128;
106 util_format_yuv_to_rgb_8unorm(uint8_t y
, uint8_t u
, uint8_t v
,
107 uint8_t *r
, uint8_t *g
, uint8_t *b
)
109 const int _y
= y
- 16;
110 const int _u
= u
- 128;
111 const int _v
= v
- 128;
113 const int _r
= (298 * _y
+ 409 * _v
+ 128) >> 8;
114 const int _g
= (298 * _y
- 100 * _u
- 208 * _v
+ 128) >> 8;
115 const int _b
= (298 * _y
+ 516 * _u
+ 128) >> 8;
117 *r
= CLAMP(_r
, 0, 255);
118 *g
= CLAMP(_g
, 0, 255);
119 *b
= CLAMP(_b
, 0, 255);
125 util_format_uyvy_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
126 const uint8_t *src_row
, unsigned src_stride
,
127 unsigned width
, unsigned height
);
130 util_format_uyvy_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
131 const uint8_t *src_row
, unsigned src_stride
,
132 unsigned width
, unsigned height
);
135 util_format_uyvy_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
136 const float *src_row
, unsigned src_stride
,
137 unsigned width
, unsigned height
);
140 util_format_uyvy_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
141 const uint8_t *src_row
, unsigned src_stride
,
142 unsigned width
, unsigned height
);
145 util_format_uyvy_fetch_rgba_float(float *dst
, const uint8_t *src
,
146 unsigned i
, unsigned j
);
149 util_format_yuyv_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
150 const uint8_t *src_row
, unsigned src_stride
,
151 unsigned width
, unsigned height
);
154 util_format_yuyv_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
155 const uint8_t *src_row
, unsigned src_stride
,
156 unsigned width
, unsigned height
);
159 util_format_yuyv_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
160 const float *src_row
, unsigned src_stride
,
161 unsigned width
, unsigned height
);
164 util_format_yuyv_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
165 const uint8_t *src_row
, unsigned src_stride
,
166 unsigned width
, unsigned height
);
169 util_format_yuyv_fetch_rgba_float(float *dst
, const uint8_t *src
,
170 unsigned i
, unsigned j
);
174 util_format_r8g8_b8g8_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
175 const uint8_t *src_row
, unsigned src_stride
,
176 unsigned width
, unsigned height
);
179 util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
180 const uint8_t *src_row
, unsigned src_stride
,
181 unsigned width
, unsigned height
);
184 util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
185 const float *src_row
, unsigned src_stride
,
186 unsigned width
, unsigned height
);
189 util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
190 const uint8_t *src_row
, unsigned src_stride
,
191 unsigned width
, unsigned height
);
194 util_format_r8g8_b8g8_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
195 unsigned i
, unsigned j
);
198 util_format_g8r8_g8b8_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
199 const uint8_t *src_row
, unsigned src_stride
,
200 unsigned width
, unsigned height
);
203 util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
204 const uint8_t *src_row
, unsigned src_stride
,
205 unsigned width
, unsigned height
);
208 util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
209 const float *src_row
, unsigned src_stride
,
210 unsigned width
, unsigned height
);
213 util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
214 const uint8_t *src_row
, unsigned src_stride
,
215 unsigned width
, unsigned height
);
218 util_format_g8r8_g8b8_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
219 unsigned i
, unsigned j
);
223 #endif /* U_FORMAT_YUV_H_ */