Ejemplo n.º 1
0
        protected override void Run(Workbench workbench, ILogger logger)
        {
            int x = FindNamedParameter("--x").Values[0].ToInt32();
            int y = FindNamedParameter("--y").Values[0].ToInt32();
            int w = FindNamedParameter("--width").Values[0].ToInt32();
            int h = FindNamedParameter("--height").Values[0].ToInt32();

            workbench.SetBitmap(workbench.GetCroppedBitmap(x, y, w, h, logger));

            logger?.Log("Cropped " + workbench.Bitmap.Width + 'x' + workbench.Bitmap.Height + " bitmap.");
        }
Ejemplo n.º 2
0
        protected override void Run(Workbench workbench, ILogger logger)
        {
            string fmtName  = FindUnnamedParameter(0).Values[0].Value;
            bool   noReduce = FindNamedParameter("--no-reduce").IsPresent;
            int    x        = FindNamedParameter("--x").Values[0].ToInt32();
            int    y        = FindNamedParameter("--y").Values[0].ToInt32();
            int    w        = FindNamedParameter("--width").Values[0].ToInt32();
            int    h        = FindNamedParameter("--height").Values[0].ToInt32();

            if (!(BitmapFormat.GetFormat(fmtName) is BitmapFormat fmt))
            {
                logger?.Log("Unknown tilemap format \"" + fmtName + "\".", LogLevel.Error);
                return;
            }

            int beforeCount = workbench.Tilemap.Count;

            using (Bitmap bmp = workbench.GetCroppedBitmap(x, y, w, h, logger)) {
                try {
                    if (fmt.IsIndexed)
                    {
                        if (workbench.PaletteSet.Count <= 0)
                        {
                            logger?.Log("Cannot generate indexed tiles: no palettes loaded.", LogLevel.Error);
                            return;
                        }
                        workbench.Tilemap.AddBitmapIndexed(bmp, workbench.Tileset, workbench.PaletteSet, fmt, !noReduce);
                    }
                    else
                    {
                        workbench.Tilemap.AddBitmap(bmp, workbench.Tileset, fmt, !noReduce);
                    }
                } catch (Exception ex) {
                    logger?.Log(ex.Message, LogLevel.Error);
                    return;
                }
            }

            workbench.TilemapFormat = fmt;

            int addedCount = workbench.Tilemap.Count - beforeCount;

            logger?.Log("Generated " + addedCount + " tilemap entries.");
        }
Ejemplo n.º 3
0
        protected override void Run(Workbench workbench, ILogger logger)
        {
            int palNum  = FindNamedParameter("--palette-number").Values[0].ToInt32();
            int palSize = FindNamedParameter("--palette-size").Values[0].ToInt32();
            int x       = FindNamedParameter("--x").Values[0].ToInt32();
            int y       = FindNamedParameter("--y").Values[0].ToInt32();
            int w       = FindNamedParameter("--width").Values[0].ToInt32();
            int h       = FindNamedParameter("--height").Values[0].ToInt32();

            if (palNum < -1)
            {
                logger?.Log("Invalid palette number.", LogLevel.Error);
                return;
            }
            if (palSize == 0 || palSize < -1)
            {
                logger?.Log("Invalid palette size.", LogLevel.Error);
                return;
            }

            int tw = workbench.Tileset.TileWidth;
            int th = workbench.Tileset.TileHeight;

            TileCutter cutter        = new TileCutter(tw, th);
            int        ti            = 0;
            int        addedPalettes = 0;
            int        addedColors   = 0;
            Palette    pal           = null;

            using (Bitmap bmp = workbench.GetCroppedBitmap(x, y, w, h, logger)) {
                foreach (Tile tile in cutter.CutTiles(bmp))
                {
                    // Get all the unique colors in the tile.
                    List <int> tileColors = tile.EnumerateTile().Distinct().ToList();

                    if (palSize != -1 && tileColors.Count > palSize)
                    {
                        logger?.Log("Tile " + ti + " has " + tileColors.Count + " colors, which is more than the " + palSize + " colors allowed by the palette.", LogLevel.Error);
                        return;
                    }

                    int bestPal = -1;
                    int bestAdd = int.MaxValue;

                    // Find palettes we can add to.
                    for (int i = 0; i < workbench.PaletteSet.Count; i++)
                    {
                        pal = workbench.PaletteSet[i].Palette;

                        // See if we can add to this palette.
                        int newSize = pal.Union(tileColors).Count();
                        if ((pal.MaximumSize >= 0 && newSize > pal.MaximumSize))
                        {
                            // Cannot add to this palette.
                            continue;
                        }

                        // Add to palette that would result in least new colors.
                        int add = newSize - pal.Count;
                        if (add < bestAdd)
                        {
                            bestPal = i;
                            bestAdd = add;
                        }
                    }

                    // Could not find a suitable palette, so have to make a new one.
                    if (bestPal < 0)
                    {
                        pal = new Palette(workbench.BitmapFormat, palSize);
                        if (palNum >= 0)
                        {
                            while (workbench.PaletteSet.ContainsPalette(palNum))
                            {
                                palNum++;
                            }
                            workbench.PaletteSet.Add(pal, palNum++);
                        }
                        else
                        {
                            workbench.PaletteSet.Add(pal);
                        }

                        addedPalettes++;

                        bestPal = workbench.PaletteSet.Count - 1;
                        bestAdd = tileColors.Count;
                    }
                    else
                    {
                        pal = workbench.PaletteSet[bestPal].Palette;
                    }

                    // Add the new colors to the palette.
                    foreach (int color in tileColors.Where(c => !pal.Contains(c)))
                    {
                        pal.Add(color);
                        addedColors++;
                    }

                    ti++;
                }

                logger?.Log("Added " + addedPalettes + " new palettes, " + addedColors + " colors total.");
            }
        }