Merge branch 'character-editor-v3' into new-editors
This commit is contained in:
@@ -2,6 +2,8 @@ package funkin.backend.utils;
|
||||
|
||||
import flixel.util.FlxColor;
|
||||
import openfl.display.BitmapData;
|
||||
import openfl.geom.Point;
|
||||
import openfl.geom.Rectangle;
|
||||
|
||||
class BitmapUtil {
|
||||
/**
|
||||
@@ -66,4 +68,40 @@ class BitmapUtil {
|
||||
}
|
||||
return mostPresentColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new bitmap without any empty transperent space on the edges
|
||||
* @param bitmap The bitmap to be cropped
|
||||
*/
|
||||
public static function crop(bitmap:BitmapData) {
|
||||
var bitmapBounds:Rectangle = BitmapUtil.bounds(bitmap);
|
||||
|
||||
var croppedBitmap:BitmapData = new BitmapData(Std.int(bitmapBounds.width), Std.int(bitmapBounds.height), true, 0x00000000);
|
||||
croppedBitmap.copyPixels(bitmap, bitmapBounds, new Point(0,0));
|
||||
return croppedBitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bounds of non empty pixels in the bitmap
|
||||
* @param bitmap
|
||||
* @return
|
||||
*/
|
||||
public static function bounds(bitmap:BitmapData, ?limit:Rectangle = null):Rectangle {
|
||||
var minX:Int = limit != null ? Std.int(limit.width) : bitmap.width;
|
||||
var minY:Int = limit != null ? Std.int(limit.height) : bitmap.height;
|
||||
var maxX:Int = limit != null ? Std.int(limit.x) : 0;
|
||||
var maxY:Int = limit != null ? Std.int(limit.y) : 0;
|
||||
|
||||
for (y in (limit != null ? Std.int(limit.x) : 0)...(limit != null ? Std.int(limit.height) : bitmap.height))
|
||||
for (x in (limit != null ? Std.int(limit.y) : 0)...(limit != null ? Std.int(limit.width) : bitmap.width)) {
|
||||
if (bitmap.getPixel32(x, y) != 0x00000000) {
|
||||
if (x < minX) minX = x;
|
||||
if (y < minY) minY = y;
|
||||
if (x > maxX) maxX = x;
|
||||
if (y > maxY) maxY = y;
|
||||
}
|
||||
}
|
||||
|
||||
return new Rectangle(minX, minY, maxX-minX+1, maxY-minY+1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user