Allow to return the smallest compressing by size
This commit is contained in:
parent
45e5703a69
commit
c09613dbc8
12
src/lib.rs
12
src/lib.rs
|
@ -144,6 +144,7 @@ pub unsafe extern "C" fn c_compress_to_size(
|
||||||
output_path: *const c_char,
|
output_path: *const c_char,
|
||||||
params: CCSParameters,
|
params: CCSParameters,
|
||||||
max_output_size: usize,
|
max_output_size: usize,
|
||||||
|
return_smallest: bool
|
||||||
) -> CCSResult {
|
) -> CCSResult {
|
||||||
let mut parameters = c_set_parameters(params);
|
let mut parameters = c_set_parameters(params);
|
||||||
|
|
||||||
|
@ -152,6 +153,7 @@ pub unsafe extern "C" fn c_compress_to_size(
|
||||||
CStr::from_ptr(output_path).to_str().unwrap().to_string(),
|
CStr::from_ptr(output_path).to_str().unwrap().to_string(),
|
||||||
&mut parameters,
|
&mut parameters,
|
||||||
max_output_size,
|
max_output_size,
|
||||||
|
return_smallest
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,6 +272,7 @@ pub fn compress_to_size_in_memory(
|
||||||
in_file: Vec<u8>,
|
in_file: Vec<u8>,
|
||||||
parameters: &mut CSParameters,
|
parameters: &mut CSParameters,
|
||||||
max_output_size: usize,
|
max_output_size: usize,
|
||||||
|
return_smallest: bool
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
let file_type = get_filetype_from_memory(&in_file);
|
let file_type = get_filetype_from_memory(&in_file);
|
||||||
|
|
||||||
|
@ -326,7 +329,11 @@ pub fn compress_to_size_in_memory(
|
||||||
quality = cmp::max(1, cmp::min(100, (last_high + last_less) / 2));
|
quality = cmp::max(1, cmp::min(100, (last_high + last_less) / 2));
|
||||||
if last_quality == quality {
|
if last_quality == quality {
|
||||||
if quality == 1 && last_high == 1 {
|
if quality == 1 && last_high == 1 {
|
||||||
return Err(CaesiumError { message: "Cannot compress to desired quality".into(), code: 10202 });
|
return if return_smallest {
|
||||||
|
Ok(compressed_file)
|
||||||
|
} else {
|
||||||
|
Err(CaesiumError { message: "Cannot compress to desired quality".into(), code: 10202 })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break compressed_file;
|
break compressed_file;
|
||||||
|
@ -343,6 +350,7 @@ pub fn compress_to_size(
|
||||||
output_path: String,
|
output_path: String,
|
||||||
parameters: &mut CSParameters,
|
parameters: &mut CSParameters,
|
||||||
max_output_size: usize,
|
max_output_size: usize,
|
||||||
|
return_smallest: bool
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
{
|
{
|
||||||
let in_file = fs::read(input_path.clone()).map_err(|e| CaesiumError { message: e.to_string(), code: 10201 })?;
|
let in_file = fs::read(input_path.clone()).map_err(|e| CaesiumError { message: e.to_string(), code: 10201 })?;
|
||||||
|
@ -351,7 +359,7 @@ pub fn compress_to_size(
|
||||||
fs::copy(input_path, output_path).map_err(|e| CaesiumError { message: e.to_string(), code: 10202 })?;
|
fs::copy(input_path, output_path).map_err(|e| CaesiumError { message: e.to_string(), code: 10202 })?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let compressed_file = compress_to_size_in_memory(in_file, parameters, max_output_size)?;
|
let compressed_file = compress_to_size_in_memory(in_file, parameters, max_output_size, return_smallest)?;
|
||||||
let mut out_file = File::create(output_path).map_err(|e| CaesiumError { message: e.to_string(), code: 10203 })?;
|
let mut out_file = File::create(output_path).map_err(|e| CaesiumError { message: e.to_string(), code: 10203 })?;
|
||||||
out_file.write_all(&compressed_file).map_err(|e| CaesiumError { message: e.to_string(), code: 10204 })?;
|
out_file.write_all(&compressed_file).map_err(|e| CaesiumError { message: e.to_string(), code: 10204 })?;
|
||||||
|
|
||||||
|
|
|
@ -21,11 +21,32 @@ fn compress_to_1_byte() {
|
||||||
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
String::from(output),
|
String::from(output),
|
||||||
&mut pars,
|
&mut pars,
|
||||||
1
|
1,
|
||||||
|
false
|
||||||
).expect_err("Cannot compress to desired quality");
|
).expect_err("Cannot compress to desired quality");
|
||||||
remove_compressed_test_file(output)
|
remove_compressed_test_file(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compress_to_1_byte_and_return() {
|
||||||
|
let output = "tests/samples/output/compressed_1b_return.jpg";
|
||||||
|
initialize(output);
|
||||||
|
let mut pars = caesium::initialize_parameters();
|
||||||
|
caesium::compress_to_size(
|
||||||
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
|
String::from(output),
|
||||||
|
&mut pars,
|
||||||
|
1,
|
||||||
|
true
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert!(File::open(output).unwrap().metadata().unwrap().len() > 1);
|
||||||
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
||||||
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compress_to_10_mb() {
|
fn compress_to_10_mb() {
|
||||||
let output = "tests/samples/output/compressed_10mb.jpg";
|
let output = "tests/samples/output/compressed_10mb.jpg";
|
||||||
|
@ -36,7 +57,8 @@ fn compress_to_10_mb() {
|
||||||
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
String::from(output),
|
String::from(output),
|
||||||
&mut pars,
|
&mut pars,
|
||||||
max_output_size
|
max_output_size,
|
||||||
|
false
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
assert_eq!(80, pars.jpeg.quality);
|
assert_eq!(80, pars.jpeg.quality);
|
||||||
|
@ -60,7 +82,8 @@ fn compress_to_range() {
|
||||||
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
String::from(output),
|
String::from(output),
|
||||||
&mut pars,
|
&mut pars,
|
||||||
max_output_size
|
max_output_size,
|
||||||
|
false
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
assert!(std::path::Path::new(output).exists());
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
|
Loading…
Reference in New Issue