1#![cfg_attr(not(feature = "std"), no_std)]
2#![deny(unsafe_code)]
3
4#[cfg(not(feature = "std"))]
5extern crate alloc;
6
7#[cfg(not(feature = "std"))]
8use alloc::{format, vec, vec::Vec};
9#[cfg(feature = "std")]
10use std::{format, vec, vec::Vec};
11
12#[derive(Debug, Clone, Default, PartialEq, Eq)]
16pub struct Pixmap {
17 pub width: u32,
18 pub height: u32,
19 pub data: Vec<u8>,
21}
22
23impl AsRef<[u8]> for Pixmap {
24 fn as_ref(&self) -> &[u8] {
25 &self.data
26 }
27}
28
29impl Pixmap {
30 const MAX_PIXELS: usize = 64 * 1024 * 1024;
34
35 pub fn new(width: u32, height: u32, r: u8, g: u8, b: u8, a: u8) -> Self {
41 let Some(pixel_count) = (width as usize).checked_mul(height as usize) else {
42 return Self::default();
43 };
44 if pixel_count > Self::MAX_PIXELS {
45 return Self::default();
46 }
47 if r == g && g == b && b == a {
49 return Pixmap {
50 width,
51 height,
52 data: vec![r; pixel_count * 4],
53 };
54 }
55 let data = [r, g, b, a].repeat(pixel_count);
58 Pixmap {
59 width,
60 height,
61 data,
62 }
63 }
64
65 pub fn white(width: u32, height: u32) -> Self {
67 Self::new(width, height, 255, 255, 255, 255)
68 }
69
70 #[inline]
73 pub fn set_rgb(&mut self, x: u32, y: u32, r: u8, g: u8, b: u8) {
74 let idx = (y as usize * self.width as usize + x as usize) * 4;
75 if let Some(pixel) = self.data.get_mut(idx..idx + 4) {
76 pixel[0] = r;
77 pixel[1] = g;
78 pixel[2] = b;
79 pixel[3] = 255;
80 }
81 }
82
83 #[inline]
85 pub fn get_pixel(&self, x: u32, y: u32) -> Option<&[u8]> {
86 if x >= self.width || y >= self.height {
87 return None;
88 }
89 let idx = (y as usize * self.width as usize + x as usize) * 4;
90 self.data.get(idx..idx + 4)
91 }
92
93 #[inline]
95 pub fn get_rgb(&self, x: u32, y: u32) -> (u8, u8, u8) {
96 let idx = (y as usize * self.width as usize + x as usize) * 4;
97 if let Some(pixel) = self.data.get(idx..idx + 4) {
98 (pixel[0], pixel[1], pixel[2])
99 } else {
100 (0, 0, 0)
101 }
102 }
103
104 pub fn to_rgb(&self) -> Vec<u8> {
106 let pixel_count = self.data.len() / 4;
107 let mut out = Vec::with_capacity(pixel_count * 3);
108 for chunk in self.data.chunks_exact(4) {
109 out.push(chunk[0]);
110 out.push(chunk[1]);
111 out.push(chunk[2]);
112 }
113 out
114 }
115
116 pub fn to_ppm(&self) -> Vec<u8> {
120 let header = format!("P6\n{} {}\n255\n", self.width, self.height);
121 let pixel_count = self.data.len() / 4;
122 let mut out = Vec::with_capacity(header.len() + pixel_count * 3);
123 out.extend_from_slice(header.as_bytes());
124 for chunk in self.data.chunks_exact(4) {
125 out.push(chunk[0]); out.push(chunk[1]); out.push(chunk[2]); }
129 out
130 }
131
132 pub fn to_gray8(&self) -> GrayPixmap {
138 let pixel_count = self.data.len() / 4;
139 let mut data = Vec::with_capacity(pixel_count);
140 for chunk in self.data.chunks_exact(4) {
141 let r = chunk[0] as u32;
142 let g = chunk[1] as u32;
143 let b = chunk[2] as u32;
144 let y = (r * 306 + g * 601 + b * 117) >> 10;
146 data.push(y.min(255) as u8);
147 }
148 GrayPixmap {
149 width: self.width,
150 height: self.height,
151 data,
152 }
153 }
154}
155
156#[derive(Debug, Clone, Default, PartialEq, Eq)]
161pub struct GrayPixmap {
162 pub width: u32,
163 pub height: u32,
164 pub data: Vec<u8>,
166}
167
168impl GrayPixmap {
169 #[inline]
171 pub fn get(&self, x: u32, y: u32) -> u8 {
172 self.data[(y as usize * self.width as usize) + x as usize]
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 #[test]
181 fn white_pixmap() {
182 let pm = Pixmap::white(2, 2);
183 assert_eq!(pm.data.len(), 16);
184 for chunk in pm.data.chunks(4) {
185 assert_eq!(chunk, &[255, 255, 255, 255]);
186 }
187 }
188
189 #[test]
190 fn set_get_rgb() {
191 let mut pm = Pixmap::white(3, 3);
192 pm.set_rgb(1, 1, 100, 150, 200);
193 assert_eq!(pm.get_rgb(1, 1), (100, 150, 200));
194 assert_eq!(pm.get_rgb(0, 0), (255, 255, 255));
195 }
196
197 #[test]
198 fn to_ppm_format() {
199 let mut pm = Pixmap::white(2, 1);
200 pm.set_rgb(0, 0, 255, 0, 0); pm.set_rgb(1, 0, 0, 0, 255); let ppm = pm.to_ppm();
203 let header = b"P6\n2 1\n255\n";
204 assert_eq!(&ppm[..header.len()], header);
205 assert_eq!(&ppm[header.len()..], &[255, 0, 0, 0, 0, 255]);
206 }
207}