忽略完全位于图片外的切分标记矩形

矩形起点超出图片边界时直接返回 None,避免在边缘绘制伪影。

新增越界矩形回归测试。
This commit is contained in:
2026-09-10 19:02:40 +08:00
parent f200cb4d3c
commit 5b441d389c
@@ -119,8 +119,11 @@ fn clipped_rect(
if width == 0 || height == 0 || w == 0 || h == 0 { if width == 0 || height == 0 || w == 0 || h == 0 {
return None; return None;
} }
let x0 = x.min(width - 1); if x >= width || y >= height {
let y0 = y.min(height - 1); return None;
}
let x0 = x;
let y0 = y;
let x1 = x.saturating_add(w).min(width).saturating_sub(1); let x1 = x.saturating_add(w).min(width).saturating_sub(1);
let y1 = y.saturating_add(h).min(height).saturating_sub(1); let y1 = y.saturating_add(h).min(height).saturating_sub(1);
(x0 <= x1 && y0 <= y1).then_some((x0, y0, x1, y1)) (x0 <= x1 && y0 <= y1).then_some((x0, y0, x1, y1))
@@ -301,6 +304,12 @@ mod tests {
assert_eq!(*image.get_pixel(1, 1), image::Rgba([1, 2, 3, 255])); assert_eq!(*image.get_pixel(1, 1), image::Rgba([1, 2, 3, 255]));
} }
#[test]
fn clipped_rect_ignores_rectangles_starting_outside_image() {
assert_eq!(clipped_rect(4, 0, 1, 1, 4, 4), None);
assert_eq!(clipped_rect(0, 4, 1, 1, 4, 4), None);
}
#[test] #[test]
fn text_mask_is_purple_before_green_frame() { fn text_mask_is_purple_before_green_frame() {
let mut image = image::RgbaImage::from_pixel(8, 8, image::Rgba([1, 2, 3, 255])); let mut image = image::RgbaImage::from_pixel(8, 8, image::Rgba([1, 2, 3, 255]));