Estorva Posted August 17, 2014 Share Posted August 17, 2014 (edited) 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: 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 August 17, 2014 by Estorva Quote Link to comment Share on other sites More sharing options...
midora Posted August 17, 2014 Share Posted August 17, 2014 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. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted August 17, 2014 Share Posted August 17, 2014 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.); } else { CurrentPixel.A = Pick(255-Grscl(CurrentPixel.R, CurrentPixel.G, CurrentPixel.); } } 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. Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
Estorva Posted August 23, 2014 Author Share Posted August 23, 2014 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. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted August 23, 2014 Share Posted August 23, 2014 Time to clean out your plugin folder. Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
Estorva Posted August 23, 2014 Author Share Posted August 23, 2014 Time to clean out your plugin folder. I don't get it. Here's what's actually happening. There are two "duplicated" alpha filters, but I have only one alpha filter installed. Quote Link to comment Share on other sites More sharing options...
TechnoRobbo Posted August 23, 2014 Share Posted August 23, 2014 Same plugin with two different DLL names Look for similar names with slight variations in your effects folder Delete the older one Quote Go out there and be amazing. Have Fun, TRSome Pretty Pictures Some Cool Plugins Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.