lunaria
lunaria

Reputation: 13

AS3 - How can I pixelate bitmapdata without Pixel Bender?

I'd like to apply a pixelate filter to a bitmap. Is there a way to do it without using pixel bender?

Upvotes: 0

Views: 901

Answers (2)

Nathan
Nathan

Reputation: 87

A very simple way to do this is by redrawing a bitmap twice. This code assumes that blockSize is chosen so that your intermediate BitmapData (smaller) aligns with the pixel grid. For example, if your source was 100x100 pixels, a blockSize of 2 will make the intermediate BitmapData 50x50 pixels. Anything more complicated probably requires Math.round() and some fudging.

//  Assumes that source.width / blockSize has no remainder
//  Same with source.height / blockSize
public function getMosaic( source:BitmapData, blockSize:int ):BitmapData
{
    var bitmap:Bitmap = new Bitmap( source );
    bitmap.smoothing = true;  // blends pixels values

    var smaller:BitmapData = new BitmapData( source.width  / blockSize,
                                             source.height / blockSize );
    var matrix:Matrix = new Matrix();
    matrix.scale( 1 / blockSize, 1 / blockSize );
    smaller.draw( bitmap, matrix );

    bitmap = new Bitmap( smaller );
    //  Avoid "bitmap.smoothing = true" here to keep it blocky

    var blocky:BitmapData = new BitmapData( source.width, source.height );
    matrix.invert();  //  gives the opposite effect from before
    blocky.draw( bitmap, matrix );

    smaller.dispose();  //  always dispose BitmapData no longer needed

    return blocky;
}

Upvotes: 1

Antoine Lassauzay
Antoine Lassauzay

Reputation: 1607

You should be able to achieve this with the BitmapData class and its getPixel()/setPixel methods. As for your effect specifically, you could calculate an average color for a given group of pixels, and apply the same color to all of them.

More on pixel manipulation : http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d64.html

Upvotes: 1

Related Questions