@@ -0,0 +1,104 @@ |
| 1 | +//! PPM/PAM encoding for garshot. |
| 2 | +//! |
| 3 | +//! PPM (Portable Pixel Map) and PAM (Portable Arbitrary Map) are simple |
| 4 | +//! uncompressed formats, ideal for piping to other tools. |
| 5 | + |
| 6 | +use std::io::Write; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use crate::error::Result; |
| 10 | + |
| 11 | +/// Encode RGBA image data to PPM file (RGB, no alpha). |
| 12 | +pub fn encode_ppm(data: &[u8], width: u32, height: u32, path: &Path) -> Result<()> { |
| 13 | + let ppm_data = encode_ppm_to_vec(data, width, height)?; |
| 14 | + std::fs::write(path, ppm_data)?; |
| 15 | + Ok(()) |
| 16 | +} |
| 17 | + |
| 18 | +/// Encode RGBA image data to PPM bytes (RGB, no alpha). |
| 19 | +pub fn encode_ppm_to_vec(data: &[u8], width: u32, height: u32) -> Result<Vec<u8>> { |
| 20 | + let mut buffer = Vec::with_capacity(width as usize * height as usize * 3 + 50); |
| 21 | + |
| 22 | + // PPM header: P6 (binary RGB) |
| 23 | + writeln!(buffer, "P6")?; |
| 24 | + writeln!(buffer, "{} {}", width, height)?; |
| 25 | + writeln!(buffer, "255")?; |
| 26 | + |
| 27 | + // Convert RGBA to RGB |
| 28 | + for pixel in data.chunks_exact(4) { |
| 29 | + buffer.push(pixel[0]); // R |
| 30 | + buffer.push(pixel[1]); // G |
| 31 | + buffer.push(pixel[2]); // B |
| 32 | + } |
| 33 | + |
| 34 | + Ok(buffer) |
| 35 | +} |
| 36 | + |
| 37 | +/// Encode RGBA image data to PAM file (with alpha). |
| 38 | +pub fn encode_pam(data: &[u8], width: u32, height: u32, path: &Path) -> Result<()> { |
| 39 | + let pam_data = encode_pam_to_vec(data, width, height)?; |
| 40 | + std::fs::write(path, pam_data)?; |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +/// Encode RGBA image data to PAM bytes (with alpha). |
| 45 | +pub fn encode_pam_to_vec(data: &[u8], width: u32, height: u32) -> Result<Vec<u8>> { |
| 46 | + let mut buffer = Vec::with_capacity(data.len() + 100); |
| 47 | + |
| 48 | + // PAM header |
| 49 | + writeln!(buffer, "P7")?; |
| 50 | + writeln!(buffer, "WIDTH {}", width)?; |
| 51 | + writeln!(buffer, "HEIGHT {}", height)?; |
| 52 | + writeln!(buffer, "DEPTH 4")?; |
| 53 | + writeln!(buffer, "MAXVAL 255")?; |
| 54 | + writeln!(buffer, "TUPLTYPE RGB_ALPHA")?; |
| 55 | + writeln!(buffer, "ENDHDR")?; |
| 56 | + |
| 57 | + // Raw RGBA data |
| 58 | + buffer.extend_from_slice(data); |
| 59 | + |
| 60 | + Ok(buffer) |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_encode_ppm_to_vec() { |
| 69 | + // Create a 2x2 red image |
| 70 | + let data = vec![ |
| 71 | + 255, 0, 0, 255, // Red pixel |
| 72 | + 0, 255, 0, 255, // Green pixel |
| 73 | + 0, 0, 255, 255, // Blue pixel |
| 74 | + 255, 255, 255, 255, // White pixel |
| 75 | + ]; |
| 76 | + |
| 77 | + let ppm_data = encode_ppm_to_vec(&data, 2, 2).unwrap(); |
| 78 | + |
| 79 | + // Check header starts correctly |
| 80 | + assert!(ppm_data.starts_with(b"P6\n2 2\n255\n")); |
| 81 | + |
| 82 | + // Header is "P6\n2 2\n255\n" = 11 bytes, then 12 bytes of pixel data |
| 83 | + assert_eq!(ppm_data.len(), 11 + 12); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_encode_pam_to_vec() { |
| 88 | + let data = vec![ |
| 89 | + 255, 0, 0, 128, // Semi-transparent red |
| 90 | + 0, 255, 0, 255, // Opaque green |
| 91 | + ]; |
| 92 | + |
| 93 | + let pam_data = encode_pam_to_vec(&data, 2, 1).unwrap(); |
| 94 | + |
| 95 | + // Check header contains expected elements |
| 96 | + let header_str = String::from_utf8_lossy(&pam_data); |
| 97 | + assert!(header_str.contains("P7")); |
| 98 | + assert!(header_str.contains("WIDTH 2")); |
| 99 | + assert!(header_str.contains("HEIGHT 1")); |
| 100 | + assert!(header_str.contains("DEPTH 4")); |
| 101 | + assert!(header_str.contains("TUPLTYPE RGB_ALPHA")); |
| 102 | + assert!(header_str.contains("ENDHDR")); |
| 103 | + } |
| 104 | +} |