Skip to content

Commit

Permalink
cleanup and finalize v 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaryen committed Jun 2, 2024
1 parent bddbb8c commit 9d2039f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/_modified
*.exe
/test
/test_modified
20 changes: 10 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "dpirust"
name = "PNGDPI"
version = "0.1.0"
edition = "2021"

Expand Down
32 changes: 22 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::fs::{self, File, OpenOptions};
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;
use walkdir::WalkDir;
use eframe::egui::{self};
use eframe::egui::{self, Button};

fn main() {
let options = eframe::NativeOptions::default();
let _ = eframe::run_native(
"DPI Modifier",
"PNG DPI Modifier",
options,
Box::new(|_cc| Box::new(App::default())),
);
Expand All @@ -23,27 +23,38 @@ struct App {
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("DPI Modifier");
ui.heading("Modify DPI:");

ui.label("Folder Path:");
ui.add_space(20.0);

ui.label("Target Folder Path:");
ui.text_edit_singleline(&mut self.folder_path);

ui.label("DPI:");


ui.label("Target DPI:");
ui.text_edit_singleline(&mut self.dpi);

ui.add_space(20.0);

if ui.button("Run").clicked() {
if ui.add_sized([80.0, 40.0], Button::new("Run")).clicked() {
let dpi: u32 = match self.dpi.parse() {
Ok(dpi) => dpi,
Err(_) => {
self.message = "Invalid DPI value".to_string();
return;
}
};

if self.folder_path.is_empty() {
self.message = "Folder path is required".to_string();
return;
}

if let Err(e) = process_folder(&self.folder_path, dpi) {
self.message = format!("Error: {:?}", e);
} else {
self.message = "Processing complete".to_string();
self.message = "Processing complete, results are in a _modified folder next to original location".to_string();
println!("Finished.");
}
}

Expand All @@ -55,7 +66,8 @@ impl eframe::App for App {

fn process_folder(folder_path: &str, new_dpi: u32) -> Result<(), Box<dyn std::error::Error>> {
let original_folder = Path::new(folder_path);
let modified_folder_path = original_folder.with_file_name("_modified");
let modified_folder_name = format!("{}_modified", original_folder.file_name().unwrap().to_string_lossy());
let modified_folder_path = original_folder.with_file_name(modified_folder_name);
fs::create_dir_all(&modified_folder_path)?;

for entry in WalkDir::new(folder_path) {
Expand Down Expand Up @@ -114,7 +126,7 @@ fn modify_dpi(path: &Path, dpi: u32, output_base: &Path, input_base: &Path) -> R
phys_chunk_found = true;
let x_ppm = u32::from_be_bytes([chunk_data[0], chunk_data[1], chunk_data[2], chunk_data[3]]);
let y_ppm = u32::from_be_bytes([chunk_data[4], chunk_data[5], chunk_data[6], chunk_data[7]]);
let x_dpi = x_ppm as f32 / 39.3701;
let x_dpi = x_ppm as f32 / 39.3701; //1000/25.4 inches per meter
let y_dpi = y_ppm as f32 / 39.3701;
println!("original DPI: x = {:.2}, y = {:.2}", x_dpi, y_dpi);

Expand Down

0 comments on commit 9d2039f

Please sign in to comment.