oar_ocr/processors/
types.rs

1//! Types used in image processing operations
2//!
3//! This module defines various enums that represent different options and configurations
4//! for image processing operations in the OCR pipeline.
5use std::str::FromStr;
6
7use crate::core::errors::ImageProcessError;
8
9/// Specifies how to crop an image when the aspect ratios don't match
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum CropMode {
12    /// Crop from the center of the image
13    Center,
14    /// Crop from the top-left corner of the image
15    TopLeft,
16    /// Crop from the top-right corner of the image
17    TopRight,
18    /// Crop from the bottom-left corner of the image
19    BottomLeft,
20    /// Crop from the bottom-right corner of the image
21    BottomRight,
22    /// Crop from custom coordinates
23    Custom { x: u32, y: u32 },
24}
25
26/// Implementation of FromStr trait for CropMode to parse crop mode from string
27impl FromStr for CropMode {
28    type Err = ImageProcessError;
29
30    /// Parses a string into a CropMode variant
31    ///
32    /// # Arguments
33    /// * `mode` - A string slice that should contain either "C" for Center or "TL" for TopLeft
34    ///
35    /// # Returns
36    /// * `Ok(CropMode)` - If the string matches a valid crop mode
37    /// * `Err(ImageProcessError::UnsupportedMode)` - If the string doesn't match any valid crop mode
38    fn from_str(mode: &str) -> Result<Self, Self::Err> {
39        match mode {
40            "C" => Ok(CropMode::Center),
41            "TL" => Ok(CropMode::TopLeft),
42            _ => Err(ImageProcessError::UnsupportedMode),
43        }
44    }
45}
46
47/// Specifies how to limit the size of an image during resizing operations
48#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
49pub enum LimitType {
50    /// Limit the smaller dimension of the image
51    Min,
52    /// Limit the larger dimension of the image
53    Max,
54    /// Resize the long dimension to a specific size while maintaining aspect ratio
55    ResizeLong,
56}
57
58/// Specifies the order of channels in an image tensor
59#[derive(Debug, Clone)]
60pub enum ChannelOrder {
61    /// Channel, Height, Width order (common in PyTorch)
62    CHW,
63    /// Height, Width, Channel order (common in TensorFlow)
64    HWC,
65}
66
67/// Specifies the type of bounding box used for text detection
68#[derive(Debug)]
69pub enum BoxType {
70    /// Quadrilateral bounding box (4 points)
71    Quad,
72    /// Polygonal bounding box (variable number of points)
73    Poly,
74}
75
76/// Specifies the mode for calculating scores in text detection/recognition
77#[derive(Debug)]
78pub enum ScoreMode {
79    /// Fast scoring algorithm (less accurate but faster)
80    Fast,
81    /// Slow scoring algorithm (more accurate but slower)
82    Slow,
83}
84
85/// Specifies different strategies for resizing images
86#[derive(Debug)]
87pub enum ResizeType {
88    /// Type 0 resize (implementation specific)
89    Type0,
90    /// Type 1 resize with specific image shape and ratio preservation option
91    Type1 {
92        /// Target image shape (height, width)
93        image_shape: (u32, u32),
94        /// Whether to maintain the aspect ratio of the original image
95        keep_ratio: bool,
96    },
97    /// Type 2 resize that resizes the long dimension to a specific size
98    Type2 {
99        /// Target size for the long dimension
100        resize_long: u32,
101    },
102    /// Type 3 resize to a specific input shape
103    Type3 {
104        /// Target input shape (channels, height, width)
105        input_shape: (u32, u32, u32),
106    },
107}