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: sketch

click to whow original / filtered
source image taken here:
http://ffffound.com/image/0ee8a947386ce9df8a81d41143069db8cd37b82e

@author Nicolas Barradeau
http://en.nicoptere.net
Get Adobe Flash player
by 0xABCDEF 10 Feb 2011
/**
 * Copyright 0xABCDEF ( http://wonderfl.net/user/0xABCDEF )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/d93h
 */

// forked from nicoptere's sketch
package 
{
    /**
     * click to whow original / filtered
     * source image taken here:
     * http://ffffound.com/image/0ee8a947386ce9df8a81d41143069db8cd37b82e
     * 
     * @author Nicolas Barradeau
     * http://en.nicoptere.net
     */
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Point;
    
    import flash.media.Camera;
    import flash.media.Video;
    
    public class Sketch extends Sprite
    {
        
        private var src:BitmapData;
        private var bd:BitmapData;
        private var flip:int = 0;
        private var bmp:Bitmap;
        
        private var webcam:Camera;
        private var video:Video;
        private var isSketchMode:Boolean;
        private var container:Sprite;
        
        public function Sketch() 
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            webcam = Camera.getCamera();
            webcam.setQuality( 0, 100 );
            webcam.setMode( stage.stageWidth, stage.stageHeight, 60 );
            video = new Video( webcam.width, webcam.height );
            video.attachCamera( webcam );
            src = new BitmapData( webcam.width, webcam.height, false );
            addEventListener( Event.ENTER_FRAME, ENTER_FRAME );
            
            container = addChild( new Sprite ) as Sprite;
            
            bd = new BitmapData( webcam.width, webcam.height, false );
            bmp = container.addChild( new Bitmap( bd ) ) as Bitmap;
            container.buttonMode = true;
            container.addEventListener( MouseEvent.MOUSE_DOWN, swap );
        }
        
        private function ENTER_FRAME( e:Event ):void
        {
            if( isSketchMode )
            src.draw( video ),
            sketch( src, bd ) else bd.draw( video );
        }
        
        private function swap(e:MouseEvent):void 
        {
            
            isSketchMode = !isSketchMode;
            
        }
        
        private function sketch( src:BitmapData, bd:BitmapData ):void 
        {
            var p:Point = new Point;
            bd.draw( src );
            
            // desaturation + brightness + contrast 
            bd.applyFilter( bd, bd.rect, p, grayscale );
            bd.applyFilter( bd, bd.rect, p, brightness( 35 ) );
            bd.applyFilter( bd, bd.rect, p, contrast( 45 ) );
            
            //bloom
            ///*
            var bloom:BitmapData = src.clone();
            bloom.applyFilter( bloom, bloom.rect, p, new BlurFilter( 10, 10, 3 ) );
            bloom.applyFilter( bloom, bloom.rect, p, grayscale );
            
                //draws the bloom
                bd.draw( bloom, null, new ColorTransform( 1, 1, 1, .999 ), BlendMode.SCREEN );
            //*/
            
            //creates the outlines
            var outlines:BitmapData = src.clone();
            outlines.applyFilter( outlines, outlines.rect, p, outline( 120 ) );
            outlines.applyFilter( outlines, outlines.rect, p, negative );
            outlines.applyFilter( outlines, outlines.rect, p, grayscale );
            //outlines.threshold( outlines, outlines.rect, p, '<=', 0xFF707070, 0xFF000000 );
            
                //draws the outlines into the bd
                bd.draw( outlines, null, new ColorTransform( 1, 1, 1, .999 ), BlendMode.MULTIPLY );
            
            //creates some additionnal noise
            var noise:BitmapData = bd.clone();
            noise.noise( 0, 0, 0x80, 7, true);
                
                //draws the extra noise
                bd.draw( noise, null, new ColorTransform( 1, 1, 1, 0.2 ), BlendMode.ADD );
            
            //final contrast pass
            bd.applyFilter( bd, bd.rect, p, contrast( 55 ) );
            
        }
        
        private function outline( value:Number = 80 ):ConvolutionFilter
        {
            var q:Number = value / 4;
            return new ConvolutionFilter(    3,     3,     [
                                                    0    ,      q    ,     0    , 
                                                    q    ,     -value    ,     q    , 
                                                    0    ,     q    ,     0    
                                                    ],     10 );
        }
        
        private function get negative():ColorMatrixFilter
        {
            return new ColorMatrixFilter(     [
                                                -1    ,    0    ,    0    ,    0    ,    0xFF,
                                                0    ,    -1    ,    0    ,    0    ,    0xFF,
                                                0    ,    0    ,    -1    ,    0    ,    0xFF,
                                                0    ,    0    ,    0    ,    1    ,    0
                                            ]    );
        }
        
        private function get grayscale():ColorMatrixFilter
        {
            return new ColorMatrixFilter(     [
                                                .3086    ,    .6094    ,    .0820    ,    0    ,    0,
                                                .3086    ,    .6094    ,    .0820    ,    0    ,    0,
                                                .3086    ,    .6094    ,    .0820    ,    0    ,    0,
                                                    0    ,    0    ,    0    ,    1    ,    0
                                            ]    );
        }
        
        private function brightness( value:Number ):ColorMatrixFilter
        {
            return new ColorMatrixFilter(     [
                                                1    ,    0    ,    0    ,    0    ,    value,
                                                0    ,    1    ,    0    ,    0    ,    value,
                                                0    ,    0    ,    1    ,    0    ,    value,
                                                0    ,    0    ,    0    ,    1    ,    0
                                            ]    );
        }
        
        private function contrast( value:Number ):ColorMatrixFilter
        {
            var a:Number = ( value * 0.01 + 1 )
            var b:Number = 0x80 * ( 1 - a );
            return new ColorMatrixFilter(     [
                                                a    ,    0    ,    0    ,    0    ,    b,
                                                0    ,    a    ,    0    ,    0    ,    b,
                                                0    ,    0    ,    a    ,    0    ,    b,
                                                0    ,    0    ,    0    ,    1    ,    0
                                            ] );
        }    
    }
}