画像の余白をトリミング
PhotoShopなどの画像処理ソフトによくある、
画像の周辺部分の透明ピクセルのトリミングをしたいときに。
function trimming(source:BitmapData):BitmapData
{
var rect:Rectangle = source.getColorBoundsRect(0xff000000, 0, false);
var bmdResult:BitmapData = new BitmapData(rect.width, rect.height, true, 0);
bmdResult.copyPixels(source, rect, new Point(0, 0));
return bmdResult;
}
元となるBitmapDataを渡すとトリミングされたBitmapDataが返ってきます。
getColorBoundsRect()でalphaが0以外のピクセルの範囲を取得して、
その範囲でソースBitmapDataを切り抜いている感じです。
getColorBoundsRectの第1、第2引数を調整することで、
指定色や左上のピクセルのカラーでトリミングなんかも出来そうですね。