Crates.io | mozjpeg |
lib.rs | mozjpeg |
version | |
source | src |
created_at | 2018-04-04 01:59:36.729628 |
updated_at | 2024-11-29 20:17:47.382647 |
description | Higher-level wrapper for Mozilla's JPEG library |
homepage | https://lib.rs/mozjpeg |
repository | https://github.com/ImageOptim/mozjpeg-rust |
max_upload_size | |
id | 58883 |
Cargo.toml error: | TOML parse error at line 25, column 1 | 25 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This library adds a safe(r) interface on top of libjpeg-turbo and MozJPEG for reading and writing well-compressed JPEG images.
The interface is still being developed, so it has rough edges and may change.
In particular, error handling is weird due to libjpeg's peculiar design. Error handling can't use Result
, but needs to depend on Rust's resume_unwind
(a panic, basically) to signal any errors in libjpeg. It's necessary to wrap all uses of this library in catch_unwind
.
In crates compiled with panic=abort
setting, any JPEG error will abort the process.
std::panic::catch_unwind(|| -> std::io::Result<Vec<rgb::Rgb::<u8>>> {
let d = mozjpeg::Decompress::with_markers(mozjpeg::ALL_MARKERS)
.from_path("tests/test.jpg")?;
d.width(); // FYI
d.height();
d.color_space() == mozjpeg::ColorSpace::JCS_YCbCr;
for marker in d.markers() { /* read metadata or color profiles */ }
// rgb() enables conversion
let mut image = d.rgb()?;
image.width();
image.height();
image.color_space() == mozjpeg::ColorSpace::JCS_RGB;
let pixels = image.read_scanlines()?;
image.finish()?;
Ok(pixels)
});
# let width = 8; let height = 8;
std::panic::catch_unwind(|| -> std::io::Result<Vec<u8>> {
let mut comp = mozjpeg::Compress::new(mozjpeg::ColorSpace::JCS_RGB);
comp.set_size(width, height);
let mut comp = comp.start_compress(Vec::new())?; // any io::Write will work
// replace with your image data
let pixels = vec![0u8; width * height * 3];
comp.write_scanlines(&pixels[..])?;
let writer = comp.finish()?;
Ok(writer)
});