• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-2,2341,944

vendor/H03-May-2022-131,53796,964

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

Cargo.lockH A D01-Jan-19709 KiB355314

Cargo.tomlH A D01-Jan-19701.9 KiB6961

Cargo.toml.orig-cargoH A D29-Nov-19731.5 KiB6150

README.mdH A D29-Nov-19734 KiB10777

README.md

1# Low-level [MozJPEG](https://github.com/mozilla/mozjpeg) bindings for [Rust](https://www.rust-lang.org/)
2
3This crate exposes the raw libjpeg API, so [the libjpeg usage manual](https://github.com/mozilla/mozjpeg/blob/master/libjpeg.txt) applies. You'll most likely want to use it via [a higher-level API instead](https://crates.rs/crates/mozjpeg) :)
4
5Many fields in structs are marked as private by default, but if you need to access them, make a pull request marking them `pub`.
6
7## Requirements
8
9* build-essentials (gcc, etc.)
10* [nasm](https://www.nasm.us/) for Intel CPUs, `gas` for ARM. Note: Apple's Xcode ships an incredibly outdated version of nasm that won't work.
11
12This crate supports x86, x86-64 and ARM64.
13
14## Usage
15
16In Rust/Cargo, add "[mozjpeg-sys][crate]" as a dependency in `Cargo.toml`.
17
18[crate]: https://crates.rs/crates/mozjpeg-sys
19
20In case you need the `jpeglib.h` header for C code built with Cargo, the required include path**s** (use [`env::split_paths()`][1]) are set for Cargo [build scripts][2] in the `DEP_JPEG_INCLUDE` env var.
21
22[1]: https://doc.rust-lang.org/std/env/fn.split_paths.html
23[2]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
24
25For non-Rust projects you can build the library using [Cargo](https://rustup.rs/):
26
27```sh
28cargo build --release
29```
30
31It creates `target/release/libmozjpeg_sys.a` and `target/release/libmozjpeg_sys.{dll,so,dylib}`, which can be linked with C and other languages.
32
33By 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.
34
35### [Example](examples/reencode.rs)
36
37```rust
38let mut err: jpeg_error_mgr = mem::zeroed();
39let mut cinfo: jpeg_decompress_struct = mem::zeroed();
40cinfo.common.err = jpeg_std_error(&mut err);
41jpeg_create_decompress(&mut cinfo);
42
43let file_name = CString::new(file_name.as_bytes()).unwrap();
44let mode = CString::new("rb").unwrap();
45let fh = libc::fopen(file_name.as_ptr(), mode.as_ptr());
46jpeg_stdio_src(&mut cinfo, fh);
47jpeg_read_header(&mut cinfo, true as boolean);
48
49// Available only after `jpeg_read_header()`
50let width = cinfo.image_width;
51let height = cinfo.image_height;
52
53// Output settings be set before calling `jpeg_start_decompress()`
54cinfo.out_color_space = J_COLOR_SPACE::JCS_RGB;
55jpeg_start_decompress(&mut cinfo);
56let row_stride = cinfo.image_width as usize * cinfo.output_components as usize;
57let buffer_size = row_stride * cinfo.image_height as usize;
58let mut buffer = vec![0u8; buffer_size];
59
60while cinfo.output_scanline < cinfo.output_height {
61    let offset = cinfo.output_scanline as usize * row_stride;
62    let mut jsamparray = [buffer[offset..].as_mut_ptr()];
63    jpeg_read_scanlines(&mut cinfo, jsamparray.as_mut_ptr(), 1);
64}
65
66jpeg_finish_decompress(&mut cinfo);
67jpeg_destroy_decompress(&mut cinfo);
68libc::fclose(fh);
69```
70
71Writing:
72
73```rust
74let quality = 98;
75let file_name = CString::new("example_result.jpg").unwrap();
76let mode = CString::new("wb").unwrap();
77let fh = libc::fopen(file_name.as_ptr(), mode.as_ptr());
78
79let mut err = mem::zeroed();
80let mut cinfo: jpeg_compress_struct = mem::zeroed();
81cinfo.common.err = jpeg_std_error(&mut err);
82jpeg_create_compress(&mut cinfo);
83jpeg_stdio_dest(&mut cinfo, fh);
84
85cinfo.image_width = width;
86cinfo.image_height = height;
87cinfo.in_color_space = J_COLOR_SPACE::JCS_RGB;
88cinfo.input_components = 3;
89jpeg_set_defaults(&mut cinfo);
90
91let row_stride = cinfo.image_width as usize * cinfo.input_components as usize;
92cinfo.dct_method = J_DCT_METHOD::JDCT_ISLOW;
93jpeg_set_quality(&mut cinfo, quality, true as boolean);
94
95jpeg_start_compress(&mut cinfo, true as boolean);
96
97while cinfo.next_scanline < cinfo.image_height {
98    let offset = cinfo.next_scanline as usize * row_stride;
99    let jsamparray = [buffer[offset..].as_ptr()];
100    jpeg_write_scanlines(&mut cinfo, jsamparray.as_ptr(), 1);
101}
102
103jpeg_finish_compress(&mut cinfo);
104jpeg_destroy_compress(&mut cinfo);
105libc::fclose(fh);
106```
107