清风浮云net 发表于 2022-10-22 08:54

基于rust实现Floyd Steinberg效果

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

#
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
页: [1]
查看完整版本: 基于rust实现Floyd Steinberg效果