aboutsummaryrefslogtreecommitdiff
path: root/src/dither.rs
blob: 99e283985140c16e91eab3794f665c2ed310c1a2 (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 {
            01 => r == (0, 0) || bayer(00, pt),
            02 => r == (2, 2) || bayer(01, pt),
            03 => r == (0, 2) || bayer(02, pt),
            04 => r == (2, 0) || bayer(03, pt),
            05 => r == (1, 1) || bayer(04, pt),
            06 => r == (3, 3) || bayer(05, pt),
            07 => r == (1, 3) || bayer(06, pt),
            08 => r == (3, 1) || bayer(07, pt),
            09 => r == (0, 1) || bayer(08, pt),
            10 => r == (0, 3) || bayer(09, 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 {
            01 => r == (1, 3) || rylander(00, pt),
            02 => r == (3, 1) || rylander(01, pt),
            03 => r == (3, 3) || rylander(02, pt),
            04 => r == (1, 1) || rylander(03, pt),
            05 => r == (1, 2) || rylander(04, pt),
            06 => r == (3, 0) || rylander(05, pt),
            07 => r == (3, 2) || rylander(06, pt),
            08 => r == (1, 0) || rylander(07, pt),
            09 => r == (0, 3) || rylander(08, pt),
            10 => r == (2, 1) || rylander(09, 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!"),
        }
    }
}