5
Apr/08
0

Adding noise with the bitmap class

This is a small test I did with the bitmap class; the code reads every pixel in an image and adds this pixel to the original position +/- 2 pixels, resulting in a “noisy” image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package
{
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;

  public class Bitmaptest extends Sprite
  {
    private var loader:Loader;
    private var color:uint;

    public function Bitmaptest():void
    {
      init();
    }

    public function init():void
    {
      loader = new Loader();
      loader.contentLoaderInfo.addEventListener( Event.INIT, initListener );
      loader.load( new URLRequest( "audi_tt_v2.png" ) );
    }

    public function initListener( e:Event ):void
    {
      addChild( loader.content );

      Bitmap( loader.content ).bitmapData.lock();

      for( var i:int = 0; i < Bitmap( loader.content ).bitmapData.height; i++ )
      {
        for( var j:int = 0; j < Bitmap( loader.content ).bitmapData.width; j++ )
        {
          color = Bitmap( loader.content ).bitmapData.getPixel32( j, i );
          Bitmap( loader.content ).bitmapData.setPixel32( j + Math.round( Math.random()*4-2 ), i + Math.round( Math.random()*4-2 ), color );
        }
      }

      Bitmap( loader.content ).bitmapData.unlock();
    }
  }
}

pre/post effect:
without noise
with noise