コード例 #1
0
ファイル: MapChip.cs プロジェクト: k-nrt/msxprojects
        public override bool Equals(object obj)
        {
            MapChip mapChip = (MapChip)obj;

            if (m_width != mapChip.m_width || m_height != mapChip.m_height)
            {
                return(false);
            }

            for (int i = 0; i < m_pixels.Length; i++)
            {
                if (m_pixels[i] != mapChip.m_pixels[i])
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string strPath = "r-type2-240x180.png";

            Bitmap bitmap = new Bitmap(strPath);

            Dictionary <uint, int> colors = new Dictionary <uint, int>();

            colors.Add(0xff000000, Int32.MaxValue);
            colors.Add(0xffe0e0e0, Int32.MaxValue);

            Bitmap bitmap333 = MakeRGB333(bitmap, colors, 15);

            int chipWidth  = 40;
            int chipHeight = 12;

            int horizontalChips = bitmap.Width / chipWidth;
            int verticalChips   = bitmap.Height / chipHeight;

            List <MapChip> mapChips = new List <MapChip>();

            int[][] mapIndices = new int[horizontalChips][];

            for (int x = 0; x < horizontalChips; x++)
            {
                mapIndices[x] = new int[verticalChips];

                for (int y = 0; y < verticalChips; y++)
                {
                    MapChip mapChip = new MapChip(x, y, chipWidth, chipHeight, bitmap333);

                    if (!mapChips.Contains(mapChip))
                    {
                        mapChips.Add(mapChip);
                        System.Console.WriteLine("{0} {1} {2}", x, y, mapChips.Count);
                    }
                    int index = mapChips.IndexOf(mapChip);
                    mapIndices[x][y] = index;
                }
            }

            int    outputChipWidth  = 256;
            int    outputChipHeight = 512;
            int    cx          = 0;
            int    cy          = 0;
            Bitmap bitmapChips = new Bitmap(outputChipWidth, outputChipHeight);

            foreach (MapChip mapChip in mapChips)
            {
                for (int y = 0; y < chipHeight; y++)
                {
                    for (int x = 0; x < chipWidth; x++)
                    {
                        Color pixel = bitmap333.GetPixel(mapChip.m_chipX * chipWidth + x, mapChip.m_chupY * chipHeight + y);
                        bitmapChips.SetPixel(cx + x, cy + y, pixel);
                    }
                }

                cx += chipWidth;
                if (outputChipWidth < (cx + chipWidth))
                {
                    cx  = 0;
                    cy += chipHeight;
                    if (outputChipHeight < (cy + chipHeight))
                    {
                        break;
                    }
                }
            }

            bitmapChips.Save("chips.png", ImageFormat.Png);

            int[][]    colorIndices = new int[bitmapChips.Width][];
            List <int> outputColors = new List <int>();

            for (int x = 0; x < bitmapChips.Width; x++)
            {
                colorIndices[x] = new int[bitmapChips.Height];

                for (int y = 0; y < bitmapChips.Height; y++)
                {
                    int argb = bitmapChips.GetPixel(x, y).ToArgb();
                    if (!outputColors.Contains(argb))
                    {
                        outputColors.Add(argb);
                    }

                    int index = outputColors.IndexOf(argb);
                    colorIndices[x][y] = index;
                }
            }


            FileStream writer = new FileStream("stage1.rbg", FileMode.Create, FileAccess.Write);

            foreach (int argb in outputColors)
            {
                int r = (argb >> 21) & 0x07;
                int g = (argb >> 13) & 0x07;
                int b = (argb >> 5) & 0x07;

                writer.WriteByte((byte)((r << 4) | b));
                writer.WriteByte((byte)g);
            }
            writer.Close();

            writer = new FileStream("stage1.pgt", FileMode.Create, FileAccess.Write);
            for (int y = 0; y < bitmapChips.Height; y++)
            {
                for (int x = 0; x < bitmapChips.Width; x += 2)
                {
                    int h = colorIndices[x][y];
                    int l = colorIndices[x + 1][y];

                    writer.WriteByte((byte)(((h & 0xf) << 4) | (l & 0xf)));
                }
            }
            writer.Close();

            writer = new FileStream("stage1.pnt", FileMode.Create, FileAccess.Write);
            for (int x = 0; x < horizontalChips; x++)
            {
                for (int y = 0; y < verticalChips; y++)
                {
                    int name = mapIndices[x][y] & 0xff;
                    writer.WriteByte((byte)name);
                }
            }

            writer.Close();
            System.Console.ReadLine();
        }