Skip to content

Commit

Permalink
Finally fix the resize_png fn (Oh god thank you PJB3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowLarkens committed Oct 9, 2020
1 parent 7d17b6e commit d399f41
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/dmi.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<()> {
Expand Down Expand Up @@ -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<std::num::ParseIntError> for ResizePngError {
fn from(error: std::num::ParseIntError) -> Self {
ResizePngError::StringParse(error)
}
}

impl From<ImageError> 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)?)
Expand Down

0 comments on commit d399f41

Please sign in to comment.