~raph/blurrr

ref: fa3bce5fbcf5640b1414c10ef93c57d4db41793e blurrr/src/image.rs -rw-r--r-- 1001 bytes
fa3bce5fRaph Levien All parameters automatically tuned 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use druid::piet::{Image, ImageFormat};
use druid::{PaintCtx, RenderContext};

// Make a grayscale image from a single intensity map.
#[allow(unused)]
pub fn make_image_one(ctx: &mut PaintCtx, w: usize, h: usize, data: &[u8]) -> Image {
    let mut buf = vec![255u8; w * h * 4];
    for i in 0..(w * h) {
        let g = data[i];
        buf[i * 4] = g;
        buf[i * 4 + 1] = g;
        buf[i * 4 + 2] = g;
    }
    ctx.make_image(w, h, &buf, ImageFormat::RgbaPremul).unwrap()
}

const N_STEPS: f64 = 6.0;

fn quantize(b: u8) -> u8 {
    ((b as f64 * (N_STEPS / 255.0)).round() * (255.0 / N_STEPS)).round() as u8
}

pub fn make_image_two(ctx: &mut PaintCtx, w: usize, h: usize, d0: &[u8], d1: &[u8]) -> Image {
    let mut buf = vec![255u8; w * h * 4];
    for i in 0..(w * h) {
        let r = quantize(d0[i]);
        let g = quantize(d1[i]);
        buf[i * 4] = r;
        buf[i * 4 + 1] = g;
        buf[i * 4 + 2] = g;
    }
    ctx.make_image(w, h, &buf, ImageFormat::RgbaPremul).unwrap()
}