Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save button to twix #1665

Merged
merged 8 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/types/src/jpeg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use color_eyre::eyre;
use image::{codecs::jpeg::JpegEncoder, ImageBuffer, ImageError, Luma, RgbImage};
use serde::{Deserialize, Serialize};
use std::path::Path;

use crate::{grayscale_image::GrayscaleImage, ycbcr422_image::YCbCr422Image};

Expand Down Expand Up @@ -36,3 +38,9 @@ impl TryFrom<&YCbCr422Image> for JpegImage {
Ok(Self { data: jpeg_buffer })
}
}

impl JpegImage {
pub fn save_to_jpeg_file(&self, file: impl AsRef<Path>) -> eyre::Result<()> {
Ok(std::fs::write(file, self.data.clone())?)
}
}
2 changes: 1 addition & 1 deletion tools/twix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "twix"
version = "0.9.2"
version = "0.9.3"
edition.workspace = true
license.workspace = true
homepage.workspace = true
Expand Down
40 changes: 39 additions & 1 deletion tools/twix/src/panels/image/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{env::temp_dir, fs::create_dir_all, path::PathBuf, sync::Arc};

use chrono::{DateTime, Utc};
use color_eyre::{eyre::eyre, Result};
Expand All @@ -7,6 +7,7 @@ use eframe::egui::{ColorImage, Response, SizeHint, TextureOptions, Ui, UiBuilder
use geometry::rectangle::Rectangle;
use image::RgbImage;
use linear_algebra::{point, vector};
use log::{info, warn};
use serde_json::{json, Value};

use types::{jpeg::JpegImage, ycbcr422_image::YCbCr422Image};
Expand Down Expand Up @@ -91,6 +92,24 @@ impl Panel for ImagePanel {
}
}

fn save_jpeg_image(buffer: &BufferHandle<JpegImage>, path: PathBuf) -> Result<()> {
let buffer = buffer
.get_last_value()?
.ok_or_else(|| eyre!("no image available"))?;
buffer.save_to_jpeg_file(&path)?;
info!("image saved to '{}'", path.display());
Ok(())
}

fn save_raw_image(buffer: &BufferHandle<YCbCr422Image>, path: PathBuf) -> Result<()> {
let buffer = buffer
.get_last_value()?
.ok_or_else(|| eyre!("no image available"))?;
buffer.save_to_ycbcr_444_file(&path)?;
info!("image saved to '{}'", path.display());
Ok(())
}

impl Widget for &mut ImagePanel {
fn ui(self, ui: &mut Ui) -> Response {
ui.horizontal(|ui| {
Expand All @@ -112,6 +131,25 @@ impl Widget for &mut ImagePanel {
let date: DateTime<Utc> = timestamp.into();
ui.label(date.format("%T%.3f").to_string());
}
if ui.button("Save").clicked() {
let time_stamp = Utc::now().format("%H:%M:%S%.3f").to_string();
let directory = temp_dir().join("twix");
if let Err(error) = create_dir_all(&directory) {
warn!("failed to create temporary folder /tmp/twix: {error}");
} else {
let cycler_name = format!("{:?}", self.cycler);
let path = directory.join(format!("image_{cycler_name}_{time_stamp}.png"));
let result = match &self.image_buffer {
RawOrJpeg::Raw(buffer) => save_raw_image(buffer, path),
RawOrJpeg::Jpeg(buffer) => {
save_jpeg_image(buffer, path.with_extension("jpeg"))
}
};
if let Err(error) = result {
warn!("failed to save image: {error}");
}
}
}
});
let (response, mut painter) = TwixPainter::allocate(
ui,
Expand Down