# 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!

##### Share on other sites

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

##### Share on other sites

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?

##### Share on other sites

Posted (edited)

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

G'MIC Filter Developer

##### 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;
}

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];
}
}
}
}
}

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×