forked from: forked from: Edgeとり。
Edgeとり。
自分自身のためのメモ。
できたから上げただけでチャレンジフェイズで殺されるレベル。
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import flash.display.Loader;
[ SWF( width = "160" , height = "120" , backgroundColor = "0xFFFFFF" , frameRate = "60" ) ]
public class Main extends Sprite
{
// 画像データ
private const data:String = "iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4BAMAAACHjORcAAAAMFBMVEX/////zkL///////////////////////////////////////////////////////9OIM9nAAAA/0lEQVR4nO2YbQ6CMBBEuzdg739ZQ4KCQtudZQRt5v0isXmZ7gcJliKEEEIICuZ0oU9co89QjTYbqSnpGZ0d0dkRnW10ttEWIa+M9IjlWxF5QuvcGa5Gpy34clq7LYmWeSuiJSrcitiJD0d0TwlrbTFPCt+2xQ7iwcLtKL4ejSCcPjVnhQ1GE5YbhNjbLXDl0YR9X/J1c6kQunNECEW8R4jceRAhUsTApowmDPn+QAjMzbXC6fk7UbhMAUm4HjgnPDgQ9u3munIiLdxXHxT2dKeEldlICuuTBgnXEnbOgPHaqxAWRj8djC0s0V3Of9xcJdw0hfy/mhBCCCGEEL/IA9soYNoy7IFIAAAAAElFTkSuQmCC"
private var bitmap:Bitmap;
/**
* コンストラクタ
*/
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);
// entry point
Base64ImageLoader.load(data, compfunc);
}
private function compfunc(loader:Loader):void {
bitmap = new Bitmap(new BitmapData(
loader.width,loader.height,true,0xFFFFFFFF));
bitmap.bitmapData.draw(loader);
edgeTrace(bitmap, 0xFFFFFFFF);
addChild(bitmap);
}
private function getColor(byteArray:ByteArray, index:uint ):uint{
var i:uint = (index * 4);
var color:uint = byteArray[i] << 8*3
| byteArray[i + 1] << 8*2
| byteArray[i + 2] << 8
| byteArray[i + 3];
return color;
}
private function edgeTrace(bitmap:Bitmap, innerColor:uint, edgeColor:uint = 0xFF000000):void {
var width:uint = bitmap.bitmapData.width;
var height:uint = bitmap.bitmapData.height;
var byteArray:ByteArray = bitmap.bitmapData.getPixels( new Rectangle(0, 0, 160, 120) );
var newByteArray:ByteArray = new ByteArray();
var indexLength:uint = byteArray.length / 4;
byteArray.position = 0;
for (var i:int = 0; i < indexLength; i++) {
newByteArray.writeUnsignedInt( byteArray.readUnsignedInt() ); //edgeColor & (0xFF000000) >> 3;
if ( getColor(byteArray, i) == innerColor )continue;
var flag:Boolean = false;
if ( i%width + 1 < width && getColor(byteArray,i +1) == innerColor )flag=true;
if ( i%width - 1 > 0 && getColor(byteArray, i - 1) == innerColor )flag=true;
if ( (i + width)/width < height && getColor( byteArray, i + width) == innerColor )flag=true;
if ( (i - width)/width > 0 && getColor( byteArray, i - width) == innerColor )flag=true;
if (!flag) continue;
newByteArray[i*4]=0xFF; //edgeColor & (0xFF000000) >> 3;
newByteArray[i*4+1]=0x00; //edgeColor & (0xFF000000) >> 3;
newByteArray[i*4+2]=0x00; //edgeColor & (0xFF000000) >> 3;
newByteArray[i*4+3]=0x00; //edgeColor & (0xFF000000) >> 3;
}
newByteArray.position = 0;
bitmap.bitmapData.setPixels( new Rectangle(0, 0, 160, 120), newByteArray);
}
}
}
class Base64ImageLoader
{
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
static public function load(data:String,compfunc:Function):Loader
{
var byteArray:ByteArray;
var base64Decoder:Base64Decoder;
var loader:Loader;
var bmp:Bitmap = new Bitmap();
base64Decoder = new Base64Decoder();
base64Decoder.decode(data);
try {
byteArray = base64Decoder.toByteArray();
byteArray.position = 0;
} catch (e:Error) {
return null;
}
loader = new Loader();
loader.loadBytes(byteArray);
//読み込みが完了したらbitmapDataに書き込む。
loader.contentLoaderInfo.addEventListener(
Event.COMPLETE,
function(e:Event):void{compfunc(loader);}
);
return loader;
}
}