Sign in to follow this  
Whiterock

Code Lab - Box Blur Effect

Recommended Posts

Hello community,

I've just tried to make a simple box blur effect in pdn. My code is:

#region UICode
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
// Delete any of these lines you don't need
Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
int CenterX = ((selection.Right - selection.Left) / 2)+selection.Left;
int CenterY = ((selection.Bottom - selection.Top) / 2)+selection.Top;
ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
ColorBgra SecondaryColor = (ColorBgra)EnvironmentParameters.SecondaryColor;
int BrushWidth = (int)EnvironmentParameters.BrushWidth;

ColorBgra CurrentPixel;
ColorBgra LeftPixel;
ColorBgra RightPixel;
ColorBgra AbovePixel;
ColorBgra BelowPixel;


for (int y = rect.Top + 1; y < rect.Bottom - 1; y++)
{
	for (int x = rect.Left + 1; x < rect.Right - 1; x++)
	{
	CurrentPixel = src[x,y];
	LeftPixel = src[x-1,y];
	AbovePixel = src[x,y-1];
	BelowPixel = src[x,y+1];
	RightPixel = src[x+1,y];

	CurrentPixel.R = (byte)((LeftPixel.R+AbovePixel.R+BelowPixel.R+RightPixel.R)/4);
	CurrentPixel.G = (byte)((LeftPixel.G+AbovePixel.G+BelowPixel.G+RightPixel.G)/4);
	CurrentPixel.B = (byte)((LeftPixel.B+AbovePixel.B+BelowPixel.B+RightPixel.B)/4);
	CurrentPixel.A = (byte)((LeftPixel.A+AbovePixel.A+BelowPixel.A+RightPixel.A)/4);

	dst[x,y] = CurrentPixel;
	}
}
}

But i don't know the reason, why it doesn't work correctly. It's so strange, because it's just working every 2y-coords.

I hope someone or BoltBait can help me:roll:

MfG whiterock

p.s: I'm from Austria

Share this post


Link to post
Share on other sites

for (int y = rect.Top + 1; y < rect.Bottom - 1; y++)
{
for (int x = rect.Left + 1; x < rect.Right - 1; x++)
{

should be

for (int y = rect.Top; y < rect.Bottom; y++)
{
for (int x = rect.Left; x < rect.Right; x++)
{

Edited by Cookies

Share this post


Link to post
Share on other sites

Render rect's coordinates don't always match those of canvas.

Thererefore y loop starting at y = rect.Top + 1 will cause unpredictable results.

You could work around though, by using coordinates from Rectangle selection :

for (int y = selection.Top + 1; y < selection.Bottom - 1; y++)

Someone will sure present a better solution. :roll:

Share this post


Link to post
Share on other sites

Ok, lets see: Why can do a "+1" say the program, that it it should calucate each second y-coord/x coord ? (strange)

I will try it and infomate (? does this word exist) you if its working or not;)

MfG Whiterock ("MfG" menas in Austria, something like "With friendly Greetings")

Share this post


Link to post
Share on other sites

Render rect's coordinates don't always match those of canvas.

Thererefore y loop starting at y = rect.Top + 1 will cause unpredictable results.

You could work around though, by using coordinates from Rectangle selection :

for (int y = selection.Top + 1; y < selection.Bottom - 1; y++)

Someone will sure present a better solution. :roll:

I wouldn't do that, if its in an irregular selection (round, shaped) it will do it outside the selection that way

Ok, lets see: Why can do a "+1" say the program, that it it should calucate each second y-coord/x coord ? (strange)

I will try it and infomate (? does this word exist) you if its working or not;)

MfG Whiterock ("MfG" menas in Austria, something like "With friendly Greetings")

read this: http://www.boltbait....lp/tutorial.asp it explains it

edit: I guess your trying to avoid going "outside" the image, if so use

src.GetBilinearSampleClamped(x, y);

Edited by Cookies

Share this post


Link to post
Share on other sites

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.

Sign in to follow this