Perlin Noise generation in C#

Fancy name for images that look like clouds.
Mar 19 2008 by James Craig

I've always found the simple fact that math defines damn near everything to be truly amazing. As of late with games like Spore on the horizon, I've been rather interested in procedural content. In fact the engine and game I'm creating utilize procedural content quite a bit, so expect there to be a lot of posts on the subject but today I'm going to talk about Perlin Noise.

Perlin Noise was developed by Ken Perlin (which he ended up getting an Academy Award for... Odd but true) and that's really all you need to know about the history. The algorithm creates pseudo random noise. We can use this for height maps, clouds, textures, an "interesting" background for your website... Although I'm sure that if you're here all you really want is the code. So here you go:

 /\*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.\*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
#endregion

namespace Utilities.Media.Image
{
/// <summary>
/// Perlin noise helper class
/// </summary>
public static class PerlinNoise
{
#region Functions

/// <summary>
/// Generates perlin noise
/// </summary>
/// <param name="Width">Width of the resulting image</param>
/// <param name="Height">Height of the resulting image</param>
/// <param name="MaxRGBValue">MaxRGBValue</param>
/// <param name="MinRGBValue">MinRGBValue</param>
/// <param name="Frequency">Frequency</param>
/// <param name="Amplitude">Amplitude</param>
/// <param name="Persistance">Persistance</param>
/// <param name="Octaves">Octaves</param>
/// <param name="Seed">Random seed</param>
/// <returns>An image containing perlin noise</returns>
public static Bitmap Generate(int Width,int Height,int MaxRGBValue,int MinRGBValue,
float Frequency,float Amplitude,float Persistance,int Octaves,int Seed)
{
Bitmap ReturnValue = new Bitmap(Width, Height);
BitmapData ImageData = Image.LockImage(ReturnValue);
int ImagePixelSize = Image.GetPixelSize(ImageData);
float\[,\] Noise=GenerateNoise(Seed,Width,Height);
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise);
Value = (Value \* 0.5f) + 0.5f;
Value \*= 255;
int RGBValue=Math.MathHelper.Clamp((int)Value, MaxRGBValue, MinRGBValue);
Image.SetPixel(ImageData, x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
}
}
Image.UnlockImage(ReturnValue, ImageData);
return ReturnValue;
}

private static float GetValue(int X, int Y, int Width,int Height,float Frequency, float Amplitude,
float Persistance, int Octaves,float\[,\]Noise)
{
float FinalValue = 0.0f;
for (int i = 0; i < Octaves; ++i)
{
FinalValue += GetSmoothNoise(X \* Frequency, Y \* Frequency,Width,Height,Noise) \* Amplitude;
Frequency \*= 2.0f;
Amplitude \*= Persistance;
}
if (FinalValue < -1.0f)
{
FinalValue = -1.0f;
}
else if (FinalValue > 1.0f)
{
FinalValue = 1.0f;
}
return FinalValue;
}

private static float GetSmoothNoise(float X, float Y,int Width,int Height,float\[,\]Noise)
{
float FractionX = X - (int)X;
float FractionY = Y - (int)Y;
int X1 = ((int)X + Width) % Width;
int Y1 = ((int)Y + Height) % Height;
int X2 = ((int)X + Width - 1) % Width;
int Y2 = ((int)Y + Height - 1) % Height;

float FinalValue = 0.0f;
FinalValue += FractionX \* FractionY \* Noise\[X1, Y1\];
FinalValue += FractionX \* (1 - FractionY) \* Noise\[X1, Y2\];
FinalValue += (1 - FractionX) \* FractionY \* Noise\[X2, Y1\];
FinalValue += (1 - FractionX) \* (1 - FractionY) \* Noise\[X2, Y2\];

return FinalValue;
}

private static float\[,\] GenerateNoise(int Seed,int Width,int Height)
{
float\[,\] Noise = new float\[Width,Height\];
System.Random RandomGenerator = new System.Random(Seed);
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
Noise\[x, y\] = ((float)(RandomGenerator.NextDouble()) - 0.5f) \* 2.0f;
}
}
return Noise;
}

#endregion
}
}

When you call the Generate function, play around with the variables a bit, but I've noticed that using a Frequency of 0.0625, an Amplitude of 1.0, a Persistance of 0.5 and 16 Octaves gives good results. Also note that the code uses a bit of code from my utility library that locks/unlocks the bitmap image.. Anyway, try out the code, leave comments, and happy coding.