bhcli

A TUI for chatting on LE PHP Chats (onion)
git clone https://git.dasho.dev/n0tr1v/bhcli.git
Log | Files | Refs | README

commit c34582b1ecbb2e0c5a2771f3e4d307997ca87c37
parent 3b2d66cf5909f0219dea9b145c62f3fd78934bb2
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 30 Mar 2023 13:29:46 -0700

use consts

Diffstat:
Msrc/lechatphp/mod.rs | 24+++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/src/lechatphp/mod.rs b/src/lechatphp/mod.rs @@ -8,6 +8,8 @@ use lazy_static::lazy_static; const B64_PREFIX: &'static str = "R0lGODlhCAAOAIAAAAAAAAAAACH5BAgAAAAALAAAAAAIAA4AgAQCBPz+/AI"; const _ALPHABET: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; const ALPHABET1: &'static str = "abdcefgh1ijkImnpoqrstyQuvwxzABCDEGJKMNHLORPFSTlUVWXYZ023456789"; +const LETTER_WIDTH: u32 = 8; +const LETTER_HEIGHT: u32 = 14; lazy_static! { static ref B64_MAP: HashMap<&'static str, char> = HashMap::from([ @@ -108,8 +110,6 @@ fn _solve_difficulty1(img: &DynamicImage) -> Option<String> { const NB_CHARS: u32 = 5; const LEFT_PADDING: u32 = 5; const TOP_PADDING: u32 = 7; - const LETTER_WIDTH: u32 = 8; - const LETTER_HEIGHT: u32 = 14; let mut answer = String::new(); for i in 0..NB_CHARS { let sub_img = img.crop_imm(LEFT_PADDING + ((LETTER_WIDTH+1)*i), TOP_PADDING, LETTER_WIDTH, LETTER_HEIGHT); @@ -136,8 +136,6 @@ fn solve_difficulty2(img: &DynamicImage) -> Option<String> { const NB_CHARS: u32 = 5; const LEFT_PADDING: u32 = 5; const TOP_PADDING: u32 = 7; - const LETTER_WIDTH: u32 = 8; - const LETTER_HEIGHT: u32 = 14; let mut answer = String::new(); for i in 0..NB_CHARS { let sub_img = img.crop_imm(LEFT_PADDING + ((LETTER_WIDTH +1)*i), TOP_PADDING, LETTER_WIDTH, LETTER_HEIGHT); @@ -210,7 +208,7 @@ fn solve_difficulty3(img: &DynamicImage) -> Result<String, CaptchaErr> { visited.insert(letter.clone()); for i in 0..4 { let top_left = Point::new(letter.offset.x-5, letter.offset.y-3); - let mut rect = Rect{p1: top_left, width: 8+5+6, height: 14+3+2}; + let mut rect = Rect{p1: top_left, width: LETTER_WIDTH+5+6, height: LETTER_HEIGHT+3+2}; let mut retry = 0; 'retry_loop: loop { retry += 1; @@ -239,9 +237,9 @@ fn find_letters(img: &DynamicImage) -> HashMap<char, Letter> { const IMAGE_HEIGHT: u32 = 200; const MIN_PX_FOR_LETTER: usize = 21; let mut letters_map: HashMap<char, Letter> = HashMap::new(); - for y in 0..IMAGE_HEIGHT-14 { - for x in 0..IMAGE_WIDTH-8 { - let letter_img = img.crop_imm(x, y, 8, 14); + for y in 0..IMAGE_HEIGHT-LETTER_HEIGHT { + for x in 0..IMAGE_WIDTH-LETTER_WIDTH { + let letter_img = img.crop_imm(x, y, LETTER_WIDTH, LETTER_HEIGHT); // We know that minimum amount of pixels on to form a letter is 21 // We can skip squares that do not have this prerequisite @@ -263,13 +261,13 @@ fn find_letters(img: &DynamicImage) -> HashMap<char, Letter> { // "w" fits in "W". So if we find "W" 1 px bellow, discard "w" if c == 'w' { let capital_w_img = get_letter_img('W'); - let one_px_down_img = img.crop_imm(x, y+1, 8, 14); + let one_px_down_img = img.crop_imm(x, y+1, LETTER_WIDTH, LETTER_HEIGHT); if img_contains_letter(&one_px_down_img, &capital_w_img) { continue; } } else if c == 'k' { let capital_k_img = get_letter_img('K'); - let one_px_up_img = img.crop_imm(x+1, y+1, 8, 14); + let one_px_up_img = img.crop_imm(x+1, y+1, LETTER_WIDTH, LETTER_HEIGHT); if img_contains_letter(&one_px_up_img, &capital_k_img) { continue; } @@ -289,7 +287,7 @@ fn get_starting_letter(img: &DynamicImage, letters_map: &HashMap<char, Letter>) let mut starting: Option<Letter> = None; // Step2: Find the starting letter for (_, letter) in letters_map.iter() { - let square = img.crop_imm(letter.offset.x-5, letter.offset.y-3, 8+5+6, 14+3+2); + let square = img.crop_imm(letter.offset.x-5, letter.offset.y-3, LETTER_WIDTH+5+6, LETTER_HEIGHT+3+2); let count_red = count_red_px(&square); if count_red > MIN_STARTING_PT_RED_PX { starting = Some(letter.clone()); @@ -428,8 +426,8 @@ fn get_pixel_in_bound(img: &DynamicImage, x: u32, y: u32) -> Option<Rgba<u8>> { // give an image and a valid letter image, return either or not the letter is in that image. fn img_contains_letter(img: &DynamicImage, letter_img: &DynamicImage) -> bool { - for y in 0..14 { - for x in 0..8 { + for y in 0..LETTER_HEIGHT { + for x in 0..LETTER_WIDTH { if let Some(good_letter_color) = get_pixel_in_bound(letter_img, x, y) { if let Some(letter_img_color) = get_pixel_in_bound(img, x, y) { // If we find an Off pixel where it's supposed to be On, skip that letter