use rustybara::encode::{save, OutputFormat};
#[test]
fn encode_jpg_embeds_dpi_in_jfif_header() {
let img = image::DynamicImage::new_rgb8(10, 10);
let dir = std::env::temp_dir().join("rustybara_test_encode");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("encode_dpi_test.jpg");
save(&img, &path, &OutputFormat::Jpg, 300).unwrap();
let bytes = std::fs::read(&path).unwrap();
assert_eq!(&bytes[0..2], &[0xFF, 0xD8], "missing JPEG SOI");
assert_eq!(&bytes[2..4], &[0xFF, 0xE0], "missing JFIF APP0 marker");
assert_eq!(bytes[13], 0x01, "JFIF units should be 0x01 (pixels/inch)");
let x_density = u16::from_be_bytes([bytes[14], bytes[15]]);
assert_eq!(x_density, 300, "JPEG Xdensity should be 300");
let y_density = u16::from_be_bytes([bytes[16], bytes[17]]);
assert_eq!(y_density, 300, "JPEG Ydensity should be 300");
std::fs::remove_file(&path).ok();
}
#[test]
fn extension_jpg() {
assert_eq!(OutputFormat::Jpg.extension(), "jpg");
}
#[test]
fn extension_png() {
assert_eq!(OutputFormat::Png.extension(), "png");
}
#[test]
fn extension_webp() {
assert_eq!(OutputFormat::WebP.extension(), "webp");
}
#[test]
fn extension_tiff() {
assert_eq!(OutputFormat::Tiff.extension(), "tiff");
}
#[test]
fn encode_save_png_writes_file() {
let img = image::DynamicImage::new_rgb8(10, 10);
let dir = std::env::temp_dir().join("rustybara_test_encode");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("encode_test.png");
save(&img, &path, &OutputFormat::Png, 150).unwrap();
assert!(path.exists());
assert!(std::fs::metadata(&path).unwrap().len() > 0);
std::fs::remove_file(&path).ok();
}
#[test]
fn encode_save_jpg_writes_file() {
let img = image::DynamicImage::new_rgb8(10, 10);
let dir = std::env::temp_dir().join("rustybara_test_encode");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("encode_test.jpg");
save(&img, &path, &OutputFormat::Jpg, 150).unwrap();
assert!(path.exists());
assert!(std::fs::metadata(&path).unwrap().len() > 0);
std::fs::remove_file(&path).ok();
}
#[test]
fn encode_save_webp_writes_file() {
let img = image::DynamicImage::new_rgb8(10, 10);
let dir = std::env::temp_dir().join("rustybara_test_encode");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("encode_test.webp");
save(&img, &path, &OutputFormat::WebP, 150).unwrap();
assert!(path.exists());
assert!(std::fs::metadata(&path).unwrap().len() > 0);
std::fs::remove_file(&path).ok();
}
#[test]
fn encode_save_tiff_writes_file() {
let img = image::DynamicImage::new_rgb8(10, 10);
let dir = std::env::temp_dir().join("rustybara_test_encode");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("encode_test.tiff");
save(&img, &path, &OutputFormat::Tiff, 150).unwrap();
assert!(path.exists());
assert!(std::fs::metadata(&path).unwrap().len() > 0);
std::fs::remove_file(&path).ok();
}