#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PixelFormat {
Rgb8,
Rgba8,
}
impl PixelFormat {
pub fn bytes_per_pixel(self) -> usize {
match self {
Self::Rgb8 => 3,
Self::Rgba8 => 4,
}
}
fn png_color_type(self) -> u8 {
match self {
Self::Rgb8 => 2,
Self::Rgba8 => 6,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Framebuffer {
width: u32,
height: u32,
format: PixelFormat,
pixels: Vec<u8>,
}
impl Framebuffer {
pub fn new(width: u32, height: u32, format: PixelFormat, mut pixels: Vec<u8>) -> Self {
let expected = (width as usize)
.saturating_mul(height as usize)
.saturating_mul(format.bytes_per_pixel());
pixels.resize(expected, 0);
Self {
width,
height,
format,
pixels,
}
}
pub fn rgb8(width: u32, height: u32, pixels: Vec<u8>) -> Self {
Self::new(width, height, PixelFormat::Rgb8, pixels)
}
pub fn rgba8(width: u32, height: u32, pixels: Vec<u8>) -> Self {
Self::new(width, height, PixelFormat::Rgba8, pixels)
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn format(&self) -> PixelFormat {
self.format
}
pub fn pixels(&self) -> &[u8] {
&self.pixels
}
pub fn pixel(&self, x: u32, y: u32) -> Option<[u8; 4]> {
if x >= self.width || y >= self.height {
return None;
}
let bytes = self.format.bytes_per_pixel();
let index = (y as usize * self.width as usize + x as usize) * bytes;
let pixel = self.pixels.get(index..index + bytes)?;
Some(match self.format {
PixelFormat::Rgb8 => [pixel[0], pixel[1], pixel[2], 255],
PixelFormat::Rgba8 => [pixel[0], pixel[1], pixel[2], pixel[3]],
})
}
pub fn from_rgb565(pixels: &[u16], width: u32, height: u32) -> Self {
let count = width as usize * height as usize;
let mut rgb = Vec::with_capacity(count * 3);
for &color in pixels.iter().take(count) {
let red = (color >> 11) & 0x1f;
let green = (color >> 5) & 0x3f;
let blue = color & 0x1f;
rgb.push((u32::from(red) * 255 / 31) as u8);
rgb.push((u32::from(green) * 255 / 63) as u8);
rgb.push((u32::from(blue) * 255 / 31) as u8);
}
Self::rgb8(width, height, rgb)
}
pub fn from_page_bits_lsb_first(bytes: &[u8], width: u32, height: u32) -> Self {
Self::monochrome(width, height, |x, y| {
let index = (y / 8) * width as usize + x;
bytes
.get(index)
.is_some_and(|byte| byte & (1 << (y % 8)) != 0)
})
}
pub fn from_row_bits_msb_first(bytes: &[u8], width: u32, height: u32) -> Self {
let stride = (width as usize).div_ceil(8);
Self::monochrome(width, height, |x, y| {
bytes
.get(y * stride + x / 8)
.is_some_and(|byte| byte & (0x80 >> (x % 8)) != 0)
})
}
fn monochrome(width: u32, height: u32, lit: impl Fn(usize, usize) -> bool) -> Self {
let mut rgb = Vec::with_capacity(width as usize * height as usize * 3);
for y in 0..height as usize {
for x in 0..width as usize {
let value = if lit(x, y) { 255 } else { 0 };
rgb.extend_from_slice(&[value, value, value]);
}
}
Self::rgb8(width, height, rgb)
}
pub fn to_png(&self) -> Vec<u8> {
let width = self.width.max(1) as usize;
let height = self.height.max(1) as usize;
let bytes = self.format.bytes_per_pixel();
let mut raw = Vec::with_capacity((width * bytes + 1) * height);
for y in 0..height {
raw.push(0);
for x in 0..width {
let pixel = self.pixel(x as u32, y as u32).unwrap_or([
0,
0,
0,
if bytes == 3 { 255 } else { 0 },
]);
raw.extend_from_slice(&pixel[..bytes]);
}
}
let mut ihdr = Vec::with_capacity(13);
ihdr.extend_from_slice(&(width as u32).to_be_bytes());
ihdr.extend_from_slice(&(height as u32).to_be_bytes());
ihdr.extend_from_slice(&[8, self.format.png_color_type(), 0, 0, 0]);
let mut png = PNG_SIGNATURE.to_vec();
chunk(&mut png, b"IHDR", &ihdr);
chunk(&mut png, b"IDAT", &zlib_stored(&raw));
chunk(&mut png, b"IEND", &[]);
png
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Diff {
pub x: u32,
pub y: u32,
pub actual: [u8; 4],
pub expected: [u8; 4],
pub mismatched: usize,
}
pub fn compare_framebuffers(
actual: &Framebuffer,
expected: &Framebuffer,
tolerance: u8,
) -> Result<(), Diff> {
if (actual.width, actual.height) != (expected.width, expected.height) {
let overlap_width = actual.width.min(expected.width);
let overlap_height = actual.height.min(expected.height);
let (x, y) = if actual.width != expected.width {
(overlap_width, 0)
} else {
(0, overlap_height)
};
let overlap = overlap_width as usize * overlap_height as usize;
let extra = actual.width as usize * actual.height as usize
+ expected.width as usize * expected.height as usize
- 2 * overlap;
return Err(Diff {
x,
y,
actual: actual.pixel(x, y).unwrap_or([0; 4]),
expected: expected.pixel(x, y).unwrap_or([0; 4]),
mismatched: extra,
});
}
let mut mismatched = 0;
let mut first = None;
for y in 0..actual.height {
for x in 0..actual.width {
let a = actual.pixel(x, y).unwrap_or([0; 4]);
let e = expected.pixel(x, y).unwrap_or([0; 4]);
let differs = (0..4).any(|channel| a[channel].abs_diff(e[channel]) > tolerance);
if differs {
mismatched += 1;
if first.is_none() {
first = Some((x, y, a, e));
}
}
}
}
match first {
None => Ok(()),
Some((x, y, actual, expected)) => Err(Diff {
x,
y,
actual,
expected,
mismatched,
}),
}
}
const PNG_SIGNATURE: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10];
fn chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
let start = out.len();
out.extend_from_slice(kind);
out.extend_from_slice(data);
let crc = crc32(&out[start..]);
out.extend_from_slice(&crc.to_be_bytes());
}
fn zlib_stored(raw: &[u8]) -> Vec<u8> {
let mut out = vec![0x78, 0x01];
let mut blocks = raw.chunks(65_535).peekable();
if blocks.peek().is_none() {
out.extend_from_slice(&[0x01, 0x00, 0x00, 0xff, 0xff]);
} else {
while let Some(block) = blocks.next() {
let last = blocks.peek().is_none();
out.push(u8::from(last));
out.extend_from_slice(&(block.len() as u16).to_le_bytes());
out.extend_from_slice(&(!(block.len() as u16)).to_le_bytes());
out.extend_from_slice(block);
}
}
out.extend_from_slice(&adler32(raw).to_be_bytes());
out
}
fn crc32(data: &[u8]) -> u32 {
let mut crc = 0xffff_ffffu32;
for &byte in data {
crc ^= u32::from(byte);
for _ in 0..8 {
crc = if crc & 1 != 0 {
0xedb8_8320 ^ (crc >> 1)
} else {
crc >> 1
};
}
}
!crc
}
fn adler32(data: &[u8]) -> u32 {
const MOD: u32 = 65_521;
let (mut a, mut b) = (1u32, 0u32);
for &byte in data {
a = (a + u32::from(byte)) % MOD;
b = (b + a) % MOD;
}
(b << 16) | a
}
#[cfg(test)]
mod tests {
use super::*;
fn header(png: &[u8]) -> (u32, u32, u8, u8) {
assert_eq!(&png[..8], &PNG_SIGNATURE, "the PNG signature");
assert_eq!(&png[12..16], b"IHDR");
let width = u32::from_be_bytes(png[16..20].try_into().unwrap());
let height = u32::from_be_bytes(png[20..24].try_into().unwrap());
(width, height, png[24], png[25])
}
#[test]
fn a_png_carries_the_framebuffer_dimensions() {
let png = Framebuffer::rgb8(7, 3, vec![0; 7 * 3 * 3]).to_png();
assert_eq!(header(&png), (7, 3, 8, 2));
let rgba = Framebuffer::rgba8(2, 5, vec![0; 2 * 5 * 4]).to_png();
assert_eq!(header(&rgba), (2, 5, 8, 6));
assert_eq!(&png[png.len() - 8..png.len() - 4], b"IEND");
}
#[test]
fn an_ili9341_rgb565_colour_becomes_rgb8() {
let panel = [0xf800u16, 0x07e0, 0x001f, 0xffff];
let image = Framebuffer::from_rgb565(&panel, 2, 2);
assert_eq!(image.pixel(0, 0), Some([255, 0, 0, 255]), "red");
assert_eq!(image.pixel(1, 0), Some([0, 255, 0, 255]), "green");
assert_eq!(image.pixel(0, 1), Some([0, 0, 255, 255]), "blue");
assert_eq!(image.pixel(1, 1), Some([255, 255, 255, 255]), "white");
}
#[test]
fn an_ssd1306_page_byte_runs_from_bit_zero_downwards() {
let image = Framebuffer::from_page_bits_lsb_first(&[0xaa, 0x0f], 2, 8);
assert_eq!(image.pixel(0, 0), Some([0, 0, 0, 255]), "bit 0 clear");
assert_eq!(image.pixel(0, 1), Some([255, 255, 255, 255]), "bit 1 set");
assert_eq!(image.pixel(0, 7), Some([255, 255, 255, 255]), "bit 7");
assert_eq!(image.pixel(1, 0), Some([255, 255, 255, 255]), "bit 0");
assert_eq!(image.pixel(1, 4), Some([0, 0, 0, 255]), "bit 4 clear");
}
#[test]
fn an_epaper_row_lights_the_msb_first() {
let image = Framebuffer::from_row_bits_msb_first(&[0x80], 8, 1);
assert_eq!(image.pixel(0, 0), Some([255, 255, 255, 255]));
assert_eq!(image.pixel(1, 0), Some([0, 0, 0, 255]));
}
#[test]
fn the_first_mismatch_is_the_first_changed_pixel() {
let actual = Framebuffer::rgb8(3, 2, vec![0; 3 * 2 * 3]);
let mut pixels = vec![0; 3 * 2 * 3];
pixels[(3 + 2) * 3] = 10; let expected = Framebuffer::rgb8(3, 2, pixels);
let diff = compare_framebuffers(&actual, &expected, 0).expect_err("they differ");
assert_eq!((diff.x, diff.y), (2, 1));
assert_eq!(diff.actual, [0, 0, 0, 255]);
assert_eq!(diff.expected, [10, 0, 0, 255]);
assert_eq!(diff.mismatched, 1);
}
#[test]
fn tolerance_is_the_largest_accepted_channel_difference() {
let actual = Framebuffer::rgb8(1, 1, vec![10, 20, 30]);
let expected = Framebuffer::rgb8(1, 1, vec![15, 20, 30]);
assert!(compare_framebuffers(&actual, &expected, 5).is_ok());
let diff = compare_framebuffers(&actual, &expected, 4).expect_err("just outside");
assert_eq!(diff.actual, [10, 20, 30, 255]);
assert_eq!(diff.expected, [15, 20, 30, 255]);
}
#[test]
fn a_size_mismatch_is_reported_before_the_overlap() {
let actual = Framebuffer::rgb8(2, 2, vec![0; 2 * 2 * 3]);
let expected = Framebuffer::rgb8(3, 2, vec![0; 3 * 2 * 3]);
let diff = compare_framebuffers(&actual, &expected, 0).expect_err("widths differ");
assert_eq!((diff.x, diff.y), (2, 0), "the first missing column");
assert_eq!(diff.mismatched, 2, "one missing pixel per row");
}
}