Skip to content

Commit

Permalink
fix threads arc and open close button
Browse files Browse the repository at this point in the history
  • Loading branch information
autergame committed Jan 16, 2023
1 parent 407e165 commit db9e955
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 186 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "jpegview-rust"
version = "0.3.0"
version = "0.3.1"
authors = ["autergame"]

[dependencies]
native-dialog = "0.6.3"
miniz_oxide = "0.6.2"
threadpool = "1.8.1"
bincode = "1.3.3"
serde = { version = "1.0.151", features = ["derive"] }
serde = { version = "1.0.152", features = ["derive"] }
image = "0.24.5"
sha2 = "0.10.6"

gl = "0.14.0"
glfw = { version = "0.47.0", default-features = false, features = ["glfw-sys"] }
glfw = "0.49.1"

imgui = { version = "0.9.0", features = ["docking"] }

Expand Down
34 changes: 16 additions & 18 deletions src/imgui_glfw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ impl imgui::ClipboardBackend for GlfwClipboardBackend {
let char_ptr = unsafe { glfw::ffi::glfwGetClipboardString(self.window) };
if !char_ptr.is_null() {
let c_str = unsafe { std::ffi::CStr::from_ptr(char_ptr) };
Some(
c_str
.to_str()
.expect("Could not get clipboard string")
.to_owned(),
)
Some(String::from(
c_str.to_str().expect("Could not get clipboard string"),
))
} else {
None
}
Expand Down Expand Up @@ -112,18 +109,18 @@ impl ImguiGLFW {
window: &glfw::Window,
imgui: &mut imgui::Context,
) {
let io = imgui.io_mut();
let mut io_mut = imgui.io_mut();

io.delta_time = delta_time;
io_mut.delta_time = delta_time;

let window_size = window.get_size();
io.display_size = [window_size.0 as f32, window_size.1 as f32];
io_mut.display_size = [window_size.0 as f32, window_size.1 as f32];

if window_size.0 > 0 && window_size.1 > 0 {
let framebuffer_size = window.get_framebuffer_size();
io.display_framebuffer_scale = [
framebuffer_size.0 as f32 / io.display_size[0],
framebuffer_size.1 as f32 / io.display_size[1],
io_mut.display_framebuffer_scale = [
framebuffer_size.0 as f32 / io_mut.display_size[0],
framebuffer_size.1 as f32 / io_mut.display_size[1],
];
}
}
Expand Down Expand Up @@ -156,10 +153,11 @@ impl ImguiGLFW {
}

fn set_mod(imgui: &mut imgui::Context, modifier: glfw::Modifiers) {
imgui.io_mut().key_ctrl = modifier.intersects(glfw::Modifiers::Control);
imgui.io_mut().key_alt = modifier.intersects(glfw::Modifiers::Alt);
imgui.io_mut().key_shift = modifier.intersects(glfw::Modifiers::Shift);
imgui.io_mut().key_super = modifier.intersects(glfw::Modifiers::Super);
let mut io_mut = imgui.io_mut();
io_mut.key_ctrl = modifier.intersects(glfw::Modifiers::Control);
io_mut.key_alt = modifier.intersects(glfw::Modifiers::Alt);
io_mut.key_shift = modifier.intersects(glfw::Modifiers::Shift);
io_mut.key_super = modifier.intersects(glfw::Modifiers::Super);
}
}

Expand Down Expand Up @@ -357,8 +355,8 @@ impl Renderer {

gl::Viewport(0, 0, fb_width as _, fb_height as _);
let matrix = [
[2.0 / width as f32, 0.0, 0.0, 0.0],
[0.0, 2.0 / -(height as f32), 0.0, 0.0],
[2.0 / width, 0.0, 0.0, 0.0],
[0.0, 2.0 / -(height), 0.0, 0.0],
[0.0, 0.0, -1.0, 0.0],
[-1.0, 1.0, 0.0, 1.0],
];
Expand Down
58 changes: 28 additions & 30 deletions src/jpeg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(clippy::needless_range_loop)]

use crate::{my_image, Vec2d, Vec3d};
use crate::{
my_image::{self, MyImage},
unwrap_arc_mutex, Vec2d, Vec3d,
};
use std::{
f32,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -94,7 +97,7 @@ impl Jpeg {
}
pub fn render(
&mut self,
my_image: &mut my_image::MyImage,
my_image: &mut MyImage,
use_ycbcr: bool,
use_threads: bool,
subsampling_index: usize,
Expand All @@ -121,12 +124,12 @@ impl Jpeg {
if !self.use_compression_rate {
let factor = if self.use_gen_qtable {
if self.quality >= 50.0f32 {
200.0f32 - (self.quality as f32 * 2.0f32)
200.0f32 - (self.quality * 2.0f32)
} else {
5000.0f32 / self.quality as f32
5000.0f32 / self.quality
}
} else {
25.0f32 * ((101.0f32 - self.quality as f32) * 0.01f32)
25.0f32 * ((101.0f32 - self.quality) * 0.01f32)
};

apply_q_matrix_factor(&mut q_matrix_luma, self.block_size, factor);
Expand All @@ -143,7 +146,7 @@ impl Jpeg {
}
pub fn encode(
&mut self,
my_image: &mut my_image::MyImage,
my_image: &mut MyImage,
q_matrix_luma: Vec<f32>,
q_matrix_chroma: Vec<f32>,
) {
Expand All @@ -161,6 +164,7 @@ impl Jpeg {
let jpeg_steps = Arc::new(jpeg_steps);

let mut image_block = Vec::with_capacity(block_width_count * block_height_count);
let mut result_block = Vec::with_capacity(block_width_count * block_height_count);

for by in 0..block_height_count {
for bx in 0..block_width_count {
Expand All @@ -182,38 +186,32 @@ impl Jpeg {
}
}

image_block.push(solo_image_block);
}
}

let mut result_block = Vec::with_capacity(block_width_count * block_height_count);
image_block.push(Arc::new(solo_image_block));

for _ in 0..(block_width_count * block_height_count) {
result_block.push(Arc::new(Mutex::new(vec![
vec![
0.0f32;
self.block_size
* self.block_size
];
3
])));
result_block.push(Arc::new(Mutex::new(vec![
vec![
0.0f32;
self.block_size
* self.block_size
];
3
])));
}
}

let image_block = Arc::new(image_block);

let q_matrix_luma = Arc::new(q_matrix_luma);
let q_matrix_chroma = Arc::new(q_matrix_chroma);

let cpu_threads = thread::available_parallelism().unwrap().get();
let pool = threadpool::ThreadPool::with_name("worker".to_string(), cpu_threads);
let pool = threadpool::ThreadPool::with_name(String::from("jpegview-worker"), cpu_threads);

for by in 0..block_height_count {
for bx in 0..block_width_count {
let start_x = bx * self.block_size;
let index = by * block_width_count + bx;

let arc_jpeg_steps = Arc::clone(&jpeg_steps);
let arc_image_block = Arc::clone(&image_block);
let arc_image_block = Arc::clone(&image_block[index]);
let arc_result_block = Arc::clone(&result_block[index]);
let arc_q_matrix_luma = Arc::clone(&q_matrix_luma);
let arc_q_matrix_chroma = Arc::clone(&q_matrix_chroma);
Expand All @@ -222,17 +220,17 @@ impl Jpeg {
let arc_locked_result_block = &mut arc_result_block.lock().unwrap();
arc_locked_result_block[0] = arc_jpeg_steps.jpeg_steps(
start_x,
&arc_image_block[index][0],
&arc_image_block[0],
&arc_q_matrix_luma,
);
arc_locked_result_block[1] = arc_jpeg_steps.jpeg_steps(
start_x,
&arc_image_block[index][1],
&arc_image_block[1],
&arc_q_matrix_chroma,
);
arc_locked_result_block[2] = arc_jpeg_steps.jpeg_steps(
start_x,
&arc_image_block[index][2],
&arc_image_block[2],
&arc_q_matrix_chroma,
);
});
Expand All @@ -241,8 +239,8 @@ impl Jpeg {
pool.join();

result_block
.iter()
.map(|i| i.lock().unwrap().to_vec())
.into_iter()
.map(unwrap_arc_mutex::<_>)
.collect()
} else {
let mut image_block: Vec3d<f32> =
Expand Down Expand Up @@ -353,7 +351,7 @@ impl JpegSteps {
use_gen_qtable: jpeg.use_gen_qtable,
use_compression_rate: jpeg.use_compression_rate,

q_control: 100.0f32 - jpeg.quality_start as f32,
q_control: 100.0f32 - jpeg.quality_start,
two_block_size: 2.0f32 / jpeg.block_size as f32,
}
}
Expand Down
Loading

0 comments on commit db9e955

Please sign in to comment.