Skip to content

Commit

Permalink
Update encode and decode from u8 array; update wasm binding
Browse files Browse the repository at this point in the history
  • Loading branch information
c12i committed Nov 5, 2023
1 parent ec2b888 commit faa8da0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
Binary file removed out.png
Binary file not shown.
4 changes: 2 additions & 2 deletions stegano-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ImgStegano {
) -> Result<Vec<u8>, ImgSteganoError> {
let image_format = ImageFormat::from_extension(image_extension)
.ok_or(ImgSteganoError::InvalidImageFormat)?;
let image = image::load_from_memory_with_format(input_image, image_format)?;
let image = image::load_from_memory(input_image)?;
let encoded_image = Self::encode_from_image(image.into(), message);
let Image(encoded_image) = encoded_image;
let mut encoded: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -92,7 +92,7 @@ impl ImgStegano {
}

pub fn decode_from_u8_array(input_image: &[u8]) -> Result<String, ImgSteganoError> {
let image = image::load_from_memory_with_format(input_image, image::ImageFormat::Png)?;
let image = image::load_from_memory(input_image)?;
let decoded = Self::decode_from_image(&image.into());
Ok(decoded)
}
Expand Down
1 change: 1 addition & 0 deletions stegano-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ img_stegano = {path = "../"}
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
image = "0.24.7"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
3 changes: 3 additions & 0 deletions stegano-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod utils;

use img_stegano::ImgStegano;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn encode_text(input_image: &[u8], image_extension: &str, message: &str) -> Vec<u8> {
utils::set_panic_hook();
ImgStegano::encode_from_u8_array(input_image, image_extension, message).unwrap()
}

Expand Down
10 changes: 10 additions & 0 deletions stegano-wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

0 comments on commit faa8da0

Please sign in to comment.