1: /// <summary>
2: /// Does turbulence manipulation of the image
3: /// </summary>
4: /// <param name="OriginalImage">Image to transform</param>
5: /// <param name="Roughness">Roughness of the movement</param>
6: /// <param name="Power">How strong the movement is</param>
7: /// <param name="Seed">Random seed</param>
8: /// <returns>A bitmap object containing the new image</returns>
9: public static Bitmap Turbulence(Bitmap OriginalImage, int Roughness, float Power, int Seed)
10: {
11: int Width = OriginalImage.Width;
12: int Height = OriginalImage.Height;
13: BitmapData OriginalData = Image.LockImage(OriginalImage);
14: int OriginalPixelSize = Image.GetPixelSize(OriginalData);
15: Bitmap ReturnValue = new Bitmap(Width, Height);
16: BitmapData ReturnData = Image.LockImage(ReturnValue);
17: int ReturnPixelSize = Image.GetPixelSize(ReturnData);
18: using (Bitmap XNoise = PerlinNoise.Generate(Width, Height, 255, 0, 0.0625f, 1.0f, 0.5f, Roughness, Seed))
19: {
20: BitmapData XNoiseData = Image.LockImage(XNoise);
21: int XNoisePixelSize = Image.GetPixelSize(XNoiseData);
22: using (Bitmap YNoise = PerlinNoise.Generate(Width, Height, 255, 0, 0.0625f, 1.0f, 0.5f, Roughness, Seed * 2))
23: {
24: BitmapData YNoiseData = Image.LockImage(YNoise);
25: int YNoisePixelSize = Image.GetPixelSize(YNoiseData);
26: for (int y = 0; y < Height; ++y)
27: {
28: for (int x = 0; x < Width; ++x)
29: {
30: float XDistortion = x + (GetHeight(x, y, XNoiseData, XNoisePixelSize) * Power);
31: float YDistortion = y + (GetHeight(x, y, YNoiseData, YNoisePixelSize) * Power);
32: int X1 = Math.MathHelper.Clamp((int)XDistortion, Width - 1, 0);
33: int Y1 = Math.MathHelper.Clamp((int)YDistortion, Height - 1, 0);
34: Image.SetPixel(ReturnData, x, y, GetPixel(OriginalData, X1, Y1, OriginalPixelSize), ReturnPixelSize);
35: }
36: }
37: UnlockImage(YNoise, YNoiseData);
38: }
39: UnlockImage(XNoise, XNoiseData);
40: }
41: UnlockImage(ReturnValue, ReturnData);
42: UnlockImage(OriginalImage, OriginalData);
43: return ReturnValue;
44: }