Renamed desired_output_size to max_output_size

This commit is contained in:
Matteo Paonessa 2023-05-06 13:05:17 +02:00
parent 917a912a79
commit 528bbaed0c
3 changed files with 18 additions and 18 deletions

View File

@ -24,14 +24,14 @@ pub fn compress_to_size(
input_path: String, input_path: String,
output_path: String, output_path: String,
parameters: &CSParameters, parameters: &CSParameters,
desired_output_size: usize, max_output_size: usize,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
``` ```
#### Parameters #### Parameters
- `input_path` - input file path (full filename) - `input_path` - input file path (full filename)
- `output_path` - output file path (full filename) - `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below) - `parameters` - options struct, containing compression parameters (see below)
- `desired_output_size` - the desired output size, in bytes - `max_output_size` - the maximum output size, in bytes
This function will attempt to compress the given file *below* the desired size. It will never exceed it. The function This function will attempt to compress the given file *below* the desired size. It will never exceed it. The function
will start looping until the best size under the desired is achieved. The function has a 2% tolerance for the output size. will start looping until the best size under the desired is achieved. The function has a 2% tolerance for the output size.
@ -129,14 +129,14 @@ pub unsafe extern "C" fn c_compress_to_size(
input_path: *const c_char, input_path: *const c_char,
output_path: *const c_char, output_path: *const c_char,
params: CCSParameters, params: CCSParameters,
desired_output_size: usize, max_output_size: usize,
) -> CCSResult ) -> CCSResult
``` ```
#### Parameters #### Parameters
- `input_path` - input file path (full filename) - `input_path` - input file path (full filename)
- `output_path` - output file path (full filename) - `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below) - `parameters` - options struct, containing compression parameters (see below)
- `desired_output_size` - the desired output size, in bytes - `max_output_size` - the maximum output size, in bytes
#### Return #### Return
A `CCSResult` struct A `CCSResult` struct
```Rust ```Rust

View File

@ -121,7 +121,7 @@ pub unsafe extern "C" fn c_compress_to_size(
input_path: *const c_char, input_path: *const c_char,
output_path: *const c_char, output_path: *const c_char,
params: CCSParameters, params: CCSParameters,
desired_output_size: usize, max_output_size: usize,
) -> CCSResult { ) -> CCSResult {
let mut parameters = c_set_parameters(params); let mut parameters = c_set_parameters(params);
@ -129,7 +129,7 @@ pub unsafe extern "C" fn c_compress_to_size(
CStr::from_ptr(input_path).to_str().unwrap().to_string(), CStr::from_ptr(input_path).to_str().unwrap().to_string(),
CStr::from_ptr(output_path).to_str().unwrap().to_string(), CStr::from_ptr(output_path).to_str().unwrap().to_string(),
&mut parameters, &mut parameters,
desired_output_size, max_output_size,
)) ))
} }
@ -208,13 +208,13 @@ pub fn compress_to_size(
input_path: String, input_path: String,
output_path: String, output_path: String,
parameters: &mut CSParameters, parameters: &mut CSParameters,
desired_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(&input_path);
let in_file = fs::read(input_path)?; let in_file = fs::read(input_path)?;
let tolerance_percentage = 2; let tolerance_percentage = 2;
let tolerance = desired_output_size * tolerance_percentage / 100; let tolerance = max_output_size * tolerance_percentage / 100;
let mut quality = 80; let mut quality = 80;
let mut last_less = 1; let mut last_less = 1;
let mut last_high = 101; let mut last_high = 101;
@ -244,11 +244,11 @@ pub fn compress_to_size(
let compressed_file_size = compressed_file.len(); let compressed_file_size = compressed_file.len();
if compressed_file_size <= desired_output_size && desired_output_size - compressed_file_size < tolerance { if compressed_file_size <= max_output_size && max_output_size - compressed_file_size < tolerance {
break compressed_file; break compressed_file;
} }
if compressed_file_size <= desired_output_size { if compressed_file_size <= max_output_size {
last_less = quality; last_less = quality;
} else { } else {
last_high = quality; last_high = quality;

View File

@ -29,19 +29,19 @@ fn compress_to_1_byte() {
#[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";
let desired_output_size = 10_000_000; let max_output_size = 10_000_000;
initialize(output); initialize(output);
let mut pars = caesium::initialize_parameters(); let mut pars = caesium::initialize_parameters();
caesium::compress_to_size( caesium::compress_to_size(
String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
String::from(output), String::from(output),
&mut pars, &mut pars,
desired_output_size max_output_size
).unwrap(); ).unwrap();
assert_eq!(100, pars.jpeg.quality); assert_eq!(100, pars.jpeg.quality);
assert!(std::path::Path::new(output).exists()); assert!(std::path::Path::new(output).exists());
assert!(File::open(output).unwrap().metadata().unwrap().len() < desired_output_size as u64); assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
let kind = infer::get_from_path(output).unwrap().unwrap(); let kind = infer::get_from_path(output).unwrap().unwrap();
assert_eq!(kind.mime_type(), "image/jpeg"); assert_eq!(kind.mime_type(), "image/jpeg");
remove_compressed_test_file(output) remove_compressed_test_file(output)
@ -51,23 +51,23 @@ fn compress_to_10_mb() {
fn compress_to_range() { fn compress_to_range() {
let output = "tests/samples/output/compressed_range.jpg"; let output = "tests/samples/output/compressed_range.jpg";
let max_desired_size = 2_000_000; let max_desired_size = 2_000_000;
let mut desired_output_size = 500_000; let mut max_output_size = 500_000;
initialize(output); initialize(output);
while desired_output_size < max_desired_size { while max_output_size < max_desired_size {
let mut pars = caesium::initialize_parameters(); let mut pars = caesium::initialize_parameters();
caesium::compress_to_size( caesium::compress_to_size(
String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
String::from(output), String::from(output),
&mut pars, &mut pars,
desired_output_size max_output_size
).unwrap(); ).unwrap();
assert!(std::path::Path::new(output).exists()); assert!(std::path::Path::new(output).exists());
assert!(File::open(output).unwrap().metadata().unwrap().len() < desired_output_size as u64); assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
let kind = infer::get_from_path(output).unwrap().unwrap(); let kind = infer::get_from_path(output).unwrap().unwrap();
assert_eq!(kind.mime_type(), "image/jpeg"); assert_eq!(kind.mime_type(), "image/jpeg");
desired_output_size += 100_000; max_output_size += 100_000;
remove_compressed_test_file(output); remove_compressed_test_file(output);
} }
} }