Jump to content

Anti-alias Plugin [v1.5.8.0 Minor fixes, August 25th, 2007]


jsonchiu
 Share

Recommended Posts

Ok, so as you know, Boltbait's feather plugin uses Gaussian Blur to soften the edges.

I have always found it doesn't quite work the way I wanted.

So, I wrote another plugin for smoothing edges, but still keep it clear, not blurred.

 

With this plugin, the result will be clear and smooth, just like any other anti-aliased shapes.

 

Amount: pretty self explanatory. Slide it around to see different effects.

Strength: opaqueness of the "feathering" Default 980 for anti-aliasing. Turn it down for feathering.

Soft outline/None: Default is on. If turned on, it would darken the edge a little bit to make it clearer. If the image has light colors, turn it off, or else there would be an ugly dark outline.

 

Example: let's feather a cat... (cut from Bob's siggie).

 

Before: the edge is too hard, and jagged

beforefeatherkk1.png

 

After: the edge is smoothed out (anti-aliased)!

afterfeathermx7.png

 

This plugin can also blur the edges with a medium-high amount and low strength

newfeatheray9.png

 

Download

 

Source:

Spoiler

int Amount1=5; //[5,50] Amount
int Amount2=980; //[0,1000] Strength
int Amount3=0; //[0,1] Soft Outline                          None

void Render(Surface dst, Surface src, Rectangle rect)
{
    PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);

    Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();

    ColorBgra CurrentPixel;
    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 (isAtEdge(x,y, src, selection) == true)
                {
                    CurrentPixel = getAveragePixel(x,y, src, selection);
                }
                dst[x,y] = CurrentPixel;
            }
        }
    }
}
bool isAtEdge(int x, int y, Surface src, Rectangle selection)
{
    ColorBgra[] c = new ColorBgra[5];
    if (x <= selection.Left) { x = selection.Left + 1; }
    if (y <= selection.Top) { y = selection.Top + 1; }
    if (x >= selection.Right-1) { x = selection.Right - 2; }
    if (y >= selection.Bottom-1) { y = selection.Bottom - 2; }
    c[0] = src[x, y-1];
    c[1] = src[x-1, y];
    c[2] = src[x+1, y];
    c[3] = src[x, y+1];
    c[4] = src[x, y];
    for (int i = 0; i < c.Length; i++)
    {
        ColorBgra temp = (ColorBgra)c[i];
        if (temp.A == 0)
        {
            return true;
            break;
        }
    }
    return false;
}
ColorBgra getAveragePixel(int x, int y, Surface src, Rectangle selection)
{
    int gridSpan = (int)(Amount1/2) - 1;
    int gridWidth = gridSpan * 2 + 1;
    int gridArea = gridWidth * gridWidth - 1;
   
    int counter = 0;
    ColorBgra[] c = new ColorBgra[gridArea];
    for (int i = -gridSpan; i <= gridSpan; i++)
    {
        for (int j = -gridSpan; j <= gridSpan; j++)
        {
            if (!(i == 0 && j == 0))
            {
                int newx = x+i;
                int newy = y+j;
               
                if (newx <= selection.Left+(gridSpan-1)) { newx = selection.Left + gridSpan; }
                if (newy <= selection.Top+(gridSpan-1)) { newy = selection.Top + gridSpan; }
                if (newx >= selection.Right-(gridSpan+1)) { newx = selection.Right - (gridSpan + 2); }
                if (newy >= selection.Bottom-(gridSpan+1)) { newy = selection.Bottom - (gridSpan + 2); }
               
                c[counter] = src[newx,newy];
                counter++;
            }
        }
    }
   
    ColorBgra returnCol = src[x,y];
   
    float newR = 0;
    float newG = 0;
    float newB = 0;
    float newA = 0;
   
    float highestA = 0;
   
    float counter2 = (float)0;
   
    for (int m = 0; m < gridArea; m++)
    {
        ColorBgra temp = (ColorBgra) c[m];
        if (temp.A > 0)
        {
            newR += (float)temp.R;
            newG += (float)temp.G;
            newB += (float)temp.B;
            newA += (float)temp.A;
           
            if ((float)temp.A > (float)highestA)
            {
                highestA = (float) temp.A;
            }
            counter2 = counter2 + 1;
        }
    }
   
    if (Amount3 == 0 && highestA == 255)
    {
        counter2 += 1;
    }
   
    newR = newR / counter2;
    newG = newG / counter2;
    newB = newB / counter2;
   
    if (counter2 > 1)
    {
        newA = (float)((counter2-2)/((1000-Amount2)/5)) * highestA;
        if (newA > highestA)
        {
            newA = highestA;
        }         
    }
    else
    {
        newA = 0;
    }
    returnCol.R = (byte)newR;
    returnCol.G = (byte)newG;
    returnCol.B = (byte)newB;
    returnCol.A = (byte)newA;
   
    return returnCol;
}

 

 

