Skip to main content

cvkg_cli/
raster_export.rs

1//! Raster export -- Exports rendered views to PNG and GIF formats.
2//!
3//! Delegates encoding to the `cvkg-export-raster` crate.
4//! No FFmpeg dependency.
5
6use std::path::PathBuf;
7
8/// Export configuration for raster output.
9pub struct RasterExport {
10    /// Output format
11    pub format: String,
12    /// Output file path
13    pub output: PathBuf,
14    /// Number of frames (for gif)
15    pub frames: u16,
16    /// Frames per second (for gif)
17    pub fps: u16,
18}
19
20impl RasterExport {
21    /// Create a new raster export configuration.
22    pub fn new(format: String, output: PathBuf, frames: u16, fps: u16) -> Self {
23        Self {
24            format,
25            output,
26            frames,
27            fps,
28        }
29    }
30
31    /// Execute the export.
32    pub fn execute(&self) -> Result<(), String> {
33        println!(
34            "Raster export: format={}, output={}, frames={}, fps={}",
35            self.format,
36            self.output.display(),
37            self.frames,
38            self.fps
39        );
40
41        if self.format == "png" {
42            self.export_png()
43        } else if self.format == "gif" {
44            self.export_gif()
45        } else {
46            Err(format!("Unsupported format: {}", self.format))
47        }
48    }
49
50    fn export_png(&self) -> Result<(), String> {
51        // Create a test pattern using cvkg-export-raster
52        let frame = cvkg_export_raster::CapturedFrame {
53            width: 200,
54            height: 200,
55            rgba: vec![128u8; 200 * 200 * 4],
56        };
57        let png_bytes = cvkg_export_raster::encode_png(&frame)?;
58        std::fs::write(&self.output, png_bytes)
59            .map_err(|e| format!("Failed to write PNG: {}", e))?;
60        println!("Wrote test PNG to {}", self.output.display());
61        Ok(())
62    }
63
64    fn export_gif(&self) -> Result<(), String> {
65        let frames: Vec<cvkg_export_raster::CapturedFrame> = (0..self.frames)
66            .map(|_| cvkg_export_raster::CapturedFrame {
67                width: 200,
68                height: 200,
69                rgba: vec![128u8; 200 * 200 * 4],
70            })
71            .collect();
72        let gif_bytes = cvkg_export_raster::encode_gif(&frames, self.fps)?;
73        std::fs::write(&self.output, gif_bytes)
74            .map_err(|e| format!("Failed to write GIF: {}", e))?;
75        println!(
76            "Wrote test GIF ({} frames) to {}",
77            self.frames,
78            self.output.display()
79        );
80        Ok(())
81    }
82}