Skip to content

Commit

Permalink
Merge branch 'master' into recent_menu_redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper authored Oct 25, 2024
2 parents 7924c82 + 03c1341 commit 32c8aa9
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 163 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

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

12 changes: 3 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ ruzstd = "0.7" # for ktx
basis-universal = "0.3.1"
mouse_position = "0.1.3"
webp-animation = { version = "0.9.0", features = ["static"] }
fluent-uri = "0.1.4"
imageproc = { version = "0.25", features = ["rayon"] }
num-traits = "0.2.18"
zerocopy = "0.7.34"
icns = "0.3.1"
jpeg2k = { version = "0.9", optional = true, default-features = false, features = [
"threads",
Expand Down Expand Up @@ -134,10 +131,7 @@ cmake = "0.1"
cmd_lib = "1.3.0"
xmltree = "0.11.0"
chrono = "0.4.38"
criterion = { version = "0.5.1", features = [
"html_reports",
"stable",
] }
criterion = { version = "0.5.1", features = ["html_reports", "stable"] }

[profile.release]
# codegen-units = 1
Expand All @@ -148,9 +142,9 @@ strip = true
panic = "abort"

[profile.dev]
debug = true
debug = false
incremental = true
opt-level = 1
opt-level = 3


[[bench]]
Expand Down
1 change: 0 additions & 1 deletion libheif
Submodule libheif deleted from 8bc6fc
109 changes: 109 additions & 0 deletions src/file_encoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! File encoders - this defines save options.
//!
//! To add more formats, add a variant to the `[FileEncoder]` struct.
use crate::ui::EguiExt;
use anyhow::Result;
use image::codecs::jpeg::JpegEncoder;
use image::codecs::png::{CompressionType, PngEncoder};
use image::DynamicImage;
use notan::egui::Ui;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::path::Path;
use strum::{Display, EnumIter};

#[derive(Default, Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Display, EnumIter)]

pub enum CompressionLevel {
Best,
#[default]
Default,
Fast,
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Display, EnumIter)]
pub enum FileEncoder {
Jpg { quality: u32 },
Png { compressionlevel: CompressionLevel },
Bmp,
WebP,
}

impl Default for FileEncoder {
fn default() -> Self {
Self::Png {
compressionlevel: CompressionLevel::Default,
}
}
}

impl FileEncoder {
pub fn matching_variant(path: &Path, variants: &Vec<Self>) -> Self {
let ext = path
.extension()
.map(|e| e.to_string_lossy().to_string())
.unwrap_or_default()
.to_lowercase()
.replace("jpeg", "jpg");

for v in variants {
if v.ext() == ext {
return v.clone();
}
}

Self::Png {
compressionlevel: CompressionLevel::Default,
}
}

pub fn ext(&self) -> String {
self.to_string().to_lowercase()
}

pub fn save(&self, image: &DynamicImage, path: &Path) -> Result<()> {
match self {
FileEncoder::Jpg { quality } => {
let w = File::create(path)?;
JpegEncoder::new_with_quality(w, *quality as u8).encode_image(image)?;
}
FileEncoder::Png { compressionlevel } => {
let w = File::create(path)?;

PngEncoder::new_with_quality(
w,
match compressionlevel {
CompressionLevel::Best => CompressionType::Best,
CompressionLevel::Default => CompressionType::Default,
CompressionLevel::Fast => CompressionType::Fast,
},
image::codecs::png::FilterType::Adaptive,
);
image.save_with_format(path, image::ImageFormat::Png)?;
}
FileEncoder::Bmp => {
image.save_with_format(path, image::ImageFormat::Bmp)?;
}
FileEncoder::WebP => {
image.save_with_format(path, image::ImageFormat::WebP)?;
}
}

Ok(())
}

pub fn ui(&mut self, ui: &mut Ui) {
match self {
FileEncoder::Jpg { quality } => {
ui.label("Quality");
ui.styled_slider(quality, 0..=100);
}
FileEncoder::Png {
compressionlevel: _,
} => {}
FileEncoder::Bmp => {}
FileEncoder::WebP => {}
}
}
}
Loading

0 comments on commit 32c8aa9

Please sign in to comment.