Enjoy!

Update1: added "strength" slide bar and expand "Amount" to 30

Update2: FULLY fixed transparency bug!

Update3: added "soft outline" slider to adjust edge enhancement

Update4: fixed "no anti-alias on low alpha" bug. The plugin should now antialias equally for any alpha value. Also, added a menu icon!

 

Edited by toe_head2001
Formatting & restore images

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

Okay, as a non-coder who can't read it, what does this do?

 

The Doctor: There was a goblin, or a trickster, or a warrior... A nameless, terrible thing, soaked in the blood of a billion galaxies. The most feared being in all the cosmos. And nothing could stop it, or hold it, or reason with it. One day it would just drop out of the sky and tear down your world.
Amy: But how did it end up in there?
The Doctor: You know fairy tales. A good wizard tricked it.
River Song: I hate good wizards in fairy tales; they always turn out to be him.

Link to comment
Share on other sites

Well, yes, that much I understood :-) I mean, how? Progressive transparency?

 

The Doctor: There was a goblin, or a trickster, or a warrior... A nameless, terrible thing, soaked in the blood of a billion galaxies. The most feared being in all the cosmos. And nothing could stop it, or hold it, or reason with it. One day it would just drop out of the sky and tear down your world.
Amy: But how did it end up in there?
The Doctor: You know fairy tales. A good wizard tricked it.
River Song: I hate good wizards in fairy tales; they always turn out to be him.

Link to comment
Share on other sites

@david.atwell

Yes, progressive transparency.

Here's how the code works:

1. take a pixel, and determine whether it's a solid or transparent one.

2. if it's a transparent one, change the color to the nearest solid pixel, and the alpha value to the average of the surrounding pixels (with a little bit of tweaking to make it not so blurred).

@Ash

I intended it to be the Photoshop-quality feather, but then I couldn't make it to feather at different amount of pixels.

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

I just tried the plugin. It's making a dark line around my object......

1. take a pixel, and determine whether it's a solid or transparent one.

2. if it's a transparent one, change the color to the nearest solid pixel, and the alpha value to the average of the surrounding pixels (with a little bit of tweaking to make it not so blurred).

That kinda sounds like what feather is doing in "Grow" mode, I'm no coder tho, Tell me if I am wrong..

The_next_thousand_words_by_0_ASH_0.png

All creations Ash + Paint.NET [ Googlepage | deviantArt | Club PDN | PDN Fan ]

Link to comment
Share on other sites

@Ash

Hmm... dark line? It doesn't work with semi-transparent objects IMO. In order for it to work as intended, you need all the pixels on the edge to be opaque. Also, it will place a very tiny line (barely visible) around it if it has a very light color.

-------

The feather plugin in the "grow" mode still use Gaussian Blur, and therefore produce blurred results on the edges, instead of clearcut anti-alias.

You can see the difference in these pictures (left: feather 1px, grow, right: anti-alias, amount: 5)

