ArtFilter2

by hrtsgt
♥0 | Line 73 | Modified 2014-11-11 13:04:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright hrtsgt ( http://wonderfl.net/user/hrtsgt )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oOiD
 */

package{
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.system.*;
    [SWF(width=465, height=465, backgroundColor=0xFFFFFF, frameRate=60)]
    
    public class ArtFilter2 extends Sprite{
        private var _canvas:BitmapData;
        private var _threshold:BitmapData;
        private var _source:BitmapData;
        private var _b:uint;
        private var _x:uint = 0;
        private var _y:uint = 0;
        private var _side:Number = 15;//分割サイズ
        private var _level:Number = 10;//階調数
    
        public function ArtFilter2(){
            var loader:Loader = new Loader();
            loader.load( new URLRequest("http://t.args.in/lab/lib/einstein.jpg"), new LoaderContext(true));
            loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete);//
        }
        
        private function onComplete( e:Event ):void{
            _source = Bitmap( LoaderInfo(e.target).loader.content ).bitmapData;
            _source.applyFilter( _source, _source.rect, new Point(0,0), new ColorMatrixFilter([ 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0, 0, 0, 1, 0]));//白黒化
            _canvas = new BitmapData( _source.width, _source.height, true, 0x0);
            _threshold = _canvas.clone();
            addChild( new Bitmap(_source) );
            addChild( new Bitmap(_canvas) );
            
            for(var i:uint=_level; i>0; i--){
                _b = Math.floor( i/_level*0xFF );
                _threshold.threshold( _source, _source.rect, new Point(0,0), "<=", _b << 16, 0xFF000000 | _b << 16 | _b << 8 | _b, 0x00FF0000, false);
            }
            _b = _level;
            this.addEventListener( Event.ENTER_FRAME, update);//
        }
        
        private function update( e:Event ):void{
            plot();
            plot();
            plot();
            plot();
            plot();
            plot();
            plot();
            plot();
            plot();
            plot();
        }
        
        private function plot():void{
            var __b:uint = Math.floor( _b/_level*0xFF );
            var __c:uint = __b << 16 | __b << 8 | __b;
            var m:Matrix = new Matrix();
            m.translate( -_x, -_y);
            var bmpd:BitmapData = new BitmapData( _side, _side, false, 0xFFFFFF);
            bmpd.draw( _threshold, m, null, null, new Rectangle( 0, 0, _side, _side) );
            var rect:Rectangle = bmpd.getColorBoundsRect( 0xFFFFFF, __c, true);
            var shape:Shape = new Shape();
            shape.graphics.beginFill(__c);
            shape.graphics.drawRect( rect.x, rect.y, rect.width, rect.height );
            m.translate( _x*2, _y*2);
            _canvas.draw( shape, m);
            
            _x += _side;
            if( _x > _threshold.width ){
                _x = 0;
                _y += _side;
                if( _y > _threshold.height ){
                    _y = 0;
                    if( --_b <= 0 ) this.removeEventListener( Event.ENTER_FRAME, update);return;//
                }
            }
        }
                
    }
}

Forked