Skip to content

Commit

Permalink
begin good stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
FerrahWolfeh committed Sep 20, 2024
1 parent 0bf14a9 commit 62bfa0a
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 54 deletions.
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "avif-converter"
description = "Simple tool to batch convert multiple images to AVIF"
authors = ["Ferrah Aiko <[email protected]>"]
license = "MIT"
version = "1.10.0"
version = "1.11.0"
edition = "2021"
readme = ""

Expand All @@ -13,7 +13,7 @@ readme = ""
bytesize = "1.2.0"
clap = { version = "4", features = ["derive", "cargo"] }
color-eyre = { version = "0.6.2", default_features = false }
env_logger = { version = "0.10.0", default_features = false, features = [
env_logger = { version = "0.11.3", default_features = false, features = [
"auto-color",
] }
hex = "0.4.3"
Expand All @@ -22,30 +22,30 @@ indicatif = { version = "0.17" }
log = "0.4"
md5 = { version = "0.7.0", default_features = false }
num_cpus = "1.16.0"
owo-colors = "3.5.0"
owo-colors = "4.0.0"
rand = "0.8.5"
rgb = "0.8.36"
sha2 = { version = "0.10.6", features = ["asm"] }
spinoff = "0.8.0"
once_cell = "1.17.1"
threadpool = "1.8.1"
image = { version = "0.24.7", default-features = false, features = [
"rgb",
"png",
"jpeg",
"webp",
"bmp",
] }
rav1e = { version = "0.6.6", default_features = false, features = ["threading", "asm"] }
image = { version = "0.25.1", default-features = false, features = ["png", "jpeg", "webp", "bmp", "avif-native", "nasm", "rayon", "avif"] }
rav1e = { version = "0.7.1", default_features = false, features = ["threading", "asm"] }
thiserror = "1.0"
loop9 = "0.1.3"
avif-serialize = "0.8.1"
notify-rust = { version = "4.8.0", features = ["images"] }
thread-priority = "0.13.1"
thread-priority = "1.1.0"
notify = "6.0.1"
blake2 = { version = "0.10.6" }
opencv = { version = "0.93.0", default-features = false, features = ["imgproc", "imgcodecs", "rgb"], optional = true}

[profile.release]
lto = false
opt-level = 3
panic = "abort"

[features]
default = ["ssim"]
ssim = ["opencv"]
opencv = ["dep:opencv"]
45 changes: 28 additions & 17 deletions src/cli/commands/avif.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::atomic::Ordering, time::Instant};
use std::{path::PathBuf, process::exit, sync::atomic::Ordering, time::Instant};

use bytesize::ByteSize;
use clap::Args;
Expand All @@ -11,16 +11,18 @@ use crate::{
cli::{Args as Globals, FINAL_STATS, ITEMS_PROCESSED, SUCCESS_COUNT},
console::ConsoleMsg,
image_file::ImageFile,
utils::{calculate_tread_count, search_dir, sys_threads, PROGRESS_BAR},
utils::{calculate_tread_count, parse_files, sys_threads, PROGRESS_BAR},
};
use color_eyre::Result;

use super::EncodeFuncs;

#[derive(Args, Debug, Clone)]
#[clap(author, about, long_about = None)]
pub struct Avif {
/// File or directory containing images to convert
#[clap(value_name = "PATH")]
pub path: PathBuf,
#[clap(value_name = "PATH", required = true)]
pub path: Vec<PathBuf>,

/// Enable benchmark mode
#[clap(
Expand All @@ -39,39 +41,48 @@ pub struct Avif {
/// Send a notification to the desktop when all jobs are finished
#[clap(short = 'N', long, default_value_t = false)]
pub notify: bool,

/// Measure SSIM of encoded vs original image/s.
#[cfg(feature = "ssim")]
#[clap(long = "ssim", default_value_t = false)]
pub ssim: bool,

/// Save SSIM difference as an image along with the encoded file.
#[cfg(feature = "ssim")]
#[clap(long = "ssim_save", default_value_t = false, requires = "ssim")]
pub ssim_save: bool,
}

impl Avif {
pub fn run_conv(self, globals: &Globals) -> Result<()> {
impl EncodeFuncs for Avif {
fn run_conv(self, globals: &Globals) -> Result<()> {
let console = ConsoleMsg::new(globals.quiet, self.notify);
let error_con = ConsoleMsg::new(globals.quiet, self.notify);

let u = {
if self.path.is_dir() {
self.dir_conv(console, globals)
} else if self.path.is_file() {
self.single_file_conv(console, globals)
} else {
bail!("Unsupported operation")
}
let l_size = self.path.len();

let u = if l_size > 1 {
self.batch_conv(console, globals)
} else {
self.single_file_conv(console, globals)
};

if let Err(error) = u {
error_con.notify_error(&error.to_string())?;
exit(1);
}

Ok(())
}

fn dir_conv(self, console: ConsoleMsg, globals: &Globals) -> Result<()> {
fn batch_conv(self, console: ConsoleMsg, globals: &Globals) -> Result<()> {
if self.output_file.is_some() {
bail!("Cannot assign an output file to a directory")
}

let mut console = console;
console.set_spinner("Searching for files...");

let mut paths = search_dir(&self.path);
let mut paths = parse_files(&self.path);
let psize = paths.len();

paths.sort_by(|a, b| a.metadata.name.cmp(&b.metadata.name));
Expand Down Expand Up @@ -203,7 +214,7 @@ impl Avif {

fn single_file_conv(self, console: ConsoleMsg, globals: &Globals) -> Result<()> {
let mut console = console;
let mut image = ImageFile::new_from_path(&self.path)?;
let mut image = ImageFile::new_from_path(&self.path[0])?;
let image_size = image.metadata.size;

console.print_message(format!(
Expand Down
14 changes: 14 additions & 0 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use clap::Subcommand;

use crate::console::ConsoleMsg;

use self::{avif::Avif, watch::Watch};

use super::Args as Globals;
use color_eyre::Result;

pub mod avif;
//pub mod png;
pub mod watch;

#[derive(Debug, Subcommand, Clone)]
Expand All @@ -12,3 +18,11 @@ pub enum Commands {
/// Watch directory for new image files and convert them
Watch(Watch),
}

pub trait EncodeFuncs {
fn run_conv(self, globals: &Globals) -> Result<()>;

fn batch_conv(self, console: ConsoleMsg, globals: &Globals) -> Result<()>;

fn single_file_conv(self, console: ConsoleMsg, globals: &Globals) -> Result<()>;
}
Loading

0 comments on commit 62bfa0a

Please sign in to comment.