Jump to content

Subtracting from rect.Top


Recommended Posts

I'm try to get a my (smaller) 'img' Surface lined up with the selection of the canvas.

 

Say I have a selection start at point (50,50), and run this code:

...

img = Surface.CopyFromBitmap(myBitmap);

void Render(Surface dst, Surface src, Rectangle rect)
{
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            dst[x,y] = img.GetBilinearSample(x - rect.Left,y - rect.Top);
        }
    }

Substracting from 'rect.Left' on 'x' makes it start from 0 as expected, but subtracting from 'rect.Top' on y doesn't seem to work.

 

Can you explain why that is?

Link to comment
Share on other sites

rect is the ROI, not the selection. It works for X because for rectangular selections, the X origins of the ROIs coincide with the X origin of the selection.

 

Get the selection (as done in CodeLab) by: Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();

Then: dst[x,y] = img.GetBilinearSample(x - selection.Left,y - selection.Top);

Edited by MJW
  • Upvote 1
Link to comment
Share on other sites

rect is the ROI, not the selection. It works for X because for rectangular selections, the X origins of the ROIs coincide with the X origin of the selection.

 

Thanks for explaining this!

 

This whole time I thought the ROI was based on the selection. I never bothered reading that part of the code to understand what was going on. :roll:

Link to comment
Share on other sites

this may gave you some insight - run this, don't even build it , just paste it in CodeLab. Then try a selection.

// Name:
// Submenu:
// Author:
// Title:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
#endregion


Random rndm = new Random((int)Guid.NewGuid().GetHashCode());
void Render(Surface dst, Surface src, Rectangle rect)
{

    ColorBgra CurrentPixel = ColorBgra.FromBgr( (byte)rndm.Next(256),
        (byte)rndm.Next(256),(byte)rndm.Next(256));
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            dst[x,y] = CurrentPixel;
        }
    }
}
  • Upvote 1

Go out there and be amazing. Have Fun, TR
TRsSig.png?raw=1
Some Pretty Pictures Some Cool Plugins

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...