MJW Posted November 17, 2016 Share Posted November 17, 2016 This is a plugin I wrote a while ago, which I posted to a thread titled crop black and white images down to the smallest rectangular size that contains the black part. Apparently inspired by the thread's title, I chose the rather awkward name Unselected Rectangle Keeper. It's in the Selection submenu. It has no user interface. This is the plugin: UnselectedRectangleKeeper.zip The icon: What it does sounds kind of strange, but is useful for cropping images to the smallest region that doesn't match the surrounding color. If you have a rectangular selection which contains a non-selected region, it erases everything in the selection that's outside the bounding rectangle of the non-selected region: To crop to the smallest rectangle containing something not matching the color of the background: Use the Magic Wand with a low tolerance to select the outside color. Run Unselected Rectangle Keeper. Deselect. Use the Magic Wand to select the transparent region. Invert the selection. Crop to the selection. Example: Before (white region is selected). After. The code: Spoiler // Author: MJW // Name: Unselected Rectangle Keeper // Title: Unselected Rectangle Keeper // Submenu: Selection // Desc: Erase the area outside the bounding rectangle of the unselected interior. // Keywords: erase unselected region rectangle #region UICode #endregion private volatile bool firstTime = true; private object myLock = new object(); int left, right, top, bottom; void Render(Surface dst, Surface src, Rectangle rect) { PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds); Rectangle selection = selectionRegion.GetBoundsInt(); ColorBgra CurrentPixel; if (firstTime) lock (myLock) { if (firstTime) { int l = int.MaxValue, r = -1, t = int.MaxValue, b = -1; for (int y = selection.Top; y < selection.Bottom; y++) { for (int x = selection.Left; x < selection.Right; x++) { if (!selectionRegion.IsVisible(x, y)) { if (x < l) l = x; if (x > r) r = x; if (t == int.MaxValue) t = y; b = y; } } } right = r; left = l; top = t; bottom = b; firstTime = false; } } // Erase everything outside the bounding rectangle for the unselected region. for (int y = rect.Top; y < rect.Bottom; y++) { for (int x = rect.Left; x < rect.Right; x++) { ColorBgra pixel; if ((y < top) || (y > bottom) || (x < left) || (x > right)) pixel = ColorBgra.Transparent; else pixel = src[x, y]; dst[x, y] = pixel; } } } EDIT: Added link to plugin (which I completely forgot to do). Thanks for the reminder, lynxster4! EDIT 2: Added the source code. (Using the "firstTime" flag is no longer necessary. CodeLab now supports PreRender.) 4 Quote Link to comment Share on other sites More sharing options...
lynxster4 Posted November 17, 2016 Share Posted November 17, 2016 @MJW....maybe you should post the zip file above, so everyone doesn't have to go the link you provided? They can go to the link for more information. 1 Quote My Art Gallery | My Shape Packs | ShapeMaker Mini Tut | Air Bubble Stained Glass Chrome Text with Reflections | Porcelain Text w/ Variegated Coloring | Realistic Knit PatternOpalescent Stained Glass | Frosted Snowman Cookie | Leather Texture | Plastic Text | Silk Embroidery Visit my Personal Website "Never, ever lose your sense of humor - you'll live longer" Link to comment Share on other sites More sharing options...
Maximilian Posted February 18, 2017 Share Posted February 18, 2017 I've just stumbled upon this plugin and thought I'd provide a compilation for PdN 3.5.11 should anyone need it. Thanks for the code and icon, MJW!Unselected Rectangle Keeper for PdN 3.5.11.zip 1 Quote Link to comment Share on other sites More sharing options...
Seerose Posted February 18, 2017 Share Posted February 18, 2017 @MJW! Thank you so much for the plugin and your effort. or Sorry! Rep. is tomorrow. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi Link to comment Share on other sites More sharing options...
Seerose Posted February 18, 2017 Share Posted February 18, 2017 Dear @Maximilian! Thank you also for your effort. Sorry! Rep. is tomorrow. 1 Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi Link to comment Share on other sites More sharing options...
Maximilian Posted February 19, 2017 Share Posted February 19, 2017 Thank you, dear Seerose. Your kindness is much appreciated ? 1 Quote 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.