In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 外部画像をビットマップ化する

just changed the image url to one without crossdomain.xml
入力欄に画像URLを入力してください。
* とりあえず、画像(BitmapData)の半分だけグレイスケール処理をかけています。
Get Adobe Flash player
by mash 24 Dec 2009
    Embed
// forked from rsakane's 外部画像をビットマップ化する
// just changed the image url to one without crossdomain.xml
/*
 * 入力欄に画像URLを入力してください。
 * とりあえず、画像(BitmapData)の半分だけグレイスケール処理をかけています。
 */
package
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.utils.*;
	import com.bit101.components.*;
 
	public class Main extends Sprite
	{
		private const SIZE:int = 400;
		
		private var loaderA:Loader;
		private var loaderB:Loader;
		
		private var input:InputText;
		private var bitmap:Bitmap;
		
		public function Main()
		{
			var label:Label = new Label(this, 25, 409, "IMAGE URL");
			input = new InputText(this, 0, 410, "http://wonderfl.net/img/top/img_study.gif");
			input.width = 300;
			input.x = (465 - input.width) / 2;
			var button:PushButton = new PushButton(this, 230, 435, "OK", inputHandler);
			button.x = (465 - button.width) / 2;
			
			inputHandler();
		}
		
		private function inputHandler(event:Event = null):void
		{
			loaderA = new Loader();
			loaderA.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
			//loaderA.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function():void { } );
			loaderA.load(new URLRequest(input.text), new LoaderContext(true));
		}
 
		private function initHandler(event:Event):void
		{	
			loaderB = new Loader();
			loaderB.contentLoaderInfo.addEventListener(Event.INIT, init);
			
			// bytes
			loaderB.loadBytes(loaderA.contentLoaderInfo.bytes);
			
			// this fails
			/*
			var bd :BitmapData = new BitmapData( loaderA.width, loaderA.height );
			bd.draw( loaderA );
			var bitmap :Bitmap = new Bitmap( bd );
			addChild( bitmap );
			*/
		}
		
		private function init(event:Event):void
		{
			var loader:Loader = event.currentTarget.loader;
			
			var scale:Number = 0;
			if (loader.width < loader.height) scale = SIZE / loader.height;
			else scale = SIZE / loader.width;
			
			var matrix:Matrix = new Matrix();
			matrix.scale(scale, scale);
			var bd:BitmapData = new BitmapData(scale * loader.width, scale * loader.height, false);
			bd.draw(loader, matrix);
			
			if (bitmap) removeChild(bitmap);
			bitmap = new Bitmap(grayscale(bd));
			bitmap.x = (465 - bitmap.width) / 2;
			addChild(bitmap);
		}
		
		private function grayscale(bd:BitmapData):BitmapData
		{
			var destbd:BitmapData = bd.clone();
		 
			var matrix:Array = new Array();
			matrix = matrix.concat([0.298912, 0.586611, 0.114478, 0, 0]);
			matrix = matrix.concat([0.298912, 0.586611, 0.114478, 0, 0]);
			matrix = matrix.concat([0.298912, 0.586611, 0.114478, 0, 0]);
			matrix = matrix.concat([0, 0, 0, 1, 0]);
			destbd.applyFilter(destbd, new Rectangle(0, 0, bd.width, bd.height / 2), new Point(), new ColorMatrixFilter(matrix));
		 
			return destbd;
		}
	}
}