Function image::imageops::overlay_bounds_ext

source ·
fn overlay_bounds_ext(
    (bottom_width, bottom_height): (u32, u32),
    (top_width, top_height): (u32, u32),
    x: i64,
    y: i64,
) -> (u32, u32, u32, u32, u32, u32)
Expand description

Calculate the region that can be copied from top to bottom.

Given image size of bottom and top image, and a point at which we want to place the top image onto the bottom image, how large can we be? Have to wary of the following issues:

  • Top might be larger than bottom
  • Overflows in the computation
  • Coordinates could be completely out of bounds

The returned value is of the form:

(origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, x_range, y_range)

The main idea is to do computations on i64’s and then clamp to image dimensions. In particular, we want to ensure that all these coordinate accesses are safe:

  1. bottom.get_pixel(origin_bottom_x + [0..x_range), origin_bottom_y + [0..y_range))
  2. top.get_pixel(origin_top_y + [0..x_range), origin_top_y + [0..y_range))