ascii webcam

generating ascii art from webcam data

ascii webcam

src/nu/xero/gfx/bitmap2ascii.as


/*
	___.                    ________                        .__.__ 
	\_ |__   _____ ______   \_____  \  _____    ______ ____ |__|__|
	 | __ \ /     \____ \   /  ____/  \__  \  /  ___// ___\|  |  |
	 | \_\ \  Y Y  \  |_> > /       \   / __ \_\___ \  \___|  |  |
	 |___  /__|_|  /   __/  \_______ \ (____  /____  >\___  >__|__|
	     \/      \/|__|             \/      \/     \/     \/   
	                                                                */
package nu.xero.gfx {
//______________________________________________________________________________
//                                                                       imports	
	import flash.display.*;
//______________________________________________________________________________
//                                                                         class	
	public class bitmap2ascii extends Object {
//______________________________________________________________________________
//                                                                          vars		
		public  var resolution			:Number = 0.025;
		public  var whiteThreshold		:Number = 200;
		public  var blackThreshold		:Number = 40;
		static private const palette	:String = "@#$%&8BMW*mwqpdbkhaoQ0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+_~-;,. ";
//______________________________________________________________________________
//                                                                   constructor
		public function bitmap2ascii(res:Number = 0.025, white:Number = 200, black:Number = 40) {
			super();
			resolution = res;
			whiteThreshold = white;
			blackThreshold = black;
		}
//______________________________________________________________________________
//                                                       convert bitmap to ascii
		public function convert(gfx:BitmapData):String {
			var verticalRes:uint;
			var horizontalRes:uint;
			var rgb:uint;
			var r:uint;
			var g:uint;
			var b:uint;
			var gray:uint;
			var char:uint;
			var ascii:String;
			var y:uint;
			var x:uint;
			
			ascii = "";
			y = 0;
			
			verticalRes = Math.floor(gfx.height * resolution);
			horizontalRes = Math.floor(gfx.width * resolution * 0.45);
			
			while (y < gfx.height) {
				x = 0;
				while (x < gfx.width) {
					rgb = gfx.getPixel(x, y);
					r = (rgb & 0xff0000) >> 16;
					g = (rgb & 0x00ff00) >> 8;
					b = (rgb & 0x0000ff);
					gray = Math.floor(0.3 * r + 0.59 * g + 0.11 * b);
					if (gray > whiteThreshold) {
						gray = 0xff;
					} else if (gray < blackThreshold) {
						gray = 0x00;
					} else {
						gray = Math.floor(0xff * ((gray - blackThreshold) / (whiteThreshold - blackThreshold)));
					}
					char = Math.floor(gray / 4);
					ascii += palette.charAt(char);
					x += horizontalRes;
				}
				ascii = ascii + "\n";
				y += verticalRes;
			}
			return ascii;
		}
	}
}
//______________________________________________________________________________
//                                                                   end of file

Download

raw zip tar