Skip to content

Commit 1a19c7e

Browse files
committed
修复屏幕尺寸问题
1 parent bc2fc81 commit 1a19c7e

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

dsclient/src/client.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use std::hash::Hasher;
1010
use std::io::Read;
1111
use std::io::Write;
1212
use std::net::TcpStream;
13-
use std::sync::atomic::AtomicI32;
14-
use std::sync::atomic::Ordering;
1513
use std::sync::Arc;
1614
use std::sync::RwLock;
1715

@@ -111,19 +109,13 @@ fn draw(host: String, pwd: String) {
111109
let iw = (((meta[0] as u16) << 8) | meta[1] as u16) as i32;
112110
let ih = (((meta[2] as u16) << 8) | meta[3] as u16) as i32;
113111

114-
let wh = Arc::new((AtomicI32::new(iw), AtomicI32::new(ih)));
115-
116-
117-
// let dlen = (w * h * 3) as usize;
118-
119-
let work_buf = Arc::new(RwLock::new((0usize, 0usize, Vec::<u8>::new())));
112+
let work_buf = Arc::new(RwLock::new(vec![0u8; (iw * ih * 3) as _]));
120113
let draw_work_buf = work_buf.clone();
121114
let mut hooked = false;
122115
let mut bmap = bitmap::Bitmap::new();
123116
let mut cmd_buf = [0u8; 5];
124-
let wh1 = wh.clone();
125117
frame.handle(move |f, ev| {
126-
let (w, h) = (wh1.0.load(Ordering::Relaxed), wh1.1.load(Ordering::Relaxed));
118+
let (w, h) = (iw, ih);
127119
match ev {
128120
Event::Enter => {
129121
// 进入窗口
@@ -223,7 +215,7 @@ fn draw(host: String, pwd: String) {
223215
if let Ok(p) = draw_work_buf.read() {
224216
unsafe {
225217
if let Ok(mut image) =
226-
image::RgbImage::from_data2(&p.2, p.0 as _, p.1 as _, enums::ColorDepth::Rgb8 as i32, 0)
218+
image::RgbImage::from_data2(&p, iw as _, ih as _, enums::ColorDepth::Rgb8 as i32, 0)
227219
{
228220
image.scale(frame.width(), frame.height(), false, true);
229221
image.draw(frame.x(), frame.y(), frame.width(), frame.height());
@@ -265,12 +257,7 @@ fn draw(host: String, pwd: String) {
265257
for ele in pkgs {
266258
let (y, u, v) = ele.data();
267259
if let Ok(mut p) = work_buf.write() {
268-
p.0 = ele.width();
269-
p.1 = ele.height();
270-
wh.0.store(ele.width() as _, Ordering::Relaxed);
271-
wh.1.store(ele.height() as _, Ordering::Relaxed);
272-
p.2.resize(ele.width() * ele.height() * 3, 0u8);
273-
dscom::convert::i420_to_rgb(ele.width(), ele.height(), y, u, v, &mut p.2);
260+
dscom::convert::i420_to_rgb(ele.width(), ele.height(), y, u, v, &mut p, iw as _, ih as _);
274261
}
275262
tx.send(Msg::Draw);
276263
}

dscom/src/convert.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,28 @@ fn clamp(x: i32) -> u8 {
4848
}
4949

5050
#[allow(dead_code)]
51-
pub fn i420_to_rgb(width: usize, height: usize, sy: &[u8], su: &[u8], sv: &[u8], dest: &mut [u8]) {
51+
pub fn i420_to_rgb(width: usize, height: usize, sy: &[u8], su: &[u8], sv: &[u8], dest: &mut [u8], crop_width: usize, crop_height: usize) {
52+
// 确保裁剪尺寸不超过原始尺寸
53+
let crop_width = crop_width.min(width);
54+
let crop_height = crop_height.min(height);
5255
let uvw = width >> 1;
53-
for i in 0..height {
56+
for i in 0..crop_height {
5457
let sw = i * width;
58+
let swc = i * crop_width;
5559
let t = (i >> 1) * uvw;
56-
for j in 0..width {
57-
let mut rgbstart = sw + j;
60+
for j in 0..crop_width {
61+
let rgbstart = sw + j;
62+
let mut rgbstartc = swc + j;
5863
let uvi = t + (j >> 1);
5964

6065
let y = sy[rgbstart] as i32;
6166
let u = su[uvi] as i32 - 128;
6267
let v = sv[uvi] as i32 - 128;
6368

64-
rgbstart *= 3;
65-
dest[rgbstart] = clamp(y + (v * 359 >> 8));
66-
dest[rgbstart + 1] = clamp(y - (u * 88 >> 8) - (v * 182 >> 8));
67-
dest[rgbstart + 2] = clamp(y + (u * 453 >> 8));
69+
rgbstartc *= 3;
70+
dest[rgbstartc] = clamp(y + (v * 359 >> 8));
71+
dest[rgbstartc + 1] = clamp(y - (u * 88 >> 8) - (v * 182 >> 8));
72+
dest[rgbstartc + 2] = clamp(y + (u * 453 >> 8));
6873

6974
}
7075
}

dsserver/src/server.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use enigo::agent::Agent;
21
use enigo::Axis;
32
use enigo::Coordinate;
43
use enigo::Direction;

0 commit comments

Comments
 (0)