Skip to content

Commit

Permalink
depreciate ratio method, use upscale/crop instead
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk1f0 committed Feb 19, 2023
1 parent bb505bc commit d24328a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 23 deletions.
62 changes: 61 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
colored = "2.0.0"
image = "0.24.5"
libvips = "1.5.0"
num = "0.4.0"
77 changes: 59 additions & 18 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use image::{GenericImageView, DynamicImage};
use image::{GenericImageView, DynamicImage, imageops::FilterType};
use num::rational::Ratio;
use std::cmp;
use colored::Colorize;

// simplify monitor ratio
fn simplify_ratio(pixel1: u32, pixel2: u32) -> (u32, u32) {
Expand All @@ -20,43 +22,82 @@ pub fn parse_resolution(resolution: String) -> Result<(u32, u32), &'static str>
}
}

// split main image into two seperate
// split main image into two seperate, utilizes scaling
pub fn split_image(input: &str, primary: &(u32, u32), secondary: &(u32, u32), offset: u32) -> Result<Vec<DynamicImage>, &'static str> {
// new vector for result imgs
let mut result_papers: Vec<DynamicImage> = Vec::new();

// open original input image
let mut img = image::open(input).map_err(|_| "")?;

// get base image dimensions
// get base image details
let (main_width, main_height) = img.dimensions();
let primary_aspect = simplify_ratio(primary.0, primary.1);
let secondary_aspect = simplify_ratio(secondary.0, secondary.1);

// get monitor aspect ratios
let asp_primary = simplify_ratio(primary.0, primary.1);
let asp_secondary = simplify_ratio(secondary.0, secondary.1);
// info proc output
println!(
"Primary: ({}:{}) Secondary ({}:{})",
primary_aspect.0,
primary_aspect.1,
secondary_aspect.0,
secondary_aspect.1,
);

// calculate overall size
/*
Assuming a configuration where monitors are side on side:
+--------------+ +-----------------+
| | | |
| | | primary |
| | | monitor |
| secondary | | |
| monitor | +-----------------+
| |
| |
| |
+--------------+
// ratio relations
let ratio_part_width = main_width / ( asp_primary.0 + asp_secondary.0 );
let ratio_part_height = main_height / asp_secondary.1;
So either primary or secondary defines max height
Width will always be the sum
*/
let overall_width = primary.0 + secondary.0;
let overall_height = cmp::max(primary.0, secondary.0);

// correctly size offset
let ratio_offset: f64 = f64::from(ratio_part_height) as f64 / f64::from(offset);
// check if we need to upscale
if overall_width > main_width || overall_height > main_height {
// we need to scale
println!(
"{}: Scaling image to fit {}x{}",
"WARNING".red().bold(),
overall_width,
overall_height
);

// upscale image to fit
img = img.resize_to_fill(
overall_width,
overall_height,
FilterType::Lanczos3
);
}

// Crop image for horizontal screen
let primary_img = img.crop(
ratio_part_width * asp_secondary.0,
(f64::from(ratio_part_height) * ratio_offset).ceil() as u32,
ratio_part_width * asp_primary.0,
ratio_part_height * asp_primary.1
secondary.0,
offset,
primary.0,
primary.1
);

// Crop image for vertical screen
let secondary_img = img.crop(
0,
0,
ratio_part_width * asp_secondary.0,
ratio_part_height * asp_secondary.1
);
secondary.0,
secondary.1
);

// Push images to result vector
result_papers.push(primary_img);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
&config.m_secondary,
config.offset
).map_err(
|_| "Split Error"
|_| "error while splitting"
)?;

// export images
Expand All @@ -69,7 +69,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
image.save(
image_name
).map_err(
|_| "Split Error"
|_| "error while saving"
)?;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::env;
use std::process;
use rwpspread::Config;
use colored::Colorize;

fn main() {
let args: Vec<String> = env::args().collect();

let config = Config::new(&args)
.unwrap_or_else(|err| {
eprintln!("rwpspread: {}", err);
eprintln!("{}: {}", "rwpspread".red().bold(), err);
process::exit(1);
});

if let Err(err) = rwpspread::run(config) {
eprintln!("rwpspread: {}", err);
eprintln!("{}: {}", "rwpspread".red().bold(), err);
process::exit(1);
}
}

0 comments on commit d24328a

Please sign in to comment.