Skip to content

Commit 4c2603d

Browse files
committed
b1display: Add heuristic to allow for grayscale images
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent a1b0029 commit 4c2603d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

inputmodule-control/src/inputmodule.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,19 @@ fn b1display_bw_image_cmd(serialdev: &str, image_path: &str) {
920920
assert!(width == 300);
921921
assert!(height == 400);
922922

923+
let (brightest, darkest) = img
924+
.pixels()
925+
.fold((0xFF, 0x00), |(brightest, darkest), pixel| {
926+
let br = pixel.0[0];
927+
let brightest = if br > brightest { br } else { brightest };
928+
let darkest = if br < darkest { br } else { darkest };
929+
(brightest, darkest)
930+
});
931+
let bright_diff = brightest - darkest;
932+
// Anything brighter than 90% between darkest and brightest counts as white
933+
// Just a heuristic. Don't use greyscale images! Use black and white instead
934+
let threshold = darkest + (bright_diff / 10) * 9;
935+
923936
for x in 0..300 {
924937
let mut vals: [u8; 2 + 50] = [0; 2 + 50];
925938
let column = (x as u16).to_le_bytes();
@@ -930,7 +943,7 @@ fn b1display_bw_image_cmd(serialdev: &str, image_path: &str) {
930943
for y in 0..400usize {
931944
let pixel = img.get_pixel(x, y as u32);
932945
let brightness = pixel.0[0];
933-
let black = brightness < 0xFF / 2;
946+
let black = brightness < threshold;
934947

935948
let bit = y % 8;
936949
if bit == 0 {

0 commit comments

Comments
 (0)