Random Jitter
This algorithm moves each pixel of a source image in random direction within a window of specified radius.
public static void ApplyRandomJitter(ref Bitmap bmp, short degree)
{
Bitmap TempBmp = (Bitmap)bmp.Clone();
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData TempBmpData = TempBmp.LockBits(new Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
byte* TempPtr = (byte*)TempBmpData.Scan0.ToPointer();
int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;
int BmpWidth = bmp.Width;
int BmpHeight = bmp.Height;
int BmpStride = bmpData.Stride;
int i = 0, X = 0, Y = 0;
int Val = 0, XVal = 0, YVal = 0;
short Half = (short)(degree / 2);
Random rand = new Random();
while ((int)ptr != stopAddress)
{
X = i % BmpWidth;
Y = i / BmpWidth;
XVal = X + (rand.Next(degree) - Half);
YVal = Y + (rand.Next(degree) - Half);
if (XVal > 0 && XVal < BmpWidth && YVal > 0 && YVal < BmpHeight)
{
Val = (YVal * BmpStride) + (XVal * 3);
ptr[0] = TempPtr[Val];
ptr[1] = TempPtr[Val + 1];
ptr[2] = TempPtr[Val + 2];
}
ptr += 3;
i++;
}
}
bmp.UnlockBits(bmpData);
TempBmp.UnlockBits(TempBmpData);
}
Example
Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyRandomJitter(ref b, 20);