Halftone
Webcam必須
/**
* Copyright Saqoosha ( http://wonderfl.net/user/Saqoosha )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ohtL
*/
// Webcam必須
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
[SWF(backgroundColor=0x000000, frameRate='30', width='465', height='465')]
public class HalftoneMono extends Sprite {
private const CAMERA_W:int = 320;
private const CAMERA_H:int = 240;
private var camera:Camera;
private var video:Video;
private var effect_bd:BitmapData;
private var pattern_bd:BitmapData;
private var hoge_bd:BitmapData;
private var rect:Rectangle;
private var zero:Point;
private var mtx:Matrix;
private var mono_cmf:ColorMatrixFilter = new ColorMatrixFilter( [
1/3, 1/3, 1/3, 0, 0,
1/3, 1/3, 1/3, 0, 0,
1/3, 1/3, 1/3, 0, 0,
0, 0, 0, 1, 0 ]
);
private var bayer_arr:Array = [
/* 0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5 */
0, 2, 5, 9,
1, 4, 8, 12,
3, 7, 11, 14,
6, 10, 13, 15
];
public function HalftoneMono() {
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.LOW;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
}
private function mouseUpHandler( e:MouseEvent ):void {
stage.removeEventListener( e.type, arguments.callee );
camera = Camera.getCamera();
camera.setMode(320, 240, 30);
if ( camera != null ) {
setupPattern();
setupEffect();
}
}
private function setupPattern():void {
var bayer_bd:BitmapData = new BitmapData( 4, 4, false, 0x000000 );
for ( var y:int = 0; y < 4; ++y ) {
for ( var x:int = 0; x < 4; ++x ) {
var c:int = bayer_arr[ 4*y+x ]*16+8;
bayer_bd.setPixel( x, y, c << 16 | c << 8 | c );
}
}
var temp:Shape = new Shape();
temp.graphics.beginBitmapFill( bayer_bd );
temp.graphics.drawRect( 0, 0, CAMERA_W, CAMERA_H );
temp.graphics.endFill();
pattern_bd = new BitmapData( CAMERA_W, CAMERA_H, false, 0x000000 );
pattern_bd.draw( temp );
// var bm:Bitmap = this.addChild(new Bitmap(pattern_bd)) as Bitmap;
// bm.y = 240;
bayer_bd.dispose();
temp.graphics.clear();
}
private function setupEffect():void {
video = new Video( CAMERA_W, CAMERA_H );
video.attachCamera( camera );
// this.addChild(video);
rect = new Rectangle( 0, 0, CAMERA_W, CAMERA_H );
zero = new Point();
mtx = new Matrix();
effect_bd = new BitmapData( CAMERA_W, CAMERA_H, false, 0x000000 );
var bm:Bitmap = addChild( new Bitmap( effect_bd ) ) as Bitmap;
bm.scaleX = bm.scaleY = 2.0;
bm.x = (465 - 640) >> 1;
bm.y = (465 - 480) >> 1;
// bm.x = 320;
hoge_bd = new BitmapData( CAMERA_W, CAMERA_H, false, 0x000000 );
//bm = addChild(new Bitmap(hoge_bd)) as Bitmap;
//bm.x = 320; bm.y = 240;
update();
stage.addEventListener( Event.ENTER_FRAME, update );
}
private function update( e:Event = null ):void {
effect_bd.lock();
effect_bd.draw( video );
effect_bd.applyFilter( effect_bd, rect, zero, mono_cmf );
effect_bd.draw( pattern_bd, mtx, null, "subtract");
hoge_bd.draw(effect_bd);
effect_bd.threshold( effect_bd, rect, zero, ">", 0x000000, 0xffffffff, 0x0000ff );
effect_bd.unlock();
}
}
}