public void ReconstructionTest() { var arr = new byte[] { 1, 2, 3, 4 }; var b = new FastBitArray(arr); var arr2 = b.ToByteArray(); Assert.Equal(arr.Length, arr2.Length); Assert.Equal(arr[0], arr2[0]); Assert.Equal(arr[arr.Length - 1], arr2[arr2.Length - 1]); Assert.Equal(4 * 8, b.Length); b.Length++; Assert.Equal((4 * 8) + 1, b.Length); arr2 = b.ToByteArray(); Assert.Equal(arr[0], arr2[0]); Assert.Equal(0, arr2[arr2.Length - 1]); b.SetBit(4 * 8, true); arr2 = b.ToByteArray(); Assert.Equal(arr[0], arr2[0]); Assert.Equal(1, arr2[arr2.Length - 1]); var b2 = new FastBitArray(arr2); var arr3 = b2.ToByteArray(); Assert.Equal(arr2.Length, arr3.Length); Assert.Equal(arr2[0], arr3[0]); Assert.Equal(arr2[arr2.Length - 1], arr3[arr3.Length - 1]); }
public static unsafe byte[] GetMaskFromImage(Bitmap bmp) { int stride = ((bmp.Width + 7) / 8) * 8; FastBitArray arr = new FastBitArray(stride * bmp.Height); int strideFactor = 0; var data = bmp.BasicLockBits(); int bmpStride = data.Stride / 4; uint *ptrInt = (uint *)data.Scan0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { if (*(ptrInt + x + (y * bmpStride)) == 0xFFFFFFFFu) { arr.SetTrueReverse(x + strideFactor); } } strideFactor += stride; } bmp.UnlockBits(data); return(arr.ToByteArray()); }
public static List <BufferRegion> GetMasksForSprite(ProjectFile pf, AssetSprite spr, out Rect maskbbox, List <Bitmap> bitmaps = null) { if (bitmaps == null) { bitmaps = GetBitmaps(pf, spr.Width, spr.Height, spr.TextureItems); } if (bitmaps.Count == 0) { maskbbox = new Rect(0, 0, 0, 0); return(new List <BufferRegion>()); } var info = spr.CollisionMask; if (info.Left == null || info.Top == null || info.Right == null || info.Bottom == null) { maskbbox = new Rect(spr.Width - 1, spr.Height - 1, 0, 0); } else { maskbbox = null; } if (spr.CollisionMask.Type == MaskType.PrecisePerFrame) { // Get masks for individual frames List <BufferRegion> res = new List <BufferRegion>(bitmaps.Count); for (int i = 0; i < bitmaps.Count; i++) { res.Add(new BufferRegion(GetMaskForBitmap(bitmaps[i], spr, ref maskbbox).ToByteArray())); } if (maskbbox != null) { maskbbox.Left = Math.Max(0, maskbbox.Left); maskbbox.Top = Math.Max(0, maskbbox.Top); maskbbox.Right = Math.Min(spr.Width - 1, maskbbox.Right); maskbbox.Bottom = Math.Min(spr.Height - 1, maskbbox.Bottom); } return(res); } else { // Get the mask for the first frame, then add following frames FastBitArray mask = GetMaskForBitmap(bitmaps[0], spr, ref maskbbox); for (int i = 1; i < bitmaps.Count; i++) { GetMaskForBitmap(bitmaps[i], spr, ref maskbbox, mask); } if (maskbbox != null) { maskbbox.Left = Math.Max(0, maskbbox.Left); maskbbox.Top = Math.Max(0, maskbbox.Top); maskbbox.Right = Math.Min(spr.Width - 1, maskbbox.Right); maskbbox.Bottom = Math.Min(spr.Height - 1, maskbbox.Bottom); } return(new List <BufferRegion> { new BufferRegion(mask.ToByteArray()) }); } }