Sign in to follow this  
jsonchiu

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

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

<broken link removed by EER>

Source:

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!

Anti-Alias by jsonchiu.zip

Share this post


Link to post
Share on other sites

I think the title can do without the "much better than the feather plugin"

IMO, each plugins can be useful in different case.

Share this post


Link to post
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.

Share this post


Link to post
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..

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Sometimes when I use magic wand to select only an image from even a pure white background I get little white pixels on the edge, and feather doesn't usually get it to go away. Will this help more?

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Would be great if it works for me!

Downloaded the plugin, put in in effects folder, copied the same cat from jsonchiu's posting, ran the plugin. Nothing happened! Setting was at the default value of 5.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
th_aa_feather_comparaison.png

^Click^

Antialias/Feather comparaison.

Heh. I'll stick with Feather. :P

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this