MCPcopy Index your code
hub / github.com/ImageOptim/mozjpeg-rust

github.com/ImageOptim/mozjpeg-rust @v0.10.13

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.10.13 ↗ · + Follow
193 symbols 515 edges 13 files 41 documented · 21%

Browse by type

Functions 172 Types & classes 21
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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(|| -> 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)
});

Encoding example

# 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)
});

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 131
Function 41
Class 14
Enum 5
Interface 2

Languages

Rust100%

Modules by API surface

src/decompress.rs74 symbols
src/compress.rs36 symbols
src/readsrc.rs19 symbols
src/writedst.rs17 symbols
tests/tests.rs10 symbols
src/qtable.rs8 symbols
src/component.rs7 symbols
tests/decode.rs5 symbols
src/errormgr.rs5 symbols
src/lib.rs4 symbols
src/density.rs3 symbols
src/colorspace.rs3 symbols

For agents

$ claude mcp add mozjpeg-rust \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page