BoltBait's Pages - Alpha to Gray

Sarcasm, just one of the services I offer.


Paint.NET Alpha to Gray Filter

Illnab1024 made an excellent plugin called Alpha Mask Import plugin.  You can find it here:

The purpose of his effect is to import the alpha layer from a graphic file.  The file is read and converted to shades of gray, then the shades of gray are applied to the current selection replacing the alpha layer.  Thus, black from the source file is converted to completely transparent and white is completely opaque--and all shades in between.  Until Paint.NET has control layers, this is an excellent way to manipulate the alpha layer.

I had the idea to go the opposite direction.  My effect converts the current layer into black and white (and shades of gray) according to the alpha value of each pixel.


The Idea

The idea is to read the alpha value of each pixel and use that value to construct a shade of gray.  I replace the current pixel with this gray color and change the alpha to 255.


The Effect DLL

You can download the effect DLL here: Alpha2Gray.dll

Just drop this DLL file in your \program files\Paint.NET\effects directory and you should be all set.

If you need help installing effects, read this page: Installing Effects 


Instructions for Use

If you run this on a layer that is not transparent, you will simply turn your canvas white.  If, on the other hand, you have some transparency on the layer, this effect will turn that transparent area into a shade of gray.

Here is a screenshot:


Usually, this will be used at 100%, but I did give the flexibility to change that.

I believe this effect can be used hand-in-hand with Illnab1024's Alpha Mask Import effect.


Source Code

And, here is the CodeLab script:

int Amount1=100;  //[0,100] Tolerance%
void Render(Surface dst, Surface src, Rectangle rect)
    byte Tolerance = (byte)(Amount1*255/100);
    PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);
    ColorBgra CurrentPixel;
    byte Shade = 0;
    for(int y = rect.Top; y < rect.Bottom; y++)
        for (int x = rect.Left; x < rect.Right; x++)
            if (selectionRegion.IsVisible(x, y))
                CurrentPixel = src[x,y];
                if (CurrentPixel.A >= 255-Tolerance) {
                    Shade = CurrentPixel.A;
                    CurrentPixel.R = Shade;
                    CurrentPixel.G = Shade;
                    CurrentPixel.B = Shade;
                } else {
                    CurrentPixel.R = 0;
                    CurrentPixel.G = 0;
                    CurrentPixel.B = 0;
                CurrentPixel.A = 255;
                dst[x,y] = CurrentPixel;

Tuesday, April 24, 2007