static SwfRectIntData CalculateBitmapTrimmedRect(SwfLibraryBitmapDefine bitmap) { var rect = new SwfRectIntData { xMin = bitmap.Width, yMin = bitmap.Height, xMax = 0, yMax = 0 }; for (var i = 0; i < bitmap.Height; ++i) { for (var j = 0; j < bitmap.Width; ++j) { var a = bitmap.ARGB32[(j + i * bitmap.Width) * 4]; if (a > 0) { rect.xMin = Mathf.Min(j, rect.xMin); rect.yMin = Mathf.Min(i, rect.yMin); rect.xMax = Mathf.Max(j + 1, rect.xMax); rect.yMax = Mathf.Max(i + 1, rect.yMax); } } } return(rect.width < 1 || rect.height < 1 ? new SwfRectIntData(0, 0, 1, 1) : rect); }
static byte[] TrimBitmapByRect(SwfBitmapData bitmap, SwfRectIntData rect) { var argb32 = new byte[rect.area * 4]; for (var i = 0; i < rect.height; ++i) { var src_index = rect.xMin + (rect.yMin + i) * bitmap.RealWidth; var dst_index = i * rect.width; Array.Copy( bitmap.ARGB32, src_index * 4, argb32, dst_index * 4, rect.width * 4); } return(argb32); }