diff --git a/src/spoiler_mangas/mod.rs b/src/spoiler_mangas/mod.rs index 4171eea..9794809 100644 --- a/src/spoiler_mangas/mod.rs +++ b/src/spoiler_mangas/mod.rs @@ -10,21 +10,29 @@ use image::{DynamicImage,GenericImageView}; use std::io::Cursor; use tokio::io::BufStream; -const TOLERANCE: i16 = 0; +const TOLERANCE: i16 = 10; const PERCENT_ACEPTANCE: u8 = 5; +const WHITE : u8 = 160; +const WHITE_ACEPTANCE: u8 = 50; -fn check_percent(img: DynamicImage) -> u8 { +fn check_percent(img: DynamicImage) -> (u8,u8) { let mut cont = 0; + let mut cont_wite = 0; for pixel in img.pixels() { let diference_rg:i16 = (pixel.2[0] as i16 - pixel.2[1] as i16).abs(); let diference_gb:i16 = (pixel.2[1] as i16 - pixel.2[2] as i16).abs(); let diference_br:i16 = (pixel.2[0] as i16 - pixel.2[2] as i16).abs(); let diference_pixel = diference_rg + diference_gb + diference_br; + if pixel.2[0] > WHITE && pixel.2[1] > WHITE && pixel.2[2] > WHITE { + cont_wite+=1 + } if diference_pixel > TOLERANCE { cont += 1; } } - ((cont as f64 /(img.width() * img.height()) as f64)*100.0) as u8 + let percent = ((cont as f64 /(img.width() * img.height()) as f64)*100.0) as u8; + let white = ((cont_wite as f64 /(img.width() * img.height()) as f64)*100.0) as u8; + (percent,white) } pub fn append_text(new_msg: MultipartRequest, old_msg: Message) -> MultipartRequest { @@ -63,6 +71,7 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> { match msg.photo() { Some(s) => { let mut percent = 0; + let mut white = 0; let mut id : Option<(String, u32)> = None; let mut failed = true; for p in s { @@ -76,8 +85,9 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> { Ok(img) => { failed = false; let img_percent = check_percent(img); - if img_percent >= percent { - percent = img_percent; + if img_percent.0 >= percent { + percent = img_percent.0; + white = img_percent.1; id = match id { Some(i) => { if i.1 > p.width { @@ -93,7 +103,7 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> { Err(_e) => continue, } } - if !failed && percent < PERCENT_ACEPTANCE { + if !failed && percent < PERCENT_ACEPTANCE && white > WHITE_ACEPTANCE { bot.delete_message(msg.chat.id, msg.id).await?; let response = match id { Some(i) => bot.send_photo(msg.chat.id, teloxide::types::InputFile::file_id(i.0)).has_spoiler(true),