All Projects → ImageOptim → mozjpeg-rust

ImageOptim / mozjpeg-rust

Licence: other
Safe Rust wrapper for the MozJPEG library

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to mozjpeg-rust

Gutenberg Parser Rs
An experimental Rust parser for WordPress Gutenberg post format
Stars: ✭ 76 (+43.4%)
Mutual labels:  rust-bindings
Nix
Rust friendly bindings to *nix APIs
Stars: ✭ 1,660 (+3032.08%)
Mutual labels:  rust-bindings
mongo-rust-driver
Mongo Rust driver built on top of the Mongo C driver
Stars: ✭ 89 (+67.92%)
Mutual labels:  rust-bindings
Lodepng Rust
All-in-one PNG image encoder/decoder in pure Rust
Stars: ✭ 78 (+47.17%)
Mutual labels:  rust-bindings
Llvm Sys.rs
Rust bindings to LLVM. (Mirror of https://gitlab.com/taricorp/llvm-sys.rs/)
Stars: ✭ 93 (+75.47%)
Mutual labels:  rust-bindings
Shaderc Rs
Rust bindings for the shaderc library.
Stars: ✭ 143 (+169.81%)
Mutual labels:  rust-bindings
Rust Rocksdb
rust wrapper for rocksdb
Stars: ✭ 815 (+1437.74%)
Mutual labels:  rust-bindings
ctp-rs
A Rust wrapper of CTP API
Stars: ✭ 74 (+39.62%)
Mutual labels:  rust-bindings
Rust Security Framework
Bindings to the macOS Security.framework
Stars: ✭ 102 (+92.45%)
Mutual labels:  rust-bindings
Midir
Cross-platform realtime MIDI processing in Rust.
Stars: ✭ 221 (+316.98%)
Mutual labels:  rust-bindings
Rust Onig
Rust bindings for the Oniguruma regex library
Stars: ✭ 81 (+52.83%)
Mutual labels:  rust-bindings
Rustkit
Fast and ergonomic Rust bindings for ObjC APIs
Stars: ✭ 89 (+67.92%)
Mutual labels:  rust-bindings
Zstd Rs
A rust binding for the zstd compression library.
Stars: ✭ 159 (+200%)
Mutual labels:  rust-bindings
Rusted Switch
Nintendo Switch Homebrew with Rust 🦀
Stars: ✭ 75 (+41.51%)
Mutual labels:  rust-bindings
voikko-rs
Rust bindings for the Voikko library
Stars: ✭ 16 (-69.81%)
Mutual labels:  rust-bindings
Stripe Rs
Moved to https://github.com/wyyerd/stripe-rs.
Stars: ✭ 16 (-69.81%)
Mutual labels:  rust-bindings
Rust Jack
Decent jack bindings for rust
Stars: ✭ 128 (+141.51%)
Mutual labels:  rust-bindings
blend2d-rs
Blend2D Bindings for Rust
Stars: ✭ 20 (-62.26%)
Mutual labels:  rust-bindings
liboqs-rust
Rust bindings for liboqs
Stars: ✭ 46 (-13.21%)
Mutual labels:  rust-bindings
Mlua
High level Lua 5.4/5.3/5.2/5.1 (including LuaJIT) bindings to Rust with async/await support
Stars: ✭ 176 (+232.08%)
Mutual labels:  rust-bindings

Rust wrapper for MozJPEG library

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.

Decoding example

std::panic::catch_unwind(|| {
    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() {}

    // rgb() enables conversiono
    let image = d.rgb()?;
    image.width();
    image.height();
    image.color_space() == mozjpeg::ColorSpace::JCS_RGB;

    let pixels = image.read_scanlines()?;
    assert!(image.finish_decompress());
    Ok(())
})?;

Encoding example

std::panic::catch_unwind(|| {
    let mut comp = mozjpeg::Compress::new(mozjpeg::ColorSpace::JCS_RGB);

    comp.set_size(width, height);
    comp.set_mem_dest();
    comp.start_compress();

    // replace with your image data
    let pixels = vec![0; width * height * 3];
    assert!(comp.write_scanlines(&pixels[..]));

    comp.finish_compress();
    let jpeg_bytes = comp.data_to_vec()?;
    // write to file, etc.
    Ok(())
})?;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].