2022-02-12 14:10:12 +01:00
|
|
|
use std::collections::HashMap;
|
2021-10-23 17:18:52 +02:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
2022-10-02 15:02:15 +02:00
|
|
|
use std::sync::Once;
|
2021-10-23 17:18:52 +02:00
|
|
|
|
2023-02-25 20:39:52 +01:00
|
|
|
use crate::cleanup::remove_compressed_test_file;
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-16 21:31:10 +02:00
|
|
|
fn jpeg_compress_80_with_metadata() {
|
2021-10-23 17:18:52 +02:00
|
|
|
let output = "tests/samples/output/compressed_80_metadata.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;
|
|
|
|
pars.keep_metadata = 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();
|
|
|
|
assert!(Path::new(output).exists());
|
|
|
|
assert!(metadata_is_equal(
|
|
|
|
Path::new("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
Path::new(output)
|
|
|
|
));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-16 21:31:10 +02:00
|
|
|
fn jpeg_optimize_with_metadata() {
|
2021-10-23 17:18:52 +02:00
|
|
|
let output = "tests/samples/output/optimized_metadata.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;
|
|
|
|
pars.keep_metadata = 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();
|
|
|
|
assert!(Path::new(output).exists());
|
|
|
|
assert!(metadata_is_equal(
|
|
|
|
Path::new("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
Path::new(output)
|
|
|
|
));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 14:10:12 +01:00
|
|
|
#[test]
|
2024-07-16 21:31:10 +02:00
|
|
|
fn jpeg_resize_optimize_with_metadata() {
|
2022-02-12 14:10:12 +01:00
|
|
|
let output = "tests/samples/output/resized_optimized_metadata.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.keep_metadata = true;
|
|
|
|
pars.width = 200;
|
|
|
|
pars.height = 200;
|
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();
|
|
|
|
assert!(Path::new(output).exists());
|
|
|
|
assert!(metadata_is_equal(
|
|
|
|
Path::new("tests/samples/uncompressed_드림캐쳐.jpg"),
|
|
|
|
Path::new(output)
|
|
|
|
));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_exif(path: &Path) -> HashMap<String, String> {
|
2022-10-02 15:02:15 +02:00
|
|
|
let file = fs::File::open(path).unwrap();
|
2022-02-12 14:10:12 +01:00
|
|
|
let mut bufreader = std::io::BufReader::new(&file);
|
|
|
|
let exif_reader = exif::Reader::new();
|
|
|
|
let exif = exif_reader.read_from_container(&mut bufreader).unwrap();
|
|
|
|
let mut exif_map = HashMap::new();
|
|
|
|
for f in exif.fields() {
|
2024-01-23 18:51:39 +01:00
|
|
|
exif_map.insert(format!("{}", f.tag), f.display_value().to_string());
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exif_map
|
|
|
|
}
|
2021-10-23 17:18:52 +02:00
|
|
|
|
2022-02-12 14:10:12 +01:00
|
|
|
fn metadata_is_equal(input: &Path, output: &Path) -> bool {
|
|
|
|
let original_exif_map = extract_exif(input);
|
|
|
|
let compressed_exif_map = extract_exif(output);
|
2021-10-23 17:18:52 +02:00
|
|
|
|
2022-02-12 14:10:12 +01:00
|
|
|
original_exif_map.eq(&compressed_exif_map)
|
2022-10-02 15:02:15 +02:00
|
|
|
}
|