use crate::core::image::OcrImage;
use crate::utils::{OcrError, Result};
use image::GenericImageView;
pub struct ImageProcessor;
impl ImageProcessor {
pub fn load_from_memory(data: &[u8]) -> Result<image::DynamicImage> {
let img = image::load_from_memory(data)
.map_err(|e| OcrError::ImageProcessing(format!("Failed to load image: {}", e)))?;
Ok(img)
}
pub fn to_ocr_image(img: image::DynamicImage, dpi: u32) -> OcrImage {
let mut ocr_image = OcrImage::new(img, dpi);
ocr_image.metadata.insert(
"color_type".to_string(),
format!("{:?}", ocr_image.data.color()),
);
let (w, h) = ocr_image.data.dimensions();
ocr_image
.metadata
.insert("dimensions".to_string(), format!("{}x{}", w, h));
ocr_image
}
pub fn preprocess_for_ocr(img: &OcrImage) -> Result<OcrImage> {
let gray_img = if img.format != crate::core::image::ImageFormat::Grayscale {
img.to_grayscale()
} else {
img.clone()
};
let preprocessed = Self::apply_basic_preprocessing(&gray_img)?;
Ok(preprocessed)
}
fn apply_basic_preprocessing(img: &OcrImage) -> Result<OcrImage> {
Ok(img.clone())
}
}