/// <summary> /// Initializes a new instance of the <see cref="Tileset"/> class from the specified source image. /// </summary> /// <param name="source">The source image.</param> public Tileset(Bitmap source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (source.Width < 8 || source.Height < 8) { throw new ArgumentException("Image must be at least 8x8 pixels.", nameof(source)); } // Create the tiles var width = source.Width / 8; var height = source.Height / 8; Tiles = new TilesetEntry[width * height]; // Copy image data from source using (var fb = DirectBitmap.FromImage(source)) { // Create the palette Palette = Palette.Create(source); // Create the tiles for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { ref var tile = ref Tiles[x + y * width]; for (int j = 0; j < 8; j++) { for (int i = 0; i < 8; i++) { var color = Color.FromArgb(fb.Pixels[(x * 8 + i) + (y * 8 + j) * fb.Width]); // Find the color index var index = Palette.IndexOf(color); if (index < 0) { throw new IndexOutOfRangeException(); } // Copy to the tile tile[i, j] = index; } } } } }
public unsafe bool Equals(ref TilesetEntry other, bool flipX = false, bool flipY = false) { for (int srcY = 0; srcY < 8; srcY++) { for (int srcX = 0; srcX < 8; srcX++) { var dstX = flipX ? (7 - srcX) : srcX; var dstY = flipY ? (7 - srcY) : srcY; if (this[srcX, srcY] != other[dstX, dstY]) { return(false); } } } return(true); }