八、抖动
1 public static bool Jitter(Bitmap b, int Offset)
2 {
3 BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
4 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
5 int stride = bmData.Stride;
6 System.IntPtr Scan0 = bmData.Scan0;
7 unsafe
8 {
9 byte * p = (byte *)(void *)Scan0;
10 byte * p1 = p;
11 int nOffset = stride - b.Width*3;
12 Random rad = new Random();
13 for(int y=0;y<b.Height;++y)
14 {
15 for(int x=0; x < b.Width; ++x )
16 {
17 int xIn = rad.Next(2 * Offset + 1) - Offset + x;
18 int yIn = rad.Next(2 * Offset + 1) - Offset + y;
19 if((xIn >= 0) && (yIn >= 0) && (xIn < b.Width) && (yIn < b.Height))
20 {
21 byte * pTemp = p1 + stride * yIn + xIn * 3;
22 for (int i = 0; i < 3; i++, p++)
23 {
24 *p = pTemp[i];
25 }
26 }
27 else
28 {
29 p += 3;
30 }
31 }
32 p += nOffset;
33 }
34 }
35 b.UnlockBits(bmData);
36 return true;
37 }
2 {
3 BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
4 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
5 int stride = bmData.Stride;
6 System.IntPtr Scan0 = bmData.Scan0;
7 unsafe
8 {
9 byte * p = (byte *)(void *)Scan0;
10 byte * p1 = p;
11 int nOffset = stride - b.Width*3;
12 Random rad = new Random();
13 for(int y=0;y<b.Height;++y)
14 {
15 for(int x=0; x < b.Width; ++x )
16 {
17 int xIn = rad.Next(2 * Offset + 1) - Offset + x;
18 int yIn = rad.Next(2 * Offset + 1) - Offset + y;
19 if((xIn >= 0) && (yIn >= 0) && (xIn < b.Width) && (yIn < b.Height))
20 {
21 byte * pTemp = p1 + stride * yIn + xIn * 3;
22 for (int i = 0; i < 3; i++, p++)
23 {
24 *p = pTemp[i];
25 }
26 }
27 else
28 {
29 p += 3;
30 }
31 }
32 p += nOffset;
33 }
34 }
35 b.UnlockBits(bmData);
36 return true;
37 }