Random Jitter

This algorithm moves each pixel of a source image in random direction within a window of specified radius.

   



									Public Shared Sub ApplyRandomJitter(ByRef bmp As Bitmap, degree As Short)
	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 IntPtr = bmpData.Scan0
	Dim TempPtr As IntPtr = TempBmpData.Scan0

	Dim stopAddress As Integer = CInt(ptr) + bmpData.Stride * bmpData.Height

	Dim BmpWidth As Integer = bmp.Width
	Dim BmpHeight As Integer = bmp.Height
	Dim BmpStride As Integer = bmpData.Stride
	Dim i As Integer = 0, X As Integer = 0, Y As Integer = 0
	Dim Val As Integer = 0, XVal As Integer = 0, YVal As Integer = 0
	Dim Half As Short = CShort(degree \ 2)
	Dim rand As New Random()

	While CInt(ptr) <> stopAddress
		X = i Mod BmpWidth
		Y = i \ BmpWidth

		XVal = X + (rand.[Next](degree) - Half)
		YVal = Y + (rand.[Next](degree) - Half)

		If XVal > 0 AndAlso XVal < BmpWidth AndAlso YVal > 0 AndAlso YVal < BmpHeight Then
			Val = (YVal * BmpStride) + (XVal * 3)

			Marshal.WriteByte(ptr, Marshal.ReadByte(TempPtr + Val))
			Marshal.WriteByte(ptr + 1, Marshal.ReadByte(TempPtr + Val + 1))
			Marshal.WriteByte(ptr + 2, Marshal.ReadByte(TempPtr + Val + 2))
		End If

		ptr += 3
		i += 1
	End While

	bmp.UnlockBits(bmpData)
	TempBmp.UnlockBits(TempBmpData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyRandomJitter(b, 20)