All Projects → kornelski → mozjpeg-sys

kornelski / mozjpeg-sys

Licence: other
Rust bindings for mozjpeg

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to mozjpeg-sys

yoga-image-optimizer
A graphical tool to convert and optimize JPEG, PNG and WebP images (based on YOGA)
Stars: ✭ 85 (+226.92%)
Mutual labels:  jpeg, mozjpeg
Mozjpeg
Improved JPEG encoder.
Stars: ✭ 4,738 (+18123.08%)
Mutual labels:  mozjpeg, libjpeg
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+2746.15%)
Mutual labels:  mozjpeg
pyguetzli
Python bindings for Google's Guetzli, a JPEG encoder that optimises JPEG compression
Stars: ✭ 28 (+7.69%)
Mutual labels:  jpeg
zImageOptimizer
Simple image optimizer for JPEG, PNG and GIF images on Linux, MacOS and FreeBSD.
Stars: ✭ 108 (+315.38%)
Mutual labels:  jpeg
jpgtxt
Generating jpg files that can be viewed both in image viewer and text editor (as ASCII art)
Stars: ✭ 24 (-7.69%)
Mutual labels:  jpeg
uvc-streamer
MJPEG webcam network streamer for linux
Stars: ✭ 25 (-3.85%)
Mutual labels:  jpeg
jimp-compact
✏️ Lightweight version of Jimp -- An image processing library written entirely in JavaScript for Node.js
Stars: ✭ 55 (+111.54%)
Mutual labels:  jpeg
tiny
compress data for better performance
Stars: ✭ 21 (-19.23%)
Mutual labels:  mozjpeg
MozJpeg-wrapper
mozjpeg wrapper for .NET coded in c#
Stars: ✭ 14 (-46.15%)
Mutual labels:  jpeg
create-optimize-images
♻️ Reusable, scalable, bash scripts to create and optimize images.
Stars: ✭ 39 (+50%)
Mutual labels:  jpeg
exif-rs
Exif parsing library written in pure Rust
Stars: ✭ 91 (+250%)
Mutual labels:  jpeg
whatsapp-jpeg-repair
A handy tool to fix jpeg files downloaded from WhatsApp and prevent errors upon opening these files in Adobe Photoshop.
Stars: ✭ 30 (+15.38%)
Mutual labels:  jpeg
rust-fitsio
FFI wrapper around cfitsio in Rust
Stars: ✭ 17 (-34.62%)
Mutual labels:  ffi-wrapper
go-jpeg-image-structure
Parse JPEG data into segments via code or CLI from pure Go. Read/export/write EXIF data. Read XMP and IPTC metadata.
Stars: ✭ 47 (+80.77%)
Mutual labels:  jpeg
srcset.sh
A command line script that generates multiple responsive versions of an image at width breakpoints -- 320,480,640,768,960,1024,1280,1440 pixels wide -- that match common Mobile and widescreen desktop/laptop viewports using Imagemagick's convert utility and outputs the needed <img/> tag
Stars: ✭ 20 (-23.08%)
Mutual labels:  jpeg
cairo jpg
Reading and writing JPEG files from/to Cairo surfaces.
Stars: ✭ 26 (+0%)
Mutual labels:  jpeg
imageman
Image manipulation library. Use Pixie instead.
Stars: ✭ 58 (+123.08%)
Mutual labels:  libjpeg
img
A python library to display images in the terminal
Stars: ✭ 55 (+111.54%)
Mutual labels:  jpeg
imagor
Fast, Docker-ready image processing server in Go and libvips
Stars: ✭ 2,276 (+8653.85%)
Mutual labels:  jpeg

Low-level MozJPEG bindings for Rust

This crate exposes the raw libjpeg API, so the libjpeg usage manual applies. You'll most likely want to use it via a higher-level API instead :)

Many fields in structs are marked as private by default, but if you need to access them, make a pull request marking them pub.

