diff --git a/src/glyph_asker_term.rs b/src/glyph_asker_term.rs index 985930d..d4cdb6b 100644 --- a/src/glyph_asker_term.rs +++ b/src/glyph_asker_term.rs @@ -1,7 +1,7 @@ use crate::ocs::{GlyphCharAsker, GlyphResult, Piece}; use compact_str::ToCompactString; use crossterm::event::{self, KeyCode, KeyEventKind}; -use image::{DynamicImage, GrayImage, Pixel}; +use image::{DynamicImage, GrayImage, Pixel, Rgb, RgbImage}; use ratatui::{prelude::Backend, Terminal}; use ratatui_image::{picker::Picker, StatefulImage}; use std::{cell::RefCell, ops::DerefMut}; @@ -29,21 +29,38 @@ impl GlyphCharAsker for GlyphAskerTerm where B: Backend, { - /// Note: return a `GlyphResult` because it can be multiple chars in some case - fn ask_char_for_glyph(&self, piece: &Piece) -> GlyphResult { + /// Note: return a `CompactString` because it can be multiple chars in some case + fn ask_char_for_glyph(&self, img: &GrayImage, piece: &Piece) -> GlyphResult { let mut self_mut = self.terminal.borrow_mut(); let (ref mut terminal, ref mut picker) = self_mut.deref_mut(); terminal .draw(|frame| { let piece_img = piece.img(); - let inverted_img = - GrayImage::from_fn(piece_img.width(), piece_img.height(), |x, y| { - let mut pixel = *piece_img.get_pixel(x, y); - pixel.invert(); - pixel + // let mut img = DynamicImage::ImageLuma8(img.clone()); + // img.invert(); + // let mut img = img.into_rgb8(); + let mut sub_img = RgbImage::from_fn(img.width(), img.height(), |x, y| { + let mut gray = *img.get_pixel(x, y); + gray.invert(); + gray.to_rgb() + }); + + // set red pixel of piece: + piece_img + .enumerate_pixels() + .filter(|(_, _, &pix)| pix.0 == [0]) + .for_each(|(x, y, _)| { + let x = x + u32::from(piece.area().left()); + let y = y + u32::from(piece.area().top()); + sub_img.put_pixel(x, y, Rgb([255, 0, 0])) }); - let mut piece_img = - picker.new_resize_protocol(DynamicImage::ImageLuma8(inverted_img)); + // let inverted_img = + // GrayImage::from_fn(piece_img.width(), piece_img.height(), |x, y| { + // let mut pixel = *piece_img.get_pixel(x, y); + // pixel.invert(); + // pixel + // }); + let mut piece_img = picker.new_resize_protocol(DynamicImage::ImageRgb8(sub_img)); //let msg = Paragraph::new("What is this glyph ?"); let image = StatefulImage::new(None); diff --git a/src/ocs.rs b/src/ocs.rs index dd82d55..f5a33cf 100644 --- a/src/ocs.rs +++ b/src/ocs.rs @@ -27,7 +27,7 @@ pub enum GlyphResult { ///TODO move pub trait GlyphCharAsker { /// Method to ask the corresponding char(s) to a `Glyph` - fn ask_char_for_glyph(&self, piece: &Piece) -> GlyphResult; + fn ask_char_for_glyph(&self, img: &GrayImage, piece: &Piece) -> GlyphResult; } #[derive(Debug, Clone)] @@ -182,6 +182,7 @@ impl Line { /// Result of a split pub struct ImagePieces { lines: Vec, + img: GrayImage, } impl ImagePieces { @@ -240,7 +241,7 @@ impl ImagePieces { }; if !ok { - let glyph_res = asker.ask_char_for_glyph(piece); + let glyph_res = asker.ask_char_for_glyph(&self.img, piece); match glyph_res { GlyphResult::Abort => { return Err(Error::StopGlyphProcess); @@ -292,6 +293,7 @@ impl ImageCharacterSplitter { /// Split image into a list of character image pub fn split_in_character_img(self) -> Result { + let bak_img = self.img.clone(); let mut image = self.img; let mut pieces = vec![]; @@ -338,11 +340,17 @@ impl ImageCharacterSplitter { // establish the base lines.iter_mut().for_each(|line| line.establish_x_base()); + // Compute space between piece + lines.iter_mut().for_each(|line| line.compute_space()); + lines .iter_mut() .for_each(|line| line.pieces.iter_mut().for_each(|piece| piece.create_img())); - Ok(ImagePieces { lines }) + Ok(ImagePieces { + lines, + img: bak_img, + }) } }