(src: &[u8], mat: &mut Mat)
| 91 | /// 将 MJPEG 数据解码为 BGR。 |
| 92 | #[cfg(feature = "turbojpeg")] |
| 93 | fn decode_mjpeg(src: &[u8], mat: &mut Mat) -> Result<()> { |
| 94 | use turbojpeg::{Decompressor, Image, PixelFormat as TJPixelFormat}; |
| 95 | |
| 96 | // Create a decompressor. In a real hot loop, this should be reused |
| 97 | // via the Camera struct. Here we create one as fallback. |
| 98 | // 创建解压器。在实际的热循环中,应通过 Camera 结构体复用。 |
| 99 | // 这里作为回退创建一个。 |
| 100 | let mut decompressor = Decompressor::new() |
| 101 | .map_err(|e| CameraError::DecodeError(format!("turbojpeg init: {}", e)))?; |
| 102 | |
| 103 | let header = decompressor |
| 104 | .read_header(src) |
| 105 | .map_err(|e| CameraError::DecodeError(format!("JPEG header: {}", e)))?; |
| 106 | |
| 107 | // Extract step before mutable borrow of mat.data to satisfy the borrow checker. |
| 108 | // 在可变借用 mat.data 之前提取 step,以满足借用检查器。 |
| 109 | let pitch = mat.step(); |
| 110 | let image = Image { |
| 111 | pixels: mat.data_mut(), |
| 112 | width: header.width, |
| 113 | pitch, |
| 114 | height: header.height, |
| 115 | format: TJPixelFormat::BGR, |
| 116 | }; |
| 117 | |
| 118 | decompressor |
| 119 | .decompress(src, image) |
| 120 | .map_err(|e| CameraError::DecodeError(format!("JPEG decompress: {}", e)))?; |
| 121 | |
| 122 | Ok(()) |
| 123 | } |
| 124 | |
| 125 | /// Fallback MJPEG decode using the `image` crate (pure Rust, slower). |
| 126 | /// 使用 `image` crate 的回退 MJPEG 解码(纯 Rust,较慢)。 |
no test coverage detected