Task: Bilinear interpolation



C#: using System;
using System.Drawing;

namespace BilinearInterpolation {
    class Program {
        private static float Lerp(float s, float e, float t) {
            return s + (e - s) * t;
        }

        private static float Blerp(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 Image Scale(Bitmap self, float scaleX, float scaleY) {
            int newWidth = (int)(self.Width * scaleX);
            int newHeight = (int)(self.Height * scaleY);
            Bitmap newImage = new Bitmap(newWidth, newHeight, self.PixelFormat);

            for (int x = 0; x < newWidth; x++) {
                for (int y = 0; y < newHeight; y++) {
                    float gx = ((float)x) / newWidth * (self.Width - 1);
                    float gy = ((float)y) / newHeight * (self.Height - 1);
                    int gxi = (int)gx;
                    int gyi = (int)gy;
                    Color c00 = self.GetPixel(gxi, gyi);
                    Color c10 = self.GetPixel(gxi + 1, gyi);
                    Color c01 = self.GetPixel(gxi, gyi + 1);
                    Color c11 = self.GetPixel(gxi + 1, gyi + 1);

                    int red = (int)Blerp(c00.R, c10.R, c01.R, c11.R, gx - gxi, gy - gyi);
                    int green = (int)Blerp(c00.G, c10.G, c01.G, c11.G, gx - gxi, gy - gyi);
                    int blue = (int)Blerp(c00.B, c10.B, c01.B, c11.B, gx - gxi, gy - gyi);
                    Color rgb = Color.FromArgb(red, green, blue);
                    newImage.SetPixel(x, y, rgb);
                }
            }

            return newImage;
        }

        static void Main(string[] args) {
            Image newImage = Image.FromFile("Lenna100.jpg");
            if (newImage is Bitmap oi) {
                Image result = Scale(oi, 1.6f, 1.6f);
                result.Save("Lenna100_larger.jpg");
            } else {
                Console.WriteLine("Could not open the source file.");
            }
        }
    }
}






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