public static void GenerateTileMaps(
            IList <Bitmap> bitmaps
            , byte paletteBankIndexOffset
            , out Color[] masterPalette
            , out Tile[] masterTileSet
            , out TileMap[] tileMaps
            , bool skipSort = false
            )
        {
            List <ProcessedPaletteContainer> bitmapPalettes = new List <ProcessedPaletteContainer>();

            // Validate bitmaps and collect palettes
            for (int bitmapIndex = 0; bitmapIndex < bitmaps.Count; ++bitmapIndex)
            {
                Bitmap bitmap = bitmaps[bitmapIndex];

                Size size = bitmap.Size;

                if (size.Width % TileConfig.TileWidth != 0 || size.Height % TileConfig.TileHeight != 0)
                {
                    throw new Exception("Size not compatible with GBA tiles. Width and height of pixels must be multiples of 8.");
                }

                int     xStart = 0;
                int     yStart = 0;
                int     width  = bitmap.Width;
                int     height = bitmap.Height;
                Color[] preProcessedPalette = PaletteHelper.GeneratePaletteFromImage(bitmap, xStart, yStart, width, height);

                // Validate pallet length
                {
                    if (preProcessedPalette.Length > PaletteHelper.MAX_PALETTE_LENGTH)
                    {
                        throw new Exception(string.Format("Palette length ({0}) out of range for the GBA ({1})", preProcessedPalette.Length, PaletteHelper.MAX_PALETTE_LENGTH));
                    }
                }

                ProcessedPaletteContainer palContainer = new ProcessedPaletteContainer();
                palContainer.bitmapIndex = bitmapIndex;
                palContainer.palette     = preProcessedPalette;

                bitmapPalettes.Add(palContainer);
            }

            if (!skipSort)
            {
                // Sort bitmap palette. Anything more than 16 colours should be grouped next to each other. Less than 16 should always be a full 16.
                bitmapPalettes.Sort(new ProcessedPaletteComparer());
            }

            // Get palettes of each image, merge into a master palette
            List <Color> masterPaletteList = new List <Color>();
            List <ProcessedBitmapContainer> processedBitmapList = new List <ProcessedBitmapContainer>();

            {
                Color transparencyColour = Color.FromArgb(0);

                for (int i = 0; i < bitmapPalettes.Count; ++i)
                {
                    var bp = bitmapPalettes[i];

                    Debug.Assert(bp.palette[0] == transparencyColour);

                    bool pal4bbp      = bp.palette.Length <= PaletteHelper.PALETTE_LENGTH_4BBP;
                    int  paletteIndex = pal4bbp ? Find4bbpPaletteIndex(masterPaletteList, bp.palette) : -1;

                    if (pal4bbp && paletteIndex < 0)    // Add the new palette in
                    {
                        // Make sure our 4bbp palette is aligned properly, every set of 16 colour palettes starts with pure transparency
                        while (masterPaletteList.Count % PaletteHelper.PALETTE_LENGTH_4BBP != 0)
                        {
                            masterPaletteList.Add(transparencyColour);
                        }

                        // TODO
                        // If 2 bitmap palettes can fit in the same GBA palette, merge them into one.
                        paletteIndex = Get4bbpPaletteIndexForSize(masterPaletteList);

                        masterPaletteList.AddRange(bp.palette);
                    }

                    if (!pal4bbp)
                    {
                        // Only add colour into the master palette if it's not already in there.
                        foreach (Color col in bp.palette)
                        {
                            if (!masterPaletteList.Contains(col))
                            {
                                masterPaletteList.Add(col);
                            }
                        }
                    }

                    if ((paletteIndex + paletteBankIndexOffset) >= PaletteHelper.MAX_PALETTE_INDEX)
                    {
                        throw new Exception(string.Format("Palette index {0} with palette bank offset {1} is not valid for map data", paletteIndex, paletteBankIndexOffset));
                    }

                    ProcessedBitmapContainer pbc = new ProcessedBitmapContainer();
                    pbc.bitmapIndex  = bp.bitmapIndex;
                    pbc.paletteIndex = paletteIndex;
                    pbc.bitmap       = bitmaps[bp.bitmapIndex];

                    processedBitmapList.Add(pbc);
                }
            }

            masterPalette = masterPaletteList.ToArray();

            Console.WriteLine("Total colours found = " + masterPalette.Length);

            if (masterPalette.Length >= PaletteHelper.MAX_PALETTE_LENGTH)
            {
                throw new Exception(string.Format("Master palette has too many colours ({0}/{1})", masterPalette.Length, PaletteHelper.MAX_PALETTE_LENGTH));
            }

            Console.WriteLine("Processing master tileset");

            // Get all unique tiles sorted via palette indicies. Need to check if local palette was greater than 16 or not
            List <Tile> uniqueTileSetList = new List <Tile>();

            foreach (ProcessedBitmapContainer pbc in processedBitmapList)
            {
                Tile[,] rawTileMap = BitmapToTileArray(pbc.bitmap, masterPalette, pbc.paletteIndex);
                pbc.rawTileMap     = rawTileMap;

                FillUniqueTileset(rawTileMap, uniqueTileSetList);
            }

            masterTileSet = uniqueTileSetList.ToArray();

            // Generate tilemaps from master list. Remember palette index, but we should be able to forget about it. We can overlap tiles that are the same but different colours.
            List <TileMap> tileMapList = new List <TileMap>();

            foreach (ProcessedBitmapContainer pbc in processedBitmapList)
            {
                TileMap tilemap = new TileMap(pbc.rawTileMap, masterTileSet, pbc.paletteIndex);
                tileMapList.Add(tilemap);
            }

            tileMaps = tileMapList.ToArray();
        }
