paddle_ocr_rs/
scale_param.rs1#[derive(Debug)]
2pub struct ScaleParam {
3 pub src_width: u32,
4 pub src_height: u32,
5 pub dst_width: u32,
6 pub dst_height: u32,
7 pub scale_width: f32,
8 pub scale_height: f32,
9}
10
11impl ScaleParam {
12 pub fn new(
13 src_width: u32,
14 src_height: u32,
15 dst_width: u32,
16 dst_height: u32,
17 scale_width: f32,
18 scale_height: f32,
19 ) -> Self {
20 Self {
21 src_width,
22 src_height,
23 dst_width,
24 dst_height,
25 scale_width,
26 scale_height,
27 }
28 }
29
30 pub fn get_scale_param(src: &image::RgbImage, target_size: u32) -> Self {
31 let src_width = src.width();
32 let src_height = src.height();
33 let mut dst_width;
34 let mut dst_height;
35
36 let ratio: f32 = if src_width > src_height {
37 target_size as f32 / src_width as f32
38 } else {
39 target_size as f32 / src_height as f32
40 };
41
42 dst_width = (src_width as f32 * ratio) as u32;
43 dst_height = (src_height as f32 * ratio) as u32;
44
45 if dst_width % 32 != 0 {
46 dst_width = (dst_width / 32) * 32;
47 dst_width = dst_width.max(32);
48 }
49 if dst_height % 32 != 0 {
50 dst_height = (dst_height / 32) * 32;
51 dst_height = dst_height.max(32);
52 }
53
54 let scale_width = dst_width as f32 / src_width as f32;
55 let scale_height = dst_height as f32 / src_height as f32;
56
57 Self::new(
58 src_width,
59 src_height,
60 dst_width,
61 dst_height,
62 scale_width,
63 scale_height,
64 )
65 }
66}
67
68impl std::fmt::Display for ScaleParam {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 write!(
71 f,
72 "src_width:{},src_height:{},dst_width:{},dst_height:{},scale_width:{},scale_height:{}",
73 self.src_width,
74 self.src_height,
75 self.dst_width,
76 self.dst_height,
77 self.scale_width,
78 self.scale_height
79 )
80 }
81}