Skip to content

Commit

Permalink
feat:2
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Jan 29, 2024
1 parent 7394c9f commit bdd49be
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 90 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod error;
mod monitor;
mod utils;
mod window;

#[cfg(target_os = "macos")]
Expand Down
2 changes: 1 addition & 1 deletion src/linux/xorg_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn xorg_capture(
_ => return Err(XCapError::new(format!("Unsupported {} depth", depth))),
};

let mut rgba = vec![0_u8; (width * height * 4) as usize];
let mut rgba = vec![0u8; (width * height * 4) as usize];
for y in 0..height {
for x in 0..width {
let index = ((y * width + x) * 4) as usize;
Expand Down
28 changes: 17 additions & 11 deletions src/macos/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use core_graphics::{
};
use image::RgbaImage;

use crate::{
error::{XCapError, XCapResult},
utils::image::{bgra_to_rgba_image, remove_extra_data},
};
use crate::error::{XCapError, XCapResult};

pub fn capture(
cg_rect: CGRect,
Expand All @@ -20,12 +17,21 @@ pub fn capture(

let width = cg_image.width();
let height = cg_image.height();
let clean_buf = remove_extra_data(
width,
height,
cg_image.bytes_per_row(),
Vec::from(cg_image.data().bytes()),
);
let bytes = Vec::from(cg_image.data().bytes());

// Some platforms e.g. MacOS can have extra bytes at the end of each row.
// See
// https://github.com/nashaofu/xcap/issues/29
// https://github.com/nashaofu/xcap/issues/38
let mut buffer = Vec::with_capacity(width * height * 4);
for row in bytes.chunks_exact(cg_image.bytes_per_row()) {
buffer.extend_from_slice(&row[..width * 4]);
}

for bgra in buffer.chunks_exact_mut(4) {
bgra.swap(0, 2);
}

bgra_to_rgba_image(width as u32, height as u32, clean_buf)
RgbaImage::from_raw(width as u32, height as u32, buffer)
.ok_or_else(|| XCapError::new("RgbaImage::from_raw failed"))
}
74 changes: 0 additions & 74 deletions src/utils/image.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/windows/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn to_rgba_image(
bmiColors: [RGBQUAD::default(); 1],
};

let mut buffer = vec![0_u8; (width * height) as usize * 4];
let mut buffer = vec![0u8; (width * height) as usize * 4];

unsafe {
// 读取数据到 buffer 中
Expand Down
2 changes: 1 addition & 1 deletion src/windows/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn get_app_name(hwnd: HWND) -> XCapResult<String> {

let file_version_info_size_w = GetFileVersionInfoSizeW(pcw_filename, None);

let mut file_version_info = vec![0_u16; file_version_info_size_w as usize];
let mut file_version_info = vec![0u16; file_version_info_size_w as usize];

GetFileVersionInfoW(
pcw_filename,
Expand Down

0 comments on commit bdd49be

Please sign in to comment.