2个不稳定版本
0.10.0 | 2023年10月28日 |
---|---|
0.9.0 | 2021年3月6日 |
在图像类别中排名第893
每月下载量67次
在 3 crate中使用
28KB
586 行
外部函数图像处理
此crate提供在Rust中轻松处理和转换图像像素的功能。它旨在与来自外部函数的图像缓冲区一起工作,这些函数可以是C API或将设备缓冲区映射到用户空间的相机驱动程序。
像素表示为数组包装类型,例如,[T; 3]
将是一个Rgb
像素的内类型。此crate提供迭代器扩展,因此您可以轻松地在颜色格式之间进行转换,例如,在Yuv
和Rgb
之间。
打包和分平面API
打包图像的像素在内存中相互堆叠,而分平面图像需要单独的内存平面来存储像素组件。
例如,一个打包RGB图像在内存中的样子如下
R1G1B1 .. RnGnnn(单个内存平面)
而一个分平面YUV420p图像在内存中的样子如下
Y1Y2 .. Yn | U1U2 .. Un | V1V2 .. Vn(三个内存平面)
用法
以下是一个快速使用此crate的示例。它介绍了图像转换所需的基本知识。
use ffimage::color::{Gray, Rgb};
use ffimage::iter::{BytesExt, ColorConvertExt, PixelsExt};
fn main() {
// This is our RGB image memory (2x2 pixels).
// Usually, this will be allocated by a foreign function (e.g. kernel driver) and contain
// read-only memory.
let rgb = [10; 4 * 3];
// We need an output buffer as well to host the converted grayscale pixels.
let mut gray = [0; 4 * 1];
// Convert from rgb to grayscale by mapping each pixel. The Pixels iterator extension creates
// a typed pixel iterator from a bytestream. The ColorConvert extension knows how to convert
// between pixel types and the Write extension finally writes the pixels back into a
// bytestream.
rgb.iter()
.copied()
.pixels::<Rgb<u8>>()
.colorconvert::<Gray<u8>>()
.bytes()
.write(&mut gray);
}
基准测试
此crate中包含基准测试套件。使用以下命令运行它
$ cargo bench
这是我在MacBook Pro 14"(M1 Pro)上对ffimage v0.10.0
的结果
输入 | 输出 | 640x480 | 1280x720 |
---|---|---|---|
Rgb[u8] | Bgr[u8] | 18.028 µs | 53.882 µs |
Rgb[u8] | Gray[u8] | 381.48 µs | 1.1442 ms |
Yuv[u8] | Rgb[u8] | 165.32 µs | 496.33 µs |
Yuv422[u8] | Rgb[u8] | 1.2097 ms | 3.6284 ms |
依赖项
~150KB