Compress in memory pub function
This commit is contained in:
parent
e001a00553
commit
74554f30d4
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "libcaesium"
|
name = "libcaesium"
|
||||||
version = "0.12.1"
|
version = "0.13.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"]
|
||||||
|
|
34
src/lib.rs
34
src/lib.rs
|
@ -9,7 +9,7 @@ use std::io::Write;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use crate::jpeg::ChromaSubsampling;
|
use crate::jpeg::ChromaSubsampling;
|
||||||
|
|
||||||
use crate::utils::{get_filetype, SupportedFileTypes};
|
use crate::utils::{get_filetype_from_memory, get_filetype_from_path, SupportedFileTypes};
|
||||||
|
|
||||||
#[cfg(feature = "gif")]
|
#[cfg(feature = "gif")]
|
||||||
mod gif;
|
mod gif;
|
||||||
|
@ -45,7 +45,7 @@ pub struct CCSResult {
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct JpegParameters {
|
pub struct JpegParameters {
|
||||||
pub quality: u32,
|
pub quality: u32,
|
||||||
pub chroma_subsampling: ChromaSubsampling
|
pub chroma_subsampling: ChromaSubsampling,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
@ -80,7 +80,7 @@ pub struct CSParameters {
|
||||||
pub fn initialize_parameters() -> CSParameters {
|
pub fn initialize_parameters() -> CSParameters {
|
||||||
let jpeg = JpegParameters {
|
let jpeg = JpegParameters {
|
||||||
quality: 80,
|
quality: 80,
|
||||||
chroma_subsampling: ChromaSubsampling::Auto
|
chroma_subsampling: ChromaSubsampling::Auto,
|
||||||
};
|
};
|
||||||
|
|
||||||
let png = PngParameters {
|
let png = PngParameters {
|
||||||
|
@ -193,7 +193,7 @@ pub fn compress(
|
||||||
parameters: &CSParameters,
|
parameters: &CSParameters,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
validate_parameters(parameters)?;
|
validate_parameters(parameters)?;
|
||||||
let file_type = get_filetype(&input_path);
|
let file_type = get_filetype_from_path(&input_path);
|
||||||
|
|
||||||
match file_type {
|
match file_type {
|
||||||
#[cfg(feature = "jpg")]
|
#[cfg(feature = "jpg")]
|
||||||
|
@ -218,6 +218,30 @@ pub fn compress(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn compress_in_memory(
|
||||||
|
in_file: Vec<u8>,
|
||||||
|
parameters: &mut CSParameters,
|
||||||
|
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||||
|
let file_type = get_filetype_from_memory(in_file.as_slice());
|
||||||
|
let compressed_file = match file_type {
|
||||||
|
#[cfg(feature = "jpg")]
|
||||||
|
SupportedFileTypes::Jpeg => {
|
||||||
|
jpeg::compress_to_memory(in_file, parameters)?
|
||||||
|
}
|
||||||
|
#[cfg(feature = "png")]
|
||||||
|
SupportedFileTypes::Png => {
|
||||||
|
png::compress_to_memory(in_file, parameters)?
|
||||||
|
}
|
||||||
|
#[cfg(feature = "webp")]
|
||||||
|
SupportedFileTypes::WebP => {
|
||||||
|
webp::compress_to_memory(in_file, parameters)?
|
||||||
|
}
|
||||||
|
_ => return Err("Format not supported for compression to size".into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(compressed_file)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn compress_to_size(
|
pub fn compress_to_size(
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
|
@ -225,7 +249,7 @@ pub fn compress_to_size(
|
||||||
max_output_size: usize,
|
max_output_size: usize,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
{
|
{
|
||||||
let file_type = get_filetype(&input_path);
|
let file_type = get_filetype_from_path(&input_path);
|
||||||
let in_file = fs::read(input_path.clone())?;
|
let in_file = fs::read(input_path.clone())?;
|
||||||
let original_size = in_file.len();
|
let original_size = in_file.len();
|
||||||
let tolerance_percentage = 2;
|
let tolerance_percentage = 2;
|
||||||
|
|
24
src/utils.rs
24
src/utils.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use infer::Type;
|
||||||
|
|
||||||
pub enum SupportedFileTypes {
|
pub enum SupportedFileTypes {
|
||||||
Jpeg,
|
Jpeg,
|
||||||
Png,
|
Png,
|
||||||
|
@ -6,18 +8,30 @@ pub enum SupportedFileTypes {
|
||||||
Unkn,
|
Unkn,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_filetype(file_path: &str) -> SupportedFileTypes {
|
pub fn get_filetype_from_path(file_path: &str) -> SupportedFileTypes {
|
||||||
match infer::get_from_path(file_path) {
|
match infer::get_from_path(file_path) {
|
||||||
Ok(v) => match v {
|
Ok(v) => match v {
|
||||||
None => SupportedFileTypes::Unkn,
|
None => SupportedFileTypes::Unkn,
|
||||||
Some(ft) => match ft.mime_type() {
|
Some(ft) => match_supported_filetypes(ft)
|
||||||
|
},
|
||||||
|
Err(_) => SupportedFileTypes::Unkn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_filetype_from_memory(buf: &[u8]) -> SupportedFileTypes {
|
||||||
|
match infer::get(buf) {
|
||||||
|
None => SupportedFileTypes::Unkn,
|
||||||
|
Some(ft) => match_supported_filetypes(ft)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn match_supported_filetypes(ft: Type) -> SupportedFileTypes {
|
||||||
|
return match ft.mime_type() {
|
||||||
"image/jpeg" => SupportedFileTypes::Jpeg,
|
"image/jpeg" => SupportedFileTypes::Jpeg,
|
||||||
"image/png" => SupportedFileTypes::Png,
|
"image/png" => SupportedFileTypes::Png,
|
||||||
"image/gif" => SupportedFileTypes::Gif,
|
"image/gif" => SupportedFileTypes::Gif,
|
||||||
"image/webp" => SupportedFileTypes::WebP,
|
"image/webp" => SupportedFileTypes::WebP,
|
||||||
_ => SupportedFileTypes::Unkn,
|
_ => SupportedFileTypes::Unkn,
|
||||||
},
|
|
||||||
},
|
|
||||||
Err(_) => SupportedFileTypes::Unkn,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue