geodesic-wallpaper 1.6.1

Animated Windows desktop wallpaper that draws glowing geodesics flowing over curved 3D surfaces (torus, Klein bottle, pseudosphere and more), rendered with wgpu. Also renders PNGs and GIFs headless.
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Image export formats.
//!
//! Writes wallpaper images as PNG, PPM, BMP or SVG. PNG, PPM and BMP go
//! through the [`image`] crate, so PNG output is deflate-compressed and every
//! file can be read back by any image viewer. SVG is a simple `<rect>` grid
//! written by hand.

use std::path::Path;
use std::time::Instant;

// ── ExportFormat ──────────────────────────────────────────────────────────────

/// Supported export formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportFormat {
    /// PNG (lossless, deflate-compressed).
    Png,
    /// Portable Pixmap (P6 binary).
    Ppm,
    /// 24-bit BMP.
    Bmp,
    /// SVG: one `<rect>` per sampled pixel block.
    Svg,
}

// ── ExportError ───────────────────────────────────────────────────────────────

/// Errors from export operations.
#[derive(Debug)]
pub enum ExportError {
    /// Creating or writing the file failed.
    Io(std::io::Error),
    /// Width or height is zero, or the pixel buffer does not hold
    /// `width * height` pixels.
    InvalidDimensions,
    /// The encoder rejected the image.
    UnsupportedFormat(String),
}

impl std::fmt::Display for ExportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExportError::Io(e) => write!(f, "IO error: {e}"),
            ExportError::InvalidDimensions => write!(
                f,
                "invalid image dimensions (zero size, or pixel count does not match width x height)"
            ),
            ExportError::UnsupportedFormat(s) => write!(f, "unsupported format: {s}"),
        }
    }
}

impl std::error::Error for ExportError {}

impl From<std::io::Error> for ExportError {
    fn from(e: std::io::Error) -> Self {
        ExportError::Io(e)
    }
}

impl From<image::ImageError> for ExportError {
    fn from(e: image::ImageError) -> Self {
        match e {
            image::ImageError::IoError(io) => ExportError::Io(io),
            other => ExportError::UnsupportedFormat(other.to_string()),
        }
    }
}

// ── ExportStats ───────────────────────────────────────────────────────────────

/// Statistics from a completed export.
#[derive(Debug, Clone)]
pub struct ExportStats {
    /// Size of the written file in bytes (read back from the file system).
    pub bytes_written: u64,
    /// Format that was written.
    pub format: ExportFormat,
    /// Image width in pixels.
    pub width: u32,
    /// Image height in pixels.
    pub height: u32,
    /// Wall-clock time spent encoding and writing.
    pub elapsed_ms: u64,
}

// ── ImageExporter ─────────────────────────────────────────────────────────────

/// Exports wallpaper images in multiple formats.
pub struct ImageExporter;

impl ImageExporter {
    /// Export `pixels` (row-major, RGB triplets) as `format` to `path`.
    ///
    /// Returns [`ExportError::InvalidDimensions`] if either side is zero or
    /// `pixels.len() != width * height`.
    pub fn export(
        pixels: &[[u8; 3]],
        width: u32,
        height: u32,
        format: ExportFormat,
        path: &Path,
    ) -> Result<ExportStats, ExportError> {
        if width == 0 || height == 0 || pixels.len() as u64 != width as u64 * height as u64 {
            return Err(ExportError::InvalidDimensions);
        }
        let start = Instant::now();
        match format {
            ExportFormat::Svg => write_svg(pixels, width, height, path)?,
            ExportFormat::Png | ExportFormat::Ppm | ExportFormat::Bmp => {
                let flat: Vec<u8> = pixels.iter().flatten().copied().collect();
                let img = image::RgbImage::from_raw(width, height, flat)
                    .ok_or(ExportError::InvalidDimensions)?;
                match format {
                    ExportFormat::Ppm => {
                        // The image crate's default PNM subtype is PAM (P7);
                        // ask for a binary pixmap (P6) explicitly.
                        use image::codecs::pnm::{PnmEncoder, PnmSubtype, SampleEncoding};
                        let file = std::io::BufWriter::new(std::fs::File::create(path)?);
                        let mut out = file;
                        PnmEncoder::new(&mut out)
                            .with_subtype(PnmSubtype::Pixmap(SampleEncoding::Binary))
                            .encode(
                                img.as_raw().as_slice(),
                                width,
                                height,
                                image::ExtendedColorType::Rgb8,
                            )?;
                        std::io::Write::flush(&mut out)?;
                    }
                    ExportFormat::Png => img.save_with_format(path, image::ImageFormat::Png)?,
                    _ => img.save_with_format(path, image::ImageFormat::Bmp)?,
                }
            }
        }
        let bytes_written = std::fs::metadata(path)?.len();
        let elapsed_ms = start.elapsed().as_millis() as u64;
        Ok(ExportStats {
            bytes_written,
            format,
            width,
            height,
            elapsed_ms,
        })
    }
}

/// Save tightly packed RGBA8 pixels as a compressed PNG.
///
/// Missing parent folders are created. Fails with
/// [`ExportError::InvalidDimensions`] if `rgba.len() != width * height * 4`.
/// Returns the size of the written file in bytes.
pub fn save_rgba_png(
    path: &Path,
    width: u32,
    height: u32,
    rgba: &[u8],
) -> Result<u64, ExportError> {
    if width == 0 || height == 0 || rgba.len() as u64 != width as u64 * height as u64 * 4 {
        return Err(ExportError::InvalidDimensions);
    }
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent)?;
        }
    }
    image::save_buffer_with_format(
        path,
        rgba,
        width,
        height,
        image::ColorType::Rgba8,
        image::ImageFormat::Png,
    )?;
    Ok(std::fs::metadata(path)?.len())
}

/// Write `rgba` (RGBA8) to `path` as a PNG.
///
/// Kept for backwards compatibility; prefer [`save_rgba_png`].
pub fn encode_png_rgba(
    path: &Path,
    width: u32,
    height: u32,
    rgba: &[u8],
) -> Result<(), std::io::Error> {
    save_rgba_png(path, width, height, rgba)
        .map(|_| ())
        .map_err(|e| match e {
            ExportError::Io(io) => io,
            other => std::io::Error::new(std::io::ErrorKind::InvalidInput, other.to_string()),
        })
}

// ── SVG writer ────────────────────────────────────────────────────────────────

/// Write an SVG with one `<rect>` per sampled pixel block.
///
/// For large images (>64x64) each pixel is drawn 8 units wide and at most
/// 256x256 cells are emitted, to keep the file a manageable size.
fn write_svg(pixels: &[[u8; 3]], width: u32, height: u32, path: &Path) -> Result<(), ExportError> {
    use std::io::Write;
    let mut f = std::fs::File::create(path)?;

    let tile = if width > 64 || height > 64 {
        8u32
    } else {
        1u32
    };
    let svg_w = width * tile;
    let svg_h = height * tile;

    let mut buf = String::new();
    buf.push_str(&format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="0 0 {} {}">
"#,
        svg_w, svg_h, svg_w, svg_h
    ));

    let max_cells = 256u32;
    let step_x = (width / max_cells).max(1);
    let step_y = (height / max_cells).max(1);

    let mut y = 0u32;
    while y < height {
        let mut x = 0u32;
        while x < width {
            let idx = (y * width + x) as usize;
            let [r, g, b] = pixels[idx];
            let rx = x * tile;
            let ry = y * tile;
            let rw = (step_x * tile).min(svg_w - rx);
            let rh = (step_y * tile).min(svg_h - ry);
            buf.push_str(&format!(
                "  <rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"#{:02X}{:02X}{:02X}\"/>\n",
                rx, ry, rw, rh, r, g, b
            ));
            x += step_x;
        }
        y += step_y;
    }

    buf.push_str("</svg>\n");
    f.write_all(buf.as_bytes())?;
    Ok(())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;

    fn sample_pixels(w: u32, h: u32) -> Vec<[u8; 3]> {
        (0..w * h)
            .map(|i| [(i % 256) as u8, ((i / 256) % 256) as u8, 128])
            .collect()
    }

    // 1. PPM export creates a file
    #[test]
    fn test_ppm_creates_file() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        let stats = ImageExporter::export(&pixels, 4, 4, ExportFormat::Ppm, tmp.path()).unwrap();
        assert!(tmp.path().exists());
        assert!(stats.bytes_written > 0);
    }

    // 2. PPM header is a valid binary PNM header
    #[test]
    fn test_ppm_header() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(2, 2);
        ImageExporter::export(&pixels, 2, 2, ExportFormat::Ppm, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        assert!(content.starts_with(b"P6"), "{:?}", &content[..8]);
    }

    // 3. PPM round-trips: decoding gives back the same pixels
    #[test]
    fn test_ppm_roundtrip() {
        let tmp = NamedTempFile::new().unwrap();
        let (w, h) = (3u32, 3u32);
        let pixels = sample_pixels(w, h);
        ImageExporter::export(&pixels, w, h, ExportFormat::Ppm, tmp.path()).unwrap();
        let img = image::load_from_memory_with_format(
            &std::fs::read(tmp.path()).unwrap(),
            image::ImageFormat::Pnm,
        )
        .unwrap()
        .to_rgb8();
        assert_eq!(img.dimensions(), (w, h));
        let back: Vec<[u8; 3]> = img.pixels().map(|p| p.0).collect();
        assert_eq!(back, pixels);
    }

    // 4. BMP export creates a file
    #[test]
    fn test_bmp_creates_file() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Bmp, tmp.path()).unwrap();
        assert!(tmp.path().exists());
    }

    // 5. BMP starts with 'BM' magic
    #[test]
    fn test_bmp_magic() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        assert_eq!(&content[..2], b"BM");
    }

    // 6. BMP file size field matches actual file
    #[test]
    fn test_bmp_file_size_field() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        let size_field = u32::from_le_bytes([content[2], content[3], content[4], content[5]]);
        assert_eq!(size_field as usize, content.len());
    }

    // 7. BMP bits per pixel is 24
    #[test]
    fn test_bmp_bits_per_pixel() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        let bpp = u16::from_le_bytes([content[28], content[29]]);
        assert_eq!(bpp, 24);
    }

    // 8. PNG export creates a file
    #[test]
    fn test_png_creates_file() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Png, tmp.path()).unwrap();
        assert!(tmp.path().exists());
    }

    // 9. PNG starts with PNG signature
    #[test]
    fn test_png_signature() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Png, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        assert_eq!(&content[..8], &[137, 80, 78, 71, 13, 10, 26, 10]);
    }

    // 10. SVG export creates a file
    #[test]
    fn test_svg_creates_file() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Svg, tmp.path()).unwrap();
        assert!(tmp.path().exists());
    }

    // 11. SVG contains <svg> tag
    #[test]
    fn test_svg_tag() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Svg, tmp.path()).unwrap();
        let content = std::fs::read_to_string(tmp.path()).unwrap();
        assert!(content.contains("<svg"));
        assert!(content.contains("</svg>"));
    }

    // 12. SVG contains rect elements
    #[test]
    fn test_svg_has_rects() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        ImageExporter::export(&pixels, 4, 4, ExportFormat::Svg, tmp.path()).unwrap();
        let content = std::fs::read_to_string(tmp.path()).unwrap();
        assert!(content.contains("<rect"));
    }

    // 13. Invalid dimensions returns error
    #[test]
    fn test_invalid_dimensions() {
        let tmp = NamedTempFile::new().unwrap();
        let err = ImageExporter::export(&[], 0, 10, ExportFormat::Ppm, tmp.path()).unwrap_err();
        assert!(matches!(err, ExportError::InvalidDimensions));
    }

    // 14. ExportStats has correct dimensions
    #[test]
    fn test_stats_dimensions() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(8, 6);
        let stats = ImageExporter::export(&pixels, 8, 6, ExportFormat::Ppm, tmp.path()).unwrap();
        assert_eq!(stats.width, 8);
        assert_eq!(stats.height, 6);
    }

    // 15. ExportStats has correct format
    #[test]
    fn test_stats_format() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(4, 4);
        let stats = ImageExporter::export(&pixels, 4, 4, ExportFormat::Bmp, tmp.path()).unwrap();
        assert_eq!(stats.format, ExportFormat::Bmp);
    }

    // 16. ExportError display is non-empty
    #[test]
    fn test_error_display() {
        let e = ExportError::InvalidDimensions;
        assert!(!e.to_string().is_empty());
    }

    // 17. BMP width field matches
    #[test]
    fn test_bmp_width_field() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(5, 3);
        ImageExporter::export(&pixels, 5, 3, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        let w = i32::from_le_bytes([content[18], content[19], content[20], content[21]]);
        assert_eq!(w, 5);
    }

    // 18. BMP height field matches
    #[test]
    fn test_bmp_height_field() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = sample_pixels(5, 3);
        ImageExporter::export(&pixels, 5, 3, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        let h = i32::from_le_bytes([content[22], content[23], content[24], content[25]]);
        assert_eq!(h, 3);
    }

    // 19. PPM export with 1x1 pixel
    #[test]
    fn test_ppm_one_pixel() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = vec![[255u8, 128u8, 0u8]];
        let stats = ImageExporter::export(&pixels, 1, 1, ExportFormat::Ppm, tmp.path()).unwrap();
        assert!(stats.bytes_written > 0);
    }

    // 20. BMP export with 1x1 pixel
    #[test]
    fn test_bmp_one_pixel() {
        let tmp = NamedTempFile::new().unwrap();
        let pixels = vec![[255u8, 128u8, 0u8]];
        ImageExporter::export(&pixels, 1, 1, ExportFormat::Bmp, tmp.path()).unwrap();
        let content = std::fs::read(tmp.path()).unwrap();
        assert_eq!(&content[..2], b"BM");
    }

    // 21. bytes_written is the real file size for every format (the old PNG
    //     path returned an estimate).
    #[test]
    fn test_bytes_written_matches_file_size() {
        for fmt in [
            ExportFormat::Png,
            ExportFormat::Ppm,
            ExportFormat::Bmp,
            ExportFormat::Svg,
        ] {
            let tmp = NamedTempFile::new().unwrap();
            let pixels = sample_pixels(37, 11);
            let stats = ImageExporter::export(&pixels, 37, 11, fmt, tmp.path()).unwrap();
            let on_disk = std::fs::metadata(tmp.path()).unwrap().len();
            assert_eq!(stats.bytes_written, on_disk, "{fmt:?}");
        }
    }

    // 22. PNG is actually compressed (the old encoder used stored deflate
    //     blocks, so a 256x256 gradient came out larger than the raw pixels).
    #[test]
    fn test_png_is_compressed_and_decodes() {
        let tmp = NamedTempFile::new().unwrap();
        let (w, h) = (256u32, 256u32);
        let pixels: Vec<[u8; 3]> = (0..w * h)
            .map(|i| [(i % w) as u8, (i / w) as u8, 64])
            .collect();
        let stats = ImageExporter::export(&pixels, w, h, ExportFormat::Png, tmp.path()).unwrap();
        assert!(
            stats.bytes_written < (w * h * 3) as u64 / 4,
            "PNG of a smooth gradient should be well under raw size, got {} bytes",
            stats.bytes_written
        );
        let img = image::load_from_memory_with_format(
            &std::fs::read(tmp.path()).unwrap(),
            image::ImageFormat::Png,
        )
        .unwrap()
        .to_rgb8();
        let back: Vec<[u8; 3]> = img.pixels().map(|p| p.0).collect();
        assert_eq!(back, pixels);
    }

    // 23. A pixel buffer that is too short is an error, not a panic (the old
    //     BMP writer indexed out of bounds).
    #[test]
    fn test_short_buffer_is_error() {
        for fmt in [
            ExportFormat::Png,
            ExportFormat::Ppm,
            ExportFormat::Bmp,
            ExportFormat::Svg,
        ] {
            let tmp = NamedTempFile::new().unwrap();
            let pixels = sample_pixels(4, 3);
            let err = ImageExporter::export(&pixels, 4, 4, fmt, tmp.path()).unwrap_err();
            assert!(matches!(err, ExportError::InvalidDimensions), "{fmt:?}");
        }
    }

    // 24. save_rgba_png creates parent folders and rejects wrong sizes.
    #[test]
    fn test_save_rgba_png() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("a").join("b").join("x.png");
        let rgba = vec![200u8; 8 * 8 * 4];
        let n = save_rgba_png(&path, 8, 8, &rgba).unwrap();
        assert_eq!(n, std::fs::metadata(&path).unwrap().len());
        assert_eq!(image::open(&path).unwrap().width(), 8);
        assert!(save_rgba_png(&path, 8, 9, &rgba).is_err());
    }
}