diff options
author | Akshay <[email protected]> | 2021-03-16 07:08:25 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-16 07:08:25 +0000 |
commit | 85b851a28beca0a2f5ef48587a5e978c9f72d7f8 (patch) | |
tree | 497e775e2433eff772f5a723ab8fcc05bc4e47c7 | |
parent | 2d4f2b5a8c0ba1dbec8e295a1b5605dd9028d071 (diff) |
add dithering primitives
-rw-r--r-- | src/dither.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/dither.rs b/src/dither.rs new file mode 100644 index 0000000..c19bd6d --- /dev/null +++ b/src/dither.rs | |||
@@ -0,0 +1,67 @@ | |||
1 | use crate::bitmap::MapPoint; | ||
2 | |||
3 | // dither intensities (rylander) | ||
4 | // 0: none | ||
5 | // 1: (1, 3) | ||
6 | // 2: (3, 1) | ||
7 | // 3: (3, 3) | ||
8 | // 4: (1, 1) | ||
9 | // 5: (1, 2) | ||
10 | // 6: (3, 0) | ||
11 | // 7: (3, 2) | ||
12 | // 8: (1, 0) | ||
13 | // 9: (0, 3) | ||
14 | // 10: (2, 1) | ||
15 | // 11: (2, 3) | ||
16 | // 12: (0, 1) | ||
17 | // 13: (0, 2) | ||
18 | // 14: (2, 0) | ||
19 | // 15: (2, 2) | ||
20 | // 16: (0, 0) | ||
21 | |||
22 | // dither intensities (bayers) | ||
23 | // 0: none | ||
24 | // 1: (0, 0) | ||
25 | // 2: (2, 2) | ||
26 | // 3: (0, 2) | ||
27 | // 4: (2, 0) | ||
28 | // 5: (1, 1) | ||
29 | // 6: (3, 3) | ||
30 | // 7: (1, 3) | ||
31 | // 8: (3, 1) | ||
32 | // 9: (0, 1) | ||
33 | // 10: (0, 3) | ||
34 | // 11: (2, 3) | ||
35 | // 12: (2, 1) | ||
36 | // 13: (1, 0) | ||
37 | // 14: (3, 2) | ||
38 | // 15: (1, 2) | ||
39 | // 16: (3, 0) | ||
40 | |||
41 | pub fn bayer(level: u8, pt: MapPoint) -> bool { | ||
42 | if level == 0 { | ||
43 | false | ||
44 | } else { | ||
45 | let MapPoint { x, y } = pt; | ||
46 | let r = (x % 4, y % 4); | ||
47 | match level { | ||
48 | 01 => r == (0, 0) || bayer(00, pt), | ||
49 | 02 => r == (2, 2) || bayer(01, pt), | ||
50 | 03 => r == (0, 2) || bayer(02, pt), | ||
51 | 04 => r == (2, 0) || bayer(03, pt), | ||
52 | 05 => r == (1, 1) || bayer(04, pt), | ||
53 | 06 => r == (3, 3) || bayer(05, pt), | ||
54 | 07 => r == (1, 3) || bayer(06, pt), | ||
55 | 08 => r == (3, 1) || bayer(07, pt), | ||
56 | 09 => r == (0, 1) || bayer(08, pt), | ||
57 | 10 => r == (0, 3) || bayer(09, pt), | ||
58 | 11 => r == (2, 3) || bayer(10, pt), | ||
59 | 12 => r == (2, 1) || bayer(11, pt), | ||
60 | 13 => r == (1, 0) || bayer(12, pt), | ||
61 | 14 => r == (3, 2) || bayer(13, pt), | ||
62 | 15 => r == (1, 2) || bayer(14, pt), | ||
63 | 16 => r == (3, 0) || bayer(15, pt), | ||
64 | _ => panic!("Invalid dither level!"), | ||
65 | } | ||
66 | } | ||
67 | } | ||