Skip to content

Commit

Permalink
Add dmi_resize_png using the image crate (#39)
Browse files Browse the repository at this point in the history
Tweak from panic to error return

Finally fix the resize_png fn (Oh god thank you PJB3005)

Allow user to pick resize mode, use rust_g::Error instead of custom enum
  • Loading branch information
ShadowLarkens authored Nov 4, 2020
1 parent 6c8674a commit ae6fa4d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
69 changes: 66 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ hex = { version = "0.4", optional = true }
percent-encoding = { version = "2.1", optional = true }
url-dep = { version = "2.1", package = "url", optional = true }
png = { version = "0.16", optional = true }
image = { version = "0.23.10", optional = true }
git2 = { version = "0.13", optional = true, default-features = false }
noise = { version = "0.6", optional = true}
reqwest = { version = "0.10", optional = true, default-features = false, features = ["blocking", "rustls-tls"] }
Expand All @@ -40,7 +41,7 @@ zip = { version = "0.5.8", optional = true }

[features]
default = ["dmi", "log", "git", "http", "json", "sql", "noise"]
dmi = ["png"]
dmi = ["png", "image"]
file = []
hash = ["md-5", "sha-1", "sha2", "hex"]
json = ["serde", "serde_json"]
Expand Down
3 changes: 2 additions & 1 deletion dmsrc/dmi.dm
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#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_create_png(path, width, height, data) call(RUST_G, "dmi_create_png")(path, width, height, data)
#define rustg_dmi_resize_png(path, width, height, resizetype) call(RUST_G, "dmi_resize_png")(path, width, height, resizetype)
25 changes: 24 additions & 1 deletion src/dmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ byond_fn! { dmi_strip_metadata(path) {
} }

byond_fn! { dmi_create_png(path, width, height, data) {
create_png(path, width, height , data).err()
create_png(path, width, height, data).err()
} }

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 @@ -63,3 +75,14 @@ fn create_png(path: &str, width: &str, height: &str, data: &str) -> Result<()> {
let mut writer = encoder.write_header()?;
Ok(writer.write_image_data(&result)?)
}

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.as_ref())?;

let newimg = img.resize(width, height, resizetype);

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};

#[cfg(feature = "unzip")]
use zip::result::ZipError;
Expand Down Expand Up @@ -36,6 +38,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 ae6fa4d

Please sign in to comment.