MJW

Mismatch Eraser

Recommended Posts

This is a very simple plugin originally intended to erase pixels in the canvas that don't match the clipboard, but extended so that it can optionally keep them instead. The clipboard image must be the same size as the canvas; otherwise, the effect does nothing. For no particularly good reason, other than I couldn't think of anything better, it's in the Color menu.

 

The plugin: MismatchEraser.zip

 

It's a CodLab plugin. Here is the source code:

Spoiler

// Author: MJW
// Name: Mismatch Eraser
// Title: Mismatch Eraser
// Submenu: color
// Desc: Keep or erase pixels that match the clipboard.
// Keywords: erase mismatch clipboard
#region UICode
RadioButtonControl Amount1 = 0; // [1] Action|Keep Pixels That Match Clipboard|Erase Pixels That Match Clipboard
#endregion

// Here is the main render loop function
void Render(Surface dst, Surface src, Rectangle rect)
{
    bool haveCbImage = (img != null) && (src.Width == img.Width) && (src.Height == img.Height);
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra CbPixel;
            ColorBgra CurrentPixel = src[x, y];
            if (IsCancelRequested) return;
            // If clipboard has an image, get it. Check every time.
            if (haveCbImage)
                haveCbImage = (img != null);          
            CbPixel = haveCbImage ? img[x, y] : CurrentPixel;
            if (Amount1 == 0)
                dst[x, y] = (CurrentPixel == CbPixel) ? CurrentPixel : ColorBgra.Transparent;
            else
                dst[x, y] = (CurrentPixel != CbPixel) ? CurrentPixel : ColorBgra.Transparent;
        }
    }
}

// Setup for getting an image from the clipboard
protected Surface img
{
    get
    {
        if (_img != null)
            return _img;
        else
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(GetImageFromClipboard));
            t.SetApartmentState(System.Threading.ApartmentState.STA);
            t.Start();
            t.Join();
            return _img;
        }
    }
}
private Surface _img = null;
private void GetImageFromClipboard()
{
    Bitmap aimg = null;
    System.Windows.Forms.IDataObject clippy;
    try
    {
        clippy = System.Windows.Forms.Clipboard.GetDataObject();
        if (clippy != null)
        {
            if (System.Windows.Forms.Clipboard.ContainsData("PNG"))
            {
                // Handle bitmaps with transparency
                Object png_object = System.Windows.Forms.Clipboard.GetData("PNG");
                if (png_object is System.IO.MemoryStream)
                {
                    System.IO.MemoryStream png_stream = png_object as System.IO.MemoryStream;
                    aimg = (Bitmap)Image.FromStream(png_stream);
                }
            }
            else if (clippy.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
            {
                // If that didn't work, try bitmaps without transparency
                aimg = (Bitmap)clippy.GetData(typeof(System.Drawing.Bitmap));
            }
        }
    }
    catch (Exception)
    {
    }
    if (aimg != null)
    {
        _img = Surface.CopyFromBitmap(aimg);
    }
    else
    {
        _img = null;
    }
}

 

 

 

  • Like 3
  • Upvote 2

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