Skip to content

Commit

Permalink
perf: improved window region capture
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiErikNiermann committed Sep 11, 2023
1 parent a25734f commit 54b2aed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
build:
name: Build and Cross Compile
name: Run unit tests
runs-on: ubuntu-latest

steps:
Expand Down
6 changes: 6 additions & 0 deletions libs/lib_gui/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use gtk::ffi::gtk_widget_set_visible;
use gtk::glib::{Receiver, Sender};
use gtk::{glib, Application, ApplicationWindow, Button, Entry, Menu, MenuBar, MenuItem};
use gtk::{prelude::*, Label};
use lib_ocr::win_sc::*;
Expand All @@ -7,7 +9,11 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::hash::Hash;
use std::io::prelude::*;
use std::thread;
use tokio::runtime;
use tokio::runtime::Runtime;

pub struct WindowLayout {
pub width: i32,
Expand Down
2 changes: 0 additions & 2 deletions libs/lib_ocr/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rusty_tesseract::{Args, Image};
use std::env;

pub fn get_image(path: &str) -> Result<Image, String> {
// print cwd
if let Ok(current_dir) = env::current_dir() {
if let Some(dir_str) = current_dir.to_str() {
println!("Current Working Directory: {}", dir_str);
Expand All @@ -14,7 +13,6 @@ pub fn get_image(path: &str) -> Result<Image, String> {
println!("Unable to retrieve current directory.");
}

// concat cwd with path
let path = format!("{}/{}", env::current_dir().unwrap().to_str().unwrap(), path);

match ImageReader::open(path) {
Expand Down
32 changes: 23 additions & 9 deletions libs/lib_ocr/src/win_sc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub struct WindowRect {
pub bottom: i32,
}

#[derive(Debug)]
struct ResourceSize {
width: u32,
height: u32
}

// The target of the capture, chosen with the picker control.
fn create_capture_item(handle: Handle) -> Result<GraphicsCaptureItem> {
let interop = windows::core::factory::<GraphicsCaptureItem, IGraphicsCaptureItemInterop>()?;
Expand All @@ -45,7 +51,7 @@ fn create_capture_item(handle: Handle) -> Result<GraphicsCaptureItem> {
}
}

fn save_as_image(bits: Vec<u8>, item_size: SizeInt32) -> Result<()> {
fn save_as_image(bits: Vec<u8>, resource_size: ResourceSize) -> Result<()> {
// Create a file in the current directory
let path = std::env::current_dir()
.unwrap()
Expand All @@ -68,8 +74,8 @@ fn save_as_image(bits: Vec<u8>, item_size: SizeInt32) -> Result<()> {
encoder.SetPixelData(
BitmapPixelFormat::Bgra8,
BitmapAlphaMode::Premultiplied,
item_size.Width as u32,
item_size.Height as u32,
resource_size.width,
resource_size.height,
1.0,
1.0,
&bits,
Expand All @@ -94,6 +100,7 @@ fn take_sc(item: &GraphicsCaptureItem, rect: &RECT) -> Result<()> {
// The size of the target of the capture.
let item_size = item.Size()?;
println!("item_size: {:?}", item_size);
println!("Compiled new");

// Create a D3D11 device
let d3d_device = devices::create_d3d_device()?;
Expand Down Expand Up @@ -168,6 +175,13 @@ fn take_sc(item: &GraphicsCaptureItem, rect: &RECT) -> Result<()> {
copy_texture
};

let subresource_size = ResourceSize {
width: (rect.right - rect.left) as u32,
height: (rect.bottom - rect.top) as u32
};

println!("{:?}", subresource_size);

let bits = unsafe {
let mut desc = D3D11_TEXTURE2D_DESC::default();
texture.GetDesc(&mut desc as *mut _);
Expand All @@ -191,13 +205,13 @@ fn take_sc(item: &GraphicsCaptureItem, rect: &RECT) -> Result<()> {
};

let bytes_per_pixel = 4;
let mut bits = vec![0u8; (desc.Width * desc.Height * bytes_per_pixel) as usize];
for row in 0..desc.Height {
let data_begin = (row * (desc.Width * bytes_per_pixel)) as usize;
let data_end = ((row + 1) * (desc.Width * bytes_per_pixel)) as usize;
let mut bits = vec![0u8; (subresource_size.width * desc.Height * bytes_per_pixel) as usize];
for row in 0..subresource_size.height {
let data_begin = (row * (subresource_size.width * bytes_per_pixel)) as usize;
let data_end = ((row + 1) * (subresource_size.width * bytes_per_pixel)) as usize;

let slice_begin = (row * mapped.RowPitch) as usize;
let slice_end = slice_begin + (desc.Width * bytes_per_pixel) as usize;
let slice_end = slice_begin + (subresource_size.width * bytes_per_pixel) as usize;

bits[data_begin..data_end].copy_from_slice(&slice[slice_begin..slice_end]);
}
Expand All @@ -207,7 +221,7 @@ fn take_sc(item: &GraphicsCaptureItem, rect: &RECT) -> Result<()> {
bits
};

save_as_image(bits, item_size)?;
save_as_image(bits, subresource_size)?;

Ok(())
}

0 comments on commit 54b2aed

Please sign in to comment.