extern crate thiserror;
pub mod capi;
pub mod leptonica;
pub mod tesseract;
mod variable;
use std::ffi::CString;
use std::os::raw::c_int;
use std::path::Path;
use tesseract::TessSetVariableError;
pub use variable::Variable;
pub struct LepTess {
tess_api: tesseract::TessApi,
}
impl LepTess {
pub fn new(data_path: Option<&str>, lang: &str) -> Result<LepTess, tesseract::TessInitError> {
Ok(LepTess {
tess_api: tesseract::TessApi::new(data_path, lang)?,
})
}
pub fn set_image(&mut self, img_uri: impl AsRef<Path>) -> Result<(), leptonica::PixError> {
let pix = leptonica::pix_read(img_uri.as_ref())?;
self.tess_api.set_image(&pix);
Ok(())
}
pub fn set_image_from_mem(&mut self, img: &[u8]) -> Result<(), leptonica::PixError> {
let pix = leptonica::pix_read_mem(img)?;
self.tess_api.set_image(&pix);
Ok(())
}
pub fn get_source_y_resolution(&mut self) -> i32 {
self.tess_api.get_source_y_resolution()
}
pub fn get_image_dimensions(&self) -> Option<(u32, u32)> {
self.tess_api.get_image_dimensions()
}
pub fn set_source_resolution(&mut self, res: i32) {
self.tess_api.set_source_resolution(res)
}
pub fn set_fallback_source_resolution(&mut self, res: i32) {
let resolution = self.get_source_y_resolution();
if !(tesseract::MIN_CREDIBLE_RESOLUTION..=tesseract::MAX_CREDIBLE_RESOLUTION)
.contains(&resolution)
{
self.tess_api.set_source_resolution(res)
}
}
pub fn recognize(&mut self) -> i32 {
self.tess_api.recognize()
}
pub fn set_rectangle(&mut self, left: i32, top: i32, width: i32, height: i32) {
self.tess_api.set_rectangle(left, top, width, height)
}
pub fn set_rectangle_from_box(&mut self, b: &leptonica::Box) {
self.tess_api.set_rectangle_from_box(b)
}
pub fn get_utf8_text(&mut self) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_utf8_text()
}
pub fn get_hocr_text(&mut self, page: c_int) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_hocr_text(page)
}
pub fn get_alto_text(&mut self, page: c_int) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_alto_text(page)
}
pub fn get_tsv_text(&mut self, page: c_int) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_tsv_text(page)
}
pub fn get_lstm_box_text(&mut self, page: c_int) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_lstm_box_text(page)
}
pub fn get_word_str_box_text(&mut self, page: c_int) -> Result<String, std::str::Utf8Error> {
self.tess_api.get_word_str_box_text(page)
}
pub fn mean_text_conf(&self) -> i32 {
self.tess_api.mean_text_conf()
}
pub fn get_regions(&self) -> Option<leptonica::Boxa> {
self.tess_api.get_regions()
}
pub fn get_component_boxes(
&self,
level: capi::TessPageIteratorLevel,
text_only: bool,
) -> Option<leptonica::Boxa> {
self.tess_api.get_component_images(level, text_only)
}
pub fn set_variable(
&mut self,
name: Variable,
value: &str,
) -> Result<(), TessSetVariableError> {
self.tess_api
.raw
.set_variable(name.as_cstr(), &CString::new(value).unwrap())
.map_err(|_| TessSetVariableError())
}
}