#edge #transparency #image #sprite #bevy

edges

a library for getting the edges of objects in images with transparency

11 unstable releases (5 breaking)

new 0.6.0 Dec 14, 2024
0.4.0 Nov 11, 2024
0.3.3 Jul 7, 2024
0.3.1 Mar 5, 2024

#393 in Game dev

Download history 11/week @ 2024-08-23 12/week @ 2024-08-30 11/week @ 2024-09-13 16/week @ 2024-09-20 21/week @ 2024-09-27 9/week @ 2024-10-04 13/week @ 2024-10-11 15/week @ 2024-10-18 3/week @ 2024-10-25 18/week @ 2024-11-01 130/week @ 2024-11-08 35/week @ 2024-11-15 7/week @ 2024-11-22 252/week @ 2024-11-29 88/week @ 2024-12-06

390 downloads per month
Used in bevy_collider_gen

MIT/Apache

30KB
590 lines

edges

Crates.io Crates.io MIT/Apache 2.0

Get the edges of objects in images with transparency.

Supported image types

  • image::DynamicImage
  • bevy::image::Image (or if you rather, bevy::prelude::Image)

Using

use edges::Edges;
use std::path::Path;

let image = image::open(Path::new("assets/car.png"));
let edges = Edges::from(image.unwrap());
println!("{:#?}", edges.single_translated());

How it works

I was inspired by a coding train (or, coding in the cabana rather) on an implementation of "marching squares". So this crate takes a "march through all the values" approach to find edges, i.e. pixels with at least 1 empty neighboring pixel, but instead of drawing a contour in place, it just keeps track of all the actual pixel coordinates. To determine "empty" I bitwise or all the bytes for each pixel and, in images with transparency, "empty" is a zero value for the pixel.

After that, we need to put the coordinates in some kind of "drawing order" so whatever we pass all the points to, knows how we want the object constructed. For this, the crate collects all pixels, in order, that are a distance of 1 from each other. If there are pixels that have a distance greater than 1 from any pixel in an existing group, that pixel begins a new group.

License

All code in this repository is dual-licensed under either:

At your option.

Dependencies

~2–38MB
~617K SLoC