feat: 截屏改为抽样,提升性能。

This commit is contained in:
2022-12-01 20:15:05 +08:00
parent 479fdba9f6
commit a6e91dfae1
6 changed files with 47 additions and 19 deletions

View File

@@ -35,8 +35,8 @@ impl Picker {
display_height: 1200,
top_led_strip: LedStripConfig {
index: 1,
global_start_position: 32,
global_end_position: 60,
global_start_position: 59,
global_end_position: 32,
},
bottom_led_strip: LedStripConfig {
index: 0,
@@ -60,8 +60,8 @@ impl Picker {
display_height: 1692,
top_led_strip: LedStripConfig {
index: 0,
global_start_position: 0,
global_end_position: 32,
global_start_position: 31,
global_end_position: 0,
},
bottom_led_strip: LedStripConfig {
index: 0,

View File

@@ -1,7 +1,7 @@
use std::ops::Range;
use color_space::{Hsv, Rgb};
use either::Either;
use super::{
config::{DisplayConfig, LedStripConfig},
@@ -28,9 +28,11 @@ impl Screenshot {
.await
}
pub fn get_top_of_led_strip_range(&self) -> Range<usize> {
pub fn get_top_of_led_start_at(&self) -> usize {
self.config.top_led_strip.global_start_position
..self.config.top_led_strip.global_end_position
}
pub fn get_top_of_led_end_at(&self) -> usize {
self.config.top_led_strip.global_end_position
}
async fn get_x_colors(
@@ -41,7 +43,11 @@ impl Screenshot {
let bitmap = &self.bitmap;
let number_of_leds = strip_config
.global_start_position
.abs_diff(strip_config.global_end_position);
.abs_diff(strip_config.global_end_position)
+ 1;
if number_of_leds == 0 {
return Ok(vec![]);
}
let cell_size_x = self.config.display_width / number_of_leds;
let cell_size_y = self.config.display_height / 8;
let cell_size = cell_size_x * cell_size_y;
@@ -50,32 +56,43 @@ impl Screenshot {
XPosition::Bottom => {
self.config.display_height - 20 - cell_size_y..self.config.display_height - 20
}
}
.step_by(5);
let x_range = if strip_config.global_start_position < strip_config.global_end_position {
Either::Left(strip_config.global_start_position..=strip_config.global_end_position)
} else {
Either::Right(
(strip_config.global_end_position..=strip_config.global_start_position).rev(),
)
};
let mut colors = Vec::new();
let stride = bitmap.len() / self.config.display_height;
for pos in strip_config.global_start_position..strip_config.global_end_position {
for pos in x_range {
let mut r = 0.0;
let mut g = 0.0;
let mut b = 0.0;
for x in pos * cell_size_x..(pos + 1) * cell_size_x {
let mut count = 0;
for x in (pos * cell_size_x..(pos + 1) * cell_size_x).step_by(5) {
for y in y_range.to_owned() {
let i = stride * y + 4 * x;
r += bitmap[i + 2] as f64;
g += bitmap[i + 1] as f64;
b += bitmap[i] as f64;
count+=1;
}
}
let rgb = Rgb::new(
r / cell_size as f64,
g / cell_size as f64,
b / cell_size as f64,
r / count as f64,
g / count as f64,
b / count as f64,
);
let hsv = Hsv::from(rgb);
// info!("HSV: {:?}", [hsv.h, hsv.s, hsv.v]);
let color = LedColor::from_hsv(hsv.h, hsv.s, hsv.v);
// info!("color: {:?}", color.get_rgb());
// paris::info!("color: {:?}", color.get_rgb());
colors.push(color);
}
return Ok(colors);