Compress to size in memory
This commit is contained in:
parent
74554f30d4
commit
9119be3516
21
Cargo.toml
21
Cargo.toml
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "libcaesium"
|
||||
version = "0.13.0"
|
||||
version = "0.13.1"
|
||||
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
|
||||
edition = "2021"
|
||||
categories = ["multimedia::images"]
|
||||
|
@ -21,27 +21,28 @@ repository = "https://github.com/Lymphatus/libcaesium"
|
|||
license = "Apache-2.0"
|
||||
|
||||
[features]
|
||||
default = ["jpg", "png", "webp", "gif"]
|
||||
jpg = ["dep:mozjpeg-sys"]
|
||||
png = ["dep:oxipng", "dep:lodepng", "dep:imagequant"]
|
||||
webp = ["dep:webp"]
|
||||
gif = ["dep:gifsicle"]
|
||||
default = ["jpg", "png", "webp", "gif", "parallel"]
|
||||
jpg = ["dep:mozjpeg-sys", "image/jpeg"]
|
||||
png = ["dep:oxipng", "dep:lodepng", "dep:imagequant", "image/png"]
|
||||
webp = ["dep:webp", "image/webp"]
|
||||
gif = ["dep:gifsicle", "image/gif"]
|
||||
parallel = ["oxipng?/parallel", "imagequant?/threads", "dssim/threads"]
|
||||
|
||||
[dependencies]
|
||||
mozjpeg-sys = { version = "1.0", optional = true }
|
||||
oxipng = { version = "8.0", optional = true }
|
||||
oxipng = { version = "8.0", default-features = false, features = ["filetime", "zopfli"], optional = true }
|
||||
libc = "0.2"
|
||||
gifsicle = { version = "1.92.5", optional = true }
|
||||
webp = { version = "0.2.2", optional = true }
|
||||
infer = "0.15.0"
|
||||
image = { version = "0.24.6", default-features = false, features = ["jpeg", "png", "webp", "gif"] }
|
||||
image = { version = "0.24.6", default-features = false }
|
||||
img-parts = "0.3"
|
||||
bytes = "1.1"
|
||||
lodepng = { version = "3.7", optional = true }
|
||||
imagequant = {version = "4.1", optional = true}
|
||||
imagequant = {version = "4.1", optional = true, default-features = false}
|
||||
|
||||
[dev-dependencies]
|
||||
dssim = "3.2.0"
|
||||
dssim = { version = "3.2.0", default-features = false, features = ["no-macos-vimage"] }
|
||||
kamadak-exif = "0.5.4"
|
||||
|
||||
[[bin]]
|
||||
|
|
35
src/lib.rs
35
src/lib.rs
|
@ -242,16 +242,13 @@ pub fn compress_in_memory(
|
|||
Ok(compressed_file)
|
||||
}
|
||||
|
||||
pub fn compress_to_size(
|
||||
input_path: String,
|
||||
output_path: String,
|
||||
pub fn compress_to_size_in_memory(
|
||||
in_file: Vec<u8>,
|
||||
parameters: &mut CSParameters,
|
||||
max_output_size: usize,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
{
|
||||
let file_type = get_filetype_from_path(&input_path);
|
||||
let in_file = fs::read(input_path.clone())?;
|
||||
let original_size = in_file.len();
|
||||
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let file_type = get_filetype_from_memory(&in_file);
|
||||
|
||||
let tolerance_percentage = 2;
|
||||
let tolerance = max_output_size * tolerance_percentage / 100;
|
||||
let mut quality = 80;
|
||||
|
@ -260,11 +257,6 @@ pub fn compress_to_size(
|
|||
let max_tries: u32 = 10;
|
||||
let mut tries: u32 = 0;
|
||||
|
||||
if original_size <= max_output_size {
|
||||
fs::copy(input_path, output_path)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let compressed_file = loop {
|
||||
if tries >= max_tries {
|
||||
return Err("Max tries reached".into());
|
||||
|
@ -313,6 +305,23 @@ pub fn compress_to_size(
|
|||
tries += 1;
|
||||
};
|
||||
|
||||
Ok(compressed_file)
|
||||
}
|
||||
|
||||
pub fn compress_to_size(
|
||||
input_path: String,
|
||||
output_path: String,
|
||||
parameters: &mut CSParameters,
|
||||
max_output_size: usize,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
{
|
||||
let in_file = fs::read(input_path.clone())?;
|
||||
let original_size = in_file.len();
|
||||
if original_size <= max_output_size {
|
||||
fs::copy(input_path, output_path)?;
|
||||
return Ok(());
|
||||
}
|
||||
let compressed_file = compress_to_size_in_memory(in_file, parameters, max_output_size)?;
|
||||
let mut out_file = File::create(output_path)?;
|
||||
out_file.write_all(&compressed_file)?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue