Skip to content

Commit

Permalink
wip: add full image to ask_char_for_glyph
Browse files Browse the repository at this point in the history
  • Loading branch information
gwen-lg committed Oct 23, 2024
1 parent 4beb587 commit 827e23a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
37 changes: 27 additions & 10 deletions src/glyph_asker_term.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -29,21 +29,38 @@ impl<B> GlyphCharAsker for GlyphAskerTerm<B>
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);
Expand Down
14 changes: 11 additions & 3 deletions src/ocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -182,6 +182,7 @@ impl Line {
/// Result of a split
pub struct ImagePieces {
lines: Vec<Line>,
img: GrayImage,
}

impl ImagePieces {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -292,6 +293,7 @@ impl ImageCharacterSplitter {

/// Split image into a list of character image
pub fn split_in_character_img(self) -> Result<ImagePieces, Error> {
let bak_img = self.img.clone();
let mut image = self.img;

let mut pieces = vec![];
Expand Down Expand Up @@ -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,
})
}
}

Expand Down

0 comments on commit 827e23a

Please sign in to comment.