use crate::render::RenderedImage;
use easypdf_core::CapabilityLevel;
#[derive(Debug, Clone)]
pub struct OcrImage {
pub width: u32,
pub height: u32,
pub pixels: Vec<u8>,
}
impl OcrImage {
#[must_use]
pub fn from_rendered(rendered: &RenderedImage) -> Self {
Self {
width: rendered.width,
height: rendered.height,
pixels: rendered.pixels.clone(),
}
}
#[must_use]
pub fn from_dynamic_image(image: &image::DynamicImage) -> Self {
let rgba = image.to_rgba8();
Self {
width: rgba.width(),
height: rgba.height(),
pixels: rgba.into_raw(),
}
}
#[must_use]
pub const fn new(width: u32, height: u32, pixels: Vec<u8>) -> Self {
Self {
width,
height,
pixels,
}
}
}
#[derive(Debug, Clone)]
pub struct OcrResult {
pub text: String,
pub confidence: Option<f32>,
pub word_boxes: Vec<WordBox>,
}
#[derive(Debug, Clone)]
pub struct WordBox {
pub text: String,
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
pub confidence: Option<f32>,
}
pub trait OcrEngine: Send + Sync {
fn recognize(
&self,
image: &OcrImage,
) -> std::result::Result<OcrResult, Box<dyn std::error::Error + Send + Sync>>;
fn name(&self) -> &'static str;
fn languages(&self) -> &[&str];
fn level(&self) -> CapabilityLevel;
}