2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2016-08-08 05:46:07 +00:00
|
|
|
using System.Collections.Generic;
|
2023-08-17 01:51:32 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-08-08 05:46:07 +00:00
|
|
|
using System.Linq;
|
2022-08-27 06:43:36 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 5 C-Gear Background Image
|
|
|
|
/// </summary>
|
|
|
|
public sealed class CGearBackground
|
2016-08-08 05:46:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public const string Extension = "cgb";
|
2023-01-22 04:02:33 +00:00
|
|
|
public const string Filter = $"C-Gear Background|*.{Extension}";
|
2022-06-18 18:04:24 +00:00
|
|
|
public const int Width = 256; // px
|
|
|
|
public const int Height = 192; // px
|
|
|
|
private const int ColorCount = 0x10;
|
|
|
|
private const int TileSize = 8;
|
|
|
|
private const int TileCount = (Width / TileSize) * (Height / TileSize); // 0x300
|
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
internal const int CountTilePool = 0xFF;
|
|
|
|
private const int LengthTilePool = CountTilePool * Tile.SIZE_TILE; // 0x1FE0
|
2023-08-17 01:51:32 +00:00
|
|
|
private const int LengthColorData = ColorCount * sizeof(ushort); // 0x20
|
2023-07-07 05:20:25 +00:00
|
|
|
private const int OffsetTileMap = LengthTilePool + LengthColorData; // 0x2000
|
2023-08-17 01:51:32 +00:00
|
|
|
private const int LengthTileMap = TileCount * sizeof(ushort); // 0x600
|
2023-07-07 05:20:25 +00:00
|
|
|
|
|
|
|
public const int SIZE_CGB = OffsetTileMap + LengthTileMap; // 0x2600
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/* CGearBackground Documentation
|
|
|
|
* CGearBackgrounds (.cgb) are tiled images.
|
|
|
|
* Tiles are 8x8, and serve as a tileset for building the image.
|
|
|
|
* The first 0x2000 bytes are the tile building region.
|
|
|
|
* A tile to have two pixels defined in one byte of space.
|
|
|
|
* A tile takes up 64 pixels, 32 bytes, 0x20 chunks.
|
|
|
|
* The last tile is actually the colors used in the image (16bit).
|
|
|
|
* Only 16 colors can be used for the entire image.
|
|
|
|
* 255 tiles may be chosen from, as (0x2000-(0x20))/0x20 = 0xFF
|
|
|
|
* The last 0x600 bytes are the tiles used.
|
|
|
|
* 256/8 = 32, 192/8 = 24
|
|
|
|
* 32 * 24 = 0x300
|
|
|
|
* The tiles are chosen based on the 16bit index of the tile.
|
|
|
|
* 0x300 * 2 = 0x600!
|
|
|
|
*
|
|
|
|
* CGearBackgrounds tilemap (when stored on BW) employs some obfuscation.
|
|
|
|
* BW obfuscates by adding 0xA0A0.
|
|
|
|
* The obfuscated number is then tweaked by adding 15*(i/17)
|
|
|
|
* To reverse, use a similar reverse calculation
|
|
|
|
* PSK files are basically raw game rips (obfuscated)
|
|
|
|
* CGB files are un-obfuscated / B2W2.
|
|
|
|
* Due to BW and B2W2 using different obfuscation adds, PSK files are incompatible between the versions.
|
|
|
|
*/
|
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public readonly int[] ColorPalette;
|
|
|
|
public readonly Tile[] Tiles;
|
|
|
|
public readonly TileMap Map;
|
|
|
|
|
|
|
|
public CGearBackground(ReadOnlySpan<byte> data)
|
2016-08-08 05:46:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (data.Length != SIZE_CGB)
|
2023-01-22 04:02:33 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(data));
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
var dataTiles = data[..LengthTilePool];
|
|
|
|
var dataColors = data.Slice(LengthTilePool, LengthColorData);
|
|
|
|
var dataArrange = data.Slice(OffsetTileMap, LengthTileMap);
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
Tiles = ReadTiles(dataTiles);
|
|
|
|
ColorPalette = ReadColorPalette(dataColors);
|
|
|
|
Map = new TileMap(dataArrange);
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
foreach (var tile in Tiles)
|
|
|
|
tile.SetTile(ColorPalette);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private CGearBackground(int[] palette, Tile[] tilelist, TileMap tm)
|
|
|
|
{
|
|
|
|
Map = tm;
|
|
|
|
ColorPalette = palette;
|
|
|
|
Tiles = tilelist;
|
|
|
|
}
|
2020-12-08 04:14:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Writes the <see cref="CGearBackground"/> data to a binary form.
|
|
|
|
/// </summary>
|
2023-07-07 05:20:25 +00:00
|
|
|
/// <param name="data">Destination buffer to write the skin to</param>
|
|
|
|
/// <param name="cgb">True if the destination game is <see cref="GameVersion.B2W2"/>, otherwise false for <see cref="GameVersion.BW"/>.</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <returns>Serialized skin data for writing to the save file</returns>
|
2023-07-07 05:20:25 +00:00
|
|
|
public void Write(Span<byte> data, bool cgb)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var dataTiles = data[..LengthTilePool];
|
|
|
|
var dataColors = data.Slice(LengthTilePool, LengthColorData);
|
|
|
|
var dataArrange = data.Slice(OffsetTileMap, LengthTileMap);
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
WriteTiles(dataTiles, Tiles);
|
|
|
|
WriteColorPalette(dataColors, ColorPalette);
|
|
|
|
Map.Write(dataArrange, cgb);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-08-08 05:46:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static Tile[] ReadTiles(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var result = new Tile[data.Length / Tile.SIZE_TILE];
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2016-08-08 05:46:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var span = data.Slice(i * Tile.SIZE_TILE, Tile.SIZE_TILE);
|
|
|
|
result[i] = new Tile(span);
|
2016-08-08 05:46:07 +00:00
|
|
|
}
|
2023-07-07 05:20:25 +00:00
|
|
|
return result;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static void WriteTiles(Span<byte> data, ReadOnlySpan<Tile> tiles)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
for (int i = 0; i < tiles.Length; i++)
|
2016-08-08 05:46:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var tile = tiles[i];
|
|
|
|
var span = data.Slice(i * Tile.SIZE_TILE, Tile.SIZE_TILE);
|
|
|
|
tile.Write(span);
|
2016-08-08 05:46:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static int[] ReadColorPalette(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
var colors = new int[data.Length / 2]; // u16->rgb32
|
|
|
|
ReadColorPalette(data, colors);
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ReadColorPalette(ReadOnlySpan<byte> data, Span<int> colors)
|
|
|
|
{
|
|
|
|
var buffer = MemoryMarshal.Cast<byte, ushort>(data)[..colors.Length];
|
|
|
|
for (int i = 0; i < colors.Length; i++)
|
|
|
|
{
|
|
|
|
var value = buffer[i];
|
|
|
|
if (!BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
|
|
|
colors[i] = Color15Bit.GetColorExpand(value);
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
}
|
2023-02-17 07:51:15 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static void WriteColorPalette(Span<byte> data, ReadOnlySpan<int> colors)
|
2023-01-22 04:02:33 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
var buffer = MemoryMarshal.Cast<byte, ushort>(data)[..colors.Length];
|
2023-07-07 05:20:25 +00:00
|
|
|
for (int i = 0; i < colors.Length; i++)
|
2016-08-08 05:46:07 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
var value = Color15Bit.GetColorCompress(colors[i]);
|
|
|
|
if (!BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
|
|
|
buffer[i] = value;
|
2016-08-08 05:46:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
/// Creates a new C-Gear Background object from an input image data byte array, with 32 bits per pixel.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Image data</param>
|
|
|
|
/// <returns>new C-Gear Background object</returns>
|
2022-08-27 06:43:36 +00:00
|
|
|
public static CGearBackground GetBackground(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
const int bpp = 4;
|
|
|
|
if (Width * Height * bpp != data.Length)
|
|
|
|
throw new ArgumentException("Invalid image data size.");
|
|
|
|
|
2023-04-15 08:58:37 +00:00
|
|
|
var colors = GetColorData(data);
|
|
|
|
var palette = colors.Distinct().ToArray();
|
|
|
|
if (palette.Length > ColorCount)
|
|
|
|
throw new ArgumentException($"Too many unique colors. Expected <= 16, got {palette.Length}");
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-04-15 08:58:37 +00:00
|
|
|
var tiles = GetTiles(colors, palette);
|
2022-06-18 18:04:24 +00:00
|
|
|
GetTileList(tiles, out List<Tile> tilelist, out TileMap tm);
|
2023-07-07 05:20:25 +00:00
|
|
|
if (tilelist.Count >= CountTilePool)
|
2022-06-18 18:04:24 +00:00
|
|
|
throw new ArgumentException($"Too many unique tiles. Expected < 256, received {tilelist.Count}.");
|
2020-12-08 04:14:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Finished!
|
2023-04-15 08:58:37 +00:00
|
|
|
return new CGearBackground(palette, tilelist.ToArray(), tm);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-01-14 05:31:02 +00:00
|
|
|
|
2023-04-15 08:58:37 +00:00
|
|
|
private static int[] GetColorData(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-15 08:58:37 +00:00
|
|
|
var pixels = MemoryMarshal.Cast<byte, int>(data);
|
2022-08-27 06:43:36 +00:00
|
|
|
int[] colors = new int[pixels.Length];
|
|
|
|
for (int i = 0; i < pixels.Length; i++)
|
2023-04-15 08:58:37 +00:00
|
|
|
{
|
|
|
|
var pixel = pixels[i];
|
|
|
|
if (!BitConverter.IsLittleEndian)
|
|
|
|
pixel = ReverseEndianness(pixel);
|
2023-08-17 01:51:32 +00:00
|
|
|
colors[i] = Color15Bit.GetColorAsOpaque(pixel);
|
2023-04-15 08:58:37 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return colors;
|
|
|
|
}
|
2018-01-14 05:31:02 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static Tile[] GetTiles(ReadOnlySpan<int> colors, ReadOnlySpan<int> palette)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var tiles = new Tile[TileCount];
|
|
|
|
for (int i = 0; i < tiles.Length; i++)
|
|
|
|
tiles[i] = GetTile(colors, palette, i);
|
|
|
|
return tiles;
|
|
|
|
}
|
2018-01-14 05:31:02 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static Tile GetTile(ReadOnlySpan<int> colors, ReadOnlySpan<int> palette, int tileIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
int x = (tileIndex * 8) % Width;
|
|
|
|
int y = 8 * ((tileIndex * 8) / Width);
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var t = new Tile();
|
2023-07-07 05:20:25 +00:00
|
|
|
var choices = t.ColorChoices;
|
2022-06-18 18:04:24 +00:00
|
|
|
for (uint ix = 0; ix < 8; ix++)
|
2018-01-14 05:31:02 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
for (uint iy = 0; iy < 8; iy++)
|
|
|
|
{
|
|
|
|
int index = ((int) (y + iy) * Width) + (int) (x + ix);
|
2023-04-15 18:28:35 +00:00
|
|
|
var c = colors[index];
|
2018-01-14 05:31:02 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
choices[(ix % 8) + (iy * 8)] = (byte)palette.IndexOf(c);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-01-14 05:31:02 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
t.SetTile(palette);
|
|
|
|
return t;
|
|
|
|
}
|
2018-01-14 05:31:02 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static void GetTileList(ReadOnlySpan<Tile> tiles, out List<Tile> tilelist, out TileMap tm)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
tilelist = new List<Tile> { tiles[0] };
|
2023-07-07 05:20:25 +00:00
|
|
|
tm = new TileMap(LengthTileMap);
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// start at 1 as the 0th tile is always non-duplicate
|
|
|
|
for (int i = 1; i < tm.TileChoices.Length; i++)
|
|
|
|
FindPossibleRotatedTile(tiles[i], tilelist, tm, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void FindPossibleRotatedTile(Tile t, IList<Tile> tilelist, TileMap tm, int tileIndex)
|
|
|
|
{
|
|
|
|
// Test all tiles currently in the list
|
2023-07-07 05:20:25 +00:00
|
|
|
for (byte i = 0; i < tilelist.Count; i++)
|
2018-01-14 05:31:02 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var rotVal = t.GetRotationValue(tilelist[i].ColorChoices);
|
2023-04-15 18:28:35 +00:00
|
|
|
if (rotVal == Tile.ROTATION_BAD)
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2023-07-07 05:20:25 +00:00
|
|
|
tm.TileChoices[tileIndex] = i;
|
2022-06-18 18:04:24 +00:00
|
|
|
tm.Rotations[tileIndex] = rotVal;
|
|
|
|
return;
|
2018-01-14 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// No tile found, add to list
|
|
|
|
tilelist.Add(t);
|
2023-04-15 18:28:35 +00:00
|
|
|
tm.TileChoices[tileIndex] = (byte)(tilelist.Count - 1);
|
2022-06-18 18:04:24 +00:00
|
|
|
tm.Rotations[tileIndex] = 0;
|
|
|
|
}
|
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public byte[] GetImageData()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
byte[] data = new byte[4 * Width * Height];
|
|
|
|
WriteImageData(data);
|
|
|
|
return data;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private void WriteImageData(Span<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
var tiles = Map.TileChoices;
|
|
|
|
var rotations = Map.Rotations;
|
|
|
|
for (int i = 0; i < tiles.Length; i++)
|
2018-01-14 05:31:02 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
var choice = tiles[i];
|
|
|
|
var rotation = rotations[i];
|
2022-06-18 18:04:24 +00:00
|
|
|
var tile = Tiles[choice];
|
2023-08-17 01:51:32 +00:00
|
|
|
var tileData = tile.Rotate(rotation);
|
|
|
|
|
|
|
|
int x = (i * TileSize) % Width;
|
|
|
|
int y = TileSize * ((i * TileSize) / Width);
|
|
|
|
|
|
|
|
for (int row = 0; row < TileSize; row++)
|
2018-01-14 05:31:02 +00:00
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
const int pixelLineSize = TileSize * sizeof(int);
|
|
|
|
int ofsSrc = row * pixelLineSize;
|
|
|
|
int ofsDest = (((y + row) * Width) + x) * sizeof(int);
|
|
|
|
var line = tileData.Slice(ofsSrc, pixelLineSize);
|
|
|
|
line.CopyTo(data[ofsDest..]);
|
2018-01-14 05:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-08 05:46:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Generation 5 image tile composed of 8x8 pixels.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>Each pixel's color choice is a nibble (4 bits).</remarks>
|
2022-06-18 18:04:24 +00:00
|
|
|
public sealed class Tile
|
|
|
|
{
|
|
|
|
internal const int SIZE_TILE = 0x20;
|
|
|
|
private const int TileWidth = 8;
|
|
|
|
private const int TileHeight = 8;
|
2023-04-15 18:28:35 +00:00
|
|
|
internal readonly byte[] ColorChoices = new byte[TileWidth * TileHeight];
|
2023-07-07 05:20:25 +00:00
|
|
|
|
|
|
|
// Keep track of known rotations for this tile.
|
2023-08-17 01:51:32 +00:00
|
|
|
// If the tile's rotated value has not yet been calculated, the field is null.
|
2023-04-15 18:28:35 +00:00
|
|
|
private byte[] PixelData = Array.Empty<byte>();
|
2022-06-18 18:04:24 +00:00
|
|
|
private byte[]? PixelDataX;
|
|
|
|
private byte[]? PixelDataY;
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
private const byte FlagFlipX = 0b0100; // 0x4
|
|
|
|
private const byte FlagFlipY = 0b1000; // 0x8
|
|
|
|
private const byte FlagFlipXY = FlagFlipX | FlagFlipY; // 0xC
|
|
|
|
private const byte FlagFlipNone = 0b0000; // 0x0
|
|
|
|
internal const byte ROTATION_BAD = byte.MaxValue;
|
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
internal Tile() { }
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
internal Tile(ReadOnlySpan<byte> data) : this()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (data.Length != SIZE_TILE)
|
2023-04-15 18:28:35 +00:00
|
|
|
throw new ArgumentException(null, nameof(data));
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
// Unpack the nibbles into the color choice array.
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < data.Length; i++)
|
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
var value = data[i];
|
2022-06-18 18:04:24 +00:00
|
|
|
var ofs = i * 2;
|
2023-04-15 18:28:35 +00:00
|
|
|
ColorChoices[ofs + 0] = (byte)(value & 0xF);
|
|
|
|
ColorChoices[ofs + 1] = (byte)(value >> 4);
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
internal void SetTile(ReadOnlySpan<int> palette) => PixelData = GetTileData(palette, ColorChoices);
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static byte[] GetTileData(ReadOnlySpan<int> Palette, ReadOnlySpan<byte> choices)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
byte[] data = new byte[choices.Length * 4];
|
|
|
|
SetTileData(data, Palette, choices);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SetTileData(Span<byte> result, ReadOnlySpan<int> palette, ReadOnlySpan<byte> colorChoices)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < colorChoices.Length; i++)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var choice = colorChoices[i];
|
|
|
|
var value = palette[choice];
|
|
|
|
var span = result.Slice(4 * i, 4);
|
2022-06-18 18:04:24 +00:00
|
|
|
WriteInt32LittleEndian(span, value);
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public void Write(Span<byte> data) => Write(data, ColorChoices);
|
|
|
|
|
|
|
|
private static void Write(Span<byte> data, ReadOnlySpan<byte> colorChoices)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var span = colorChoices.Slice(i * 2, 2);
|
2023-08-17 01:51:32 +00:00
|
|
|
var second = span[1] & 0xF;
|
|
|
|
var first = span[0] & 0xF;
|
|
|
|
data[i] = (byte)(first | (second << 4));
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public ReadOnlySpan<byte> Rotate(int rotFlip)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (rotFlip == 0)
|
2019-10-28 03:39:07 +00:00
|
|
|
return PixelData;
|
2023-08-17 01:51:32 +00:00
|
|
|
if ((rotFlip & FlagFlipXY) == FlagFlipXY)
|
|
|
|
return FlipY(PixelDataX ??= FlipX(PixelData, TileWidth), TileHeight);
|
|
|
|
if ((rotFlip & FlagFlipX) == FlagFlipX)
|
2022-06-18 18:04:24 +00:00
|
|
|
return PixelDataX ??= FlipX(PixelData, TileWidth);
|
2023-08-17 01:51:32 +00:00
|
|
|
if ((rotFlip & FlagFlipY) == FlagFlipY)
|
2022-06-18 18:04:24 +00:00
|
|
|
return PixelDataY ??= FlipY(PixelData, TileHeight);
|
|
|
|
return PixelData;
|
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
private static byte[] FlipX(ReadOnlySpan<byte> data, [ConstantExpected(Min = 0)] int width, [ConstantExpected(Min = 4, Max = 4)] int bpp = 4)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
byte[] result = new byte[data.Length];
|
2023-08-17 01:51:32 +00:00
|
|
|
FlipX(data, result, width, bpp);
|
2023-07-07 05:20:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
private static byte[] FlipY(ReadOnlySpan<byte> data, [ConstantExpected(Min = 0)] int height, [ConstantExpected(Min = 4, Max = 4)] int bpp = 4)
|
2023-07-07 05:20:25 +00:00
|
|
|
{
|
|
|
|
byte[] result = new byte[data.Length];
|
|
|
|
FlipY(data, result, height, bpp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
private static void FlipX(ReadOnlySpan<byte> data, Span<byte> result, [ConstantExpected(Min = 0)] int width, [ConstantExpected(Min = 4, Max = 4)] int bpp)
|
2023-07-07 05:20:25 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
int pixels = data.Length / bpp;
|
2023-08-17 01:51:32 +00:00
|
|
|
var resultInt = MemoryMarshal.Cast<byte, int>(result);
|
|
|
|
var dataInt = MemoryMarshal.Cast<byte, int>(data);
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < pixels; i++)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int x = i % width;
|
|
|
|
int y = i / width;
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
x = width - x - 1; // flip x
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
int dest = ((y * width) + x);
|
|
|
|
resultInt[dest] = dataInt[i];
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
private static void FlipY(ReadOnlySpan<byte> data, Span<byte> result, [ConstantExpected(Min = 0)] int height, [ConstantExpected(Min = 4, Max = 4)] int bpp)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
int pixels = data.Length / bpp;
|
2022-06-18 18:04:24 +00:00
|
|
|
int width = pixels / height;
|
2023-08-17 01:51:32 +00:00
|
|
|
var resultInt = MemoryMarshal.Cast<byte, int>(result);
|
|
|
|
var dataInt = MemoryMarshal.Cast<byte, int>(data);
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < pixels; i++)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int x = i % width;
|
|
|
|
int y = i / width;
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
y = height - y - 1; // flip y
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
int dest = ((y * width) + x);
|
|
|
|
resultInt[dest] = dataInt[i];
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
internal byte GetRotationValue(ReadOnlySpan<byte> tileColors)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// Check all rotation types
|
|
|
|
if (tileColors.SequenceEqual(ColorChoices))
|
2023-08-17 01:51:32 +00:00
|
|
|
return FlagFlipNone;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
if (IsMirrorX(tileColors))
|
2023-08-17 01:51:32 +00:00
|
|
|
return FlagFlipX;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsMirrorY(tileColors))
|
2023-08-17 01:51:32 +00:00
|
|
|
return FlagFlipY;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsMirrorXY(tileColors))
|
2023-08-17 01:51:32 +00:00
|
|
|
return FlagFlipXY;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
return ROTATION_BAD;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
private bool IsMirrorX(ReadOnlySpan<byte> tileColors)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
const int pixels = TileWidth * TileHeight;
|
|
|
|
for (int i = 0; i < pixels; i++)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
var index = (7 - (i & 7)) + (8 * (i / 8));
|
|
|
|
if (ColorChoices[index] != tileColors[i])
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
private bool IsMirrorY(ReadOnlySpan<byte> tileColors)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
const int pixels = TileWidth * TileHeight;
|
|
|
|
for (int i = 0; i < pixels; i++)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-09 07:43:29 +00:00
|
|
|
var index = pixels - (8 * (1 + (i / 8))) + (i & 7);
|
|
|
|
if (ColorChoices[index] != tileColors[i])
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-04-15 18:28:35 +00:00
|
|
|
private bool IsMirrorXY(ReadOnlySpan<byte> tileColors)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
const int pixels = TileWidth * TileHeight;
|
|
|
|
for (int i = 0; i < pixels; i++)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-09 07:43:29 +00:00
|
|
|
var index = pixels - 1 - i;
|
|
|
|
if (ColorChoices[index] != tileColors[i])
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
return true;
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public sealed class TileMap
|
|
|
|
{
|
2023-04-15 18:28:35 +00:00
|
|
|
public readonly byte[] TileChoices;
|
|
|
|
public readonly byte[] Rotations;
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public TileMap(int length)
|
|
|
|
{
|
|
|
|
TileChoices = new byte[length / 2];
|
|
|
|
Rotations = new byte[length / 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
internal TileMap(ReadOnlySpan<byte> data) : this(data.Length) => LoadData(data, TileChoices, Rotations);
|
|
|
|
|
|
|
|
private static void LoadData(ReadOnlySpan<byte> data, Span<byte> tiles, Span<byte> rotations)
|
|
|
|
{
|
|
|
|
bool isCGB = IsCGB(data);
|
|
|
|
if (!isCGB)
|
|
|
|
LoadDataPSK(data, tiles, rotations);
|
|
|
|
else
|
|
|
|
LoadDataCGB(data, tiles, rotations);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void LoadDataCGB(ReadOnlySpan<byte> data, Span<byte> tiles, Span<byte> rotations)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; i += 2)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var span = data.Slice(i, 2);
|
|
|
|
var tile = span[0];
|
|
|
|
var rot = span[1];
|
|
|
|
|
|
|
|
tiles[i / 2] = tile;
|
|
|
|
rotations[i / 2] = rot;
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-28 03:39:07 +00:00
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
private static void LoadDataPSK(ReadOnlySpan<byte> data, Span<byte> tiles, Span<byte> rotations)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; i += 2)
|
2019-10-28 03:39:07 +00:00
|
|
|
{
|
2023-07-07 05:20:25 +00:00
|
|
|
var span = data.Slice(i, 2);
|
|
|
|
var value = ReadUInt16LittleEndian(span);
|
|
|
|
var (tile, rot) = DecomposeValuePSK(value);
|
|
|
|
tiles[i / 2] = tile;
|
|
|
|
rotations[i / 2] = rot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsCGB(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
// check odd bytes for anything not rotation flag
|
|
|
|
for (int i = 0; i < data.Length; i += 2)
|
|
|
|
{
|
|
|
|
if ((data[i + 1] & ~0b1100) != 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Write(Span<byte> data, bool cgb) => Write(data, TileChoices, Rotations, cgb);
|
|
|
|
|
|
|
|
private static void Write(Span<byte> data, ReadOnlySpan<byte> choices, ReadOnlySpan<byte> rotations, bool cgb)
|
|
|
|
{
|
|
|
|
if (choices.Length != rotations.Length)
|
|
|
|
throw new ArgumentException($"length of {nameof(TileChoices)} and {nameof(Rotations)} must be equal");
|
|
|
|
if (data.Length != choices.Length + rotations.Length)
|
|
|
|
throw new ArgumentException($"data length must be twice the length of the {nameof(TileMap)}");
|
|
|
|
|
|
|
|
if (!cgb)
|
|
|
|
WriteDataPSK(data, choices, rotations);
|
|
|
|
else
|
|
|
|
WriteDataCGB(data, choices, rotations);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void WriteDataCGB(Span<byte> data, ReadOnlySpan<byte> choices, ReadOnlySpan<byte> rotations)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; i += 2)
|
|
|
|
{
|
|
|
|
var span = data.Slice(i, 2);
|
|
|
|
span[0] = choices[i / 2];
|
|
|
|
span[1] = rotations[i / 2];
|
2019-10-28 03:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-07 05:20:25 +00:00
|
|
|
|
|
|
|
private static void WriteDataPSK(Span<byte> data, ReadOnlySpan<byte> choices, ReadOnlySpan<byte> rotations)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; i += 2)
|
|
|
|
{
|
|
|
|
var span = data.Slice(i, 2);
|
|
|
|
var tile = choices[i / 2];
|
|
|
|
var rot = rotations[i / 2];
|
|
|
|
int val = GetPSKValue(tile, rot);
|
|
|
|
WriteUInt16LittleEndian(span, (ushort)val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static (byte Tile, byte Rotation) DecomposeValuePSK(ushort val)
|
|
|
|
{
|
2023-08-17 01:51:32 +00:00
|
|
|
ushort value = UnmapPSKValue(val);
|
2023-07-07 05:20:25 +00:00
|
|
|
byte tile = (byte)value;
|
|
|
|
byte rot = (byte)(value >> 8);
|
|
|
|
if (tile == CGearBackground.CountTilePool) // out of range?
|
|
|
|
tile = 0;
|
|
|
|
return (tile, rot);
|
|
|
|
}
|
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
private static ushort UnmapPSKValue(ushort val)
|
|
|
|
{
|
|
|
|
var rot = val & 0xFC00;
|
|
|
|
var trunc = (val & 0x3FF);
|
|
|
|
if (trunc is < 0xA0 or > 0x280)
|
|
|
|
return (ushort)(rot | CGearBackground.CountTilePool); // default empty
|
|
|
|
return (ushort)(rot | ((val & 0x1F) + (17 * ((trunc - 0xA0) >> 5))));
|
|
|
|
}
|
|
|
|
|
2023-07-07 05:20:25 +00:00
|
|
|
public static ushort GetPSKValue(byte tile, byte rot)
|
|
|
|
{
|
|
|
|
if (tile == CGearBackground.CountTilePool) // out of range?
|
|
|
|
tile = 0;
|
|
|
|
|
2023-08-17 01:51:32 +00:00
|
|
|
var result = ((rot & 0x0C) << 8) | ((15 * (tile / 17)) + tile + 0xA0) | 0xA000;
|
2023-07-07 05:20:25 +00:00
|
|
|
return (ushort)result;
|
|
|
|
}
|
|
|
|
}
|