Skip to content

Commit

Permalink
Add dmi_resize_png using the image crate
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)
  • Loading branch information
ShadowLarkens committed Oct 14, 2020
1 parent a7e803a commit 2e3cf13
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 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 @@ -39,7 +40,7 @@ dashmap = { version = "3.11", 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
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +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)
"#).unwrap();
}

Expand Down
36 changes: 35 additions & 1 deletion 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 @@ -10,7 +11,11 @@ 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) {
resize_png(path, width, height).err().map(|e| format!("{:?}", e))
} }

fn strip_metadata(path: &str) -> Result<()> {
Expand Down Expand Up @@ -63,3 +68,32 @@ fn create_png(path: &str, width: &str, height: &str, data: &str) -> Result<()> {
let mut writer = encoder.write_header()?;
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> {
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)?)
}

0 comments on commit 2e3cf13

Please sign in to comment.