1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! Where rendered text leaves the building: export formats for Typf
//!
//! The final stage of the pipeline. Turns your carefully rendered glyphs
//! into files, streams, or whatever format your application needs.
use std::io::Write;
use typf_core::{
error::{ExportError, Result},
traits::Exporter,
types::{BitmapData, BitmapFormat, RenderOutput},
};
pub mod json;
pub mod png;
pub mod svg;
pub use json::JsonExporter;
pub use png::{encode_bitmap_to_png, PngExporter};
pub use svg::SvgExporter;
/// Simple bitmap exporter for when you just need to see what happened
pub struct PnmExporter {
/// Choose your flavor: black-and-white, grayscale, or color
format: PnmFormat,
}
#[derive(Debug, Clone, Copy)]
pub enum PnmFormat {
/// PBM - Just black and white pixels
Pbm,
/// PGM - 256 shades of gray
Pgm,
/// PPM - Full RGB color
Ppm,
}
impl PnmExporter {
/// Creates an exporter for your chosen PNM format
pub fn new(format: PnmFormat) -> Self {
Self { format }
}
/// Quick way to get a color exporter
pub fn ppm() -> Self {
Self::new(PnmFormat::Ppm)
}
/// Quick way to get a grayscale exporter
pub fn pgm() -> Self {
Self::new(PnmFormat::Pgm)
}
/// Converts bitmap data into PNM's simple text format
fn export_bitmap(&self, bitmap: &BitmapData) -> Result<Vec<u8>> {
let mut output = Vec::new();
match self.format {
PnmFormat::Ppm => {
// PPM needs a simple header first
writeln!(&mut output, "P3")?; // Magic number for ASCII PPM
writeln!(&mut output, "{} {}", bitmap.width, bitmap.height)?;
writeln!(&mut output, "255")?; // Maximum RGB value
// Transform bitmap data into PPM's text format
match bitmap.format {
BitmapFormat::Rgba8 => {
// Strip alpha, keep just RGB
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = ((y * bitmap.width + x) * 4) as usize;
write!(
&mut output,
"{} {} {} ",
bitmap.data[idx], // Red
bitmap.data[idx + 1], // Green
bitmap.data[idx + 2] // Blue
)?;
}
writeln!(&mut output)?; // New line after each row
}
},
BitmapFormat::Rgb8 => {
// Copy RGB values directly
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = ((y * bitmap.width + x) * 3) as usize;
write!(
&mut output,
"{} {} {} ",
bitmap.data[idx],
bitmap.data[idx + 1],
bitmap.data[idx + 2]
)?;
}
writeln!(&mut output)?;
}
},
BitmapFormat::Gray8 => {
// Make gray look like color (triplet the value)
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = (y * bitmap.width + x) as usize;
let gray = bitmap.data[idx];
write!(&mut output, "{} {} {} ", gray, gray, gray)?;
}
writeln!(&mut output)?;
}
},
BitmapFormat::Gray1 => {
// Expand 1-bit to full RGB
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let byte_idx = ((y * bitmap.width + x) / 8) as usize;
let bit_idx = ((y * bitmap.width + x) % 8) as usize;
let bit = (bitmap.data[byte_idx] >> (7 - bit_idx)) & 1;
let value = if bit == 1 { 255 } else { 0 };
write!(&mut output, "{} {} {} ", value, value, value)?;
}
writeln!(&mut output)?;
}
},
}
},
PnmFormat::Pgm => {
// PGM header (grayscale version of PPM)
writeln!(&mut output, "P2")?; // Magic number for ASCII PGM
writeln!(&mut output, "{} {}", bitmap.width, bitmap.height)?;
writeln!(&mut output, "255")?; // Maximum gray value
// Flatten everything to grayscale
match bitmap.format {
BitmapFormat::Gray8 => {
// Already grayscale, just copy
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = (y * bitmap.width + x) as usize;
write!(&mut output, "{} ", bitmap.data[idx])?;
}
writeln!(&mut output)?;
}
},
BitmapFormat::Rgba8 => {
// Convert color to grayscale using luminance
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = ((y * bitmap.width + x) * 4) as usize;
let r = bitmap.data[idx] as u32;
let g = bitmap.data[idx + 1] as u32;
let b = bitmap.data[idx + 2] as u32;
// ITU-R BT.709 luminance formula
let gray = ((r * 299 + g * 587 + b * 114) / 1000) as u8;
write!(&mut output, "{} ", gray)?;
}
writeln!(&mut output)?;
}
},
BitmapFormat::Rgb8 => {
// Color to grayscale conversion
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let idx = ((y * bitmap.width + x) * 3) as usize;
let r = bitmap.data[idx] as u32;
let g = bitmap.data[idx + 1] as u32;
let b = bitmap.data[idx + 2] as u32;
let gray = ((r * 299 + g * 587 + b * 114) / 1000) as u8;
write!(&mut output, "{} ", gray)?;
}
writeln!(&mut output)?;
}
},
BitmapFormat::Gray1 => {
// Expand 1-bit to 8-bit grayscale
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let byte_idx = ((y * bitmap.width + x) / 8) as usize;
let bit_idx = ((y * bitmap.width + x) % 8) as usize;
let bit = (bitmap.data[byte_idx] >> (7 - bit_idx)) & 1;
let value = if bit == 1 { 255 } else { 0 };
write!(&mut output, "{} ", value)?;
}
writeln!(&mut output)?;
}
},
}
},
PnmFormat::Pbm => {
// PBM header (the simplest format - just 0s and 1s)
writeln!(&mut output, "P1")?; // Magic number for ASCII PBM
writeln!(&mut output, "{} {}", bitmap.width, bitmap.height)?;
// Everything becomes black (0) or white (1)
match bitmap.format {
BitmapFormat::Gray1 => {
// Already 1-bit, just copy
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let byte_idx = ((y * bitmap.width + x) / 8) as usize;
let bit_idx = ((y * bitmap.width + x) % 8) as usize;
let bit = (bitmap.data[byte_idx] >> (7 - bit_idx)) & 1;
write!(&mut output, "{} ", bit)?;
}
writeln!(&mut output)?;
}
},
_ => {
// Convert Everything to 1-bit with a simple threshold
for y in 0..bitmap.height {
for x in 0..bitmap.width {
let gray = match bitmap.format {
BitmapFormat::Gray8 => {
bitmap.data[(y * bitmap.width + x) as usize]
},
BitmapFormat::Rgba8 => {
let idx = ((y * bitmap.width + x) * 4) as usize;
let r = bitmap.data[idx] as u32;
let g = bitmap.data[idx + 1] as u32;
let b = bitmap.data[idx + 2] as u32;
((r * 299 + g * 587 + b * 114) / 1000) as u8
},
BitmapFormat::Rgb8 => {
let idx = ((y * bitmap.width + x) * 3) as usize;
let r = bitmap.data[idx] as u32;
let g = bitmap.data[idx + 1] as u32;
let b = bitmap.data[idx + 2] as u32;
((r * 299 + g * 587 + b * 114) / 1000) as u8
},
_ => 0,
};
// 127 is a reasonable threshold
let bit = if gray > 127 { 1 } else { 0 };
write!(&mut output, "{} ", bit)?;
}
writeln!(&mut output)?;
}
},
}
},
}
Ok(output)
}
}
impl Exporter for PnmExporter {
fn name(&self) -> &'static str {
match self.format {
PnmFormat::Pbm => "pbm",
PnmFormat::Pgm => "pgm",
PnmFormat::Ppm => "ppm",
}
}
fn export(&self, output: &RenderOutput) -> Result<Vec<u8>> {
match output {
RenderOutput::Bitmap(bitmap) => self.export_bitmap(bitmap),
_ => Err(ExportError::FormatNotSupported(
"PNM exporter only supports bitmap output".into(),
)
.into()),
}
}
fn extension(&self) -> &'static str {
match self.format {
PnmFormat::Pbm => "pbm",
PnmFormat::Pgm => "pgm",
PnmFormat::Ppm => "ppm",
}
}
fn mime_type(&self) -> &'static str {
match self.format {
PnmFormat::Pbm => "image/x-portable-bitmap",
PnmFormat::Pgm => "image/x-portable-graymap",
PnmFormat::Ppm => "image/x-portable-pixmap",
}
}
}
impl Default for PnmExporter {
fn default() -> Self {
Self::ppm() // Default to color
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ppm_export() {
let exporter = PnmExporter::ppm();
// Create a small test bitmap
let bitmap = BitmapData {
width: 2,
height: 2,
format: BitmapFormat::Rgba8,
data: vec![
255, 0, 0, 255, // Red pixel
0, 255, 0, 255, // Green pixel
0, 0, 255, 255, // Blue pixel
255, 255, 255, 255, // White pixel
],
};
let output = RenderOutput::Bitmap(bitmap);
let exported = exporter.export(&output).unwrap();
let text = String::from_utf8(exported).unwrap();
assert!(text.starts_with("P3"));
assert!(text.contains("2 2")); // Dimensions
assert!(text.contains("255")); // Max value
}
#[test]
fn test_pgm_export() {
let exporter = PnmExporter::pgm();
let bitmap = BitmapData {
width: 2,
height: 1,
format: BitmapFormat::Gray8,
data: vec![128, 255],
};
let output = RenderOutput::Bitmap(bitmap);
let exported = exporter.export(&output).unwrap();
let text = String::from_utf8(exported).unwrap();
assert!(text.starts_with("P2"));
assert!(text.contains("2 1"));
assert!(text.contains("128"));
assert!(text.contains("255"));
}
#[test]
fn test_extension_and_mime() {
let ppm = PnmExporter::ppm();
assert_eq!(ppm.extension(), "ppm");
assert_eq!(ppm.mime_type(), "image/x-portable-pixmap");
let pgm = PnmExporter::pgm();
assert_eq!(pgm.extension(), "pgm");
assert_eq!(pgm.mime_type(), "image/x-portable-graymap");
}
}