Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 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
Expand Up @@ -4,7 +4,7 @@ members = ["client", "daemon", "common", "tests"]
default-members = ["client", "daemon"]

[workspace.package]
version = "0.11.2-master"
version = "0.11.2-master2"
authors = ["Leonardo Gibrowski Faé <[email protected]>"]
edition = "2024"
license-file = "LICENSE"
Expand Down
61 changes: 44 additions & 17 deletions client/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// Note: this file only has basic declarations and some definitions in order to be possible to
/// import it in the build script, to automate shell completion
use clap::{Parser, ValueEnum};
use std::fmt::Display;
use std::path::PathBuf;

fn from_hex(hex: &str) -> Result<[u8; 4], String> {
Expand Down Expand Up @@ -41,7 +40,7 @@ pub enum PixelFormat {
Argb,
}

#[derive(Clone)]
#[derive(Clone, Copy)]
pub enum Filter {
Nearest,
Bilinear,
Expand All @@ -50,6 +49,18 @@ pub enum Filter {
Lanczos3,
}

impl Filter {
pub fn as_str(&self) -> &'static str {
match self {
Self::Nearest => "Nearest",
Self::Bilinear => "Bilinear",
Self::CatmullRom => "CatmullRom",
Self::Mitchell => "Mitchell",
Self::Lanczos3 => "Lanczos3",
}
}
}

impl std::str::FromStr for Filter {
type Err = &'static str;

Expand All @@ -60,26 +71,13 @@ impl std::str::FromStr for Filter {
"CatmullRom" => Ok(Self::CatmullRom),
"Mitchell" => Ok(Self::Mitchell),
"Lanczos3" => Ok(Self::Lanczos3),
_ => Err("unrecognized filter. Valid filters are:\
Nearest | Bilinear | CatmullRom | Mitchell | Lanczos3\
_ => Err("unrecognized filter. Valid filters are:\n\
\tNearest | Bilinear | CatmullRom | Mitchell | Lanczos3\n\
see swww img --help for more details"),
}
}
}

impl Display for Filter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Self::Nearest => "Nearest",
Self::Bilinear => "Bilinear",
Self::CatmullRom => "CatmullRom",
Self::Mitchell => "Mitchell",
Self::Lanczos3 => "Lanczos3",
};
write!(f, "{}", str)
}
}

#[derive(Clone)]
pub enum TransitionType {
None,
Expand Down Expand Up @@ -294,6 +292,35 @@ pub enum ResizeStrategy {
Stretch,
}

impl ResizeStrategy {
pub fn as_str(&self) -> &'static str {
match self {
ResizeStrategy::No => "no",
ResizeStrategy::Crop => "crop",
ResizeStrategy::Fit => "fit",
ResizeStrategy::Stretch => "stretch",
}
}
}

impl std::str::FromStr for ResizeStrategy {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"no" => Ok(Self::No),
"crop" => Ok(Self::Crop),
"fit" => Ok(Self::Fit),
"stretch" => Ok(Self::Stretch),
_ => Err(
"unrecognized resize strategy. Valid resize strategies are:\n\
no | crop | fit | stretch\n\
see swww img --help for more details",
),
}
}
}

#[derive(Parser)]
pub struct Restore {
/// Restore all swww-daemon instances (all namespaces)
Expand Down
47 changes: 28 additions & 19 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ fn make_img_request(
let transition = make_transition(img);
let mut img_req_builder = ipc::ImageRequestBuilder::new(transition);

let filter = img.filter.as_str();
let resize = img.resize.as_str();

match &img.image {
CliImage::Color(color) => {
for (&dim, outputs) in dims.iter().zip(outputs) {
Expand All @@ -159,8 +162,9 @@ fn make_img_request(
dim,
format: pixel_format,
},
namespace.to_string(),
Filter::Lanczos3.to_string(),
namespace,
resize,
filter,
outputs,
None,
);
Expand All @@ -186,14 +190,18 @@ fn make_img_request(

let animation = if !imgbuf.is_animated() {
None
} else if img.resize == ResizeStrategy::Crop {
match cache::load_animation_frames(path.as_ref(), dim, pixel_format) {
} else {
match cache::load_animation_frames(
path.as_ref(),
dim,
resize,
pixel_format,
) {
Ok(Some(animation)) => Some(animation),
otherwise => {
if let Err(e) = otherwise {
eprintln!("Error loading cache for {:?}: {e}", img_path);
}

Some({
ipc::Animation {
animation: compress_frames(
Expand All @@ -209,11 +217,8 @@ fn make_img_request(
})
}
}
} else {
None
};

let filter = img.filter.to_string();
let img = match img.resize {
ResizeStrategy::No => img_pad(&img_raw, dim, &img.fill_color)?,
ResizeStrategy::Crop => {
Expand All @@ -237,7 +242,8 @@ fn make_img_request(
dim,
format: pixel_format,
},
namespace.to_string(),
namespace,
resize,
filter,
outputs,
animation,
Expand All @@ -258,7 +264,7 @@ fn make_img_request(
}
}
};
let filter = img.filter.to_string();
let filter = img.filter.as_str();
let img_raw = imgbuf.decode(pixel_format, dim.0, dim.1)?;
let img = match img.resize {
ResizeStrategy::No => img_pad(&img_raw, dim, &img.fill_color)?,
Expand All @@ -282,7 +288,8 @@ fn make_img_request(
dim,
format: pixel_format,
},
namespace.to_string(),
namespace,
resize,
filter,
outputs,
None,
Expand Down Expand Up @@ -365,23 +372,25 @@ fn restore_from_cache(requested_outputs: &[String], namespace: &str) -> Result<(
}

fn restore_output(output: &str, namespace: &str) -> Result<(), String> {
let (filter, img_path) = common::cache::get_previous_image_filter_and_path(output, namespace)
.map_err(|e| format!("failed to get previous image path: {e}"))?;
if img_path.is_empty() {
return Err("cache file does not exist".to_string());
}
let cache_data = common::cache::read_cache_file(output)
.map_err(|e| format!("failed to read cache file: {e}"))?;
let cache = match common::cache::get_previous_image_cache(output, namespace, &cache_data) {
Ok(Some(cache)) => cache,
Ok(None) => return Err("cache entry does not exist".to_string()),
Err(e) => return Err(e.to_string()),
};

process_swww_args(
&Swww::Img(cli::Img {
all: false,
image: cli::parse_image(&img_path)?,
image: cli::parse_image(cache.img_path)?,
outputs: output.to_string(),
namespace: vec![namespace.to_string()],
#[allow(deprecated)]
no_resize: false,
resize: ResizeStrategy::Crop,
resize: ResizeStrategy::from_str(cache.resize).unwrap_or(ResizeStrategy::Crop),
fill_color: [0, 0, 0, 255],
filter: Filter::from_str(&filter).unwrap_or(Filter::Lanczos3),
filter: Filter::from_str(cache.filter).unwrap_or(Filter::Lanczos3),
transition_type: cli::TransitionType::None,
transition_step: std::num::NonZeroU8::MAX,
transition_duration: 0.0,
Expand Down
Loading
Loading