2021-10-24 11:57:05 +02:00
|
|
|
# libcaesium [](https://github.com/Lymphatus/libcaesium/actions/workflows/rust.yml)
|
2016-11-14 23:43:52 +01:00
|
|
|
|
2024-10-10 14:05:59 +02:00
|
|
|
Libcaesium is a simple library performing JPEG, PNG, WebP and GIF (experimental) compression/optimization written in
|
|
|
|
Rust, with a C interface.
|
|
|
|
|
2024-10-11 14:32:51 +02:00
|
|
|
> [!WARNING]
|
2024-10-10 14:05:59 +02:00
|
|
|
> 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.
|
2021-10-23 17:18:52 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
## Usage example
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
Libcaesium exposes two functions, auto-detecting the input file type
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
```Rust
|
2024-10-11 15:17:51 +02:00
|
|
|
use caesium::parameters::CSParameters;
|
|
|
|
use caesium::compress;
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
let mut parameters = CSParameters::new();
|
|
|
|
parameters.keep_metadata = true;
|
|
|
|
parameters.jpeg.quality = 60;
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
let success = compress(input, output, ¶meters).is_ok();
|
2016-11-14 23:43:52 +01:00
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
## Compilation
|
2021-10-23 17:18:52 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
Compilation is available for all supported platforms: Windows, macOS and Linux.
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +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.
|
|
|
|
>
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
```bash
|
|
|
|
cargo build --release
|
2024-02-15 14:25:15 +01:00
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
The result will be a dynamic library usable by external applications through its C interface.
|
2023-05-03 21:39:18 +02:00
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
## Usage in C
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-11-13 07:53:29 +01:00
|
|
|
*You can find the C header file in the include folder in the project root directory.*
|
|
|
|
|
|
|
|
Libcaesium exposes there C functions, auto-detecting the input file type:
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
### Based on quality values
|
2024-10-10 14:05:59 +02:00
|
|
|
|
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
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
#### Parameters
|
2024-10-10 14:05:59 +02:00
|
|
|
|
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)
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
#### Return
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2022-04-11 09:58:52 +02:00
|
|
|
A `CCSResult` struct
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2022-04-11 09:58:52 +02:00
|
|
|
```Rust
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct CCSResult {
|
|
|
|
pub success: bool,
|
2024-11-13 07:53:29 +01:00
|
|
|
pub code: u32,
|
2022-04-11 09:58:52 +02:00
|
|
|
pub error_message: *const c_char,
|
|
|
|
}
|
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2022-04-11 09:58:52 +02:00
|
|
|
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
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
```Rust
|
|
|
|
pub unsafe extern "C" fn c_compress_to_size(
|
|
|
|
input_path: *const c_char,
|
|
|
|
output_path: *const c_char,
|
|
|
|
params: CCSParameters,
|
2023-05-06 13:05:17 +02:00
|
|
|
max_output_size: usize,
|
2024-11-13 07:53:29 +01:00
|
|
|
return_smallest: bool,
|
2023-05-03 21:39:18 +02:00
|
|
|
) -> CCSResult
|
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
#### Parameters
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
- `input_path` - input file path (full filename)
|
|
|
|
- `output_path` - output file path (full filename)
|
|
|
|
- `parameters` - options struct, containing compression parameters (see below)
|
2023-05-06 13:05:17 +02:00
|
|
|
- `max_output_size` - the maximum output size, in bytes
|
2024-11-13 07:53:29 +01:00
|
|
|
- `return_smallest` - whether to return the smallest
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
#### Return
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
A `CCSResult` struct
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
```Rust
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct CCSResult {
|
|
|
|
pub success: bool,
|
2024-11-13 07:53:29 +01:00
|
|
|
pub code: u32,
|
|
|
|
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.
|
|
|
|
|
|
|
|
### Based on convert output
|
|
|
|
|
|
|
|
```Rust
|
|
|
|
pub unsafe extern "C" fn c_convert(
|
|
|
|
input_path: *const c_char,
|
|
|
|
output_path: *const c_char,
|
|
|
|
format: SupportedFileTypes,
|
|
|
|
params: CCSParameters,
|
|
|
|
) -> CCSResult
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
- `input_path` - input file path (full filename)
|
|
|
|
- `output_path` - output file path (full filename)
|
|
|
|
- `format` - target image format (see below)
|
|
|
|
- `parameters` - options struct, containing compression parameters (see below)
|
|
|
|
|
|
|
|
#### Return
|
|
|
|
|
|
|
|
A `CCSResult` struct
|
|
|
|
|
|
|
|
```Rust
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct CCSResult {
|
|
|
|
pub success: bool,
|
|
|
|
pub code: u32,
|
2023-05-03 21:39:18 +02:00
|
|
|
pub error_message: *const c_char,
|
|
|
|
}
|
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2023-05-03 21:39:18 +02:00
|
|
|
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
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
The C options struct is slightly different from the Rust one:
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
```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,
|
2024-10-24 09:41:44 +02:00
|
|
|
pub jpeg_progressive: bool,
|
2022-07-31 11:24:07 +02:00
|
|
|
pub png_quality: u32,
|
2024-02-16 18:33:13 +01:00
|
|
|
pub png_optimization_level: u32,
|
2021-10-23 17:18:52 +02:00
|
|
|
pub png_force_zopfli: bool,
|
|
|
|
pub gif_quality: u32,
|
|
|
|
pub webp_quality: u32,
|
2024-02-15 14:25:15 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
```
|
2024-10-10 14:05:59 +02:00
|
|
|
|
2024-10-11 15:17:51 +02:00
|
|
|
The option description is the same as the Rust counterpart.
|
|
|
|
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 `[1 (Fast), 6 (Balanced), 9 (Best)]`. Any other value will be ignored and `Best`
|
2024-10-10 14:05:59 +02:00
|
|
|
will be used.
|
2017-12-29 23:09:13 +01:00
|
|
|
|
2024-11-13 07:53:29 +01:00
|
|
|
### Supported file types
|
|
|
|
|
|
|
|
```rust
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum SupportedFileTypes {
|
|
|
|
Jpeg,
|
|
|
|
Png,
|
|
|
|
Gif,
|
|
|
|
WebP,
|
|
|
|
Tiff,
|
|
|
|
Unkn,
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-14 23:43:52 +01:00
|
|
|
## Compression vs Optimization
|
2024-10-10 14:05:59 +02:00
|
|
|
|
|
|
|
JPEG is a lossy format: that means you will always lose some information after each compression. So, compressing a file
|
2024-10-11 15:17:51 +02:00
|
|
|
with 100 quality for 10 times will result in an always different image, even though you can't really see the difference.
|
2025-01-03 12:04:52 +01:00
|
|
|
Libcaesium also supports optimization. This performs a lossless process, resulting in the
|
|
|
|
same exact image, but with a smaller size (10-12% usually).
|
2021-10-23 17:18:52 +02:00
|
|
|
GIF optimization is possible, but currently not supported.
|
2024-10-10 14:05:59 +02: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.
|