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: forked from: BitmapDataのhistogram()

BitmapDataのhistogramってどんなのか実験。
Get Adobe Flash player
by cpu_t 26 Feb 2010
    Embed
/**
 * Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/odzn
 */

// forked from cpu_t's forked from: BitmapDataのhistogram()
// forked from cpu_t's BitmapDataのhistogram()
// BitmapDataのhistogramってどんなのか実験。
// 
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
    import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextField;
    public class FlashTest extends Sprite {
		private var bmp:Bitmap;
		private var bmpdata:BitmapData;
		private var tfoutput:TextField;
		private var graph:Sprite;
		private var input:TextField;
        public function FlashTest() {
			addChild(bmp = new Bitmap());
			bmp.y = 80;
			
			tfoutput = createTextfield();
			tfoutput.y = 25;
			
			var sp:Sprite = new Sprite();
			addChild(sp);
			sp.y = stage.stageHeight;
			sp.graphics.beginFill(0xFFFFFF);
			sp.graphics.drawRect(0, -100, 256, 100);
			sp.addChild(graph = new Sprite());
			
			var btn:Sprite = new Sprite();
			addChild(btn);
			btn.graphics.beginFill(0xE0E0E0);
			btn.graphics.drawRect(0, 0, 40, 20);
			btn.graphics.endFill();
			var t:TextField;
			btn.addChild(t = new TextField());
			t.text = "reload";
			t.autoSize = "left";
			btn.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void { loadImg(); } );
			
			input = createTextfield();
			input.x = 50;
			input.type = "input";
			input.text = "http://wonderfl.net/static/tmp/related_images/d707916d0a76acf0e2618e7567d708efa65b094em";
			
			loadImg();
        }
		
		private function createTextfield():TextField
		{
			var t:TextField = new TextField;
			addChild(t);
			t.background = true;
			t.backgroundColor = 0xE0E0E0;
			t.autoSize = "left";
			return t;
		}
		
		private function loadImg():void
		{
			var loader:Loader = new Loader();
			var req:URLRequest = new URLRequest(input.text);
			tfoutput.appendText("load\n");
			loader.load(req, new LoaderContext(true));
			loader.contentLoaderInfo.addEventListener(Event.INIT, function(e:Event):void
			{
				var l:Loader = new Loader();
				tfoutput.appendText("load2\n");
				l.loadBytes(loader.contentLoaderInfo.bytes);
				l.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void
				{
					tfoutput.appendText("complete");
					bmpdata = new BitmapData(l.width, l.height);
					bmpdata.draw(l);
					bmp.bitmapData = bmpdata;
					reset();
				});
			});
			
		}
		
		private function compare(x:Number, y:Number):Number { return x - y; }
		
		private function reset():void
		{
			var vec:Vector.<Vector.<Number>> = bmpdata.histogram(bmpdata.rect);
			var max:Vector.<Number> = new Vector.<Number>();
			var v:Vector.<Number>;
			var m:Number, i:int, c:int;
			
			// ソートして上から3つ取り除く
			for (c = 0; c < 3; c++)
			{
				v = vec[c].concat().sort(compare);
				for (i = 0; i < 3; i++)
				{
					v[i] = vec[c].indexOf(v[v.length - i - 1]);
					vec[c][v[i]] = 0;
				}
				for (i = 0; i < 3; i++)
				{
					if (v[i] - 1 >= 0) vec[c][v[i]] += vec[c][v[i] - 1] * .5;
					if (v[i] + 1 < vec[c].length) vec[c][v[i]] += vec[c][v[i] + 1] * .5;
				}
				max[c] = v[v.length - i - 1];
			}
			
			// 全データを最大値で割る
			for (c = 0; c < 3; c++)
			{
				if (max[c] == 0) continue;
				for (i = 0; i < vec[c].length; i++)
					vec[c][i] /= max[c];
			}
			
			// データをテキストエリアに表示
			tfoutput.text = "";
			tfoutput.appendText("r:" + vec[0].toString() + "\n");
			tfoutput.appendText("g:" + vec[1].toString() + "\n");
			tfoutput.appendText("b:" + vec[2].toString());
			
			// グラフ描画
			var g:Graphics = graph.graphics;
			g.clear();
			g.lineStyle( -1, 0);
			g.moveTo(0, 0);
			g.lineTo(255, 0);
			g.endFill();
			
			for (c = 0; c < 3; c++)
			{
				g.lineStyle( -1, 0xFF0000 >> (c * 8));
				g.moveTo(0, -vec[c][0]*100);
				for (i = 0; i < vec[c].length; i++)
					g.lineTo(i, -vec[c][i]*100);
				g.endFill();
			}
		}
    }
}