Skip to content

Commit

Permalink
Allow user to pick resize mode, use rust_g::Error instead of custom enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowLarkens committed Oct 15, 2020
1 parent 2e3cf13 commit 19f9367
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() {
write!(f, r#"
#define rustg_dmi_strip_metadata(fname) call(RUST_G, "dmi_strip_metadata")(fname)
#define rustg_dmi_create_png(path, width, height, data) call(RUST_G, "dmi_create_png")(path, width, height, data)
#define rustg_dmi_resize_png(path, width, height) call(RUST_G, "dmi_resize_png")(path, width, height)
#define rustg_dmi_resize_png(path, width, height, resizetype) call(RUST_G, "dmi_resize_png")(path, width, height, resizetype)
"#).unwrap();
}

Expand Down
39 changes: 14 additions & 25 deletions src/dmi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::{Error, Result};
use image::error::ImageError;
use png::{Decoder, Encoder, OutputInfo};
use std::{
fs::{create_dir_all, File},
Expand All @@ -14,8 +13,16 @@ byond_fn! { dmi_create_png(path, width, height, data) {
create_png(path, width, height, data).err()
} }

byond_fn! { dmi_resize_png(path, width, height) {
resize_png(path, width, height).err().map(|e| format!("{:?}", e))
byond_fn! { dmi_resize_png(path, width, height, resizetype) {
let resizetype = match resizetype {
"catmull" => image::imageops::CatmullRom,
"gaussian" => image::imageops::Gaussian,
"lanczos3" => image::imageops::Lanczos3,
"nearest" => image::imageops::Nearest,
"triangle" => image::imageops::Triangle,
_ => image::imageops::Nearest,
};
resize_png(path, width, height, resizetype).err()
} }

fn strip_metadata(path: &str) -> Result<()> {
Expand Down Expand Up @@ -69,31 +76,13 @@ fn create_png(path: &str, width: &str, height: &str, data: &str) -> Result<()> {
Ok(writer.write_image_data(&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> {
fn resize_png<P: AsRef<Path>>(path: P, width: &str, height: &str, resizetype: image::imageops::FilterType) -> std::result::Result<(), Error> {
let width = u32::from_str_radix(width, 10)?;
let height = u32::from_str_radix(height, 10)?;

let img = image::open(path)?;
let img = image::open(path.as_ref())?;

let newimg = img.resize(width, height, image::imageops::Nearest);
let newimg = img.resize(width, height, resizetype);

Ok(newimg.save_with_format(&path, image::ImageFormat::Png)?)
Ok(newimg.save_with_format(path.as_ref(), image::ImageFormat::Png)?)
}
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use thiserror::Error;

#[cfg(feature = "png")]
use png::{DecodingError, EncodingError};
#[cfg(feature = "png")]
use image::error::{ImageError};

pub type Result<T> = result::Result<T, Error>;

Expand All @@ -33,6 +35,8 @@ pub enum Error {
ParseIntError(#[from] ParseIntError),
#[error(transparent)]
ParseFloatError(#[from] ParseFloatError),
#[error(transparent)]
GenericImageError(#[from] ImageError),
#[cfg(feature = "png")]
#[error("Invalid png data.")]
InvalidPngDataError,
Expand Down

0 comments on commit 19f9367

Please sign in to comment.