Jump to content

To increase the speed of rendering?


Estorva

Recommended Posts

Why hello here! So, I was building my plugin with Boltbait's CodeLab, the the speed of rendering is too slow for me.

 

The plugin I was making will be used to make a pixel transparent depending on its brightness/darkness, while one can set the range of transparency gradient. That is:

MvWN6ZK.png

And the code:

Hidden Content:

#region UICode
int Amount1 = 0; // [0,255] Start
int Amount2 = 255; // [0,255] Range
byte Amount3 = 0; // Alpha value depends on...|Brightness|Darkness
bool Amount4 = false; // Convert to grayscale before alpha filting
#endregion

public byte Pick(double Vxx)
{
	if ( Amount1 > Vxx )
	return 0;
	else if ( Vxx <= Amount1 + Amount2 )
	return (byte)Math.Ceiling((float) (Vxx-Amount1)*255/Amount2);
	else
	return 255;
}

public double Grscl(byte VxR, byte VxG, byte VxB)
{
	return (double)Math.Ceiling(VxR*0.2988-0.4516-0.4999+VxG*0.5863-0.3252-0.4999+VxB*0.1137-0.4669-0.4999);
}

void Render(Surface dst, Surface src, Rectangle rect)
{
	Rectangle selct = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
	ColorBgra CurrentPixel;
	
	for (int y = selct.Top; y < selct.Bottom; y++)
	{
		for (int x = selct.Left; x < selct.Right; x++)
		{
			CurrentPixel = src[x,y];
			
			if ( Amount4 == false )
			{
				if ( Amount3 == 0 )
				CurrentPixel.A = Pick(HsvColor.FromColor(CurrentPixel.ToColor()).Value*2.55);
				else
				CurrentPixel.A = Pick(255-HsvColor.FromColor(CurrentPixel.ToColor()).Value*2.55);
			}
			else
			{
				if ( Amount3 == 0 )
				CurrentPixel.A = Pick(Grscl(CurrentPixel.R, CurrentPixel.G, CurrentPixel.);
				else
				CurrentPixel.A = Pick(255-Grscl(CurrentPixel.R, CurrentPixel.G, CurrentPixel.);
			}
			
			dst[x,y] = CurrentPixel;
		}
	}
}

 

I don't know which part makes the plugin render slowly. But once the problem is solved, this plugin will be very useful for diffusion effect.

Edited by Estorva
Link to comment
Share on other sites

You are iterating <selct> but the render function has to generate all pixels in <rect>.

Render() will be called multiple times with small rectangle slices to generate the selected part of the image.

midoras signature.gif

Link to comment
Share on other sites

Try like this:

 

#region UICode
int Amount1 = 0; // [0,255] Start
int Amount2 = 255; // [0,255] Range
byte Amount3 = 0; // Alpha value depends on...|Brightness|Darkness
bool Amount4 = false; // Convert to grayscale before alpha filting
#endregion

public byte Pick(double Vxx)
{
    if (Amount1 > Vxx)
        return 0;
    else if (Vxx <= Amount1 + Amount2)
        return (byte)Math.Ceiling((float) (Vxx-Amount1)*255/Amount2);
    else
        return 255;
}

public double Grscl(byte VxR, byte VxG, byte VxB)
{
    return (double)Math.Ceiling(VxR*0.2988-0.4516-0.4999+VxG*0.5863-0.3252-0.4999+VxB*0.1137-0.4669-0.4999);
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra CurrentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x,y];
            if (Amount4 == false)
            {
                if (Amount3 == 0)
                {
                    CurrentPixel.A = Pick(HsvColor.FromColor(CurrentPixel.ToColor()).Value*2.55);
                }
                else
                {
                    CurrentPixel.A = Pick(255-HsvColor.FromColor(CurrentPixel.ToColor()).Value*2.55);
                }
            }
            else
            {
                if (Amount3 == 0)
                {
                    CurrentPixel.A = Pick(Grscl(CurrentPixel.R, CurrentPixel.G, CurrentPixel.B));
                }
                else
                {
                    CurrentPixel.A = Pick(255-Grscl(CurrentPixel.R, CurrentPixel.G, CurrentPixel.B));
                }
            }
            dst[x,y] = CurrentPixel;
        }
    }
}

Basically, you changed the loops are are iterating over the entire selection instead of only the rectangle passed to you by Paint.NET.

You should probably review the tutorials starting here:

http://boltbait.com/pdn/CodeLab/help/overview.php

There are other ways to speed this up even further, but I don't think you'll need to go any further.

BTW, I have a very similar plugin in my pack called Effects > Object > Switch Gray to Alpha. It's just not adjustable. Try it on a black-to-white gradient to see what it does.

Link to comment
Share on other sites

Yeah it's faster than before. Thanks for help!

By the way, when I have two different plugins installed, there are actually two "same" plugins in the menu. Same title and same icon.

Both of them are made by me with CodeLab, and I have no idea why this could happen.

Link to comment
Share on other sites

Time to clean out your plugin folder.

I don't get it. Here's what's actually happening.

O9e1mhm.png

There are two "duplicated" alpha filters, but I have only one alpha filter installed.

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

×
×
  • Create New...