Inconsistent results with Marshal.ReadInt32.
The title may be confusing. Let me explain. I have the following method:
vb.net Code:
Sub SpeedStitchTest()
Drawn = False
bmpData = bmpMap.LockBits(New Rectangle(0, 0, bmpMap.Width, bmpMap.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppPArgb)
Dim data As BitmapData = bmpImage.LockBits(New Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
Dim AlphaInt As Int32 = Alpha.ToArgb
Dim ImageColorInt As Int32 = 0
Dim MapColorInt As Int32 = 0
Dim ShortCircuit As Boolean = False
Dim sw As Stopwatch = Stopwatch.StartNew
For CanvasY = 0 To bmpMap.Height - bmpImage.Height
For CanvasX = 0 To bmpMap.Width - bmpImage.Width
If Drawn Then
Exit For
End If
MatchCount = 0
PixelCount = bmpImage.Width * bmpImage.Height
For nY = 0 To bmpImage.Height - 1
If ShortCircuit Then
ShortCircuit = False
Exit For
End If
For nX = 0 To bmpImage.Width - 1
' Read image pixel.
ImageColorInt = Marshal.ReadInt32(data.Scan0, (data.Stride * nY) + (4 * nX))
' Discount alpha pixels.
If ImageColorInt = AlphaInt Then
PixelCount -= 1
Else
' Read map pixel.
MapColorInt = Marshal.ReadInt32(bmpData.Scan0, (bmpData.Stride * (CanvasY + nY)) + ((CanvasX + nX) * 4))
' Discount alpha pixels.
If MapColorInt = AlphaInt Then
PixelCount -= 1
Else
' If the pixels match, increase the match count.
If ImageColorInt = MapColorInt Then
MatchCount += 1
Else
ShortCircuit = True
End If
End If
End If
Next
Next
' Check the match %.
HighestMatch = Math.Max(HighestMatch, (MatchCount / PixelCount) * 100)
Next
Next
MessageBox.Show(CStr(sw.ElapsedMilliseconds))
MessageBox.Show(CStr(HighestMatch))
HighestMatch = 0
bmpImage.UnlockBits(data)
bmpMap.UnlockBits(bmpData)
End Sub
Both images I use (bmpMap and bmpImage) are from the exact same file (PNG file, 960 x 976). When the method runs for the first time, the match percent is 100, because they are the same image. However, when it runs a second time, immediately after the first time, I get 9.43 (rounded) as a match percent. I am unsure why. To my knowledge, nothing is being changed and the method is running twice, back to back without anything running between. The pixel it gets to is (537, 91), if that matters.
Any ideas what may be happening?