Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] set png compression/filter type through arguments #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use nix::{
use image::{
codecs::{
jpeg::JpegEncoder,
png::PngEncoder,
png::{CompressionType, FilterType, PngEncoder},
pnm::{self, PnmEncoder},
},
ColorType,
Expand Down Expand Up @@ -87,7 +87,7 @@ pub enum EncodingFormat {
/// Jpeg / jpg encoder.
Jpg,
/// Png encoder.
Png,
Png(CompressionType, FilterType),
/// Ppm encoder
Ppm,
}
Expand Down Expand Up @@ -372,8 +372,8 @@ pub fn write_to_file(
)?;
output_file.flush()?;
}
EncodingFormat::Png => {
PngEncoder::new(&mut output_file).write_image(
EncodingFormat::Png(c, f) => {
PngEncoder::new_with_quality(&mut output_file, c, f).write_image(
&frame_copy.frame_mmap,
frame_copy.frame_format.width,
frame_copy.frame_format.height,
Expand Down
15 changes: 15 additions & 0 deletions src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn set_flags() -> Command<'static> {
arg!(-e --extension <FILE_EXTENSION>)
.required(false)
.takes_value(true)
.default_value("png")
.help("Set image encoder (Png is default)"),
)
.arg(
Expand All @@ -55,6 +56,20 @@ pub fn set_flags() -> Command<'static> {
.takes_value(true)
.conflicts_with("slurp")
.help("Choose a particular display to screenshot"),
)
.arg(
arg!(-C --png_compression_type <TYPE>)
.required(false)
.takes_value(true)
.default_value("fast")
.help("Set png compression type"),
)
.arg(
arg!(-F --png_filter_type <TYPE>)
.required(false)
.takes_value(true)
.default_value("adaptive")
.help("Set png compression type"),
);
app
}
60 changes: 44 additions & 16 deletions src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use image::codecs::png::{CompressionType, FilterType};
use wayland_client::{protocol::wl_output::WlOutput, Display};

mod backend;
Expand Down Expand Up @@ -110,24 +111,51 @@ fn main() -> Result<(), Box<dyn Error>> {
backend::capture_output_frame(display, cursor_overlay, output, None)?
};

let extension = if args.is_present("extension") {
let ext: &str = &args.value_of("extension").unwrap().trim().to_lowercase();
match ext {
"jpeg" | "jpg" => backend::EncodingFormat::Jpg,
"png" => backend::EncodingFormat::Png,
"ppm" => backend::EncodingFormat::Ppm,
_ => {
log::error!("Invalid extension provided.\nValid extensions:\n1) jpeg\n2) jpg\n3) png\n4) ppm");
exit(1);
}
let ext = args.value_of("extension").unwrap().trim();
let extension: backend::EncodingFormat = match ext {
"jpeg" | "jpg" => backend::EncodingFormat::Jpg,
"png" => {
let compression_type: CompressionType = match args
.value_of("png_compression_type")
.unwrap()
.trim()
.to_lowercase()
.as_ref()
{
"default" => CompressionType::Default,
"best" => CompressionType::Best,
"huffman" => CompressionType::Huffman,
"rle" => CompressionType::Rle,
_ => CompressionType::Fast,
};
let filter_type: FilterType = match args
.value_of("png_filter_type")
.unwrap()
.trim()
.to_lowercase()
.as_ref()
{
"nofilter" => FilterType::NoFilter,
"sub" => FilterType::Sub,
"up" => FilterType::Up,
"avg" => FilterType::Avg,
"paeth" => FilterType::Paeth,
_ => FilterType::Adaptive,
};
backend::EncodingFormat::Png(compression_type, filter_type)
}
"ppm" => backend::EncodingFormat::Ppm,
_ => {
log::error!(
"Invalid extension provided.\nValid extensions:\n1) jpeg\n2) jpg\n3) png\n4) ppm"
);
exit(1);
}
} else {
backend::EncodingFormat::Png
};

if extension != backend::EncodingFormat::Png {
log::debug!("Using custom extension: {:#?}", extension);
}
// if extension != backend::EncodingFormat::Png {
// log::debug!("Using custom extension: {:#?}", extension);
// }

if args.is_present("stdout") {
let stdout = stdout();
Expand All @@ -146,7 +174,7 @@ fn main() -> Result<(), Box<dyn Error>> {
};

time + match extension {
backend::EncodingFormat::Png => "-wayshot.png",
backend::EncodingFormat::Png(_, _) => "-wayshot.png",
backend::EncodingFormat::Jpg => "-wayshot.jpg",
backend::EncodingFormat::Ppm => "-wayshot.ppm",
}
Expand Down