Recipe 8.10. Creating Noise


Problem

You want to create a random noise pattern in a bitmap.

Solution

Use the noise( ) method of the BitmapData class.

Discussion

A noise filter or pattern is simply sets each pixel in an affected area to a random value. Although you could loop through each pixel in a bitmap and use setPixel( ) to randomly change its color, there is an easier and more powerful way of doing the same thing in one line: the noise( ) method.

By itself, noise( ) creates a random, speckled image, like what you see when a television is tuned to a channel with no signal. However, by combining noise with other filters, such as the blur filter, you can get some very useful effects.

The noise( ) method is called directly on an instance of BitmapData, and its parameters are straightforward:

bitmap.noise(seed, low, high, channel, grayscale);

Use the seed parameter to determine the random pattern; it can be any integer. If you call the method twice with the same seed, you get the same noise pattern. To ensure you get a different pattern each time you call it, pass in a random number like so:

Math.random(  ) * 100000

The low and high parameters determine the minimum and maximum values for each pixel. They can range from 0 to 255. Setting them higher gives you a brighter noise pattern; setting them lower makes darker noise.

The channel parameter specifies to which color channel of the bitmap the noise will be applied. You can specify 1, 2, 4, or 8 for red, green, blue, and alpha, respectively; or use the static properties of the BitmapDataChannel class: RED, GREEN, BLUE, and ALPHA, to guard against typos.

Finally, grayscale is a Boolean value. true applies the random value to the three color channels equally, resulting in a colorless noise.

Here are some examples. First, create a bitmap, add the noise, and then display it:

bitmap = new BitmapData(stage.stageWidth, stage.stageHeight,                      false, 0xff000000); bitmap.noise(1000, 0, 255, BitmapDataChannel.RED, false); var image:Bitmap = new Bitmap(_bitmap); addChild(image);

This creates a random pattern in the red channel. Upping the low value gives you a lighter noise pattern:

bitmap.noise(1000, 200, 255, BitmapDataChannel.RED, false);

You can easily convert that to a grayscale image by setting the last parameter to true:

bitmap.noise(1000, 200, 255, BitmapDataChannel.RED, true);

If you have set grayscale to true, it doesn't matter which channel you create the noise on. You can also combine various channels to create multicolored noise, using the | operator:

bitmap.noise(1000, 0, 255, BitmapDataChannel.RED |                         BitmapDataChannel.GREEN |                         BitmapDataChannel.BLUE,                         false);

Finally, as mentioned earlier, perhaps one of the best uses of noise is when you combine it with other filters or effects. The following code creates some noise in a bitmap, and then applies a horizontal blur filter, producing a passable brushed metal effect:

bitmap = new BitmapData(stage.stageWidth, stage.stageHeight,                      false, 0xff000000); bitmap.noise(1000, 128, 255, BitmapDataChannel.RED, true); bitmap.applyFilter(bitmap,                 bitmap.rect,                 new Point(  ),                 new BlurFilter(30, 1, 3)); var image:Bitmap = new Bitmap(bitmap); addChild(image);

This is just one example of combining noise with other effects. With some experimenting and practice, you can come up with many more.

See Also

Recipes 8.5, 8.6, 8.7, 8.8, 8.9, and 8.11 for other ways to add graphical content to a bitmap.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net