This code is extremely slow and ideally I would appreciate someone walking me through getting this going on the GPU, but if nobody has time for that which is understandable, any and all optimisations would be most welcome!
Private Sub Picture(source As Image, picturebox As PictureBox)
Dim r As Double
Dim g As Double
Dim b As Double
Dim bmp As New Bitmap(source)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
Dim bytes As Integer = bmpData.Stride * bmp.Height
Dim rgbValues As Byte() = New Byte(bytes - 1) {}
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
Dim linWhiteMult = 1 / (((LinearWhiteTextBox.Text * ((ShoulderStrengthTextBox.Text * LinearWhiteTextBox.Text) + (LinearAngleTextBox.Text * LinearStrengthTextBox.Text)) + (ToeStrengthTextBox.Text * ToeNumeratorTextBox.Text)) _
/ (LinearWhiteTextBox.Text * ((ShoulderStrengthTextBox.Text * LinearWhiteTextBox.Text) + LinearStrengthTextBox.Text) + ToeStrengthTextBox.Text * ToeDenominatorTextBox.Text) - (ToeNumeratorTextBox.Text / ToeDenominatorTextBox.Text)))
'precalculations for optimisation
Dim LaLs = LinearAngleTextBox.Text * LinearStrengthTextBox.Text
Dim TsTn = ToeStrengthTextBox.Text * ToeNumeratorTextBox.Text
Dim TsTd = ToeStrengthTextBox.Text * ToeDenominatorTextBox.Text
Dim Tn_Td = ToeNumeratorTextBox.Text / ToeDenominatorTextBox.Text
Dim list As New List(Of String)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim position As Integer = (y * bmpData.Stride) + (x * Image.GetPixelFormatSize(bmpData.PixelFormat) / 8)
b = rgbValues(position)
g = rgbValues(position + 1)
r = rgbValues(position + 2)
'^2.2 lookup table (for optimisation) for gamma correction
b = table.Item(b)
g = table.Item(g)
r = table.Item(r)
'the following is the filmic tone map formula, i.e.
'x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F)) - E/F
r = (((r * ((ShoulderStrengthTextBox.Text * r) + (LaLs)) + (TsTn)) _
/ (r * ((ShoulderStrengthTextBox.Text * r) + LinearStrengthTextBox.Text) + TsTd) - (Tn_Td))) * linWhiteMult
g = (((g * ((ShoulderStrengthTextBox.Text * g) + (LaLs)) + (TsTn)) _
/ (g * ((ShoulderStrengthTextBox.Text * g) + LinearStrengthTextBox.Text) + TsTd) - (Tn_Td))) * linWhiteMult
b = (((b * ((ShoulderStrengthTextBox.Text * b) + (LaLs)) + (TsTn)) _
/ (b * ((ShoulderStrengthTextBox.Text * b) + LinearStrengthTextBox.Text) + TsTd) - (Tn_Td))) * linWhiteMult
If r > 1 Then
r = 1
End If
If g > 1 Then
g = 1
End If
If b > 1 Then
b = 1
End If
rgbValues(position) = Math.Round((b ^ 0.45) * 255)
rgbValues(position + 1) = Math.Round((g ^ 0.45) * 255)
rgbValues(position + 2) = Math.Round((r ^ 0.45) * 255)
Next
Next
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
bmp.UnlockBits(bmpData)
' apply the bmp to the avatars and dispose old
Dim OldImage = picturebox.Image
picturebox.Image = bmp
OldImage.Dispose()
End Sub