Jump to content

Scaling an image with a plugin


Recommended Posts

Hi, right now I'm making a plugin that needs to clone and scale an image multiple times by a pixel or so. Doing it without CodeLab, and I have no clue about the math behind that. If anybody could help, that would be great!

Link to comment
Share on other sites

1 minute ago, toe_head2001 said:

Are you aware a plugin can not change the size of the canvas?

Yes, I am. I'm attempting to "zoom out" an image, similar to the rotate/zoom effect. (Minus the rotation, of course). Would that be possible?

Link to comment
Share on other sites

Here's the codelab code for starters. You can use 1/zoom instead for zoom out.

 

#region UICode
DoubleSliderControl zoom = 4; // [1,10] Zoom Level
#endregion
void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need
    Rectangle selection = EnvironmentParameters.SelectionBounds;
    double cx = (src.Width - 1) / 2;
    double cy = (src.Height - 1) / 2;

    double ix,iy,nx,ny,tx,ty;
    int fx,fy;

    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ix=(double)(x)-cx;
            iy=(double)(y)-cy;
            nx=ix*zoom;
            ny=iy*zoom;
            tx=nx-ix;
            ty=ny-iy;
            fx=Math.Min(Math.Max(x+(int)(tx),0),src.Width - 1 );
            fy=Math.Min(Math.Max(y+(int)(ty),0),src.Height - 1 );
            dst[x,y] = src[fx,fy];
        }
    }
}

 

Edited by Reptillian
  • Thanks 1

G'MIC Filter Developer

Link to comment
Share on other sites

Alright, tried implementing it in a for loop... Now it's colliding with itself! Help is appreciated. (Don't mind the other variables, I'm also making a "trail" effect with some of pyrochild's code as reference.)

        protected unsafe override void OnRender(Rectangle[] renderRects, int startIndex, int length)
        {
            Surface destination = DstArgs.Surface;
            Surface source = SrcArgs.Surface;
            Rectangle bounds = SrcArgs.Bounds;

            Surface customSurface = new Surface(source.Width, source.Height);
            customSurface.FitSurface(ResamplingAlgorithm.Fant, source);
            
            destination.CopySurface(source, renderRects);

            double cx = (source.Width - 1) / 2;
            double cy = (source.Height - 1) / 2;

            double ix, iy, nx, ny, tx, ty;
            int fx, fy;

            foreach (Rectangle rect in renderRects)
            {
                for (
                    double xOffset = 0, yOffset = 0;
                    xOffset <= DistanceX && yOffset <= DistanceY;
                    xOffset += SpacingX, yOffset += SpacingY
                    )
                {
                    if (IsCancelRequested) return;

                    float xDirectionOffset = -(float)xOffset * DirectionX;
                    float yDirectionOffset = -(float)yOffset * DirectionY;

                    for (int y = rect.Top; y < rect.Bottom; ++y)
                    {
                        float sourceY = y + yDirectionOffset;
                        if (!(bounds.Top <= sourceY && sourceY < bounds.Bottom))
                        {
                            continue;
                        }
                        ColorBgra* destinationPoint = destination.GetPointAddressUnchecked(rect.Left, y);
                        ColorBgra* sourcePoint = source.GetPointAddressUnchecked(0, (int)sourceY);

                        for (int x = rect.Left; x < rect.Right; ++x)
                        {
                            double zoom = 1 + ((x + y) * 0.001);
                            ix = (double)(x) - cx;
                            iy = (double)(y) - cy;
                            nx = ix * zoom;
                            ny = iy * zoom;
                            tx = nx - ix;
                            ty = ny - iy;
                            fx = Math.Min(Math.Max(x + (int)(tx), 0), destination.Width - 1);
                            fy = Math.Min(Math.Max(y + (int)(ty), 0), destination.Height - 1);
                            destination[x, y] = destination[fx, fy];
                        }
                    }
                }
            }
        }

 

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.

 Share

×
×
  • Create New...