forked from: VTR Filter for web camera
...
@author gaina
sound by AlainMikuni
/**
* Copyright gaina ( http://wonderfl.net/user/gaina )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/b6Lq
*/
// forked from wellflat's VTR Filter for web camera
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.media.SoundMixer;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.ByteArray;
/**
* ...
* @author gaina
* sound by AlainMikuni
*/
public class Main extends Sprite
{
private var w:Number;
private var h:Number;
private const WIDTH:int = 465;
private const HEIGHT:int = 465;
private var _path:String = "http://www.takasumi-nagai.com/videofiles/";
private var _url:String = "20100912.f4v";
private var _nc:NetConnection;
private var _ns:NetStream;
private var _bmpd:BitmapData;
private var _bitmap:Bitmap;
private var _line:VTR;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var _sound:PlaySound = new PlaySound("http://www.takasumi-nagai.com/soundfiles/sound006.mp3");
w = stage.stageWidth;
h = stage.stageHeight;
_bmpd = new BitmapData(w, h, false);
_bitmap = new Bitmap(_bmpd);
addChild(_bitmap);
_line = new VTR(_bitmap);
_line.setCondition(2, 2, 2, 2.0);
_nc = new NetConnection();
_nc.addEventListener(NetStatusEvent.NET_STATUS, ConnectCheck);
_nc.connect(null);
}
private function ConnectCheck(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
_ns = new NetStream(_nc);
_ns.client = { };
_line.attachNetStream(_ns);
_ns.checkPolicyFile = true;
_ns.play(_path + _url);
_ns.addEventListener(NetStatusEvent.NET_STATUS, onCompleteEvent);
addEventListener(Event.ENTER_FRAME, loop);
}
}
private function loop(e:Event):void
{
_bmpd.draw(_line);
_line.apply();
var _byte:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(_byte, true);
var p:Number = 0;
for (var i:int = 0; i < 144; i++)
{
var data:Number = _byte.readFloat();
p += data;
if (p > 4) {
_line.setCondition(int(p / 3), int(p / 3), int(p / 2) , 2.0);
}else {
_line.setCondition(0, 0, 0, 1.0);
}
}
}
private function onCompleteEvent(e:NetStatusEvent):void
{
if (e.info.code == "NetStream.Play.Stop")
{
_ns.play(_path + _url);
}
}
}
}
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.media.SoundTransform;
import flash.net.URLRequest;
class PlaySound
{
private var sound:Sound;
public function PlaySound(url:String)
{
sound = new Sound();
var _context:SoundLoaderContext = new SoundLoaderContext(1000, true);
sound.addEventListener(Event.COMPLETE, SoundLoadeComplete);
sound.load(new URLRequest(url), _context);
}
private function SoundLoadeComplete(e:Event):void
{
sound.play(0, 10, new SoundTransform(0.3, 0));
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.media.Video;
class VTR extends Video {
private var _contrast:int;
private var _thick:int;
private var _ghost:int;
private var gtbl:Vector.<uint>;
private var frame:BitmapData;
public function VTR(screen:Bitmap) {
super(screen.width, screen.height);
frame = screen.bitmapData;
gtbl = new Vector.<uint>(256, true);
}
public function setCondition(contrast:int, ghost:int, thick:int,
gamma:Number):void {
_contrast = contrast;
_ghost = ghost;
_thick = thick; // require 2^x
for(var i:int=0; i<256; i++) { // create table for gamma correction.
gtbl[i] = Math.pow(i/255.0, gamma)*255.0;
}
}
public function start():void { // start apply effect (onEnterFrame).
addEventListener(Event.ENTER_FRAME, apply, false, 0, true);
}
/** main algorithm
add scannning lines, ghost and gamma collection. **/
public function apply():void {
frame.draw(super);
frame.lock();
var data:Vector.<uint> = frame.getVector(frame.rect);
var w:int = frame.width, h:int = frame.height;
var r:uint = 0, g:uint = 0, b:uint = 0;
var i:int = 0, j:int = 0, step:int = w;
var f:int = 1;
var len:int = data.length;
for(var y:int = 0; y < h; y++, step += w) {
f = (y & _thick - 1) << 1 < _thick ? 1 : 0;
if(step + _ghost >= len) break;
for(j = i + _ghost; i < step; i++, j++) {
r = ((data[i] >> 16) & 0xff) + ((data[j] >> 16) & 0xff) >> 1;
g = ((data[i] >> 8) & 0xff) + ((data[j] >> 16) & 0xff) >> 1;
b = (data[i] & 0xff) + (data[j] & 0xff) >> 1;
if(f && (r = g = b += _contrast) > 0xff) {
r = g = b = 0xff;
}
data[i] = gtbl[r] << 16 | gtbl[g] << 8 | gtbl[b];
}
}
frame.setVector(frame.rect, data);
frame.unlock();
}
}