#edge #transparency #image #sprite #bevy

edges

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

8 unstable releases (3 breaking)

new 0.4.0 Nov 11, 2024
0.3.4 Aug 13, 2024
0.3.3 Jul 7, 2024
0.3.2 May 12, 2024
0.1.0 Mar 2, 2024

#434 in Game dev

Download history 22/week @ 2024-07-29 4/week @ 2024-08-05 163/week @ 2024-08-12 6/week @ 2024-08-19 20/week @ 2024-08-26 13/week @ 2024-09-16 24/week @ 2024-09-23 16/week @ 2024-09-30 5/week @ 2024-10-07 24/week @ 2024-10-14 5/week @ 2024-10-21 3/week @ 2024-10-28 25/week @ 2024-11-04 139/week @ 2024-11-11

181 downloads per month
Used in bevy_collider_gen

MIT/Apache

45KB
607 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::render::texture::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_image_edge_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 eachother. 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–42MB
~700K SLoC