From 85b851a28beca0a2f5ef48587a5e978c9f72d7f8 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 16 Mar 2021 12:38:25 +0530 Subject: add dithering primitives --- src/dither.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/dither.rs 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 @@ +use crate::bitmap::MapPoint; + +// 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) + +// 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!"), + } + } +} -- cgit v1.2.3