Here's the test project. I've tidied the code significantly, making it error free was a task and a half
Sharpen_Image_Test.zip
The relevant code...
Code:Public Sub Convolution3x3(ByRef bmp As Bitmap) Dim Factor As Integer = Matrix.Factor If Factor = 0 Then Return End If Dim TopLeft As Integer = Matrix.TopLeft Dim TopMid As Integer = Matrix.TopMid Dim TopRight As Integer = Matrix.TopRight Dim MidLeft As Integer = Matrix.MidLeft Dim MidRight As Integer = Matrix.MidRight Dim BottomLeft As Integer = Matrix.BottomLeft Dim BottomMid As Integer = Matrix.BottomMid Dim BottomRight As Integer = Matrix.BottomRight Dim Pixel As Integer = Matrix.Pixel Dim Offset As Integer = Matrix.Offset Dim TempBmp As Bitmap = DirectCast(bmp.Clone(), Bitmap) Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb) Dim TempBmpData As BitmapData = TempBmp.LockBits(New Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb) Dim ptr As Integer = bmpData.Scan0.ToInt32 Dim TempPtr As Integer = TempBmpData.Scan0.ToInt32 Dim Pix As Integer = 0 Dim Stride As Integer = bmpData.Stride Dim DoubleStride As Integer = Stride * 2 Dim Width As Integer = bmp.Width - 2 Dim Height As Integer = bmp.Height - 2 Dim stopAddress As Integer = ptr + bmpData.Stride * bmpData.Height For y As Integer = 0 To Height - 1 For x As Integer = 0 To Width - 1 Pix = (((Marshal.ReadByte(New IntPtr((TempPtr + 2))) * TopLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 5))) * TopMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 8))) * TopRight + _ Marshal.ReadByte(New IntPtr((TempPtr + 2 + Stride))) * MidLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 5 + Stride))) * Pixel + _ Marshal.ReadByte(New IntPtr((TempPtr + 8 + Stride))) * MidRight + _ Marshal.ReadByte(New IntPtr((TempPtr + 2 + DoubleStride))) * BottomLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 5 + DoubleStride))) * BottomMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 8 + DoubleStride))) * BottomRight) \ _ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If 'Dim b As Byte = Marshal.ReadByte(New IntPtr(ptr + 5 + Stride)) 'If b Then ' how to detect if dest pixel is transparent??? Marshal.WriteByte(New IntPtr(ptr + 5 + Stride), CByte(Pix)) Pix = (((Marshal.ReadByte(New IntPtr((TempPtr + 1))) * TopLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 4))) * TopMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 7))) * TopRight + _ Marshal.ReadByte(New IntPtr((TempPtr + 1 + Stride))) * MidLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 1 + Stride))) * Pixel + _ Marshal.ReadByte(New IntPtr((TempPtr + 4 + Stride))) * MidRight + _ Marshal.ReadByte(New IntPtr((TempPtr + 1 + DoubleStride))) * BottomLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 4 + DoubleStride))) * BottomMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 7 + DoubleStride))) * BottomRight) \ _ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If 'b = Marshal.ReadByte(New IntPtr(ptr + 4 + Stride)) 'If b Then ' how to detect if dest pixel is transparent??? Marshal.WriteByte(New IntPtr(ptr + 4 + Stride), CByte(Pix)) Pix = (((Marshal.ReadByte(New IntPtr((TempPtr))) * TopLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 3))) * TopMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 6))) * TopRight + _ Marshal.ReadByte(New IntPtr((TempPtr + Stride))) * MidLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 3 + Stride))) * Pixel + _ Marshal.ReadByte(New IntPtr((TempPtr + 6 + Stride))) * MidRight + _ Marshal.ReadByte(New IntPtr((TempPtr + DoubleStride))) * BottomLeft + _ Marshal.ReadByte(New IntPtr((TempPtr + 3 + DoubleStride))) * BottomMid + _ Marshal.ReadByte(New IntPtr((TempPtr + 6 + DoubleStride))) * BottomRight) \ _ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If 'b = Marshal.ReadByte(New IntPtr(ptr + 3 + Stride)) 'If b Then ' how to detect if dest pixel is transparent??? Marshal.WriteByte(New IntPtr(ptr + 3 + Stride), CByte(Pix)) ptr += 3 TempPtr += 3 Next Next bmp.UnlockBits(bmpData) TempBmp.UnlockBits(TempBmpData) End Sub





Reply With Quote