1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Collections.Generic;
25: using System.Linq;
26: using System.Text;
27: using System.Drawing;
28: using System.Drawing.Imaging;
29: #endregion
30:
31: namespace Utilities.Media.Image
32: {
33: /// <summary>
34: /// Perlin noise helper class
35: /// </summary>
36: public static class PerlinNoise
37: {
38: #region Functions
39:
40: /// <summary>
41: /// Generates perlin noise
42: /// </summary>
43: /// <param name="Width">Width of the resulting image</param>
44: /// <param name="Height">Height of the resulting image</param>
45: /// <param name="MaxRGBValue">MaxRGBValue</param>
46: /// <param name="MinRGBValue">MinRGBValue</param>
47: /// <param name="Frequency">Frequency</param>
48: /// <param name="Amplitude">Amplitude</param>
49: /// <param name="Persistance">Persistance</param>
50: /// <param name="Octaves">Octaves</param>
51: /// <param name="Seed">Random seed</param>
52: /// <returns>An image containing perlin noise</returns>
53: public static Bitmap Generate(int Width,int Height,int MaxRGBValue,int MinRGBValue,
54: float Frequency,float Amplitude,float Persistance,int Octaves,int Seed)
55: {
56: Bitmap ReturnValue = new Bitmap(Width, Height);
57: BitmapData ImageData = Image.LockImage(ReturnValue);
58: int ImagePixelSize = Image.GetPixelSize(ImageData);
59: float[,] Noise=GenerateNoise(Seed,Width,Height);
60: for (int x = 0; x < Width; ++x)
61: {
62: for (int y = 0; y < Height; ++y)
63: {
64: float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise);
65: Value = (Value * 0.5f) + 0.5f;
66: Value *= 255;
67: int RGBValue=Math.MathHelper.Clamp((int)Value, MaxRGBValue, MinRGBValue);
68: Image.SetPixel(ImageData, x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
69: }
70: }
71: Image.UnlockImage(ReturnValue, ImageData);
72: return ReturnValue;
73: }
74:
75: private static float GetValue(int X, int Y, int Width,int Height,float Frequency, float Amplitude,
76: float Persistance, int Octaves,float[,]Noise)
77: {
78: float FinalValue = 0.0f;
79: for (int i = 0; i < Octaves; ++i)
80: {
81: FinalValue += GetSmoothNoise(X * Frequency, Y * Frequency,Width,Height,Noise) * Amplitude;
82: Frequency *= 2.0f;
83: Amplitude *= Persistance;
84: }
85: if (FinalValue < -1.0f)
86: {
87: FinalValue = -1.0f;
88: }
89: else if (FinalValue > 1.0f)
90: {
91: FinalValue = 1.0f;
92: }
93: return FinalValue;
94: }
95:
96: private static float GetSmoothNoise(float X, float Y,int Width,int Height,float[,]Noise)
97: {
98: float FractionX = X - (int)X;
99: float FractionY = Y - (int)Y;
100: int X1 = ((int)X + Width) % Width;
101: int Y1 = ((int)Y + Height) % Height;
102: int X2 = ((int)X + Width - 1) % Width;
103: int Y2 = ((int)Y + Height - 1) % Height;
104:
105: float FinalValue = 0.0f;
106: FinalValue += FractionX * FractionY * Noise[X1, Y1];
107: FinalValue += FractionX * (1 - FractionY) * Noise[X1, Y2];
108: FinalValue += (1 - FractionX) * FractionY * Noise[X2, Y1];
109: FinalValue += (1 - FractionX) * (1 - FractionY) * Noise[X2, Y2];
110:
111: return FinalValue;
112: }
113:
114: private static float[,] GenerateNoise(int Seed,int Width,int Height)
115: {
116: float[,] Noise = new float[Width,Height];
117: System.Random RandomGenerator = new System.Random(Seed);
118: for (int x = 0; x < Width; ++x)
119: {
120: for (int y = 0; y < Height; ++y)
121: {
122: Noise[x, y] = ((float)(RandomGenerator.NextDouble()) - 0.5f) * 2.0f;
123: }
124: }
125: return Noise;
126: }
127:
128: #endregion
129: }
130: }