diff --git a/src/dmi.rs b/src/dmi.rs index 4536bc4e..9630b3c0 100644 --- a/src/dmi.rs +++ b/src/dmi.rs @@ -1,4 +1,5 @@ use crate::error::{Error, Result}; +use image::error::ImageError; use png::{Decoder, Encoder, OutputInfo}; use std::{ fs::{create_dir_all, File}, @@ -14,7 +15,7 @@ byond_fn! { dmi_create_png(path, width, height, data) { } } byond_fn! { dmi_resize_png(path, width, height) { - resize_png(path, width, height).err() + resize_png(path, width, height).err().map(|e| format!("{:?}", e)) } } fn strip_metadata(path: &str) -> Result<()> { @@ -68,11 +69,30 @@ fn create_png(path: &str, width: &str, height: &str, data: &str) -> Result<()> { Ok(writer.write_image_data(&result)?) } -fn resize_png(path: &str, width: &str, height: &str) -> Result<()> { +#[derive(Debug)] +enum ResizePngError { + StringParse(std::num::ParseIntError), + Image(ImageError), +} + +impl From for ResizePngError { + fn from(error: std::num::ParseIntError) -> Self { + ResizePngError::StringParse(error) + } +} + +impl From for ResizePngError { + fn from(error: ImageError) -> Self { + ResizePngError::Image(error) + } +} + +fn resize_png(path: &str, width: &str, height: &str) -> std::result::Result<(), ResizePngError> { let width = u32::from_str_radix(width, 10)?; let height = u32::from_str_radix(height, 10)?; let img = image::open(path)?; + let newimg = img.resize(width, height, image::imageops::Nearest); Ok(newimg.save_with_format(&path, image::ImageFormat::Png)?)