Skip to content

Commit dc132b3

Browse files
committed
refine code
Signed-off-by: Wang, Yi A <[email protected]>
1 parent f384e0f commit dc132b3

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

router/src/validation.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -648,37 +648,18 @@ fn image_tokens(
648648
let max_longest_edge_for_image_resize = config.get_max_longest_edge_for_image_resize();
649649
let max_image_size = config.get_max_image_size();
650650

651-
// resize image to max_longest_edge_for_image_resize and keep aspect ratio
652651
let (height, width) = {
653-
let aspect_ratio = height as f32 / width as f32;
654-
if height > width {
655-
(
656-
max_longest_edge_for_image_resize,
657-
(max_longest_edge_for_image_resize as f32 / aspect_ratio) as usize,
658-
)
659-
} else {
660-
(
661-
(max_longest_edge_for_image_resize as f32 * aspect_ratio) as usize,
662-
max_longest_edge_for_image_resize,
663-
)
664-
}
665-
};
652+
let h = height as f32;
653+
let w = width as f32;
666654

667-
let (height, width) = {
668-
let aspect_ratio = height as f32 / width as f32;
669-
if height >= width && height > max_image_size {
670-
(
671-
max_image_size,
672-
(max_image_size as f32 / aspect_ratio) as usize,
673-
)
674-
} else if width > height && width > max_image_size {
675-
(
676-
(max_image_size as f32 * aspect_ratio) as usize,
677-
max_image_size,
678-
)
679-
} else {
680-
(height, width)
681-
}
655+
// First resize to max_longest_edge (always scale to this size)
656+
let scale1 = max_longest_edge_for_image_resize as f32 / h.max(w);
657+
let (h, w) = (h * scale1, w * scale1);
658+
659+
// Ensure we dont exceed max_size (only scale down)
660+
let scale2 = (max_image_size as f32 / h.max(w)).min(1.0);
661+
662+
((h * scale2) as usize, (w * scale2) as usize)
682663
};
683664

684665
let image_seq_len = config.get_number_of_features();

0 commit comments

Comments
 (0)