Requirements

  • build-essentials (gcc, etc.)
  • nasm for Intel CPUs, gas for ARM. Note: Apple's Xcode ships an incredibly outdated version of nasm that won't work.

This crate supports x86, x86-64 and ARM64.

Usage

In Rust/Cargo, add "mozjpeg-sys" as a dependency in Cargo.toml.

In case you need the jpeglib.h header for C code built with Cargo, the required include paths (use env::split_paths()) are set for Cargo build scripts in the DEP_JPEG_INCLUDE env var.

For non-Rust projects you can build the library using Cargo:

cargo build --release

It creates target/release/libmozjpeg_sys.a and target/release/libmozjpeg_sys.{dll,so,dylib}, which can be linked with C and other languages.

By default nasm_simd feature is enabled, and this crate will try to compile SIMD support. Additionally, you can set TARGET_CPU environmental variable (equivalent to -march=$TARGET_CPU) to optimize all of C code for a specific CPU model.

Example

let mut err: jpeg_error_mgr = mem::zeroed();
let mut cinfo: jpeg_decompress_struct = mem::zeroed();
cinfo.common.err = jpeg_std_error(&mut err);
jpeg_create_decompress(&mut cinfo);

let file_name = CString::new(file_name.as_bytes()).unwrap();
let mode = CString::new("rb").unwrap();
let fh = libc::fopen(file_name.as_ptr(), mode.as_ptr());
jpeg_stdio_src(&mut cinfo, fh);
jpeg_read_header(&mut cinfo, true as boolean);

// Available only after `jpeg_read_header()`
let width = cinfo.image_width;
let height = cinfo.image_height;

// Output settings be set before calling `jpeg_start_decompress()`
cinfo.out_color_space = J_COLOR_SPACE::JCS_RGB;
jpeg_start_decompress(&mut cinfo);
let row_stride = cinfo.image_width as usize * cinfo.output_components as usize;
let buffer_size = row_stride * cinfo.image_height as usize;
let mut buffer = vec![0u8; buffer_size];

while cinfo.output_scanline < cinfo.output_height {
    let offset = cinfo.output_scanline as usize * row_stride;
    let mut jsamparray = [buffer[offset..].as_mut_ptr()];
    jpeg_read_scanlines(&mut cinfo, jsamparray.as_mut_ptr(), 1);
}

jpeg_finish_decompress(&mut cinfo);
jpeg_destroy_decompress(&mut cinfo);
libc::fclose(fh);

Writing:

let quality = 98;
let file_name = CString::new("example_result.jpg").unwrap();
let mode = CString::new("wb").unwrap();
let fh = libc::fopen(file_name.as_ptr(), mode.as_ptr());

let mut err = mem::zeroed();
let mut cinfo: jpeg_compress_struct = mem::zeroed();
cinfo.common.err = jpeg_std_error(&mut err);
jpeg_create_compress(&mut cinfo);
jpeg_stdio_dest(&mut cinfo, fh);

cinfo.image_width = width;
cinfo.image_height = height;
cinfo.in_color_space = J_COLOR_SPACE::JCS_RGB;
cinfo.input_components = 3;
jpeg_set_defaults(&mut cinfo);

let row_stride = cinfo.image_width as usize * cinfo.input_components as usize;
cinfo.dct_method = J_DCT_METHOD::JDCT_ISLOW;
jpeg_set_quality(&mut cinfo, quality, true as boolean);

jpeg_start_compress(&mut cinfo, true as boolean);

while cinfo.next_scanline < cinfo.image_height {
    let offset = cinfo.next_scanline as usize * row_stride;
    let jsamparray = [buffer[offset..].as_ptr()];
    jpeg_write_scanlines(&mut cinfo, jsamparray.as_ptr(), 1);
}

jpeg_finish_compress(&mut cinfo);
jpeg_destroy_compress(&mut cinfo);
libc::fclose(fh);
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].