libcaesium/README.md

209 lines
8.4 KiB
Markdown
Raw Normal View History

2021-10-24 11:57:05 +02:00
# libcaesium [![Rust](https://github.com/Lymphatus/libcaesium/actions/workflows/rust.yml/badge.svg)](https://github.com/Lymphatus/libcaesium/actions/workflows/rust.yml)
2016-11-14 23:43:52 +01:00
2021-10-23 17:18:52 +02:00
Libcaesium is a simple library performing JPEG, PNG, WebP and GIF (experimental) compression/optimization written in Rust, with a C interface.\
**IMPORTANT**: starting from v0.6.0 the library is written in Rust and no longer in C. There's a C interface, but it's not backward compatible with the <0.6.0.
## Usage in Rust
2023-05-03 21:39:18 +02:00
Libcaesium exposes two functions, auto-detecting the input file type
### Based on quality values
2021-10-23 17:18:52 +02:00
```Rust
pub fn compress(
input_path: String,
output_path: String,
2022-11-14 23:00:14 +01:00
parameters: &CSParameters
2021-10-23 17:18:52 +02:00
) -> Result<(), Box<dyn Error>>
2016-11-14 23:43:52 +01:00
```
#### Parameters
2021-10-23 17:18:52 +02:00
- `input_path` - input file path (full filename)
- `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below)
2017-02-21 12:54:52 +01:00
2023-05-03 21:39:18 +02:00
### Based on output size
```Rust
pub fn compress_to_size(
input_path: String,
output_path: String,
parameters: &CSParameters,
max_output_size: usize,
2023-05-03 21:39:18 +02:00
) -> Result<(), Box<dyn Error>>
```
#### Parameters
- `input_path` - input file path (full filename)
- `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below)
- `max_output_size` - the maximum output size, in bytes
2023-05-03 21:39:18 +02:00
This function will attempt to compress the given file *below* the desired size. It will never exceed it. The function
will start looping until the best size under the desired is achieved. The function has a 2% tolerance for the output size.
All quality value set to the parameters will be ignored and overwritten during the compression.
2022-04-11 09:58:52 +02:00
NOTE: The output folder where the file is compressed **must** exist.
2021-10-23 17:18:52 +02:00
### Compression options
Libcaesium supports a few compression parameters for each file it supports.
2016-11-14 23:43:52 +01:00
They are defined into a top level struct containing each supported file parameters, as follows:
2021-10-23 17:18:52 +02:00
```Rust
pub struct CSParameters {
pub jpeg: jpeg::Parameters,
pub png: png::Parameters,
pub gif: gif::Parameters,
pub webp: webp::Parameters,
pub tiff: tiff::Parameters,
2021-10-23 17:18:52 +02:00
pub keep_metadata: bool,
pub optimize: bool,
2022-02-12 14:10:12 +01:00
pub width: u32,
pub height: u32,
2021-10-23 17:18:52 +02:00
}
2016-11-14 23:43:52 +01:00
```
2021-10-23 17:18:52 +02:00
Each file type has its own options, but the last two are generic:
- `keep_metadata`: will keep metadata information for any supported type. JPEG and PNG supported. Default `false`.
- `optimize`: forces optimization, when available. With this option enabled the compression will be lossless. JPEG, PNG and WebP supported. Default `false`.
2022-02-12 14:10:12 +01:00
- `width`: Resizes the image to the given width. If this value is `0` and the height value is also `0`, no resizing will be done. If this is `0` and height is `> 0`, the image will be scaled based on height keeping the aspect ratio. Default `0`.
- `height`: Resizes the image to the given height. If this value is `0` and the width value is also `0`, no resizing will be done. If this is `0` and width is `> 0`, the image will be scaled based on width keeping the aspect ratio. Default `0`.
2021-10-23 17:18:52 +02:00
#### jpeg
```Rust
pub struct Parameters {
pub quality: u32,
2023-06-24 16:01:21 +02:00
pub chroma_subsampling: jpeg::ChromaSubsampling
2021-10-23 17:18:52 +02:00
}
2016-11-14 23:43:52 +01:00
```
2021-10-23 17:18:52 +02:00
- `quality`: in a range from 1 to 100, the quality of the resulting image. Default `80`.
2023-06-24 16:01:21 +02:00
- `chroma_subsampling`: [chroma subsampling](https://en.wikipedia.org/wiki/Chroma_subsampling) to apply during compression. Default `Auto`.
2021-10-23 17:18:52 +02:00
#### png
```Rust
pub struct Parameters {
2022-07-29 12:19:00 +02:00
pub quality: u32,
2021-10-23 17:18:52 +02:00
pub force_zopfli: bool
}
```
2022-07-31 11:24:07 +02:00
- `quality`: in a range from 0 to 100, the quality of the resulting image. Default `80`.
2022-04-11 09:58:52 +02:00
- `force_zopfli`: if `optimization` is `true` and this option is also `true`, will use zopfli algorithm for compression, resulting in a smaller image, but it may take minutes to finish the process. Default `false`.
2021-10-23 17:18:52 +02:00
#### gif
GIF support is experimental, has many know issues and does not support optimization. Expect bugs (especially on Windows).
```Rust
pub struct Parameters {
pub quality: u32,
}
```
- `quality`: in a range from 0 to 100, the quality of the resulting image. If the optimization flag is `true`, the level is set to `100`. Default: `80`.
#### webp
2022-04-11 09:58:52 +02:00
WebP's compression is tricky. The format is already well optimized and using the `optimize` flag will probably result in a bigger image.
2021-10-23 17:18:52 +02:00
```Rust
pub struct Parameters {
pub quality: u32,
}
2016-11-14 23:43:52 +01:00
```
2021-10-23 17:18:52 +02:00
- `quality`: in a range from 0 to 100, the quality of the resulting image. If the optimization flag is `true`, this option will be ignored. Default: `60`.
#### tiff
Supported TIFF compression is only lossless. The supported algorithms are: Lzw, Deflate, Packbits, Uncompressed.
```Rust
pub struct Parameters {
pub algorithm: TiffCompression,
pub deflate_level: DeflateLevel,
}
```
- `deflate_level`: can be one of `Fast`, `Balanced`, `Best`.
2023-05-03 21:39:18 +02:00
_________________
2021-10-23 17:18:52 +02:00
## Usage in C
2023-05-03 21:39:18 +02:00
Libcaesium exposes two C functions, auto-detecting the input file type:
### Based on quality values
2021-10-23 17:18:52 +02:00
```Rust
2023-05-03 21:39:18 +02:00
pub unsafe extern "C" fn c_compress(
2021-10-23 17:18:52 +02:00
input_path: *const c_char,
output_path: *const c_char,
2022-02-12 14:10:12 +01:00
params: CCSParameters
2022-04-11 09:58:52 +02:00
) -> CCSResult
2021-10-23 17:18:52 +02:00
```
#### Parameters
- `input_path` - input file path (full filename)
- `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below)
#### Return
2022-04-11 09:58:52 +02:00
A `CCSResult` struct
```Rust
#[repr(C)]
pub struct CCSResult {
pub success: bool,
pub error_message: *const c_char,
}
```
If `success` is `true` the compression process ended successfully and `error_message` will be empty.
On failure, the `error_message` will be filled with a string containing a brief explanation of the error.
2021-10-23 17:18:52 +02:00
2023-05-03 21:39:18 +02:00
### Based on output size
```Rust
pub unsafe extern "C" fn c_compress_to_size(
input_path: *const c_char,
output_path: *const c_char,
params: CCSParameters,
max_output_size: usize,
2023-05-03 21:39:18 +02:00
) -> CCSResult
```
#### Parameters
- `input_path` - input file path (full filename)
- `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below)
- `max_output_size` - the maximum output size, in bytes
2023-05-03 21:39:18 +02:00
#### Return
A `CCSResult` struct
```Rust
#[repr(C)]
pub struct CCSResult {
pub success: bool,
pub error_message: *const c_char,
}
```
If `success` is `true` the compression process ended successfully and `error_message` will be empty.
On failure, the `error_message` will be filled with a string containing a brief explanation of the error.
2021-10-23 17:18:52 +02:00
### Compression options
The C options struct is slightly different from the Rust one:
```Rust
#[repr(C)]
2022-02-12 14:10:12 +01:00
pub struct CCSParameters {
2024-02-15 18:16:24 +01:00
pub keep_metadata: bool,
2021-10-23 17:18:52 +02:00
pub jpeg_quality: u32,
2023-06-24 16:01:21 +02:00
pub jpeg_chroma_subsampling: u32,
2022-07-31 11:24:07 +02:00
pub png_quality: u32,
2021-10-23 17:18:52 +02:00
pub png_force_zopfli: bool,
pub gif_quality: u32,
pub webp_quality: u32,
pub tiff_compression: u32,
pub tiff_deflate_level: u32,
2021-10-23 17:18:52 +02:00
pub optimize: bool,
2022-02-12 14:10:12 +01:00
pub width: u32,
pub height: u32,
2021-10-23 17:18:52 +02:00
}
```
The option description is the same as the Rust counterpart.
2023-06-24 16:01:21 +02:00
Valid values for `jpeg_chroma_subsampling` are [444, 422, 420, 411]. Any other value will be ignored and will be used the default option.
Valid values for `tiff_compression` are [0 (Uncompressed), 1 (Lzw), 2 (Deflate), 3 (Packbits)]. Any other value will be ignored and `0` will be used.
Valid values for `tiff_deflate_level` are [3 (Fast), 6 (Balanced), 9 (Best)]. Any other value will be ignored and `Best` will be used.
2017-12-29 23:09:13 +01:00
2021-10-23 17:18:52 +02:00
## Download
Binaries not available. Please refer to the compilation section below.
2016-11-14 23:43:52 +01:00
2016-11-16 09:41:46 +01:00
## Compilation and Installation
2023-08-12 14:44:59 +02:00
Compilation is available for all supported platforms: Windows, macOS and Linux.
2016-11-14 23:43:52 +01:00
2016-11-16 09:41:46 +01:00
```
2021-10-23 17:18:52 +02:00
cargo build --release
2016-11-16 09:41:46 +01:00
```
2021-10-23 17:18:52 +02:00
Note: if you don't use the `--release` flag, the PNG optimizations can take a very long time to complete, especially using the zopfli algorithm.
2016-11-16 09:41:46 +01:00
2021-10-23 17:18:52 +02:00
The result will be a dynamic library usable by external applications through its C interface.
2016-11-14 23:43:52 +01:00
## Compression vs Optimization
JPEG is a lossy format: that means you will always lose some information after each compression. So, compressing a file with
2021-10-23 17:18:52 +02:00
100 quality for 10 times will result in an always different image, even though you can't really see the difference.
2016-11-14 23:45:20 +01:00
Libcaesium also supports optimization, by setting the _quality_ to 0. This performs a lossless process, resulting in the same image,
2021-10-23 17:18:52 +02:00
but with a smaller size (10-12% usually).
GIF optimization is possible, but currently not supported.
2022-12-27 15:19:25 +01:00
WebP's optimization is also possible, but it will probably result in a bigger output file as it's well suited to losslessly convert from PNG or JPEG.