Пример #1
0
        public static Color[] createPaletteForImage(Bitmap b, int palLen)
        {
            List <Bitmap> bl = new List <Bitmap>();

            bl.Add(b);

            ImageIndexer i = new ImageIndexer(bl, palLen, true, 0);

            return(i.palettes[0]);
        }
Пример #2
0
        public bool import()
        {
            checkStuff();

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = LanguageManager.Get("Filters", "png");
            if (ofd.ShowDialog() == DialogResult.Cancel)
            {
                return(false);
            }

            calcSizes();

            Bitmap b = new Bitmap(ofd.FileName);

            if (b.Width != tx || b.Height != ty)
            {
                throw new Exception("Wrong input image size");
            }

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

            for (int i = 0; i < pals.Count; i++)
            {
                Bitmap   bbb    = new Bitmap(b.Width, tys);
                Graphics bbbgfx = Graphics.FromImage(bbb);
                bbbgfx.DrawImage(b, 0, -i * tys, b.Width, b.Height);
                bl.Add(bbb);
            }
            b.Dispose();

            ImageIndexer ii = new ImageIndexer(bl, pals[0].pal.Length, true, 0);


            int x = 0;

            foreach (PixelPalettedImage i in imgs)
            {
                i.setPixelData(ii.imageData, x, 0);
                x += i.getWidth();
            }

            for (int i = 0; i < pals.Count; i++)
            {
                pals[i].pal = ii.palettes[i];
            }

            return(false);
        }
Пример #3
0
        public int getClosestColor(Color c)
        {
            if (c.A == 0)
            {
                return(0);
            }

            int   bestInd = 0;
            float bestDif = ImageIndexer.colorDifferenceWithoutAlpha(pal[0], c);

            for (int i = 0; i < pal.Length; i++)
            {
                float d = ImageIndexer.colorDifferenceWithoutAlpha(pal[i], c);
                if (d < bestDif)
                {
                    bestDif = d;
                    bestInd = i;
                }
            }

            return(bestInd);
        }
Пример #4
0
        public bool import()
        {
            checkStuff();

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = LanguageManager.Get("Filters", "png");
            if (ofd.ShowDialog() == DialogResult.Cancel) return false;

            calcSizes();

            Bitmap b = new Bitmap(ofd.FileName);
            if (b.Width != tx || b.Height != ty)
                throw new Exception("Wrong input image size");

            List<Bitmap> bl = new List<Bitmap>();
            for (int i = 0; i < pals.Count; i++)
            {
                Bitmap bbb = new Bitmap(b.Width, tys);
                Graphics bbbgfx = Graphics.FromImage(bbb);
                bbbgfx.DrawImage(b, 0, -i*tys, b.Width, b.Height);
                bl.Add(bbb);
            }
            b.Dispose();

            ImageIndexer ii = new ImageIndexer(bl, 256, true, 0);

            int x = 0;
            foreach (PixelPalettedImage i in imgs)
            {
                i.setPixelData(ii.imageData, x, 0);
                x += i.getWidth();
            }

            for (int i = 0; i < pals.Count; i++)
            {
                pals[i].pal = ii.palettes[i];
            }

            return false;
        }
Пример #5
0
        public static Color[] createPaletteForImage(Bitmap b, int palLen)
        {
            List<Bitmap> bl = new List<Bitmap>();
            bl.Add(b);

            ImageIndexer i = new ImageIndexer(bl, palLen, true, 0);

            return i.palettes[0];
        }
Пример #6
0
        private void importPNGButton_Click(object sender, EventArgs e)
        {
            getFiles();

            if (GFXFile == null)
            {
                return;
            }
            if (PalFile == null)
            {
                return;
            }
            if (LayoutFile == null)
            {
                return;
            }

            int offs    = bg.topLayer ? 256 : 576;
            int palOffs = bg.topLayer ? 8 : 10;

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter          = LanguageManager.Get("Filters", "png");
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string filename = ofd.FileName;

            Bitmap b = new Bitmap(filename);

            ImageTiler ti = new ImageTiler(b);

            Color[] palette = ImageIndexer.createPaletteForImage(b);

            ByteArrayOutputStream oo = new ByteArrayOutputStream();

            for (int i = 0; i < palette.Length; i++)
            {
                oo.writeUShort(NSMBTileset.toRGB15(palette[i]));
            }
            for (int i = 0; i < 256; i++)
            {
                oo.writeUShort(0);
            }

            PalFile.beginEdit(this);
            PalFile.replace(ROM.LZ77_Compress(oo.getArray()), this);
            PalFile.endEdit(this);
            GFXFile.beginEdit(this);
            GFXFile.replace(ROM.LZ77_Compress(ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette)), this);
            GFXFile.endEdit(this);
            b.Dispose();

            ByteArrayOutputStream layout = new ByteArrayOutputStream();

            for (int y = 0; y < 64; y++)
            {
                for (int x = 0; x < 64; x++)
                {
                    layout.writeUShort((ushort)((ti.tileMap[x, y] + offs) | (palOffs << 12)));
                }
            }

            LayoutFile.beginEdit(this);
            LayoutFile.replace(ROM.LZ77_Compress(layout.getArray()), this);
            LayoutFile.endEdit(this);
        }
 public override void replaceImgAndPal(Bitmap b, Palette p)
 {
     p.pal = ImageIndexer.createPaletteForImage(b, p.pal.Length);
     replaceWithPal(b, p);
 }