rust backend for processing wallpaper images

This commit is contained in:
Vomitblood 2024-08-08 02:35:06 +08:00
parent 3b7c771c85
commit aafa0b9430

View file

@ -23,25 +23,24 @@ enum ImageType {
// // determine the file format // // determine the file format
// } // }
/// Determines the image type of the file at the given path. /// determines the image type of a file that is passed in using the filepath
/// Returns `Ok(ImageType)` on success, or an `Err(String)` if there is an error.
fn determine_image_type(file_path: &std::path::Path) -> Result<ImageType, String> { fn determine_image_type(file_path: &std::path::Path) -> Result<ImageType, String> {
// Open the file // open the file
let mut file = let mut file =
std::fs::File::open(file_path).map_err(|e| format!("Failed to open file: {e}"))?; std::fs::File::open(file_path).map_err(|e| format!("Failed to open file: {e}"))?;
// Read the first few bytes to determine the format // read the first few bytes to determine the format
let mut buffer = [0; 12]; let mut buffer = [0; 12];
std::io::Read::read_exact(&mut file, &mut buffer) std::io::Read::read_exact(&mut file, &mut buffer)
.map_err(|e| format!("Failed to read file: {e}"))?; .map_err(|e| format!("Failed to read file: {e}"))?;
// Check static formats using the image crate // with hopes and prayers, try to guess the format and pray that it is correct haha
match image::guess_format(&buffer) { match image::guess_format(&buffer) {
Ok(image::ImageFormat::Jpeg) => Ok(ImageType::Jpeg), Ok(image::ImageFormat::Jpeg) => Ok(ImageType::Jpeg),
Ok(image::ImageFormat::Png) => Ok(ImageType::Png), Ok(image::ImageFormat::Png) => Ok(ImageType::Png),
Ok(image::ImageFormat::Gif) => Ok(ImageType::Gif), Ok(image::ImageFormat::Gif) => Ok(ImageType::Gif),
Ok(image::ImageFormat::WebP) => { Ok(image::ImageFormat::WebP) => {
// Additional check for animation in WebP // additional check for webp
is_animated_webp(file_path).map(|animated| { is_animated_webp(file_path).map(|animated| {
if animated { if animated {
ImageType::AnimatedWebP ImageType::AnimatedWebP
@ -70,6 +69,45 @@ fn is_animated_webp(file_path: &std::path::Path) -> Result<bool, String> {
.map(|anim| anim.has_animation()) .map(|anim| anim.has_animation())
.map_err(|_| "File is not a valid WebP image".to_string()) .map_err(|_| "File is not a valid WebP image".to_string())
} }
/// crop static images (jpeg, png and still webp) to a maximum of 1280x800 and maintaining aspect ratio
fn crop_static_image(
file_path: &std::path::Path,
destination: &std::path::Path,
) -> Result<(), String> {
let img = image::open(file_path).map_err(|e| format!("Failed to open image: {e}"))?;
let (width, height) = image::GenericImageView::dimensions(&img);
let resized_img = if width > 1280 || height > 800 {
img.resize(1280, 800, image::imageops::FilterType::Lanczos3)
} else {
img
};
let mut output_file = std::fs::File::create(destination)
.map_err(|e: std::io::Error| format!("Failed to create file: {e}"))?;
resized_img
.write_to(&mut output_file, image::ImageFormat::WebP)
.map_err(|e| format!("Failed to save image as WebP: {}", e))?;
Ok(())
}
/// crop animated webp images
fn crop_animated_webp(
file_path: &std::path::Path,
destination: &std::path::Path,
) -> Result<(), String> {
let mut buffer = Vec::new();
std::fs::File::open(file_path)
.and_then(|mut file| std::io::Read::read_to_end(&mut file, &mut buffer))
.map_err(|e| format!("Failed to read file: {}", e))?;
let decoder = webp::AnimDecoder::new(&buffer);
let decoded = decoder
.decode()
.map_err(|e| format!("Failed to decode WebP animation: {e}"))?;
}
fn main() { fn main() {
// Example usage // Example usage
let path = std::path::Path::new("/home/vomitblood/Downloads/title.keys"); let path = std::path::Path::new("/home/vomitblood/Downloads/title.keys");