Skip to content

Commit

Permalink
wip: do mini preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Dec 11, 2024
1 parent 8c0ae8a commit 4fb43b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl<'a> Roseate<'a> {
}

fn set_app_style(&self, ctx: &Context) {
// #1d0a0a # dark mode secondary colour for roseate
let mut custom_style = Style {
override_text_style: Some(TextStyle::Monospace),
..Default::default()
Expand Down
23 changes: 12 additions & 11 deletions src/image/fast_downsample.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rayon::prelude::*;
use imagesize::ImageSize;
use std::{f32::consts::PI, sync::{Arc, Mutex}};

use super::image::ImageSizeT;

// math :akko_shrug:
// SINNNNNNN, SIN CITY WASN'T MADE FOR YOU!!! ANGLES LIKEEEEE YOUUUU!
fn sinc(x: f32) -> f32 {
Expand Down Expand Up @@ -34,21 +35,21 @@ fn precomputed_lanczos(window_size: f32, scale_factor: f32) -> Vec<f32> {

pub fn fast_downsample(
pixels: Vec<u8>,
image_size: &ImageSize,
image_size: &ImageSizeT,
target_size: (u32, u32),
has_alpha: bool
) -> (Vec<u8>, (u32, u32)) {
) -> (Vec<u8>, ImageSizeT) {
let window_size: f32 = 3.0; // the window size that determines the level
// of influence the kernel has on each original pixel. Larger values result in more smoothing
// but may also result in slower computation time so beware.

let (target_width, target_height) = target_size;

let scale_factor = (image_size.width as f32 / target_width as f32)
.max(image_size.height as f32 / target_height as f32);
let scale_factor = (image_size.0 as f32 / target_width as f32)
.max(image_size.1 as f32 / target_height as f32);

let new_width = (image_size.width as f32 / scale_factor) as u32;
let new_height = (image_size.height as f32 / scale_factor) as u32;
let new_width = (image_size.0 as f32 / scale_factor) as u32;
let new_height = (image_size.1 as f32 / scale_factor) as u32;

let kernel_lookup = precomputed_lanczos(window_size, scale_factor);

Expand Down Expand Up @@ -81,9 +82,9 @@ pub fn fast_downsample(
for vertical_offset in -lanczos_window..=lanczos_window {
for horizontal_offset in -lanczos_window..=lanczos_window {
let relative_vertical_pos = (original_vertical_pos as isize + vertical_offset)
.clamp(0, (image_size.height - 1) as isize);
.clamp(0, (image_size.1 - 1) as isize);
let relative_horizontal_pos = (original_horizontal_pos as isize + horizontal_offset)
.clamp(0, (image_size.width - 1) as isize);
.clamp(0, (image_size.0 - 1) as isize);

// Each neighbouring pixel's influence is calculated based on it's
// distance from the relative and original pixel position using the Lanczos kernel.
Expand All @@ -96,7 +97,7 @@ pub fn fast_downsample(
// Weights determine how much each original pixel contributes to the new resized pixel RGB colour.
let weight = kernel_lookup[relative_horizontal_distance] * kernel_lookup[relative_vertical_distance];
let index = (
relative_vertical_pos as usize * image_size.width + relative_horizontal_pos as usize
relative_vertical_pos as usize * image_size.0 as usize + relative_horizontal_pos as usize
) * index_times;

rgb_sum[0] += pixels[index] as f32 * weight; // red owo
Expand Down Expand Up @@ -130,7 +131,7 @@ pub fn fast_downsample(
.into_inner()
.unwrap(),
(
new_width,
new_width,
new_height
)
)
Expand Down
29 changes: 25 additions & 4 deletions src/image/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{error::Error, notifier::NotifierAPI};

use super::{backends::ImageProcessingBackend, image_formats::ImageFormat, optimization::ImageOptimization};

pub type ImageSizeT = (u32, u32);

#[derive(Clone)]
pub struct Image {
pub image_size: ImageSize,
Expand Down Expand Up @@ -108,7 +110,25 @@ impl Image {
)
}

pub fn load_image(&mut self, optimizations: &[ImageOptimization], notifier: &mut NotifierAPI, image_processing_backend: &ImageProcessingBackend) -> Result<(), Error> {
pub fn reload_image(
&mut self,
optimizations: &[ImageOptimization],
notifier: &mut NotifierAPI,
image_processing_backend: &ImageProcessingBackend
) -> Result<(), Error> {
if optimizations.is_empty() {
return Ok(());
}

Ok(())
}

pub fn load_image(
&mut self,
optimizations: &[ImageOptimization],
notifier: &mut NotifierAPI,
image_processing_backend: &ImageProcessingBackend
) -> Result<(), Error> {
if optimizations.is_empty() {
debug!("No optimizations were set so loading with fs::read instead...");

Expand All @@ -124,8 +144,7 @@ impl Image {
}

let (mut actual_width, mut actual_height) = (
self.image_size.width as u32,
self.image_size.height as u32
self.image_size.width as u32, self.image_size.height as u32
);

notifier.set_loading(Some("Opening file...".into()));
Expand Down Expand Up @@ -186,7 +205,9 @@ impl Image {
_ => false,
};

(pixels, (actual_width, actual_height)) = optimization.apply_custom(pixels, &self.image_size, has_alpha);
(pixels, (actual_width, actual_height)) = optimization.apply_roseate(
pixels, &(self.image_size.width as u32, self.image_size.height as u32), has_alpha
);
}

notifier.set_loading(
Expand Down
4 changes: 2 additions & 2 deletions src/image/optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::debug;
use imagesize::ImageSize;
use display_info::DisplayInfo;

use super::fast_downsample::fast_downsample;
use super::{fast_downsample::fast_downsample, image::ImageSizeT};

#[derive(Debug)]
pub enum ImageOptimization {
Expand Down Expand Up @@ -38,7 +38,7 @@ impl ImageOptimization {
}
}

pub fn apply_custom(&self, pixels: Vec<u8>, image_size: &ImageSize, has_alpha: bool) -> (Vec<u8>, (u32, u32)) {
pub fn apply_roseate(&self, pixels: Vec<u8>, image_size: &ImageSizeT, has_alpha: bool) -> (Vec<u8>, ImageSizeT) {
match self {
ImageOptimization::Downsample(width, height) => {
fast_downsample(pixels, image_size, (*width, *height), has_alpha)
Expand Down
7 changes: 2 additions & 5 deletions src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ impl ImageLoader {
}
}

pub fn update(&mut self) {
pub fn update(&mut self, ) {
// I use an update function to keep the public fields update to date with their Arc<Mutex<T>> twins.
//
// I also use this to append the queued toast messages
// from threads as we cannot take ownership of "&mut Toasts" sadly.

if let Ok(value) = self.image_loaded_arc.try_lock() {
self.image_loaded = value.clone(); // TODO: find a way to reference instead of clone to save memory here.
self.image_loaded = value.clone(); // cloning here shouldn't too expensive
self.image_loading = false;
}
}
Expand Down

0 comments on commit 4fb43b4

Please sign in to comment.