2022-02-12 15:23:25 +01:00
|
|
|
use dssim::Val;
|
2021-10-23 17:18:52 +02:00
|
|
|
use std::sync::Once;
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub fn initialize(file: &str) {
|
|
|
|
INIT.call_once(|| {
|
|
|
|
if fs::metadata(file).is_ok() {
|
|
|
|
fs::remove_file(file).unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cleanup(file: &str) {
|
|
|
|
if fs::metadata(file).is_ok() {
|
|
|
|
fs::remove_file(file).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 100;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_80() {
|
|
|
|
let output = "tests/samples/output/compressed_80.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 80;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_50() {
|
|
|
|
let output = "tests/samples/output/compressed_50.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 50;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_10() {
|
|
|
|
let output = "tests/samples/output/compressed_10_드림캐쳐.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 10;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn optimize_jpeg() {
|
|
|
|
let output = "tests/samples/output/compressed_optimized_드림캐쳐.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.optimize = true;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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
|
|
|
|
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downscale_exact() {
|
|
|
|
let output = "tests/samples/output/downscale_800_600.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2022-02-12 14:10:12 +01:00
|
|
|
pars.jpeg.quality = 80;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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));
|
|
|
|
cleanup(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downscale_exact_optimize() {
|
|
|
|
let output = "tests/samples/output/downscale_optimize_800_600.jpg";
|
|
|
|
initialize(output);
|
2022-02-23 17:50:48 +01:00
|
|
|
let mut pars = caesium::initialize_parameters();
|
2022-02-12 14:10:12 +01:00
|
|
|
pars.optimize = true;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
2022-02-23 17:50:48 +01:00
|
|
|
caesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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));
|
2021-10-23 17:18:52 +02:00
|
|
|
cleanup(output)
|
|
|
|
}
|