aboutsummaryrefslogtreecommitdiff
path: root/src/dither.rs
blob: 25c59c819774ffa3c0043ac95497114f308de1b2 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::bitmap::MapPoint;

// dither intensities (bayers)
//  0: none
//  1: (0, 0)
//  2: (2, 2)
//  3: (0, 2)
//  4: (2, 0)
//  5: (1, 1)
//  6: (3, 3)
//  7: (1, 3)
//  8: (3, 1)
//  9: (0, 1)
// 10: (0, 3)
// 11: (2, 3)
// 12: (2, 1)
// 13: (1, 0)
// 14: (3, 2)
// 15: (1, 2)
// 16: (3, 0)

pub fn bayer(level: u8, pt: MapPoint) -> bool {
    if level == 0 {
        false
    } else {
        let MapPoint { x, y } = pt;
        let r = (x % 4, y % 4);
        match level {
            1 => r == (0, 0) || bayer(0, pt),
            2 => r == (2, 2) || bayer(1, pt),
            3 => r == (0, 2) || bayer(2, pt),
            4 => r == (2, 0) || bayer(3, pt),
            5 => r == (1, 1) || bayer(4, pt),
            6 => r == (3, 3) || bayer(5, pt),
            7 => r == (1, 3) || bayer(6, pt),
            8 => r == (3, 1) || bayer(7, pt),
            9 => r == (0, 1) || bayer(8, pt),
            10 => r == (0, 3) || bayer(9, pt),
            11 => r == (2, 3) || bayer(10, pt),
            12 => r == (2, 1) || bayer(11, pt),
            13 => r == (1, 0) || bayer(12, pt),
            14 => r == (3, 2) || bayer(13, pt),
            15 => r == (1, 2) || bayer(14, pt),
            16 => r == (3, 0) || bayer(15, pt),
            _ => panic!("Invalid dither level!"),
        }
    }
}

// dither intensities (rylander)
//  0: none
//  1: (1, 3)
//  2: (3, 1)
//  3: (3, 3)
//  4: (1, 1)
//  5: (1, 2)
//  6: (3, 0)
//  7: (3, 2)
//  8: (1, 0)
//  9: (0, 3)
// 10: (2, 1)
// 11: (2, 3)
// 12: (0, 1)
// 13: (0, 2)
// 14: (2, 0)
// 15: (2, 2)
// 16: (0, 0)

pub fn rylander(level: u8, pt: MapPoint) -> bool {
    if level == 0 {
        false
    } else {
        let MapPoint { x, y } = pt;
        let r = (x % 4, y % 4);
        match level {
            1 => r == (1, 3) || rylander(0, pt),
            2 => r == (3, 1) || rylander(1, pt),
            3 => r == (3, 3) || rylander(2, pt),
            4 => r == (1, 1) || rylander(3, pt),
            5 => r == (1, 2) || rylander(4, pt),
            6 => r == (3, 0) || rylander(5, pt),
            7 => r == (3, 2) || rylander(6, pt),
            8 => r == (1, 0) || rylander(7, pt),
            9 => r == (0, 3) || rylander(8, pt),
            10 => r == (2, 1) || rylander(9, pt),
            11 => r == (2, 3) || rylander(10, pt),
            12 => r == (0, 1) || rylander(11, pt),
            13 => r == (0, 2) || rylander(12, pt),
            14 => r == (2, 0) || rylander(13, pt),
            15 => r == (2, 2) || rylander(14, pt),
            16 => r == (0, 0) || rylander(15, pt),
            _ => panic!("Invalid dither level!"),
        }
    }
}