You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To avoid such artifacts, the pixel's contribution to the color change should be weighted by its alpha channel value. Fully transparent pixels should get multiplied by 0 and not contribute to the changes in non-alpha channels at all.
Code used for testing:
use std::env;use std::error::Error;use std::path::Path;use std::process;fnmain() -> Result<(),Box<dynError>>{// Collect command-line argumentslet args:Vec<String> = env::args().collect();// Ensure that we have 2 CLI argumentsif args.len() != 3{eprintln!("Usage: {} <path> <radius>", args[0]);
process::exit(1);}let radius:f32 = args[2].parse()?;// Load the input imagelet path_str = &args[1];let path = Path::new(path_str);let input = image::open(path)?;// Correct but slow Gaussian blurletmut out_path_gauss = path.to_owned();
out_path_gauss.set_extension("gaussian.png");let blurred = input.blur(radius);
blurred.save(out_path_gauss)?;Ok(())}
Originally found in #2302 (comment), moving it to the issue tracker so that it doesn't get lost.
Wow, good catch! I think this is a general issue coming from image::imageops::sample::horizontal_sample which basically affects all filters defined by convolution with 2d functions. I think your suggested fix (using the alpha channel as weight) is correct and I am willing to implement this as long as this is the suggested fix.
This StackOverflow answer seems to suggest the same thing, with the contributions being weighted by alpha naturally if the RBG values are multiplied by alpha first: https://computergraphics.stackexchange.com/a/5517
With this test image the blur implementation shows artifacts:
blur-test-input
It's just a white rectangle in the middle, surrounded by fully transparent pixels.
Gaussian blur in
image
bleeds color from fully transparent pixels into the white rectangle, resulting in the following image:The fully transparent pixels have R set to 255 while all the other channels are set to 0, and that red color bleeds into the white rectangle.
The expected blur as produced by GIMP is like this - blurred white rectangle without any red color:
blurred-gimp-20px
To avoid such artifacts, the pixel's contribution to the color change should be weighted by its alpha channel value. Fully transparent pixels should get multiplied by 0 and not contribute to the changes in non-alpha channels at all.
Code used for testing:
Originally found in #2302 (comment), moving it to the issue tracker so that it doesn't get lost.
Tested on
image
from git on commit 98ceb71The text was updated successfully, but these errors were encountered: