mirror of
https://github.com/Vomitblood/stort.git
synced 2024-11-26 05:45:26 +08:00
rust backend for processing wallpaper images
This commit is contained in:
parent
3b7c771c85
commit
aafa0b9430
|
@ -23,25 +23,24 @@ enum ImageType {
|
|||
// // determine the file format
|
||||
// }
|
||||
|
||||
/// Determines the image type of the file at the given path.
|
||||
/// Returns `Ok(ImageType)` on success, or an `Err(String)` if there is an error.
|
||||
/// determines the image type of a file that is passed in using the filepath
|
||||
fn determine_image_type(file_path: &std::path::Path) -> Result<ImageType, String> {
|
||||
// Open the file
|
||||
// open the file
|
||||
let mut file =
|
||||
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];
|
||||
std::io::Read::read_exact(&mut file, &mut buffer)
|
||||
.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) {
|
||||
Ok(image::ImageFormat::Jpeg) => Ok(ImageType::Jpeg),
|
||||
Ok(image::ImageFormat::Png) => Ok(ImageType::Png),
|
||||
Ok(image::ImageFormat::Gif) => Ok(ImageType::Gif),
|
||||
Ok(image::ImageFormat::WebP) => {
|
||||
// Additional check for animation in WebP
|
||||
// additional check for webp
|
||||
is_animated_webp(file_path).map(|animated| {
|
||||
if animated {
|
||||
ImageType::AnimatedWebP
|
||||
|
@ -70,6 +69,45 @@ fn is_animated_webp(file_path: &std::path::Path) -> Result<bool, String> {
|
|||
.map(|anim| anim.has_animation())
|
||||
.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() {
|
||||
// Example usage
|
||||
let path = std::path::Path::new("/home/vomitblood/Downloads/title.keys");
|
||||
|
|
Loading…
Reference in a new issue