#[cfg(not(feature = "std"))]
use alloc::{format, vec::Vec};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Pixmap {
pub width: u32,
pub height: u32,
pub data: Vec<u8>,
}
impl AsRef<[u8]> for Pixmap {
fn as_ref(&self) -> &[u8] {
&self.data
}
}
impl Pixmap {
const MAX_PIXELS: usize = 64 * 1024 * 1024;
pub fn new(width: u32, height: u32, r: u8, g: u8, b: u8, a: u8) -> Self {
let Some(pixel_count) = (width as usize).checked_mul(height as usize) else {
return Self::default();
};
if pixel_count > Self::MAX_PIXELS {
return Self::default();
}
let mut data = Vec::with_capacity(pixel_count * 4);
for _ in 0..pixel_count {
data.push(r);
data.push(g);
data.push(b);
data.push(a);
}
Pixmap {
width,
height,
data,
}
}
pub fn white(width: u32, height: u32) -> Self {
Self::new(width, height, 255, 255, 255, 255)
}
#[inline]
pub fn set_rgb(&mut self, x: u32, y: u32, r: u8, g: u8, b: u8) {
let idx = (y as usize * self.width as usize + x as usize) * 4;
if let Some(pixel) = self.data.get_mut(idx..idx + 4) {
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
pixel[3] = 255;
}
}
#[inline]
pub fn get_pixel(&self, x: u32, y: u32) -> Option<&[u8]> {
if x >= self.width || y >= self.height {
return None;
}
let idx = (y as usize * self.width as usize + x as usize) * 4;
self.data.get(idx..idx + 4)
}
#[inline]
pub fn get_rgb(&self, x: u32, y: u32) -> (u8, u8, u8) {
let idx = (y as usize * self.width as usize + x as usize) * 4;
if let Some(pixel) = self.data.get(idx..idx + 4) {
(pixel[0], pixel[1], pixel[2])
} else {
(0, 0, 0)
}
}
pub fn to_rgb(&self) -> Vec<u8> {
let pixel_count = self.data.len() / 4;
let mut out = Vec::with_capacity(pixel_count * 3);
for chunk in self.data.chunks_exact(4) {
out.push(chunk[0]);
out.push(chunk[1]);
out.push(chunk[2]);
}
out
}
pub fn to_ppm(&self) -> Vec<u8> {
let header = format!("P6\n{} {}\n255\n", self.width, self.height);
let pixel_count = self.data.len() / 4;
let mut out = Vec::with_capacity(header.len() + pixel_count * 3);
out.extend_from_slice(header.as_bytes());
for chunk in self.data.chunks_exact(4) {
out.push(chunk[0]); out.push(chunk[1]); out.push(chunk[2]); }
out
}
pub fn to_gray8(&self) -> GrayPixmap {
let pixel_count = self.data.len() / 4;
let mut data = Vec::with_capacity(pixel_count);
for chunk in self.data.chunks_exact(4) {
let r = chunk[0] as u32;
let g = chunk[1] as u32;
let b = chunk[2] as u32;
let y = (r * 306 + g * 601 + b * 117) >> 10;
data.push(y.min(255) as u8);
}
GrayPixmap {
width: self.width,
height: self.height,
data,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GrayPixmap {
pub width: u32,
pub height: u32,
pub data: Vec<u8>,
}
impl GrayPixmap {
#[inline]
pub fn get(&self, x: u32, y: u32) -> u8 {
self.data[(y as usize * self.width as usize) + x as usize]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn white_pixmap() {
let pm = Pixmap::white(2, 2);
assert_eq!(pm.data.len(), 16);
for chunk in pm.data.chunks(4) {
assert_eq!(chunk, &[255, 255, 255, 255]);
}
}
#[test]
fn set_get_rgb() {
let mut pm = Pixmap::white(3, 3);
pm.set_rgb(1, 1, 100, 150, 200);
assert_eq!(pm.get_rgb(1, 1), (100, 150, 200));
assert_eq!(pm.get_rgb(0, 0), (255, 255, 255));
}
#[test]
fn to_ppm_format() {
let mut pm = Pixmap::white(2, 1);
pm.set_rgb(0, 0, 255, 0, 0); pm.set_rgb(1, 0, 0, 0, 255); let ppm = pm.to_ppm();
let header = b"P6\n2 1\n255\n";
assert_eq!(&ppm[..header.len()], header);
assert_eq!(&ppm[header.len()..], &[255, 0, 0, 0, 0, 255]);
}
}