saving images

This commit is contained in:
Joshua Perry 2024-11-04 21:34:23 +00:00
parent 1545cc8752
commit 27c803dd20
5 changed files with 817 additions and 6 deletions

778
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,4 +10,6 @@ eframe = "0.29.1"
egui = "0.29.1" egui = "0.29.1"
egui-file-dialog = "0.7.0" egui-file-dialog = "0.7.0"
egui_plot = "0.29.0" egui_plot = "0.29.0"
image = "0.25.4"
plotters = "0.3.3"
rand = "0.8.5" rand = "0.8.5"

View File

@ -19,7 +19,7 @@ pub fn display_plot<'a>(mut circles: Vec<crate::Circle>, circle_length: &'a usiz
pub fn display_gui() -> Display<'static> { pub fn display_gui() -> Display<'static> {
Box::new(move |ui| { Box::new(move |_ui| {
//TODO: UI (on unfocus update values) //TODO: UI (on unfocus update values)
// Message Content input // Message Content input
// start shift input // start shift input

View File

@ -20,10 +20,11 @@ pub fn run() -> anyhow::Result<()> {
} }
} }
fn encode(message: &mut crate::Message, circle_length: &mut usize, output: crate::OutputMode) -> anyhow::Result<()> { fn encode(message: &mut crate::Message, circle_length: &mut usize, output: crate::OutputMode) -> anyhow::Result<()> {
match output { match output {
crate::OutputMode::Display => crate::gui::gui(message, circle_length, false), crate::OutputMode::Display => crate::gui::gui(message, circle_length, false),
crate::OutputMode::Save { path } => Ok(()), //TODO: Create plot and save crate::OutputMode::Save { path } => save(&path, message, circle_length),
} }
} }
@ -69,5 +70,6 @@ pub use encoding::encrypt_message;
pub use gui::Error; pub use gui::Error;
pub use message::Message; pub use message::Message;
pub use plotting::plot; pub use plotting::plot;
pub use plotting::save;

View File

@ -1,4 +1,4 @@
use std::f64::{self, consts::PI}; use std::{f64::{self, consts::PI}, path::PathBuf};
pub fn plot(ui: &mut egui::Ui, circles: &mut Vec<crate::Circle>, num_points: &usize, line_colors: &Vec<egui::Color32>) { pub fn plot(ui: &mut egui::Ui, circles: &mut Vec<crate::Circle>, num_points: &usize, line_colors: &Vec<egui::Color32>) {
egui_plot::Plot::new("Encoded Message") egui_plot::Plot::new("Encoded Message")
@ -23,7 +23,40 @@ pub fn plot(ui: &mut egui::Ui, circles: &mut Vec<crate::Circle>, num_points: &us
gaussian_distribution(num_points, &points, &(count as f64)) gaussian_distribution(num_points, &points, &(count as f64))
).color(line_colors[count])); ).color(line_colors[count]));
}); });
}); });
}
pub fn save(path: &PathBuf, message: &mut crate::Message, circle_length: &usize) -> anyhow::Result<()> { //TODO: Add line_colors
use plotters::prelude::*;
let root = BitMapBackend::new(path, (1000,1000)).into_drawing_area();
root.fill(&WHITE)?;
let mut circles = crate::encrypt_message(message, circle_length);
let length = (circles.len() as f64) + 1.0;
let mut chart = ChartBuilder::on(&root)
.margin(5)
.build_cartesian_2d(-length..length, -length..length)?;
circles.iter_mut().enumerate().for_each(|(count, circle)| {
let peak_values = match count {
count if count % 2 == 0 => crate::circle_to_points(circle),
_ => {
circle.reverse();
crate::circle_to_points(circle)
},
};
let points: Vec<(f64, f64)> = gaussian_distribution(circle_length, &peak_values, &(count as f64)).points().iter()
.map(|point| {
(point.x, point.y)
}).collect();
chart.draw_series(LineSeries::new(points, &RED)).unwrap();
});
root.present()?;
Ok(())
} }
fn gaussian_distribution(num_points: &usize, peak_values: &Vec<f64>, offset: &f64) -> egui_plot::PlotPoints { fn gaussian_distribution(num_points: &usize, peak_values: &Vec<f64>, offset: &f64) -> egui_plot::PlotPoints {