Test
エッジだけぼかさないテスト(あまりうまくいってない)
//
// エッジだけぼかさないテスト(あまりうまくいってない)
//
package {
import flash.display.Sprite;
import flash.events.*;
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="30")]
public class FlashTest extends Sprite {
public function FlashTest() {
Main = this;
startLoad();
stage.addEventListener(Event.ENTER_FRAME,update);
}
}
}
import flash.display.*;
import flash.events.*
import flash.text.TextField;
import flash.geom.*;
import flash.utils.getTimer;
import flash.net.*;
var Main:Sprite;
var SCREEN_W:Number = 465;
var SCREEN_H:Number = 465;
var Text:TextField
var View: Bitmap;
var BmpData: BitmapData;
var OutBmpData: BitmapData;
var BITMAP_W:int = SCREEN_W;
var BITMAP_H:int = SCREEN_H;
var loaderA:Loader;
var loaderB:Loader;
var bLoad:Boolean = false;
function startLoad():void{
loaderA = new Loader();
loaderA.load( new URLRequest("http://img.f.hatena.ne.jp/images/fotolife/N/Nao_u/20090913/20090913133218.jpg") );
loaderA.contentLoaderInfo.addEventListener( Event.COMPLETE, loadComplete );
}
function loadComplete(e:Event):void {
loaderB = new Loader();
loaderB.contentLoaderInfo.addEventListener(Event.INIT, initialize);
loaderB.loadBytes(loaderA.contentLoaderInfo.bytes);
}
function initialize(event:Event):void
{
var loader:Loader = loaderB;
BmpData = new BitmapData(loader.width, loader.height, false);
BmpData.draw(loader);
OutBmpData = new BitmapData(loader.width, loader.height, false);
View = new Bitmap(OutBmpData);
View.scaleX = 1.0;
View.scaleY = 1.0;
Main.addChild(View);
Text = new TextField();
Text.text = "----";
Text.autoSize = "left";
Main.addChild(Text);
bLoad = true;
}
var Cnt:int;
function update(e :Event):void{
if( bLoad == false ){
return;
}
Cnt++;
if( Cnt == 3 ){
var time:int = getTimer();
var cont:Number = 64;
var mul:Number = 128 + cont;
Filter(BmpData, OutBmpData);
Filter(OutBmpData, BmpData );
Filter(BmpData, OutBmpData);
var endTime:int = getTimer() - time;
Text.text = "生成時間:" + endTime + "[ms]";
}
Text.text = "時間:"+ Cnt +"[ms]";
}
function Filter( inBmp:BitmapData, outBmp:BitmapData ):void{
var b:Color = new Color(0);
var col:Color = new Color(0);
var c:Color = new Color( 0 );
var sum:Number = 0;
var sr:Number = 0;
var sg:Number = 0;
var sb:Number = 0;
var w:Number = 0;
outBmp.lock();
for( var x:int=0; x<BITMAP_W; x++ ){
for( var y:int=0; y<BITMAP_H; y++ ){
sum = 0;
col.r = col.g = col.b = 0;
b.set( inBmp.getPixel(x, y) );
for( var lx:int=-5; lx<=5; lx++ ){
for( var ly:int=-5; ly<=5; ly++ ){
c.set( inBmp.getPixel(x+lx, y+ly) );
sr = c.r - b.r;
sg = c.g - b.g;
sb = c.b - b.b;
if( sr < 0 ) sr = -sr;
if( sg < 0 ) sg = -sg;
if( sb < 0 ) sb = -sb;
w = 1-(sr+sg+sb)*0.9;
if( w > 0 ){
col.r += c.r * w;
col.g += c.g * w;
col.b += c.b * w;
sum += w;
}
}
}
var mul:Number = 1.0 / sum;
col.r *= mul;
col.g *= mul;
col.b *= mul;
outBmp.setPixel(x, y, col.getInt() );
}
}
outBmp.unlock();
}
// Noiseを生成
function PerlinNoise3( x:Number, y:Number, frequency:Number, presistence:Number, octave:int ):Number{
var total:Number = 0;
var amplitude:Number = presistence;
frequency *= 2;
presistence *= 2;
for( var i:int=0; i<octave; i++ ){
var sx:Number = x;
var sy:Number = y;
x = sx * 1.5 + sy * -1.1;
y = sx * 1.1 + sy * 1.5;
total += 0.7*(makeNoise( x, y, frequency, amplitude ));
amplitude *= presistence;
frequency *= 1.00;
}
return (total);
}
// Noiseを生成
function PerlinNoise2( x:Number, y:Number, frequency:Number, presistence:Number, octave:int ):Number{
var total:Number = 0;
var amplitude:Number = presistence;
for( var i:int=0; i<octave; i++ ){
var sx:Number = 0.5 + x / BITMAP_W * 3.14 *4;
var sy:Number = y / BITMAP_H * 3.14 *3;
sx = sx + Math.sin(sy) ;
// sy = sy + Math.sin(sx) ;
total += 1.5*makeNoise( 100+x+5.5*Math.sin(sy), 100+y+17.5*Math.sin(sx), frequency, amplitude );
amplitude *= presistence;
frequency *= 0.5;
}
return total;
}
// PerlinNoiseを生成
function PerlinNoise( x:Number, y:Number, frequency:Number, presistence:Number, octave:int ):Number{
var total:Number = 0;
var amplitude:Number = presistence;
for( var i:int=0; i<octave; i++ ){
total += makeNoise( x, y, frequency, amplitude );
amplitude *= presistence;
frequency *= 0.5;
}
// if( total < -1.0 ) total = -1.0;
// if( total > 1.0 ) total = 1.0;
return total;
}
// 周波数に対応したノイズを生成
function makeNoise( x:Number, y:Number, frequency:Number, amplitude:Number ):Number{
return Math.abs(rndSmooth(x/frequency,y/frequency) * amplitude);
}
// 入力値( x, y ) に対応した、滑らかな擬似乱数を生成 (補間の結果-1.0を下回ったり、1.0を超えることがある)
function rndSmooth( x:Number, y:Number ):Number{
var tx:Number = (x - Math.floor(x));
var txInv:Number = 1.0 - tx;
var ty:Number = (y - Math.floor(y));
var tyInv:Number = 1.0 - ty;
var ix:int = x;
var iy:int = y;
// cos補間
var x0:Number = interpolate( rnd(ix, iy), rnd(ix+1, iy), tx );
var x1:Number = interpolate( rnd(ix, iy+1), rnd(ix+1, iy+1), tx );
return interpolate( x0, x1, ty );
}
// a から b をcosでなめらかに補間
function interpolate( a:Number, b:Number, t:Number ):Number{
var ft:Number = t * Math.PI;
var f:Number = (1.0 - Math.cos( ft )) * 0.5;
return a * (1.0 - f) + b * f;
}
// 入力値( x, y ) に対応した、-1.0 ~ 1.0 の擬似乱数を生成
function rnd( x:int, y:int ):Number{
x += y * 465 + 789221; // 465=2次元での横幅に対応
x = (x>>10) ^ x;
var ret:int = (( (x * (x * x * 15731 + 789221) + 1376312589) ) / 1000000);
return ((((ret & 0xff) + ((ret & 0xff00)>>8)+ ((ret & 0xff0000)>>16))&0x1ff) / 256) - 1.0;
}
// 色クラス
class Color{
public var r:Number;
public var g:Number;
public var b:Number;
public function Color( c:int ){
r = ((c & 0xff0000)>>16)*0.003921568627;
g = ((c & 0x00ff00)>>8)*0.003921568627;
b = ((c & 0x0000ff))*0.003921568627;
}
public function set( c:int ):void{
r = ((c & 0xff0000)>>16)*0.003921568627;
g = ((c & 0x00ff00)>>8)*0.003921568627;
b = ((c & 0x0000ff))*0.003921568627;
}
public function getInt():int{
if( r > 1.0 ) r = 1.0;
if( g > 1.0 ) g = 1.0;
if( b > 1.0 ) b = 1.0;
if( r < 0.0 ) r = 0.0;
if( g < 0.0 ) g = 0.0;
if( b < 0.0 ) b = 0.0;
var col:int = ((r*255) << 16) + ((g*255)<<8) + (b*255);
return col;
}
}