2021-10-23 17:18:52 +02:00
|
|
|
use imgref::{Img, ImgVec};
|
|
|
|
use std::path::Path;
|
|
|
|
use dssim::{RGBAPLU, ToRGBAPLU, Val};
|
|
|
|
use load_image::ImageData;
|
|
|
|
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 load<P: AsRef<Path>>(path: P) -> Result<ImgVec<RGBAPLU>, lodepng::Error> {
|
|
|
|
let img = load_image::load_image(path.as_ref(), false)?;
|
|
|
|
match img.bitmap {
|
|
|
|
ImageData::RGB8(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::RGB16(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::RGBA8(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::RGBA16(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::GRAY8(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::GRAY16(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::GRAYA8(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
ImageData::GRAYA16(ref bitmap) => Ok(Img::new(bitmap.to_rgbaplu(), img.width, img.height)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn diff(compressed: &str) -> Val {
|
|
|
|
let attr = dssim::Dssim::new();
|
|
|
|
let orig = attr.create_image(&load("tests/samples/uncompressed_드림캐쳐.jpg").unwrap()).unwrap();
|
|
|
|
let comp = attr.create_image(&load(compressed).unwrap()).unwrap();
|
|
|
|
let (diff, _) = attr.compare(&orig, comp);
|
|
|
|
diff
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_100() {
|
|
|
|
let output = "tests/samples/output/compressed_100.jpg";
|
|
|
|
initialize(output);
|
2022-01-24 16:21:05 +01:00
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 100;
|
2022-01-24 16:21:05 +01:00
|
|
|
libcaesium::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-01-24 16:21:05 +01:00
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 80;
|
2022-01-24 16:21:05 +01:00
|
|
|
libcaesium::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-01-24 16:21:05 +01:00
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 50;
|
2022-01-24 16:21:05 +01:00
|
|
|
libcaesium::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-01-24 16:21:05 +01:00
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.jpeg.quality = 10;
|
2022-01-24 16:21:05 +01:00
|
|
|
libcaesium::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-01-24 16:21:05 +01:00
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
2021-10-23 17:18:52 +02:00
|
|
|
pars.optimize = true;
|
2022-01-24 16:21:05 +01:00
|
|
|
libcaesium::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);
|
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
|
|
|
pars.jpeg.quality = 80;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
|
|
|
libcaesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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_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);
|
|
|
|
let mut pars = libcaesium::initialize_parameters();
|
|
|
|
pars.optimize = true;
|
|
|
|
pars.width = 800;
|
|
|
|
pars.height = 600;
|
|
|
|
libcaesium::compress(String::from("tests/samples/uncompressed_드림캐쳐.jpg"), String::from(output), pars).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_eq!(image::image_dimensions(output).unwrap(), (800, 600));
|
2021-10-23 17:18:52 +02:00
|
|
|
cleanup(output)
|
|
|
|
}
|