-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into recent_menu_redesign
- Loading branch information
Showing
12 changed files
with
416 additions
and
163 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule libheif
deleted from
8bc6fc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => {} | ||
} | ||
} | ||
} |
Oops, something went wrong.