image-rs / imageproc

Image processing operations
MIT License
723 stars 144 forks source link

Rounded corners on rectangles and images #672

Open zaddok opened 3 weeks ago

zaddok commented 3 weeks ago

This library is awesome, thanks for all your work on this.

Is it correct to say it's not possible to create a "rounded rect" or "rounded image" (i.e. like CSS border-radius)? Or is it possible and I have missed it?

Can I put in a vote for this one.

ripytide commented 3 weeks ago

Interesting, how would you expect this to work?

Perhaps a function taking a normal rectangular image and then giving it a background color or 0-opacity around the corners at a given radius?

Something like:

fn border_radius<P>(image: &Image<P>, border_radius: f32) -> Image<P> {
    ...
}

Or maybe an explicit background pixel:

fn border_radius<P>(image: &Image<P>, border_radius: f32, background_pixel: P) -> Image<P> {
    ...
}

Or if rgb v0.9 lands then maybe:

fn border_radius<P>(image: &Image<P>, border_radius: f32, background_pixel: P) -> Image<P> 
    where P: HasAlpha
{
    ...
}
zaddok commented 3 weeks ago

Personally, in my opinion, one border_radius applied to all corners is enough. But css (and some other rust libraries) support border radius on individual corners, i.e.

https://docs.rs/kurbo/latest/kurbo/struct.RoundedRect.html

ripytide commented 3 weeks ago

Yep that sounds reasonable, so something like:

//copied from kurbo
pub struct RoundedRectRadii {
    pub top_left: f64,
    pub top_right: f64,
    pub bottom_right: f64,
    pub bottom_left: f64,
}

pub fn rounded_corners<P>(image: &Image, rounded_rect_radii: RoundedRectRadii) -> Image<P>
    where P: HasAlpha
{
...
}

I must admit this kind of function isn't that high of a priority for me personally, but I have no problems with it being added to the library were someone to make a PR.

zaddok commented 3 weeks ago

Thanks, for now I'll just get the task at hand done with a quick and dirty hack using a custom background png. I'll poke around in the code later and see how hard/easy it might be for me to look at.