package {
import __AS3__.vec.Vector;
import flash.display.*;
public class Test extends Sprite {
[Embed(source = './assets/gray.jpg')]
private var EmbedImage:Class;
private var srcBmp:Bitmap;
private var dstBmp:Bitmap;
private var imgData:BitmapData;
private var fftTest:FFT;
private var re:Vector.<Number>;
private var im:Vector.<Number>;
private var tmp:Vector.<Number>;
private var max:Number = 0.0;
private var data:Number = 0.0;
private var w:int = 0;
private var h:int = 0;
private var bias:int = 0;
public function Test() {
srcBmp = new EmbedImage() as Bitmap;
imgData = srcBmp.bitmapData.clone();
w = imgData.width;
h = imgData.height;
fftTest = new FFT(w);
re = new Vector.<Number>(w*h, true);
im = re.concat();
tmp = re.concat();
// 画像データの直列化
for(var y:int=0; y<h; y++) {
for(var x:int=0; x<w; x++) {
re[x + y*w] = imgData.getPixel(x, y) & 0xff;
}
}
// 2D-FFT
fftTest.fft2d(re, im);
// スペクトルの振幅で正規化
for(var i:int=0; i<w*h; i++) {
tmp[i] = Math.sqrt(re[i]*re[i] + im[i]*im[i]);
if(tmp[i] > max) max = tmp[i];
re[i] /= tmp[i];
im[i] /= tmp[i];
}
// 逆2D-FFT
fftTest.fft2d(re, im, true);
// 8ビットに正規化 (表示用)
max = 0.0;
bias = 32;
for(var i:int=0; i<w*h; i++) {
if(re[i] > max) max = re[i];
}
for(var i:int=0; i<w*h; i++) {
re[i] = re[i]/max*255 + bias;
}
// 出力画像の生成
for(var y:int=0; y<h; y++) {
for(var x:int=0; x<w; x++) {
data = re[x + y*w];
if(data>255) data = 255;
if(data<0) data = 0;
color = data<<16 | data<<8 | data;
imgData.setPixel(x, y, color);
}
}
dstBmp = new Bitmap(imgData);
addChild(srcBmp);
addChild(dstBmp);
dstBmp.x += w;
}
}
}