const W: usize = 512; fn blur_row(above: &[u8], here: &[u8], below: &[u8], out: &mut [u8]) { assert!(above.len() == W && here.len() == W && below.len() == W && out.len() == W); for x in 1..W - 1 { let sum = i64::from(above[x - 1]) + i64::from(above[x]) + i64::from(above[x + 1]) + i64::from(here[x - 1]) + i64::from(here[x]) + i64::from(here[x + 1]) + i64::from(below[x - 1]) + i64::from(below[x]) + i64::from(below[x + 1]); out[x] = u8::try_from(sum / 9).unwrap(); } } fn main() { for seed in [0_usize,1,7,255] { let above:[u8;W]=std::array::from_fn(|i|((i*3+seed)%256) as u8); let here:[u8;W]=std::array::from_fn(|i|((i*5+seed)%256) as u8); let below:[u8;W]=std::array::from_fn(|i|((i*7+seed)%256) as u8); let mut out=[123_u8;W];blur_row(&above,&here,&below,&mut out); assert_eq!((out[0],out[W-1]),(123,123)); for x in 1..W-1 { let sum:u32=[&above,&here,&below].iter().map(|row|row[x-1..=x+1].iter().map(|&b|u32::from(b)).sum::()).sum(); assert_eq!(u32::from(out[x]),sum/9); } } let max=[255_u8;W];let mut out=[0_u8;W];blur_row(&max,&max,&max,&mut out);assert!(out[1..W-1].iter().all(|&b|b==255)); }