MCPcopy Create free account
hub / github.com/RustCV/RustCV / yuyv_to_bgr

Function yuyv_to_bgr

rustcv-camera/src/decode.rs:160–191  ·  view source on GitHub ↗

Convert YUYV (YUV 4:2:2) packed data to BGR24. 将 YUYV(YUV 4:2:2)打包数据转换为 BGR24。 Uses BT.601 standard with integer arithmetic (no floating point). 使用 BT.601 标准的整数运算(无浮点)。 Each 4-byte YUYV macro-pixel `[Y0, U, Y1, V]` produces 2 BGR pixels (6 bytes). 每 4 字节的 YUYV 宏像素 `[Y0, U, Y1, V]` 产生 2 个 BGR 像素(6 字节)。 Integer approximation of the BT.601 conversion: BT.601 转换的整数近似: ```text R = (298*(Y-16) + 409*

(src: &[u8], dst: &mut [u8], width: usize, height: usize)

Source from the content-addressed store, hash-verified

158/// B = (298*(Y-16) + 516*(U-128) + 128) >> 8
159/// ```
160pub fn yuyv_to_bgr(src: &[u8], dst: &mut [u8], width: usize, height: usize) {
161 let pixel_pairs = width * height / 2;
162 let src_needed = pixel_pairs * 4;
163 let dst_needed = pixel_pairs * 6;
164
165 if src.len() < src_needed || dst.len() < dst_needed {
166 return;
167 }
168
169 for i in 0..pixel_pairs {
170 let si = i * 4;
171 let di = i * 6;
172
173 let y0 = src[si] as i32;
174 let u = src[si + 1] as i32 - 128;
175 let y1 = src[si + 2] as i32;
176 let v = src[si + 3] as i32 - 128;
177
178 let c0 = y0 - 16;
179 let c1 = y1 - 16;
180
181 // Pixel 0: BGR
182 dst[di] = clamp((298 * c0 + 516 * u + 128) >> 8); // B
183 dst[di + 1] = clamp((298 * c0 - 100 * u - 208 * v + 128) >> 8); // G
184 dst[di + 2] = clamp((298 * c0 + 409 * v + 128) >> 8); // R
185
186 // Pixel 1: BGR
187 dst[di + 3] = clamp((298 * c1 + 516 * u + 128) >> 8); // B
188 dst[di + 4] = clamp((298 * c1 - 100 * u - 208 * v + 128) >> 8); // G
189 dst[di + 5] = clamp((298 * c1 + 409 * v + 128) >> 8); // R
190 }
191}
192
193// ─── BGRA32 → BGR24 ─────────────────────────────────────────────────────────
194

Callers 3

decode_frameFunction · 0.70
yuyv_to_bgr_basicFunction · 0.70
yuyv_to_bgr_blackFunction · 0.70

Calls 1

clampFunction · 0.70

Tested by 2

yuyv_to_bgr_basicFunction · 0.56
yuyv_to_bgr_blackFunction · 0.56