# Bilinear Interpolation Scaling help

• 06-15-2010, 12:07 AM
Xpectro
Bilinear Interpolation Scaling help
Hi,

I'm trying to program a class for a project, that reads a picture as BufferedImage, and resize it using Bilinear Interpolation (Using the mathematical formulas, not the methods included in java). I almost sure that the Bilinear Interpolation part of the method is done, but after many thinking, searching and losing hope, I just can't figure out how to apply the scaling part to the method.

Here's the method:

Code:

```public static BufferedImage resize(BufferedImage image, int w2, int h2) {   BufferedImage outputImage = new BufferedImage( w2, h2, BufferedImage.TYPE_INT_RGB );   // Ratio between original picture and resized picture float x_ratio = ((float) (image.getWidth()-1))/w2 ; float y_ratio = ((float) (image.getHeight()-1))/h2;   // Four pixels a,b,c,d int a, b, c, d, i, j, color; float alpha, beta, xabRed, xabGreen, xabBlue, xcdRed, xcdGreen, xcdBlue, xfinalRed, xfinalGreen, xfinalBlue; for (int x = 0; x < image.getHeight()-1; x++)   for( int y = 0; y < image.getWidth()-1; y++){ //Find the color values of the 4 pixels around the desired point to interpolate   a = image.getRGB(x, y);   b = image.getRGB(x, y + 1);   c = image.getRGB(x + 1, y);   d = image.getRGB(x + 1, y + 1);   i = (int) (x_ratio * x);   j = (int) (y_ratio * y);   alpha = (x_ratio * x) - i;   beta = (y_ratio * y) - j; // a,b represents 2 of the 4 points for interpolation   xabRed = alpha * ((b >> 16)&0xff) + (1 - alpha) * ((a >> 16)&0xff);   xabGreen = alpha * ((b >> 8)&0xff) + (1 - alpha) * ((a >> 8)&0xff);   xabBlue = alpha * (b &0xff) + (1 - alpha) * (a &0xff); // c,d represents 2 of the 4 points for interpolation   xcdRed = alpha * ((d >> 16)&0xff) + (1 - alpha) * ((c >> 16)&0xff);   xcdGreen = alpha * ((d >> 8)&0xff) + (1 - alpha) * ((c >> 8)&0xff);   xcdBlue = alpha * (d &0xff) + (1 - alpha) * (c &0xff); // Desired point   xfinalRed = beta * xcdRed + (1 - beta) * xabRed;   xfinalGreen = beta * xcdGreen + (1 - beta) * xabGreen;      xfinalBlue = beta * xcdBlue + (1 - beta) * xabBlue; // Prints the RGB values, for bound confirmation    System.out.println((int) xfinalRed + " " + (int) xfinalGreen + " " + (int) xfinalBlue); // Sets the new color value for the pixel   color = new Color((int)xfinalRed, (int)xfinalGreen, (int)xfinalBlue).getRGB(); // Set the pixel   outputImage.setRGB(x, y, color); }  return outputImage; }```

```for(int y = 0; y < outputImage.getHeight(); y++) {     for(int x = 0; x < outputImage.getWidth(); x++) {         //the point in the original image that maps to (x,y)         double p = x/scaleX;         double q = y/scaleY;         //find the four points that surround (p,q)         ...         //do the bilinear interpolation         ...                 //store the results in (x,y)         int color = (255<<24)|(xfinalRed<<16)|(xfinalGreen<<8)|(xfinalBlue);         outputImage.setRGB(x, y, color);     } }```