Skip to content
Snippets Groups Projects
Commit ba5de2df authored by Stephen D's avatar Stephen D
Browse files

pixel interpolation and correct for y flip

parent c41c8000
No related branches found
No related tags found
No related merge requests found
use anyhow::bail;
use image::{GenericImageView, ImageReader};
use image::{GenericImageView, ImageReader, Rgba};
const TOAST_TIME_SECONDS: u64 = 30;
const TOAST_TIME_SECONDS: u64 = 10;
const CENTER_X: f64 = 100.0;
const CENTER_Y: f64 = 100.0;
const BREAD_HEIGHT: f64 = 0.2;
const BREAD_HEIGHT: f64 = 0.3;
const IDLE_HEIGHT: f64 = BREAD_HEIGHT + 8.0;
const PIXEL_SIZE_MM: f64 = 3.0;
const PIXEL_SIZE_MM: f64 = 1.5;
fn main() -> anyhow::Result<()> {
let img = ImageReader::open("/home/stephen/Downloads/scd31-qr.png")?.decode()?;
preamble();
for x in 0..img.width() {
for y in 0..img.height() {
let p = img.get_pixel(x, y);
match p.0 {
[0, 0, 0, 255] => {
toast(
x as f64 - (img.width() as f64 / 2.0),
y as f64 - (img.height() as f64 / 2.0),
);
}
[255, 255, 255, 255] => {
// white
}
_ => bail!("Pixel was not entirely black nor entirely white: {:?}", p),
// 2x oversampling
for x in 0..(img.width() * 2 - 1) {
for y in 0..(img.height() * 2 - 1) {
let black = is_black(img.get_pixel(x / 2, y / 2))
&& is_black(img.get_pixel(x / 2 + x % 2, y / 2))
&& is_black(img.get_pixel(x / 2, y / 2 + y % 2))
&& is_black(img.get_pixel(x / 2 + x % 2, y / 2 + y % 2));
if black {
toast(
x as f64 - (img.width() as f64 / 2.0),
y as f64 + (img.height() as f64 / 2.0), // correct for reflection when printing
);
}
}
}
......@@ -35,6 +32,14 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
fn is_black(p: Rgba<u8>) -> bool {
match p.0 {
[0, 0, 0, 255] => true,
[255, 255, 255, 255] => false,
_ => panic!("Pixel was not entirely black nor entirely white: {:?}", p),
}
}
fn preamble() {
println!(
r#"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment