2024-01-23 18:51:39 +01:00
|
|
|
use crate::cleanup::remove_compressed_test_file;
|
2024-10-10 20:02:38 +02:00
|
|
|
use caesium::parameters::CSParameters;
|
2025-02-21 13:35:34 +01:00
|
|
|
use dssim::Val;
|
|
|
|
use std::{fs::File, sync::Once};
|
2023-02-25 20:39:52 +01:00
|
|
|
|
|
|
|
mod cleanup;
|
2021-10-23 17:18:52 +02:00
|
|
|
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub fn initialize(file: &str) {
|
|
|
|
INIT.call_once(|| {
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(file);
|
2021-10-23 17:18:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn diff(compressed: &str) -> Val {
|
|
|
|
let attr = dssim::Dssim::new();
|
2022-02-12 15:23:25 +01:00
|
|
|
let orig = dssim::load_image(&attr, "tests/samples/uncompressed_드림캐쳐.jpg").unwrap();
|
|
|
|
let comp = dssim::load_image(&attr, compressed).unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
let (diff, _) = attr.compare(&orig, comp);
|
|
|
|
diff
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_100() {
|
|
|
|
let output = "tests/samples/output/compressed_100.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 100;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2022-02-12 14:10:12 +01:00
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (2400, 1600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_80() {
|
|
|
|
let output = "tests/samples/output/compressed_80.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 80;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2022-02-12 14:10:12 +01:00
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (2400, 1600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_50() {
|
|
|
|
let output = "tests/samples/output/compressed_50.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 50;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2022-02-12 14:10:12 +01:00
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (2400, 1600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_10() {
|
|
|
|
let output = "tests/samples/output/compressed_10_드림캐쳐.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 10;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2022-02-12 14:10:12 +01:00
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (2400, 1600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
2023-06-10 14:29:42 +02:00
|
|
|
#[test]
|
|
|
|
fn compress_corrupted_lossy() {
|
|
|
|
let output = "tests/samples/output/corrupted_lossy.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2023-06-10 14:29:42 +02:00
|
|
|
pars.jpeg.quality = 50;
|
2025-02-21 13:35:34 +01:00
|
|
|
assert!(caesium::compress(String::from("tests/samples/corrupted.jpg"), String::from(output), &pars,).is_err())
|
2023-06-10 14:29:42 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
#[test]
|
|
|
|
fn optimize_jpeg() {
|
|
|
|
let output = "tests/samples/output/compressed_optimized_드림캐쳐.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.optimize = true;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2022-02-12 14:10:12 +01:00
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (2400, 1600));
|
2021-10-23 17:18:52 +02:00
|
|
|
|
|
|
|
//Floats error
|
|
|
|
assert!(diff(output) < 0.001);
|
2022-02-12 14:10:12 +01:00
|
|
|
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-10 14:29:42 +02:00
|
|
|
#[test]
|
|
|
|
fn compress_corrupted_lossless() {
|
|
|
|
let output = "tests/samples/output/corrupted_lossless.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2023-06-10 14:29:42 +02:00
|
|
|
pars.optimize = true;
|
2025-02-21 13:35:34 +01:00
|
|
|
assert!(caesium::compress(String::from("tests/samples/corrupted.jpg"), String::from(output), &pars,).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downscale_to_size() {
|
|
|
|
let max_output_size = 2_000_000;
|
|
|
|
let output = "tests/samples/output/downscale_800_600_to_size.jpg";
|
|
|
|
initialize(output);
|
|
|
|
let mut pars = CSParameters::new();
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
|
|
|
caesium::compress_to_size(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
2023-06-10 14:29:42 +02:00
|
|
|
String::from(output),
|
2025-02-21 13:35:34 +01:00
|
|
|
&mut pars,
|
|
|
|
max_output_size,
|
|
|
|
false,
|
2024-01-23 18:51:39 +01:00
|
|
|
)
|
2025-02-21 13:35:34 +01:00
|
|
|
.unwrap();
|
|
|
|
assert!(std::path::Path::new(output).exists());
|
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (800, 600));
|
|
|
|
remove_compressed_test_file(output)
|
2023-06-10 14:29:42 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 14:10:12 +01:00
|
|
|
#[test]
|
|
|
|
fn downscale_exact() {
|
|
|
|
let output = "tests/samples/output/downscale_800_600.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2022-02-12 14:10:12 +01:00
|
|
|
pars.jpeg.quality = 80;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-12 14:10:12 +01:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (800, 600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downscale_exact_optimize() {
|
|
|
|
let output = "tests/samples/output/downscale_optimize_800_600.jpg";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut pars = CSParameters::new();
|
2022-02-12 14:10:12 +01:00
|
|
|
pars.optimize = true;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
&pars,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-12 14:10:12 +01:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
|
|
|
let kind = infer::get_from_path(output).unwrap().unwrap();
|
|
|
|
assert_eq!(kind.mime_type(), "image/jpeg");
|
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (800, 600));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-10-02 15:02:15 +02:00
|
|
|
}
|