oldfeatheraz5.pngafterfeathermx7.png

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

@Ash

Hmm... dark line? It doesn't work with semi-transparent objects IMO. In order for it to work as intended, you need all the pixels on the edge to be opaque. Also, it will place a very tiny line (barely visible) around it if it has a very light color.

-------

The feather plugin in the "grow" mode still use Gaussian Blur, and therefore produce blurred results on the edges, instead of clearcut anti-alias.

You can see the difference in these pictures (left: feather 1px, grow, right: anti-alias, amount: 5)

I see it. Can the amout of your AA plugin go above 5?

Thanks :)

The_next_thousand_words_by_0_ASH_0.png

All creations Ash + Paint.NET [ Googlepage | deviantArt | Club PDN | PDN Fan ]

Link to comment
Share on other sites

The cat is on a white border in his post. It needs to be on a transparent background.

 

The Doctor: There was a goblin, or a trickster, or a warrior... A nameless, terrible thing, soaked in the blood of a billion galaxies. The most feared being in all the cosmos. And nothing could stop it, or hold it, or reason with it. One day it would just drop out of the sky and tear down your world.
Amy: But how did it end up in there?
The Doctor: You know fairy tales. A good wizard tricked it.
River Song: I hate good wizards in fairy tales; they always turn out to be him.

Link to comment
Share on other sites

Thanks Pyrochild.

I actually selected the white background with the selection tool at the default setting, and then deleted it. Then I ran the plugin and nothing worked.

Discovered the problem: I set the selection tool at 0 tolerance, and then repeated the whole procedure with successful feathering effect.

Thanks for the response.

Link to comment
Share on other sites

My browser crashed...

th_aa_feather_comparaison.png

^Click^

Antialias/Feather comparaison.

All kudos to Cute Overload for the cat.

Oh yeah!

I HAZ ANTIALIAS.

No. Way. I've just seen Bob. And... *poof!*—just like that—he disappears into the mist again. ~Helio

Link to comment
Share on other sites

Updated: added the "strength" slidebar, and expand the "Amount" slidebar to 30

You should now be able to do the "true" feathering (low strength) as well as anti-aliasing (high strength).

Left: amount 13 strength 669, Right: amount 5 strength 985

newfeatheray9.pngafterfeathermx7.png

The "strength" should be kept above 900 if you want clearcut, solid anti-alias.

The higher it is, the clearer the anti-alias, and 990 will produce pixelated results.

The "amount" is expanded to 30. Anything above 30 is freaking slow because calculating the average of 1000 surrounding pixels (about 32x32) for every pixel on the screen isn't a very efficient operation.

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

This is a very nice plugin! This will definitely stop all those blocky pictures I get. Thank you so much! :D I recommend this for anyone that uses the Crop the selection a lot. Or just for anyone.

Have you made any other plugins?

EDIT:

Updated: added the "strength" slidebar, and expand the "Amount" slidebar to 30

You should now be able to do the "true" feathering (low strength) as well as anti-aliasing (high strength).

Left: amount 13 strength 669, Right: amount 5 strength 985

newfeatheray9.pngafterfeathermx7.png

The "strength" should be kept above 900 if you want clearcut, solid anti-alias.

The higher it is, the clearer the anti-alias, and 990 will produce pixelated results.

The "amount" is expanded to 30. Anything above 30 is freaking slow because calculating the average of 1000 surrounding pixels (about 32x32) for every pixel on the screen isn't a very efficient operation.

That is very cool. That will come in handy too.

47306796ff8.png
Link to comment
Share on other sites

BIG UPDATE!

1. FULLY fixed transparency bug. If you turn the outline off, then semitransparent stuff should be smoothed like any other objects!

2. Add "soft outline" option for edge enhancement. Turn it off if your image has very light colors.

Enjoy!

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

  • toe_head2001 changed the title to Anti-alias Plugin [v1.5.8.0 Minor fixes, August 25th, 2007]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...