use std::cell::RefCell;
use image::{imageops, RgbImage};
use windows::core::HSTRING;
use windows::Globalization::Language;
use windows::Graphics::Imaging::{BitmapPixelFormat, SoftwareBitmap};
use windows::Media::Ocr::OcrEngine as WinOcrEngine;
use windows::Security::Cryptography::CryptographicBuffer;
use crate::types::{OcrLine, OcrRect};
thread_local! {
static ENGINE: RefCell<Option<WinOcrEngine>> = const { RefCell::new(None) };
}
pub fn recognize(img: &RgbImage) -> Result<Vec<OcrLine>, String> {
ENGINE.with(|slot| {
let mut slot = slot.borrow_mut();
if slot.is_none() {
*slot = Some(create_engine()?);
}
recognize_with(slot.as_ref().expect("engine cached above"), img)
})
}
fn create_engine() -> Result<WinOcrEngine, String> {
if let Ok(engine) = WinOcrEngine::TryCreateFromUserProfileLanguages() {
return Ok(engine);
}
Language::CreateLanguage(&HSTRING::from("en-US"))
.ok()
.and_then(|lang| WinOcrEngine::TryCreateFromLanguage(&lang).ok())
.ok_or_else(|| {
format!(
"{} no OCR language pack installed \
(probed user-profile languages and en-US)",
crate::types::WINDOWS_OCR_UNAVAILABLE
)
})
}
pub(crate) fn union_normalized(
words: impl IntoIterator<Item = (f32, f32, f32, f32)>,
w: f32,
h: f32,
) -> Option<OcrRect> {
let mut acc: Option<(f32, f32, f32, f32)> = None; for (x, y, ww, wh) in words {
let (l, t, r, b) = (x, y, x + ww, y + wh);
acc = Some(match acc {
None => (l, t, r, b),
Some((al, at, ar, ab)) => (al.min(l), at.min(t), ar.max(r), ab.max(b)),
});
}
let (l, t, r, b) = acc?;
Some(OcrRect {
x: l / w,
y: t / h,
width: (r - l) / w,
height: (b - t) / h,
})
}
fn recognize_with(engine: &WinOcrEngine, img: &RgbImage) -> Result<Vec<OcrLine>, String> {
let max = WinOcrEngine::MaxImageDimension().map_err(|e| format!("MaxImageDimension: {e}"))?;
let scaled;
let img = if img.width() > max || img.height() > max {
let scale = max as f32 / img.width().max(img.height()) as f32;
let nw = ((img.width() as f32 * scale) as u32).clamp(1, max);
let nh = ((img.height() as f32 * scale) as u32).clamp(1, max);
scaled = imageops::resize(img, nw, nh, imageops::FilterType::Lanczos3);
&scaled
} else {
img
};
let (w, h) = img.dimensions();
if w == 0 || h == 0 {
return Err("empty image".into());
}
let mut bgra = Vec::with_capacity(w as usize * h as usize * 4);
for p in img.pixels() {
bgra.extend_from_slice(&[p[2], p[1], p[0], 255]);
}
let buffer =
CryptographicBuffer::CreateFromByteArray(&bgra).map_err(|e| format!("ibuffer: {e}"))?;
let bitmap =
SoftwareBitmap::CreateCopyFromBuffer(&buffer, BitmapPixelFormat::Bgra8, w as i32, h as i32)
.map_err(|e| format!("software bitmap: {e}"))?;
let result = engine
.RecognizeAsync(&bitmap)
.map_err(|e| format!("RecognizeAsync: {e}"))?
.join()
.map_err(|e| format!("RecognizeAsync wait: {e}"))?;
let (fw, fh) = (w as f32, h as f32);
let lines = result.Lines().map_err(|e| format!("lines: {e}"))?;
let mut out = Vec::new();
for line in &lines {
let text = line
.Text()
.map_err(|e| format!("line text: {e}"))?
.to_string();
let words = line.Words().map_err(|e| format!("words: {e}"))?;
let rects = words.into_iter().filter_map(|word| {
word.BoundingRect()
.ok()
.map(|r| (r.X, r.Y, r.Width, r.Height))
});
if let Some(rect) = union_normalized(rects, fw, fh) {
out.push(OcrLine {
text,
rect,
confidence: None, });
}
}
Ok(out)
}