public Bitmap GetBitmap()
        {
            Bitmap tileImg = new Bitmap(TileSize, TileSize);

            BitmapData bmpData = tileImg.LockBits(new Rectangle(0, 0, tileImg.Width, tileImg.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            unsafe
            {
                Triplet <byte[, ]> triplet = GetMatrices <byte>();
                byte *cusor = (byte *)bmpData.Scan0.ToPointer();
                for (int y = 0; y < bmpData.Height; y++)
                {
                    for (int x = 0; x < bmpData.Width; x++)
                    {
                        cusor[0] = triplet.Z[x, y];
                        cusor[1] = triplet.Y[x, y];
                        cusor[2] = triplet.X[x, y];
                        cusor   += 3;
                    }
                    cusor += (bmpData.Stride - 3 * (bmpData.Width));
                }
            }
            tileImg.UnlockBits(bmpData);

            return(tileImg);
        }
        public static Tile RandomTile()
        {
            var buffer = new Triplet <byte[]>(
                x: new byte[0x1000],
                y: new byte[0x1000],
                z: new byte[0x1000]);
            var random = new Random(Guid.NewGuid().GetHashCode());

            buffer.ForEach(buf => random.NextBytes(buf));
            return(Tile.FromArrays(buffer));
        }
 public static Tile FromMatrices <T>(Triplet <T[, ]> input)
     where T : struct, IConvertible
 {
     return(new Tile
     {
         _triplet = new Triplet <int[]> {
             X = input.X == null ? null : input.X.Flatten().Select(i => Convert.ToInt32(i)).ToArray(),
             Y = input.Y == null ? null : input.Y.Flatten().Select(i => Convert.ToInt32(i)).ToArray(),
             Z = input.Z == null ? null : input.Z.Flatten().Select(i => Convert.ToInt32(i)).ToArray(),
         }
     });
 }
 public static Tile FromArrays <T>(Triplet <T[]> input)
     where T : struct, IConvertible
 {
     return(new Tile {
         _triplet = new Triplet <int[]> {
             // Convert should work since T is IConvertible
             // SRL encoded raw data may be a NULL
             X = input.X == null ? null : input.X.Select(i => Convert.ToInt32(i)).ToArray(),
             Y = input.Y == null ? null : input.Y.Select(i => Convert.ToInt32(i)).ToArray(),
             Z = input.Z == null ? null : input.Z.Select(i => Convert.ToInt32(i)).ToArray()
         }
     });
 }
        public Tile Deserialize(Triplet <string> serializedTile)
        {
            var dataList = new List <int[]>();

            foreach (var data in serializedTile)
            {
                string[] split    = data.Split(new[] { ' ', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                var      intArray = new int[split.Length];
                for (int i = 0; i < split.Length; i++)
                {
                    intArray[i] = Convert.ToInt16(split[i]);
                }
                dataList.Add(intArray);
            }

            Triplet <int[]> triplet = new Triplet <int[]>(dataList[0], dataList[1], dataList[2]);
            Tile            tile    = Tile.FromArrays <int>(triplet);

            return(tile);
        }
        public bool Add(Tile addition)
        {
            Triplet <int[]> triplet = addition.GetArrays <int>();

            if (triplet.X != null && triplet.Y != null && triplet.Z != null &&
                _triplet.X != null && _triplet.Y != null && _triplet.Z != null &&
                triplet.X.Length == _triplet.X.Length && triplet.Y.Length == _triplet.Y.Length && triplet.Z.Length == _triplet.Z.Length)
            {
                for (int i = 0; i < _triplet.X.Length; i++)
                {
                    _triplet.X[i] += triplet.X[i];
                }
                for (int i = 0; i < _triplet.Y.Length; i++)
                {
                    _triplet.Y[i] += triplet.Y[i];
                }
                for (int i = 0; i < _triplet.Z.Length; i++)
                {
                    _triplet.Z[i] += triplet.Z[i];
                }
            }
            return(false);
        }
        public static Tile FromBitmap(Image image, int leftOffset, int topOffset)
        {
            var bitmap = new Bitmap(image);
            var RSet   = new byte[TileSize, TileSize];
            var GSet   = new byte[TileSize, TileSize];
            var BSet   = new byte[TileSize, TileSize];

            int right  = Math.Min(image.Width - 1, leftOffset + TileSize - 1);
            int bottom = Math.Min(image.Height - 1, topOffset + TileSize - 1);

            BitmapData bmpData = bitmap.LockBits(new Rectangle(leftOffset, topOffset, right - leftOffset + 1, bottom - topOffset + 1), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            unsafe
            {
                byte *cusor = (byte *)bmpData.Scan0.ToPointer();
                for (int y = topOffset; y <= bottom; y++)
                {
                    for (int x = leftOffset; x <= right; x++)
                    {
                        int tileX = x - leftOffset;
                        int tileY = y - topOffset;
                        BSet[tileX, tileY] = cusor[0];
                        GSet[tileX, tileY] = cusor[1];
                        RSet[tileX, tileY] = cusor[2];
                        cusor += 3;
                    }
                    cusor += (bmpData.Stride - 3 * (bmpData.Width));
                }
            }
            bitmap.UnlockBits(bmpData);

            var  triplet = new Triplet <byte[, ]>(RSet, GSet, BSet);
            Tile tile    = Tile.FromMatrices <byte>(triplet);

            return(tile);
        }
 // privatize the constructor.
 // user can only get a tile object from input methods.
 private Tile()
 {
     _triplet = new Triplet <int[]>();
 }
 public static Tile FromStrings(Triplet <String> input, ITileSerializer serializer)
 {
     return(serializer.Deserialize(input));
 }
 public Tile Deserialize(Triplet <string> serializedTile)
 {
     // this is joking
     return(Tile.RandomTile());
 }
 public bool TryDeserialize(Triplet <string> input, out Tile output)
 {
     throw new NotImplementedException();
 }