libcaesium/tests/metadata.rs

104 lines
2.8 KiB
Rust
Raw Normal View History

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]
fn compress_80_with_metadata() {
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]
fn optimize_with_metadata() {
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]
fn resize_optimize_with_metadata() {
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() {
2022-10-02 15:02:15 +02:00
exif_map.insert(
format!("{}", f.tag),
2023-08-12 14:44:59 +02:00
f.display_value().to_string(),
2022-10-02 15:02:15 +02:00
);
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
}