Skip to content

Commit

Permalink
use a better image handling function
Browse files Browse the repository at this point in the history
  • Loading branch information
FerrahWolfeh committed Jul 7, 2023
1 parent e4397ef commit 9104392
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
22 changes: 12 additions & 10 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,25 @@ impl Args {
)?;

if !self.benchmark {
let outp = image.save_avif(self.output_file, self.name_type, self.keep)?;
console.notify_image(
&format!(
"Finished in {:.2?} \n {} → {}",
start.elapsed(),
ByteSize::b(image.metadata.size).to_string_as(true),
ByteSize::b(fsz).to_string_as(true)
),
&outp,
)?;
image.save_avif(self.output_file, self.name_type, self.keep)?;
}

console.notify_image(
&format!(
"Finished in {:.2?} \n {} → {}",
start.elapsed(),
ByteSize::b(image.metadata.size).to_string_as(true),
ByteSize::b(fsz).to_string_as(true)
),
&image.bitmap,
)?;

console.finish_spinner(&format!(
"Encoding finished in {:?} ({})",
start.elapsed(),
ByteSize::b(fsz).to_string_as(true).bold().green()
));

Ok(())
}
}
8 changes: 4 additions & 4 deletions src/image_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl ImageFile {
Ok(self.encoded_data.len() as u64)
}

pub fn save_avif(&self, path: Option<PathBuf>, name: Name, keep: bool) -> Result<PathBuf> {
pub fn save_avif(&self, path: Option<PathBuf>, name: Name, keep: bool) -> Result<()> {
let fname = name.generate_name(self);

let binding = self.metadata.path.canonicalize()?;
Expand All @@ -125,7 +125,7 @@ impl ImageFile {

if let Some(new_path) = path {
fs::write(new_path, &self.encoded_data)?;
return Ok(avif_name);
return Ok(());
}

if !keep {
Expand All @@ -138,12 +138,12 @@ impl ImageFile {

fs::rename(&binding, &avif_name)?;

return Ok(avif_name);
return Ok(());
}

fs::write(&avif_name, &self.encoded_data)?;

Ok(avif_name)
Ok(())
}

pub fn original_name(&self) -> String {
Expand Down
13 changes: 10 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fmt::Write, fs, path::Path, time::Duration};

use color_eyre::Result;
use image::DynamicImage;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use notify_rust::Notification;
use notify_rust::{Image, Notification};
use once_cell::sync::Lazy;
use spinoff::{spinners, Color, Spinner, Streams};

Expand Down Expand Up @@ -78,13 +79,19 @@ impl ConsoleMsg {
Ok(())
}

pub fn notify_image(&self, message: &str, image_path: &Path) -> Result<()> {
pub fn notify_image(&self, message: &str, image: &DynamicImage) -> Result<()> {
let img = image.resize(512, 512, image::imageops::FilterType::Nearest);

if self.notify {
Notification::new()
.appname("AVIF Converter")
.summary("Conversion Completed")
.body(message)
.image_path(image_path.as_os_str().to_str().unwrap())
.image_data(Image::from_rgba(
img.width() as i32,
img.height() as i32,
img.to_rgba8().into_vec(),
)?)
.show()?;
}

Expand Down

0 comments on commit 9104392

Please sign in to comment.