Jump to content

Scaling an image with a plugin


Recommended Posts

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

 

I am away from this forum for undetermined amount of time: If you really need anything related to my PDN plugin or my G'MIC filter within G'MIC plugin, then you can contact me via Paint.NET discord, and mention me.

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.

×
×
  • Create New...