|
-
Mar 17th, 2023, 04:47 PM
#1
Re: Problem sharpening semi-transparent image
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 19th, 2023, 09:06 PM
#2
Re: Problem sharpening semi-transparent image
Looking at that code, I would expect the Alpha is being stripped off, and all the transparent pixels would end up being black.
It appears you're converting the pixels to 24bpp which ignores the Alpha, when putting the pixels into a byte array.
That also explains why the destination offsets of 3,4,5 didn't make sense to me. I would have expected 4,5,6 or 5,6,7 depending on which of the four bytes was the alpha byte.
i.e. the first pixel would be at offsets 0,1,2,3 and the second at 4,5,6,7 and third at 8,... if there were 32-bits per pixel.
But with 24-bpp the first pixel would be at offset 0,1,2 and the second at 3,4,5 and 3,4,5 offsets is what is in your code for the WriteBytes.
So, the sharpening code is expecting 24-bit rgb, not 32-bit argb.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|