Ejemplo n.º 1
0
        private static int[] getLeftTop(HSV[,] colors, Dictionary <int, HSV[, ]> colorDict)
        {
            double score = 1e9;
            int    ax = 0, ay = 0;

            for (int offsetX = -MARGIN; offsetX < 2 * MARGIN; offsetX++)
            {
                for (int offsetY = -MARGIN; offsetY < 2 * MARGIN; offsetY++)
                {
                    double tmpScore = 1e9;
                    foreach (HSV[,] value in colorDict.Values)
                    {
                        double s = 0;
                        for (int x = MARGIN; x < 32 - MARGIN; x++)
                        {
                            for (int y = MARGIN; y < 32 - MARGIN; y++)
                            {
                                // s += Math.Abs(value[x, y] - grays[x + offsetX, y + offsetY]);
                                try
                                {
                                    s += HSV.getDistance(value[x, y], colors[x + offsetX, y + offsetY]);
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                        if (tmpScore > s)
                        {
                            tmpScore = s;
                        }
                    }
                    if (score > tmpScore)
                    {
                        score = tmpScore;
                        ax    = offsetX; ay = offsetY;
                    }
                }
            }
            return(new int[] { ax, ay });
        }
Ejemplo n.º 2
0
        private void loadResources(Bitmap ground)
        {
            dict = new Dictionary <int, Bitmap>();

            string directory = ".\\";

            if (!Directory.Exists(directory + "images"))
            {
                directory = "..\\";
            }

            if (!Directory.Exists(directory + "images"))
            {
                MessageBox.Show("images目录不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            // 读取JS
            string js = @"main={'instance':{}};";

            js += File.ReadAllText(directory + "libs\\icons.js");
            js += File.ReadAllText(directory + "libs\\maps.js");
            js += @"
                main.instance.icons.init();
                var icons=main.instance.icons.getIcons();

                var cls = [], indexes = [];

                var point = 0;
                for(var i=1; i<400; i++){
                    var indexBlock = main.instance.maps.getBlock(0,0,i);
                    if('event' in indexBlock){
                        var id = indexBlock.event.id;
                        var clss = indexBlock.event.cls;
                        if(clss=='autotile'){
                            cls[i] = id;
                            indexes[i] = 0;
                            continue;
                        }
                        cls[i] = clss;
                        indexes[i] = icons[clss][id];
                    }
                  }
            ";

            var engine  = new Engine().Execute(js);
            var cls     = engine.GetValue("cls").AsArray();
            var indexes = engine.GetValue("indexes").AsArray();

            Dictionary <string, Bitmap> dictionary = new Dictionary <string, Bitmap>();

            dict.Add(0, ground);
            for (int i = 1; i < 400; i++)
            {
                string filename = cls.Get(Convert.ToString(i)).ToString();
                if (!"undefined".Equals(filename))
                {
                    if (!dictionary.ContainsKey(filename))
                    {
                        Bitmap bitmap = loadBitmap(directory + "images\\" + filename + ".png");
                        dictionary.Add(filename, bitmap);
                    }
                    Bitmap image = dictionary[filename];
                    try
                    {
                        dict.Add(i,
                                 clipImage(image, 0, Convert.ToInt32(indexes.Get(Convert.ToString(i)).ToString()), ground));
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            colorDict = new Dictionary <int, HSV[, ]>();
            foreach (int id in dict.Keys)
            {
                Bitmap bitmap = dict[id];
                HSV[,] colors = new HSV[32, 32];
                for (int i = 0; i < 32; i++)
                {
                    for (int j = 0; j < 32; j++)
                    {
                        colors[i, j] = new HSV(bitmap.GetPixel(i, j));
                    }
                }
                colorDict.Add(id, colors);
            }

            SendMessage(textBox1.Handle, EM_SETTABSTOPS, 1, new int[] { 4 * 4 });
        }
Ejemplo n.º 3
0
        public static string calculate(Bitmap bitmap, Dictionary <int, HSV[, ]> colorDict)
        {
            int width = (bitmap.Width + 16) / 32, height = (bitmap.Height + 16) / 32;

            // int[,] grays = bitmapToGray(bitmap);

            HSV[,] colors = new HSV[bitmap.Width, bitmap.Height];
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    colors[i, j] = new HSV(bitmap.GetPixel(i, j));
                }
            }

            int[,] ans = new int[width, height];

            int[] offset = getLeftTop(colors, colorDict);

            int offsetX = offset[0], offsetY = offset[1];

            // calculate
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    double t = 1e9;
                    int    v = 0;
                    foreach (int id in colorDict.Keys)
                    {
                        // Bitmap value = dict[id];
                        HSV[,] value = colorDict[id];
                        double s = 0;
                        for (int x = MARGIN; x < 32 - MARGIN; x++)
                        {
                            for (int y = MARGIN; y < 32 - MARGIN; y++)
                            {
                                try
                                {
                                    s += HSV.getDistance(value[x, y], colors[32 * j + x + offsetX, 32 * i + y + offsetY]);
                                }
                                catch (Exception) { }
                            }
                        }
                        if (t > s)
                        {
                            t = s;
                            v = id;
                        }
                    }
                    ans[i, j] = v;
                }
            }
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    builder.Append(ans[i, j]);
                    if (j != height - 1)
                    {
                        builder.Append('\t');
                    }
                    else
                    {
                        builder.Append("\r\n");
                    }
                }
            }
            // text = builder.ToString();

            // textBox1.Text = text;
            // button2_Click(null, null);
            return(builder.ToString());
        }
Ejemplo n.º 4
0
        public static double getDistance(HSV hsv1, HSV hsv2)
        {
            double dx = hsv1.X - hsv2.X, dy = hsv1.Y - hsv2.Y, dz = hsv1.Z - hsv2.Z;

            return(dx * dx + dy * dy + dz * dz);
        }
Ejemplo n.º 5
0
        private void loadResources(Bitmap ground)
        {
            dict = new Dictionary <int, Bitmap>();

            string directory = ".\\";

            if (!Directory.Exists(directory + "project"))
            {
                directory = "..\\";
            }

            if (!Directory.Exists(directory + "project"))
            {
                MessageBox.Show("project目录不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return;
            }

            // 读取JS
            string js = "";

            js += File.ReadAllText(directory + "project\\icons.js") + ";";
            js += File.ReadAllText(directory + "project\\maps.js") + ";";
            js += @"
                var icons=icons_4665ee12_3a1f_44a4_bea3_0fccba634dc1;
                var maps=maps_90f36752_8815_4be8_b32b_d7fad1d0542e;

                var cls = [], indexes = [];

                var point = 0;
                for(var i=1; i<1000; i++){
                    if (maps[i]) {
                        var id = maps[i].id;
                        var clss = maps[i].cls;
                        if(clss=='autotile'){
                            continue;
                        }
                        cls[i] = clss;
                        indexes[i] = icons[clss][id];
                    }
                  }
            ";

            var engine  = new Engine().Execute(js);
            var cls     = engine.GetValue("cls").AsArray();
            var indexes = engine.GetValue("indexes").AsArray();

            Dictionary <string, Bitmap> dictionary = new Dictionary <string, Bitmap>();

            dict.Add(0, ground);
            for (int i = 1; i < 1000; i++)
            {
                string filename = cls.Get(Convert.ToString(i)).ToString();
                if (!"undefined".Equals(filename))
                {
                    if (!dictionary.ContainsKey(filename))
                    {
                        Bitmap bitmap = loadBitmap(directory + "\\project\\materials\\" + filename + ".png");
                        dictionary.Add(filename, bitmap);
                    }
                    Bitmap image = dictionary[filename];
                    try
                    {
                        var height = 32;
                        if (filename.Contains("48"))
                        {
                            height = 48;
                        }
                        dict.Add(i,
                                 clipImage(image, 0, Convert.ToInt32(indexes.Get(Convert.ToString(i)).ToString()), ground, height));
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            colorDict = new Dictionary <int, HSV[, ]>();
            foreach (int id in dict.Keys)
            {
                Bitmap bitmap = dict[id];
                HSV[,] colors = new HSV[32, 32];
                for (int i = 0; i < 32; i++)
                {
                    for (int j = 0; j < 32; j++)
                    {
                        colors[i, j] = new HSV(bitmap.GetPixel(i, j));
                    }
                }
                colorDict.Add(id, colors);
            }

            SendMessage(textBox1.Handle, EM_SETTABSTOPS, 1, new int[] { 4 * 4 });
        }