Optional features
This commit is contained in:
parent
e166a7a708
commit
505b6c55c6
21
Cargo.toml
21
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "libcaesium"
|
name = "libcaesium"
|
||||||
version = "0.9.3"
|
version = "0.10.0"
|
||||||
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
|
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
categories = ["multimedia::images"]
|
categories = ["multimedia::images"]
|
||||||
|
@ -22,17 +22,24 @@ repository = "https://github.com/Lymphatus/libcaesium"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mozjpeg-sys = "1.0.2"
|
mozjpeg-sys = { version = "1.0.2", optional = true }
|
||||||
oxipng = "6.0.1"
|
oxipng = { version = "6.0.1", optional = true }
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
gifsicle = "1.92.5"
|
gifsicle = { version = "1.92.5", optional = true }
|
||||||
webp = "0.2"
|
webp = { version = "0.2", optional = true }
|
||||||
infer = "0.9"
|
infer = "0.9"
|
||||||
image = { version = "0.24.3", default-features = false, features = ["jpeg", "png", "webp", "gif"] }
|
image = { version = "0.24.3", default-features = false, features = ["jpeg", "png", "webp", "gif"] }
|
||||||
img-parts = "0.3"
|
img-parts = "0.3"
|
||||||
bytes = "1.1"
|
bytes = "1.1"
|
||||||
lodepng = "3.7"
|
lodepng = { version = "3.7", optional = true }
|
||||||
imagequant = {git = "https://github.com/Lymphatus/libimagequant", rev = "67f1686"}
|
imagequant = {git = "https://github.com/Lymphatus/libimagequant", rev = "67f1686", optional = true}
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["jpg", "png", "webp", "gif"]
|
||||||
|
jpg = ["dep:mozjpeg-sys"]
|
||||||
|
png = ["dep:oxipng", "dep:lodepng", "dep:imagequant"]
|
||||||
|
webp = ["dep:webp"]
|
||||||
|
gif = ["dep:gifsicle"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
dssim = "3.2.0"
|
dssim = "3.2.0"
|
||||||
|
|
|
@ -50,12 +50,10 @@ pub struct Parameters {
|
||||||
#### png
|
#### png
|
||||||
```Rust
|
```Rust
|
||||||
pub struct Parameters {
|
pub struct Parameters {
|
||||||
pub oxipng: oxipng::Options,
|
|
||||||
pub quality: u32,
|
pub quality: u32,
|
||||||
pub force_zopfli: bool
|
pub force_zopfli: bool
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- `oxipng`: oxipng options. Should be left as default unless you want to do something advanced. Refer to [oxipng](https://github.com/shssoichiro/oxipng) for documentation.
|
|
||||||
- `quality`: in a range from 0 to 100, the quality of the resulting image. Default `80`.
|
- `quality`: in a range from 0 to 100, the quality of the resulting image. Default `80`.
|
||||||
- `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`.
|
- `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`.
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,6 @@ use std::os::raw::{c_int, c_void};
|
||||||
|
|
||||||
use crate::CSParameters;
|
use crate::CSParameters;
|
||||||
|
|
||||||
pub struct Parameters {
|
|
||||||
pub quality: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compress(
|
pub fn compress(
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
|
|
|
@ -10,10 +10,6 @@ use std::{io, mem};
|
||||||
use crate::resize::resize;
|
use crate::resize::resize;
|
||||||
use crate::CSParameters;
|
use crate::CSParameters;
|
||||||
|
|
||||||
pub struct Parameters {
|
|
||||||
pub quality: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compress(
|
pub fn compress(
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
|
|
45
src/lib.rs
45
src/lib.rs
|
@ -4,11 +4,15 @@ use std::os::raw::c_char;
|
||||||
|
|
||||||
use crate::utils::get_filetype;
|
use crate::utils::get_filetype;
|
||||||
|
|
||||||
|
#[cfg(feature = "gif")]
|
||||||
mod gif;
|
mod gif;
|
||||||
|
#[cfg(feature = "jpg")]
|
||||||
mod jpeg;
|
mod jpeg;
|
||||||
|
#[cfg(feature = "png")]
|
||||||
mod png;
|
mod png;
|
||||||
mod resize;
|
mod resize;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
#[cfg(feature = "webp")]
|
||||||
mod webp;
|
mod webp;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
@ -30,29 +34,46 @@ pub struct CCSResult {
|
||||||
pub error_message: *const c_char,
|
pub error_message: *const c_char,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct JpegParameters {
|
||||||
|
pub quality: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PngParameters {
|
||||||
|
pub quality: u32,
|
||||||
|
pub force_zopfli: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GifParameters {
|
||||||
|
pub quality: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WebPParameters {
|
||||||
|
pub quality: u32,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CSParameters {
|
pub struct CSParameters {
|
||||||
pub jpeg: jpeg::Parameters,
|
pub jpeg: JpegParameters,
|
||||||
pub png: png::Parameters,
|
pub png: PngParameters,
|
||||||
pub gif: gif::Parameters,
|
pub gif: GifParameters,
|
||||||
pub webp: webp::Parameters,
|
pub webp: WebPParameters,
|
||||||
pub keep_metadata: bool,
|
pub keep_metadata: bool,
|
||||||
pub optimize: bool,
|
pub optimize: bool,
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn initialize_parameters() -> CSParameters {
|
|
||||||
let jpeg = jpeg::Parameters { quality: 80 };
|
|
||||||
|
|
||||||
let png = png::Parameters {
|
pub fn initialize_parameters() -> CSParameters {
|
||||||
oxipng: oxipng::Options::default(),
|
let jpeg = JpegParameters { quality: 80 };
|
||||||
|
|
||||||
|
let png = PngParameters {
|
||||||
quality: 80,
|
quality: 80,
|
||||||
force_zopfli: false,
|
force_zopfli: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let gif = gif::Parameters { quality: 80 };
|
let gif = GifParameters { quality: 80 };
|
||||||
|
|
||||||
let webp = webp::Parameters { quality: 80 };
|
let webp = WebPParameters { quality: 80 };
|
||||||
|
|
||||||
CSParameters {
|
CSParameters {
|
||||||
jpeg,
|
jpeg,
|
||||||
|
@ -121,15 +142,19 @@ pub fn compress(
|
||||||
let file_type = get_filetype(&input_path);
|
let file_type = get_filetype(&input_path);
|
||||||
|
|
||||||
match file_type {
|
match file_type {
|
||||||
|
#[cfg(feature = "jpg")]
|
||||||
utils::SupportedFileTypes::Jpeg => {
|
utils::SupportedFileTypes::Jpeg => {
|
||||||
jpeg::compress(input_path, output_path, parameters)?;
|
jpeg::compress(input_path, output_path, parameters)?;
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "png")]
|
||||||
utils::SupportedFileTypes::Png => {
|
utils::SupportedFileTypes::Png => {
|
||||||
png::compress(input_path, output_path, parameters)?;
|
png::compress(input_path, output_path, parameters)?;
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "gif")]
|
||||||
utils::SupportedFileTypes::Gif => {
|
utils::SupportedFileTypes::Gif => {
|
||||||
gif::compress(input_path, output_path, parameters)?;
|
gif::compress(input_path, output_path, parameters)?;
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "webp")]
|
||||||
utils::SupportedFileTypes::WebP => {
|
utils::SupportedFileTypes::WebP => {
|
||||||
webp::compress(input_path, output_path, parameters)?;
|
webp::compress(input_path, output_path, parameters)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,6 @@ use std::{fs, io};
|
||||||
use crate::resize::resize_image;
|
use crate::resize::resize_image;
|
||||||
use crate::CSParameters;
|
use crate::CSParameters;
|
||||||
|
|
||||||
pub struct Parameters {
|
|
||||||
pub oxipng: oxipng::Options,
|
|
||||||
pub quality: u32,
|
|
||||||
pub force_zopfli: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compress(
|
pub fn compress(
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
|
@ -93,7 +87,7 @@ fn lossy(input_path: String, parameters: CSParameters) -> Result<Vec<u8>, io::Er
|
||||||
|
|
||||||
fn lossless(input_path: String, parameters: CSParameters) -> Result<Vec<u8>, io::Error> {
|
fn lossless(input_path: String, parameters: CSParameters) -> Result<Vec<u8>, io::Error> {
|
||||||
let in_file = fs::read(input_path)?;
|
let in_file = fs::read(input_path)?;
|
||||||
let mut oxipng_options = parameters.png.oxipng;
|
let mut oxipng_options = oxipng::Options::default();
|
||||||
if !parameters.keep_metadata {
|
if !parameters.keep_metadata {
|
||||||
oxipng_options.strip = oxipng::Headers::Safe;
|
oxipng_options.strip = oxipng::Headers::Safe;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ use image::DynamicImage;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn resize(
|
pub fn resize(
|
||||||
image_buffer: Vec<u8>,
|
image_buffer: Vec<u8>,
|
||||||
width: u32,
|
width: u32,
|
||||||
|
@ -31,6 +32,7 @@ pub fn resize(
|
||||||
Ok(resized_file)
|
Ok(resized_file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn resize_image(
|
pub fn resize_image(
|
||||||
image: DynamicImage,
|
image: DynamicImage,
|
||||||
width: u32,
|
width: u32,
|
||||||
|
@ -42,6 +44,7 @@ pub fn resize_image(
|
||||||
Ok(resized_image)
|
Ok(resized_image)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn compute_dimensions(
|
fn compute_dimensions(
|
||||||
original_width: u32,
|
original_width: u32,
|
||||||
original_height: u32,
|
original_height: u32,
|
||||||
|
|
|
@ -6,10 +6,6 @@ use std::ops::Deref;
|
||||||
use crate::resize::resize_image;
|
use crate::resize::resize_image;
|
||||||
use crate::CSParameters;
|
use crate::CSParameters;
|
||||||
|
|
||||||
pub struct Parameters {
|
|
||||||
pub quality: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compress(
|
pub fn compress(
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
|
|
Loading…
Reference in New Issue