Task: Bilinear interpolation





Java: import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BilinearInterpolation {
    
    private static int get(int self, int n) {
        return (self >> (n * 8)) & 0xFF;
    }

    private static float lerp(float s, float e, float t) {
        return s + (e - s) * t;
    }

    private static float blerp(final Float c00, float c10, float c01, float c11, float tx, float ty) {
        return lerp(lerp(c00, c10, tx), lerp(c01, c11, tx), ty);
    }

    private static BufferedImage scale(BufferedImage self, float scaleX, float scaleY) {
        int newWidth = (int) (self.getWidth() * scaleX);
        int newHeight = (int) (self.getHeight() * scaleY);
        BufferedImage newImage = new BufferedImage(newWidth, newHeight, self.getType());
        for (int x = 0; x < newWidth; ++x) {
            for (int y = 0; y < newHeight; ++y) {
                float gx = ((float) x) / newWidth * (self.getWidth() - 1);
                float gy = ((float) y) / newHeight * (self.getHeight() - 1);
                int gxi = (int) gx;
                int gyi = (int) gy;
                int rgb = 0;
                int c00 = self.getRGB(gxi, gyi);
                int c10 = self.getRGB(gxi + 1, gyi);
                int c01 = self.getRGB(gxi, gyi + 1);
                int c11 = self.getRGB(gxi + 1, gyi + 1);
                for (int i = 0; i <= 2; ++i) {
                    float b00 = get(c00, i);
                    float b10 = get(c10, i);
                    float b01 = get(c01, i);
                    float b11 = get(c11, i);
                    int ble = ((int) blerp(b00, b10, b01, b11, gx - gxi, gy - gyi)) << (8 * i);
                    rgb = rgb | ble;
                }
                newImage.setRGB(x, y, rgb);
            }
        }
        return newImage;
    }

    public static void main(String[] args) throws IOException {
        File lenna = new File("Lenna100.jpg");
        BufferedImage image = ImageIO.read(lenna);
        BufferedImage image2 = scale(image, 1.6f, 1.6f);
        File lenna2 = new File("Lenna100_larger.jpg");
        ImageIO.write(image2, "jpg", lenna2);
    }
}




VB: Imports System.Drawing

Module Module1

    Function Lerp(s As Single, e As Single, t As Single) As Single
        Return s + (e - s) * t
    End Function

    Function Blerp(c00 As Single, c10 As Single, c01 As Single, c11 As Single, tx As Single, ty As Single) As Single
        Return Lerp(Lerp(c00, c10, tx), Lerp(c01, c11, tx), ty)
    End Function

    Function Scale(self As Bitmap, scaleX As Single, scaleY As Single) As Image
        Dim newWidth = CInt(Math.Floor(self.Width * scaleX))
        Dim newHeight = CInt(Math.Floor(self.Height * scaleY))
        Dim newImage As New Bitmap(newWidth, newHeight, self.PixelFormat)

        For x = 0 To newWidth - 1
            For y = 0 To newHeight - 1
                Dim gx = CSng(x) / newWidth * (self.Width - 1)
                Dim gy = CSng(y) / newHeight * (self.Height - 1)
                Dim gxi = CInt(Math.Floor(gx))
                Dim gyi = CInt(Math.Floor(gy))
                Dim c00 = self.GetPixel(gxi, gyi)
                Dim c10 = self.GetPixel(gxi + 1, gyi)
                Dim c01 = self.GetPixel(gxi, gyi + 1)
                Dim c11 = self.GetPixel(gxi + 1, gyi + 1)

                Dim red = CInt(Blerp(c00.R, c10.R, c01.R, c11.R, gx - gxi, gy - gyi))
                Dim green = CInt(Blerp(c00.G, c10.G, c01.G, c11.G, gx - gxi, gy - gyi))
                Dim blue = CInt(Blerp(c00.B, c10.B, c01.B, c11.B, gx - gxi, gy - gyi))
                Dim rgb = Color.FromArgb(red, green, blue)

                newImage.SetPixel(x, y, rgb)
            Next
        Next

        Return newImage
    End Function

    Sub Main()
        Dim newImage = Image.FromFile("Lenna100.jpg")
        If TypeOf newImage Is Bitmap Then
            Dim oi As Bitmap = newImage
            Dim result = Scale(oi, 1.6, 1.6)
            result.Save("Lenna100_larger.jpg")
        Else
            Console.WriteLine("Could not open the source file.")
        End If
    End Sub

End Module