Cleaned up matrix transformation and added functionality to save to pdf

This commit is contained in:
2024-02-23 17:16:21 -06:00
parent 60abb4cd3d
commit 37be4f19d7
4 changed files with 252 additions and 49 deletions

View File

@@ -3,7 +3,7 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge
use gtk::cairo::{Context, Operator};
use relm4::drawing::DrawHandler;
use waywrite::process_point::print_written;
use waywrite::process_point::print_words;
use waywrite::Point;
#[derive(Debug)]
@@ -113,7 +113,7 @@ impl SimpleComponent for AppModel {
match message {
AppInput::Input => {
print_written(&self.points).unwrap();
print_words(&self.points).unwrap();
}
AppInput::AddPoint((x, y)) => {
self.points.push(Point { x, y, new_line: false })

View File

@@ -5,12 +5,20 @@ use std::time::Instant;
use rten_tensor::{NdTensor, AsView};
use rten::Model;
use ocrs::{OcrEngine, OcrEngineParams};
use image::{ColorType, ImageFormat};
use crate::Point;
const MATRIX_SIZE: usize = 800;
const MATRIX_LEN: usize = 800;
const TEXT_MATRIX_RATIO: f64 = 0.5;
const LINE_WIDTH: f64 = 10.0;
pub fn print_written(points: &Vec<Point>) -> Result<(), Box<dyn Error>> {
const MATRIX_X_SIZE: f64 = MATRIX_LEN as f64;
const TEXT_X_SIZE: f64 = (MATRIX_X_SIZE as f64) * TEXT_MATRIX_RATIO;
const TEXT_X_OFFSET: f64 = (MATRIX_X_SIZE - TEXT_X_SIZE) / 2.0;
const LINE_WIDTH_X_OFFSET: f64 = LINE_WIDTH / 2.0;
pub fn print_words(points: &Vec<Point>) -> Result<(), Box<dyn Error>> {
let begin = Instant::now();
let processed_data = process(points);
@@ -28,7 +36,10 @@ fn process(points: &Vec<Point>) -> NdTensor<f32, 3> {
let matrix = to_matrix(points);
let y_len = matrix[0].len();
let x_len = matrix[0][0].len();
let image_data: Box<[u8]> = matrix.iter().flatten().flatten().map(|f| if *f > 0.5 { u8::from(0) } else { u8::from(255) }).collect();
let data: Vec<f32> = matrix.into_iter().flatten().flatten().map(|f| f as f32).collect();
image::save_buffer_with_format("./image.png", &image_data, x_len as u32, y_len as u32, ColorType::L8, ImageFormat::Png).unwrap();
NdTensor::from_data([1, y_len, x_len], data)
@@ -91,53 +102,43 @@ fn to_matrix(points: &Vec<Point>) -> Vec<Vec<Vec<f64>>> {
let y_len = max_y - min_y;
let y_ratio = y_len / x_len;
let matrix_y_size = MATRIX_X_SIZE * y_ratio;
let text_y_size = TEXT_X_SIZE * y_ratio;
let text_y_offset = TEXT_X_OFFSET * y_ratio;
let line_width_y_offset = LINE_WIDTH_X_OFFSET * y_ratio;
let x_size = MATRIX_SIZE as f64 * 0.5;
let y_size = (MATRIX_SIZE as f64 * 0.5) * y_ratio;
let x_offset = (MATRIX_SIZE as f64 - x_size) / 2.0;
let y_offset = ((MATRIX_SIZE as f64 * y_ratio) - y_size) / 2.0;
let x_scale = x_size / x_len;
let y_scale = y_size / y_len;
let mut matrix: Vec<Vec<f64>> = vec![vec![0.0; MATRIX_SIZE]; (MATRIX_SIZE as f64 * y_ratio) as usize];
let x_scale = MATRIX_X_SIZE / x_len;
let y_scale = matrix_y_size / y_len;
let scaled_points = points
let mut matrix: Vec<Vec<f64>> = vec![
vec![0.0; MATRIX_LEN]; (matrix_y_size as usize) + 1
];
let scaled_points: Vec<((f64, f64), bool)> = points
.iter()
.map(|point| {
let x_scaled = ((point.x - min_x) * x_scale) + x_offset;
let y_scaled = ((point.y - min_y) * y_scale) + y_offset;
let x_scaled = ((point.x - min_x) * x_scale) + TEXT_X_OFFSET;
let y_scaled = ((point.y - min_y) * y_scale) + text_y_offset;
((x_scaled, y_scaled), point.new_line)
}).collect::<Vec<_>>();
let line_width_x = MATRIX_SIZE as f64 / 80.0;
let line_width_y = (MATRIX_SIZE as f64 * y_ratio) / 80.0;
}).collect();
let mut last_x = 0.0;
let mut last_y = 0.0;
for ((x, y), newline) in scaled_points {
for ((current_x, current_y), newline) in scaled_points {
if !newline {
let curr_x_start = x - (line_width_x / 2.0);
let curr_x_end = x + (line_width_x / 2.0);
let curr_x_start = current_x - LINE_WIDTH_X_OFFSET;
let curr_x_end = current_x + LINE_WIDTH_X_OFFSET;
let last_x_start = last_x - (line_width_x / 2.0);
let last_x_end = last_x + (line_width_x / 2.0);
let last_x_start = last_x - LINE_WIDTH_X_OFFSET;
let last_x_end = last_x + LINE_WIDTH_X_OFFSET;
let top_y: f64;
let bottom_y: f64;
if y > last_y {
top_y = y + ((line_width_y / 2.0) * y_scale);
bottom_y = last_y - ((line_width_y / 2.0) * y_scale);
} else {
top_y = last_y + ((line_width_y / 2.0) * y_scale);
bottom_y = y - ((line_width_y / 2.0) * y_scale);
}
let top_y = current_y.max(last_y) + line_width_y_offset;
let bottom_y = current_y.min(last_y) - line_width_y_offset;
let start_x = (last_x_start.min(curr_x_start)) as usize;
let end_x = (last_x_end.max(curr_x_end)) as usize + 1;
@@ -145,25 +146,25 @@ fn to_matrix(points: &Vec<Point>) -> Vec<Vec<Vec<f64>>> {
for x in start_x..(end_x + 1) {
let left_line_y = line(x as f64, (last_x_start, last_y), (curr_x_start, y));
let right_line_y = line(x as f64, (last_x_end, last_y), (curr_x_end, y));
let left_line_y = line(x as f64, (last_x_start, last_y), (curr_x_start, current_y));
let right_line_y = line(x as f64, (last_x_end, last_y), (curr_x_end, current_x));
let top_line = left_line_y.max(right_line_y);
let bottom_line = left_line_y.min(right_line_y);
let top_line_y = left_line_y
.max(right_line_y)
.min(top_y) as usize;
let bottom_line_y = left_line_y
.min(right_line_y)
.max(bottom_y) as usize;
let top_line = top_line.min(top_y) as usize + 1;
let bottom_line = bottom_line.max(bottom_y) as usize;
for line_y in bottom_line..(top_line + 1) {
matrix[line_y][x] = 1.0;
for y in bottom_line_y..(top_line_y + 1) {
matrix[y][x] = 1.0;
}
}
}
last_x = x;
last_y = y;
last_x = current_x;
last_y = current_y;
}
vec![matrix]