Compress in memory pub function

This commit is contained in:
Matteo Paonessa 2023-12-12 09:53:27 +01:00
parent e001a00553
commit 74554f30d4
3 changed files with 52 additions and 14 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "libcaesium"
version = "0.12.1"
version = "0.13.0"
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
edition = "2021"
categories = ["multimedia::images"]

View File

@ -9,7 +9,7 @@ use std::io::Write;
use std::os::raw::c_char;
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")]
mod gif;
@ -45,7 +45,7 @@ pub struct CCSResult {
#[derive(Copy, Clone)]
pub struct JpegParameters {
pub quality: u32,
pub chroma_subsampling: ChromaSubsampling
pub chroma_subsampling: ChromaSubsampling,
}
#[derive(Copy, Clone)]
@ -80,7 +80,7 @@ pub struct CSParameters {
pub fn initialize_parameters() -> CSParameters {
let jpeg = JpegParameters {
quality: 80,
chroma_subsampling: ChromaSubsampling::Auto
chroma_subsampling: ChromaSubsampling::Auto,
};
let png = PngParameters {
@ -193,7 +193,7 @@ pub fn compress(
parameters: &CSParameters,
) -> Result<(), Box<dyn Error>> {
validate_parameters(parameters)?;
let file_type = get_filetype(&input_path);
let file_type = get_filetype_from_path(&input_path);
match file_type {
#[cfg(feature = "jpg")]
@ -218,6 +218,30 @@ pub fn compress(
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(
input_path: String,
output_path: String,
@ -225,7 +249,7 @@ pub fn compress_to_size(
max_output_size: usize,
) -> 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 original_size = in_file.len();
let tolerance_percentage = 2;

View File

@ -1,3 +1,5 @@
use infer::Type;
pub enum SupportedFileTypes {
Jpeg,
Png,
@ -6,18 +8,30 @@ pub enum SupportedFileTypes {
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) {
Ok(v) => match v {
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/png" => SupportedFileTypes::Png,
"image/gif" => SupportedFileTypes::Gif,
"image/webp" => SupportedFileTypes::WebP,
_ => SupportedFileTypes::Unkn,
},
},
Err(_) => SupportedFileTypes::Unkn,
}
}