Return to Snippet

Revision: 39716
at January 20, 2011 02:30 by adrianparr


Initial Code
package com.terrypaton.effect {

	import flash.display.*
	import flash.geom.Matrix;
	
	public class PixelateBitmap {
		public function PixelateBitmap ():void {
			_instance = this
			
		}
		public function setup (_bitmapData:BitmapData):void {
			_bmpData = _bitmapData
		}
		public function process (_source:BitmapData,amount:Number):void {
			
			var scaleFactor:Number = 1 / amount
			var bmpX:int = scaleFactor*_bmpData.width
			var bmpY:int = scaleFactor * _bmpData.height
			if (bmpX < 1) {
				bmpX = 10
			}
			if (bmpY < 1) {
				bmpY = 10
			}
			// scale image down
			_pixelateMatrix.identity ()
			_pixelateMatrix.scale (scaleFactor, scaleFactor)
			try {
				var _tempBmpData:BitmapData = new BitmapData (bmpX,bmpY,false,0xFF0000)
			}catch (e:Error) {
				trace("bmpX = "+bmpX)
				trace("bmpY = "+bmpY)
			}
			
			_tempBmpData.draw (_source, _pixelateMatrix)
			// now scale it back
			_pixelateMatrix.identity ()
			_pixelateMatrix.scale (amount, amount)
			
			_bmpData.draw(_tempBmpData,_pixelateMatrix)
		}
		public var _pixelateMatrix:Matrix = new Matrix()
		public var _bmpData:BitmapData
		
		public static function getInstance():PixelateBitmap {
			return _instance
		}
		public static var _instance:PixelateBitmap
	}
}

// Usage Example
//
// import com.terrypaton.effect.PixelateBitmap;
// 
// var myBitmapData:BitmapData = new myImage(320,240); // myImage is a bitmap in the library with Export for ActionScript set
// var myPixelatedBitmapData:BitmapData = new BitmapData(myBitmapData.width,myBitmapData.height);
// var myBitmap:Bitmap = new Bitmap(myPixelatedBitmapData);
// var pixelateBitmap:PixelateBitmap = new PixelateBitmap();
// pixelateBitmap.setup(myPixelatedBitmapData);
// 
// addChild(myBitmap);
// 
// addEventListener(Event.ENTER_FRAME, loop);
// function loop(event:Event):void {
// 	var amount:Number = 320 / mouseX;
// 	if (amount > 0) {
// 		pixelateBitmap.process(myBitmapData, amount);
// 	}
// }

Initial URL
http://www.terrypaton.com/as3-pixelate-bitmapdata/

Initial Description
Credit for this goes to Terry Paton

Initial Title
AS3 Pixelate bitmapData

Initial Tags


Initial Language
ActionScript 3