# Image Scaling

Sometimes, we’ll have to scale an image to fit into an article or enlarge and image to have a better view on the smaller aspects in an image.

There’re a few methods of image scaling methods out there, some of them are: nearest neighbour replication, bilinear interpolation and bicubic interpolation. The method that I’ll be showing here is Bilinear Interpolation which is a widely used method.

The main formula for bilinear interpolation is:
$v(x,y) = ax + by + cxy +d$

Which is derived from the linear interpolation of a straight line.

We have Y in between 2 pixels (A and B), from that we can derive the equation

We know that an image exist in a 2D space, thus in a 2D space we have:

Same as above, we’ll apply the same formula mentioned above to derive the equation from i and j with respective to A&B and C&D respectively, thus we have:

and

Thus with i and j we can apply the same formula for Y:

After some algebraic manipulation of substituting equation 1 and 2 into 3 we get:

Here we have the same formula for the main formula of bilinear interpolation we mentioned in the very beginning.

## Implementation

The method above will be applied for every channel of a pixel (R,G,B,Alpha) or (R,G,B) for 3 Bytes representation of a pixel.

A, B, C, D are from the image to be scaled. They are all neighbouring pixels with a distance of 1.

Y is the resulting pixel of the scaled image.

w, h are the ratio of input image over scaled image width and height.

Plug them into the equation and we’ll get our desired result!

Here is the source code of my implementation: Image Scaling