Optional features

This commit is contained in:
Matteo Paonessa 2022-10-17 14:16:26 +02:00
parent e166a7a708
commit 505b6c55c6
8 changed files with 53 additions and 38 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "libcaesium"
version = "0.9.3"
version = "0.10.0"
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
edition = "2021"
categories = ["multimedia::images"]
@ -22,17 +22,24 @@ repository = "https://github.com/Lymphatus/libcaesium"
license = "Apache-2.0"
[dependencies]
mozjpeg-sys = "1.0.2"
oxipng = "6.0.1"
mozjpeg-sys = { version = "1.0.2", optional = true }
oxipng = { version = "6.0.1", optional = true }
libc = "0.2"
gifsicle = "1.92.5"
webp = "0.2"
gifsicle = { version = "1.92.5", optional = true }
webp = { version = "0.2", optional = true }
infer = "0.9"
image = { version = "0.24.3", default-features = false, features = ["jpeg", "png", "webp", "gif"] }
img-parts = "0.3"
bytes = "1.1"
lodepng = "3.7"
imagequant = {git = "https://github.com/Lymphatus/libimagequant", rev = "67f1686"}
lodepng = { version = "3.7", optional = true }
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]
dssim = "3.2.0"

View File

@ -50,12 +50,10 @@ pub struct Parameters {
#### png
```Rust
pub struct Parameters {
pub oxipng: oxipng::Options,
pub quality: u32,
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`.
- `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`.

View File

@ -4,10 +4,6 @@ use std::os::raw::{c_int, c_void};
use crate::CSParameters;
pub struct Parameters {
pub quality: u32,
}
pub fn compress(
input_path: String,
output_path: String,

View File

@ -10,10 +10,6 @@ use std::{io, mem};
use crate::resize::resize;
use crate::CSParameters;
pub struct Parameters {
pub quality: u32,
}
pub fn compress(
input_path: String,
output_path: String,

View File

@ -4,11 +4,15 @@ use std::os::raw::c_char;
use crate::utils::get_filetype;
#[cfg(feature = "gif")]
mod gif;
#[cfg(feature = "jpg")]
mod jpeg;
#[cfg(feature = "png")]
mod png;
mod resize;
mod utils;
#[cfg(feature = "webp")]
mod webp;
#[repr(C)]
@ -30,29 +34,46 @@ pub struct CCSResult {
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 jpeg: jpeg::Parameters,
pub png: png::Parameters,
pub gif: gif::Parameters,
pub webp: webp::Parameters,
pub jpeg: JpegParameters,
pub png: PngParameters,
pub gif: GifParameters,
pub webp: WebPParameters,
pub keep_metadata: bool,
pub optimize: bool,
pub width: u32,
pub height: u32,
}
pub fn initialize_parameters() -> CSParameters {
let jpeg = jpeg::Parameters { quality: 80 };
let png = png::Parameters {
oxipng: oxipng::Options::default(),
pub fn initialize_parameters() -> CSParameters {
let jpeg = JpegParameters { quality: 80 };
let png = PngParameters {
quality: 80,
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 {
jpeg,
@ -121,15 +142,19 @@ pub fn compress(
let file_type = get_filetype(&input_path);
match file_type {
#[cfg(feature = "jpg")]
utils::SupportedFileTypes::Jpeg => {
jpeg::compress(input_path, output_path, parameters)?;
}
#[cfg(feature = "png")]
utils::SupportedFileTypes::Png => {
png::compress(input_path, output_path, parameters)?;
}
#[cfg(feature = "gif")]
utils::SupportedFileTypes::Gif => {
gif::compress(input_path, output_path, parameters)?;
}
#[cfg(feature = "webp")]
utils::SupportedFileTypes::WebP => {
webp::compress(input_path, output_path, parameters)?;
}

View File

@ -9,12 +9,6 @@ use std::{fs, io};
use crate::resize::resize_image;
use crate::CSParameters;
pub struct Parameters {
pub oxipng: oxipng::Options,
pub quality: u32,
pub force_zopfli: bool,
}
pub fn compress(
input_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> {
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 {
oxipng_options.strip = oxipng::Headers::Safe;
}

View File

@ -5,6 +5,7 @@ use image::DynamicImage;
use std::io;
use std::io::Cursor;
#[allow(dead_code)]
pub fn resize(
image_buffer: Vec<u8>,
width: u32,
@ -31,6 +32,7 @@ pub fn resize(
Ok(resized_file)
}
#[allow(dead_code)]
pub fn resize_image(
image: DynamicImage,
width: u32,
@ -42,6 +44,7 @@ pub fn resize_image(
Ok(resized_image)
}
#[allow(dead_code)]
fn compute_dimensions(
original_width: u32,
original_height: u32,

View File

@ -6,10 +6,6 @@ use std::ops::Deref;
use crate::resize::resize_image;
use crate::CSParameters;
pub struct Parameters {
pub quality: u32,
}
pub fn compress(
input_path: String,
output_path: String,