欢迎光临
我们一直在努力

基于rust实现Floyd Steinberg效果

use image::{DynamicImage, ImageBuffer};
use wasm_bindgen::prelude::*;
use web_sys::ImageData;

#[wasm_bindgen]
pub fn floyd_steinberg(image_data: ImageData) -> Vec<u8> {
    let width = image_data.width();
    let height = image_data.height();
    let raw_pixels = image_data.data().to_vec();
    _floyd_steinberg(width, height, raw_pixels)
}

fn _floyd_steinberg(width: u32, height: u32, data: Vec<u8>) -> Vec<u8> {
    let image_buf = ImageBuffer::from_vec(width, height, data).unwrap();
    let image = DynamicImage::ImageRgba8(image_buf);
    let raw_pixels = image.to_luma8().to_vec();
    let color_map = image::imageops::colorops::BiLevel;
    let mut img_buffer = ImageBuffer::from_vec(width, height, raw_pixels).unwrap();
    image::imageops::colorops::dither(&mut img_buffer, &color_map);
    let dither_img = image::DynamicImage::ImageLuma8(img_buffer);
    dither_img.to_rgba8().to_vec()
}

效果:

更多效果查看:https://yirandidi.github.io/yr_rust_konva/
源码:https://github.com/yirandidi/yr_rust_konva

赞(0) 打赏
未经允许不得转载:哈哈网 » 基于rust实现Floyd Steinberg效果

相关推荐

  • 暂无文章

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