Dither images using wasm plugin
This commit is contained in:
parent
179d0653bb
commit
92ec72ab4b
14 changed files with 116 additions and 15 deletions
|
|
@ -6,9 +6,11 @@ edition = { workspace = true }
|
|||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies.wasm-minimal-protocol]
|
||||
git = "https://github.com/astrale-sharp/wasm-minimal-protocol.git"
|
||||
rev = "90336ebf2d99844fd8f8e99ea7096af96526cbf4"
|
||||
[dependencies]
|
||||
image = { workspace = true, features = ["png"] }
|
||||
mark = { workspace = true }
|
||||
palette = { workspace = true }
|
||||
wasm-minimal-protocol = { workspace = true }
|
||||
|
||||
[profile.release]
|
||||
lto = true # Enable link-time optimization
|
||||
|
|
|
|||
|
|
@ -1,8 +1,29 @@
|
|||
use wasm_minimal_protocol::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
use image::ImageFormat;
|
||||
use mark::dither::{AlgoFloydSteinberg, Algorithm, DiffEuclid, Palette};
|
||||
use palette::LinSrgb;
|
||||
use wasm_minimal_protocol::{initiate_protocol, wasm_func};
|
||||
|
||||
initiate_protocol!();
|
||||
|
||||
#[wasm_func]
|
||||
pub fn debug_print(arg: &[u8]) -> Vec<u8> {
|
||||
format!("{arg:?}").into_bytes()
|
||||
pub fn dither(image: &[u8]) -> Result<Vec<u8>, String> {
|
||||
let image = image::load_from_memory(image)
|
||||
.map_err(|it| format!("Failed to read image: {it:?}"))?
|
||||
.to_rgba8();
|
||||
|
||||
let palette = Palette::new(vec![
|
||||
LinSrgb::new(0.0, 0.0, 0.0),
|
||||
LinSrgb::new(1.0, 1.0, 1.0),
|
||||
]);
|
||||
|
||||
let dithered = <AlgoFloydSteinberg as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette);
|
||||
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
dithered
|
||||
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
|
||||
.map_err(|it| format!("Failed to write image: {it:?}"))?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue