2 个不稳定版本
0.5.0-rc0 | 2024年4月7日 |
---|---|
0.4.0 | 2023年11月16日 |
#785 在 图像
每月483 次下载
用于 2 crate
175KB
3K SLoC
zune-jpegxl
一个简单的 jpeg-xl 编码器
该工具具有以下功能的简单 jpeg-xl 无损编码器:
- 无损编码
- 8 位和 16 位支持
- 灰度模式和 RGB{A} 编码
- 线程能力
用法
首先将最新版本添加到您的 cargo toml 中
使用 cargo add
cargo add zune-jpegxl
或直接添加到您的 Cargo.toml
zune-jpegxl = "0.4"
然后使用 JxlSimpleEncoder
结构体来编码图像
use zune_core::bit_depth::BitDepth;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
// this example won't work
fn main()->Result<(),JxlEncodeErrors> {
let mut encoder = JxlSimpleEncoder::new(&[255,0,255,0], EncoderOptions::new(2,2,co));
encoder.encode().unwrap();
}
lib.rs
:
Zune-jpegxl
支持纯 Rust 编码 jpeg xl 图像
这是一个基于 libjxl crate 的 POC 编码器,用于 JPEG-XL 格式
它支持以下功能:
-
无损压缩
-
最多 16 位深度
-
最多 4 个图像通道
-
不支持的功能 -> 调色板支持
与 png 相比,对于非照片内容,它速度较快但压缩效果稍差,在其他情况下则压缩效果更好。
库也是完全安全的
功能
std
:启用与标准库链接threads
:启用使用标准库的线程功能,此功能需要启用std
功能(no-std 中不存在线程),如果未启用上述功能,则此操作无效果log
:启用使用log
报告编码配置和状态
两个功能默认启用。
16 位数据
- 16 位数据应重新解释为
native endian
中的 2 个 u8
示例
- 编码一个 2x2 8 位图像
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
use zune_jpegxl::JxlEncodeErrors;
// encode a 2x2 image
fn main()->Result<(),JxlEncodeErrors>{
let mut encoder = JxlSimpleEncoder::new(&[0,0,0,0],EncoderOptions::new(2,2,ColorSpace::Luma,BitDepth::Eight));
let mut write_to = vec![];
encoder.encode(&mut write_to)?;
Ok(())
}
- 编码一个 2x2 16 位图像
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
use zune_jpegxl::JxlEncodeErrors;
// encode a 2x2 image
fn main()->Result<(),JxlEncodeErrors>{
// convert a 16 bit input to 8 bit native endian output, each two bytes represent one sample
let sixteen_bit = [0,u16::MAX,0,u16::MAX].iter().flat_map(|x| x.to_ne_bytes()).collect::<Vec<u8>>();
let mut encoder = JxlSimpleEncoder::new(&sixteen_bit,EncoderOptions::new(2,2,ColorSpace::Luma,BitDepth::Sixteen));
let mut write_to = vec![];
encoder.encode(&mut write_to)?;
Ok(())
}