Skip to content

Commit db9e955

Browse files
committed
fix threads arc and open close button
1 parent 407e165 commit db9e955

6 files changed

Lines changed: 182 additions & 186 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
[package]
22
name = "jpegview-rust"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["autergame"]
55

66
[dependencies]
77
native-dialog = "0.6.3"
88
miniz_oxide = "0.6.2"
99
threadpool = "1.8.1"
1010
bincode = "1.3.3"
11-
serde = { version = "1.0.151", features = ["derive"] }
11+
serde = { version = "1.0.152", features = ["derive"] }
1212
image = "0.24.5"
1313
sha2 = "0.10.6"
1414

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

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

src/imgui_glfw.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ impl imgui::ClipboardBackend for GlfwClipboardBackend {
1919
let char_ptr = unsafe { glfw::ffi::glfwGetClipboardString(self.window) };
2020
if !char_ptr.is_null() {
2121
let c_str = unsafe { std::ffi::CStr::from_ptr(char_ptr) };
22-
Some(
23-
c_str
24-
.to_str()
25-
.expect("Could not get clipboard string")
26-
.to_owned(),
27-
)
22+
Some(String::from(
23+
c_str.to_str().expect("Could not get clipboard string"),
24+
))
2825
} else {
2926
None
3027
}
@@ -112,18 +109,18 @@ impl ImguiGLFW {
112109
window: &glfw::Window,
113110
imgui: &mut imgui::Context,
114111
) {
115-
let io = imgui.io_mut();
112+
let mut io_mut = imgui.io_mut();
116113

117-
io.delta_time = delta_time;
114+
io_mut.delta_time = delta_time;
118115

119116
let window_size = window.get_size();
120-
io.display_size = [window_size.0 as f32, window_size.1 as f32];
117+
io_mut.display_size = [window_size.0 as f32, window_size.1 as f32];
121118

122119
if window_size.0 > 0 && window_size.1 > 0 {
123120
let framebuffer_size = window.get_framebuffer_size();
124-
io.display_framebuffer_scale = [
125-
framebuffer_size.0 as f32 / io.display_size[0],
126-
framebuffer_size.1 as f32 / io.display_size[1],
121+
io_mut.display_framebuffer_scale = [
122+
framebuffer_size.0 as f32 / io_mut.display_size[0],
123+
framebuffer_size.1 as f32 / io_mut.display_size[1],
127124
];
128125
}
129126
}
@@ -156,10 +153,11 @@ impl ImguiGLFW {
156153
}
157154

158155
fn set_mod(imgui: &mut imgui::Context, modifier: glfw::Modifiers) {
159-
imgui.io_mut().key_ctrl = modifier.intersects(glfw::Modifiers::Control);
160-
imgui.io_mut().key_alt = modifier.intersects(glfw::Modifiers::Alt);
161-
imgui.io_mut().key_shift = modifier.intersects(glfw::Modifiers::Shift);
162-
imgui.io_mut().key_super = modifier.intersects(glfw::Modifiers::Super);
156+
let mut io_mut = imgui.io_mut();
157+
io_mut.key_ctrl = modifier.intersects(glfw::Modifiers::Control);
158+
io_mut.key_alt = modifier.intersects(glfw::Modifiers::Alt);
159+
io_mut.key_shift = modifier.intersects(glfw::Modifiers::Shift);
160+
io_mut.key_super = modifier.intersects(glfw::Modifiers::Super);
163161
}
164162
}
165163

@@ -357,8 +355,8 @@ impl Renderer {
357355

358356
gl::Viewport(0, 0, fb_width as _, fb_height as _);
359357
let matrix = [
360-
[2.0 / width as f32, 0.0, 0.0, 0.0],
361-
[0.0, 2.0 / -(height as f32), 0.0, 0.0],
358+
[2.0 / width, 0.0, 0.0, 0.0],
359+
[0.0, 2.0 / -(height), 0.0, 0.0],
362360
[0.0, 0.0, -1.0, 0.0],
363361
[-1.0, 1.0, 0.0, 1.0],
364362
];

src/jpeg.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![allow(clippy::needless_range_loop)]
22

3-
use crate::{my_image, Vec2d, Vec3d};
3+
use crate::{
4+
my_image::{self, MyImage},
5+
unwrap_arc_mutex, Vec2d, Vec3d,
6+
};
47
use std::{
58
f32,
69
sync::{Arc, Mutex},
@@ -94,7 +97,7 @@ impl Jpeg {
9497
}
9598
pub fn render(
9699
&mut self,
97-
my_image: &mut my_image::MyImage,
100+
my_image: &mut MyImage,
98101
use_ycbcr: bool,
99102
use_threads: bool,
100103
subsampling_index: usize,
@@ -121,12 +124,12 @@ impl Jpeg {
121124
if !self.use_compression_rate {
122125
let factor = if self.use_gen_qtable {
123126
if self.quality >= 50.0f32 {
124-
200.0f32 - (self.quality as f32 * 2.0f32)
127+
200.0f32 - (self.quality * 2.0f32)
125128
} else {
126-
5000.0f32 / self.quality as f32
129+
5000.0f32 / self.quality
127130
}
128131
} else {
129-
25.0f32 * ((101.0f32 - self.quality as f32) * 0.01f32)
132+
25.0f32 * ((101.0f32 - self.quality) * 0.01f32)
130133
};
131134

132135
apply_q_matrix_factor(&mut q_matrix_luma, self.block_size, factor);
@@ -143,7 +146,7 @@ impl Jpeg {
143146
}
144147
pub fn encode(
145148
&mut self,
146-
my_image: &mut my_image::MyImage,
149+
my_image: &mut MyImage,
147150
q_matrix_luma: Vec<f32>,
148151
q_matrix_chroma: Vec<f32>,
149152
) {
@@ -161,6 +164,7 @@ impl Jpeg {
161164
let jpeg_steps = Arc::new(jpeg_steps);
162165

163166
let mut image_block = Vec::with_capacity(block_width_count * block_height_count);
167+
let mut result_block = Vec::with_capacity(block_width_count * block_height_count);
164168

165169
for by in 0..block_height_count {
166170
for bx in 0..block_width_count {
@@ -182,38 +186,32 @@ impl Jpeg {
182186
}
183187
}
184188

185-
image_block.push(solo_image_block);
186-
}
187-
}
188-
189-
let mut result_block = Vec::with_capacity(block_width_count * block_height_count);
189+
image_block.push(Arc::new(solo_image_block));
190190

191-
for _ in 0..(block_width_count * block_height_count) {
192-
result_block.push(Arc::new(Mutex::new(vec![
193-
vec![
194-
0.0f32;
195-
self.block_size
196-
* self.block_size
197-
];
198-
3
199-
])));
191+
result_block.push(Arc::new(Mutex::new(vec![
192+
vec![
193+
0.0f32;
194+
self.block_size
195+
* self.block_size
196+
];
197+
3
198+
])));
199+
}
200200
}
201201

202-
let image_block = Arc::new(image_block);
203-
204202
let q_matrix_luma = Arc::new(q_matrix_luma);
205203
let q_matrix_chroma = Arc::new(q_matrix_chroma);
206204

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

210208
for by in 0..block_height_count {
211209
for bx in 0..block_width_count {
212210
let start_x = bx * self.block_size;
213211
let index = by * block_width_count + bx;
214212

215213
let arc_jpeg_steps = Arc::clone(&jpeg_steps);
216-
let arc_image_block = Arc::clone(&image_block);
214+
let arc_image_block = Arc::clone(&image_block[index]);
217215
let arc_result_block = Arc::clone(&result_block[index]);
218216
let arc_q_matrix_luma = Arc::clone(&q_matrix_luma);
219217
let arc_q_matrix_chroma = Arc::clone(&q_matrix_chroma);
@@ -222,17 +220,17 @@ impl Jpeg {
222220
let arc_locked_result_block = &mut arc_result_block.lock().unwrap();
223221
arc_locked_result_block[0] = arc_jpeg_steps.jpeg_steps(
224222
start_x,
225-
&arc_image_block[index][0],
223+
&arc_image_block[0],
226224
&arc_q_matrix_luma,
227225
);
228226
arc_locked_result_block[1] = arc_jpeg_steps.jpeg_steps(
229227
start_x,
230-
&arc_image_block[index][1],
228+
&arc_image_block[1],
231229
&arc_q_matrix_chroma,
232230
);
233231
arc_locked_result_block[2] = arc_jpeg_steps.jpeg_steps(
234232
start_x,
235-
&arc_image_block[index][2],
233+
&arc_image_block[2],
236234
&arc_q_matrix_chroma,
237235
);
238236
});
@@ -241,8 +239,8 @@ impl Jpeg {
241239
pool.join();
242240

243241
result_block
244-
.iter()
245-
.map(|i| i.lock().unwrap().to_vec())
242+
.into_iter()
243+
.map(unwrap_arc_mutex::<_>)
246244
.collect()
247245
} else {
248246
let mut image_block: Vec3d<f32> =
@@ -353,7 +351,7 @@ impl JpegSteps {
353351
use_gen_qtable: jpeg.use_gen_qtable,
354352
use_compression_rate: jpeg.use_compression_rate,
355353

356-
q_control: 100.0f32 - jpeg.quality_start as f32,
354+
q_control: 100.0f32 - jpeg.quality_start,
357355
two_block_size: 2.0f32 / jpeg.block_size as f32,
358356
}
359357
}

0 commit comments

Comments
 (0)