Ejemplo n.º 2
0
        public void Convert(string inputPath, string outputPath, Bitmap inputBitmap)
        {
            string namespaceName = string.Format(NAMESPACE_FORMAT, Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)));

            List <Bitmap> bitmaps = new List <Bitmap>();

            bitmaps.Add(inputBitmap);

            List <ProcessedPaletteContainer> bitmapPalettes = new List <ProcessedPaletteContainer>();

            Console.WriteLine("Processing master palette");

            // Validate bitmaps and collect palettes
            for (int bitmapIndex = 0; bitmapIndex < bitmaps.Count; ++bitmapIndex)
            {
                Bitmap bitmap = bitmaps[bitmapIndex];

                Size size = bitmap.Size;
                if (size.Width % TileConfig.c_TILEWIDTH != 0 || size.Height % TileConfig.c_TILEHEIGHT != 0)
                {
                    throw new Exception("Size not compatible with GBA tiles. Width and height of pixels must be multiples of 8.");
                }

                int     xStart = 0;
                int     yStart = 0;
                int     width  = bitmap.Width;
                int     height = bitmap.Height;
                Color[] preProcessedPalette = PaletteHelper.GeneratePaletteFromImage(bitmap, xStart, yStart, width, height);

                // Validate pallet length
                {
                    if (preProcessedPalette.Length > PaletteHelper.MAX_PALETTE_LENGTH)
                    {
                        throw new Exception(string.Format("Palette length ({0}) out of range for the GBA ({1})", preProcessedPalette.Length, PaletteHelper.MAX_PALETTE_LENGTH));
                    }
                }

                ProcessedPaletteContainer palContainer = new ProcessedPaletteContainer();
                palContainer.bitmapIndex = bitmapIndex;
                palContainer.palette     = preProcessedPalette;

                bitmapPalettes.Add(palContainer);
            }

            // Sort bitmap palette. Anything more than 16 colours should be grouped next to each other. Less than 16 should always be a full 16.
            bitmapPalettes.Sort(new ProcessedPaletteComparer());

            // Get palettes of each image, merge into a master palette
            List <Color> masterPaletteList = new List <Color>();
            List <ProcessedBitmapContainer> processedBitmapList = new List <ProcessedBitmapContainer>();

            {
                Color transparencyColour = Color.FromArgb(0);

                for (int i = 0; i < bitmapPalettes.Count; ++i)
                {
                    var bp = bitmapPalettes[i];

                    bool pal4bbp = bp.palette.Length <= PaletteHelper.PALETTE_LENGTH_4BBP;
                    if (pal4bbp)
                    {
                        // Make sure our 4bbp palette align properly
                        while (masterPaletteList.Count % PaletteHelper.PALETTE_LENGTH_4BBP != 0)
                        {
                            masterPaletteList.Add(transparencyColour);
                        }
                    }

                    int currentPaletteIndex = masterPaletteList.Count / PaletteHelper.PALETTE_LENGTH_4BBP;
                    masterPaletteList.AddRange(bp.palette);

                    ProcessedBitmapContainer pbc = new ProcessedBitmapContainer();
                    pbc.bitmapIndex  = bp.bitmapIndex;
                    pbc.paletteIndex = pal4bbp ? currentPaletteIndex : -1;
                    pbc.bitmap       = bitmaps[bp.bitmapIndex];

                    processedBitmapList.Add(pbc);
                }
            }

            Color[] masterPalette = masterPaletteList.ToArray();

            Console.WriteLine("Total colours found = " + masterPalette.Length);

            Console.WriteLine("Processing master tileset");

            // Get all unique tiles sorted via palette indicies. Need to check if local palette was greater than 16 or not
            List <Tile> uniqueTileSetList = new List <Tile>();

            foreach (ProcessedBitmapContainer pbc in processedBitmapList)
            {
                Tile[,] rawTileMap = BitmapToTileArray(pbc.bitmap, masterPalette, pbc.paletteIndex);
                pbc.rawTileMap     = rawTileMap;

                FillUniqueTileset(rawTileMap, uniqueTileSetList);
            }

            Tile[] masterTileSet = uniqueTileSetList.ToArray();

            if (masterTileSet.Length > MAX_UNIQUE_TILES)
            {
                throw new Exception(string.Format("Too many tiles present in tilemap. Max tiles = {0}. Total titles found = {1}", MAX_UNIQUE_TILES, masterTileSet.Length));
            }

            Console.WriteLine("Total unique tiles = " + masterTileSet.Length);
            Console.WriteLine("Processing tilemaps");

            // Generate tilemaps from master list. Remember palette index, but we should be able to forget about it. We can overlap tiles that are the same but different colours.
            List <TileMap> tileMapList = new List <TileMap>();

            foreach (ProcessedBitmapContainer pbc in processedBitmapList)
            {
                TileMap tilemap = new TileMap(pbc.rawTileMap, masterTileSet, pbc.paletteIndex);
                tileMapList.Add(tilemap);
            }

            Compression.CompressionType compressionType = Compression.CompressionType.BitPacked;
            uint destBpp = 4;

            foreach (Tile tile in masterTileSet)
            {
                foreach (int index in tile.paletteIndicies)
                {
                    if (index >= PaletteHelper.PALETTE_LENGTH_4BBP)
                    {
                        destBpp = 8;
                        goto WriteMasterTileSetToSb;
                    }
                }
            }

WriteMasterTileSetToSb:

            int tilesetLength;
            StringBuilder tilesetSb = new StringBuilder();

            WriteTileSet(masterTileSet, tilesetSb, compressionType, destBpp, out tilesetLength);

            StringBuilder sb = new StringBuilder();

            sb.Append(MACRO_DEFINES);
            sb.Append(namespaceName);

            WriteHeader(masterPalette, tilesetLength, sb);
            WritePalette(masterPalette, sb);

            // Real write tileset
            sb.Append(tilesetSb);

            // Write tile maps
            WriteMapData(tileMapList, sb);

            sb.Append("}\n");

            Console.WriteLine("Tilemap \"" + outputPath + "\" successfully converted");
            File.WriteAllText(outputPath, sb.ToString());
        }