private int[][] getColors(Info n, SpecialBitmap b, List<int> pallet)
        {


            var md = Enumerable.Range(0, b.Height / 128).SelectMany(a =>
                                                                        {
                                                                            Console.Write("COlor color: " + a);
                                                                            return Enumerable.Range(0, b.Width / 128).Select(bc =>
                                                                                                                               {

                                                                                                                                   int[] j = new int[128 * 128];

                                                                                                                                   for (int k = 0; k < 128; k++)
                                                                                                                                   {
                                                                                                                                       for (int d = 0; d < 128; d++)
                                                                                                                                       {
                                                                                                                                           var m = b.GetPixel(k + bc * 128, d + a * 128);
                                                                                                                                           j[k + d * 128] = pallet.IndexOf(m);
                                                                                                                                       }
                                                                                                                                   }
                                                                                                                                   return j;
                                                                                                                               });
                                                                        }).ToArray();
            return md;
        }
        private void getHightMap(Info n, SpecialBitmap b, Dictionary<string, int> keycache, int map)
        {

            for (int x = 0; x < plane.Width; x++)
            {
                Color black = Color.Black;
                for (int y = 0; y < plane.Height; y++)
                {
                    var j = b.GetPixel(x, y);
                    if (plane.GetPixel(x, y) == j)
                    {
                        b.SetPixel(x, y, black);
                    }

                }
            }

            Console.WriteLine("solids ss");
            TileChunk curChunk = n.Chunks[n.ChunkMap[0][0]];
            for (int a = 0; a < b.Height; a += 16)
            {
                for (int bc = 0; bc < b.Width; bc += 16)
                {
                    int chunky = a / 128;
                    int chunkx = bc / 128;

                    curChunk = n.Chunks[n.ChunkMap[chunkx][chunky]];
                    byte[] dff = new byte[17];
                    int piecex = (bc - chunkx * 128) / 16;
                    int piecey = (a - chunky * 128) / 16;

                    for (int x = 0; x < 16; x++)
                    {
                        bool lineStarted = false;
                        for (int y = 0; y < 16; y++)
                        {
                            var m = b.GetPixel((bc) + x, (a) + y);

                            if ((m == Yellow) || (m == Grey) || (m == Whitish))
                            {
                                if (!lineStarted)
                                {
                                    lineStarted = true;
                                    dff[x + 1] = (byte)(16 - y);
                                    if (dff[0] != 2)
                                        dff[0] = 0;

                                }
                            }
                            else if (lineStarted)
                            {
                                dff[x + 1] = (byte)(y);

                                dff[0] = 2;
                                break;
                            }
                        }
                    }

                    string mf = "";
                    for (int i = 0; i < dff.Length; i++)
                    {
                        mf += toBase17(dff[i]);
                    }
                    int f;
                    if (!keycache.TryGetValue(mf, out f))
                    {
                        keycache.Add(mf, f = keycache.Count);
                    }
                    if (map == 1)
                        curChunk.heightMap1[piecex][piecey] = f;
                    else
                        curChunk.heightMap2[piecex][piecey] = f;


                }
            }
            b.close();
        }
        private Info fullLoad()
        {
            plane = new SpecialBitmap(dir2 + "plane.png");
            Info n = new Info(plane.Width / 128, plane.Height / 128);

            n.Tiles = new List<Tiles>();
            List<int> pallet = new List<int>();

            int last;
            for (int y = 0; y < plane.Height; y += 8)
            {
                for (int x = 0; x < plane.Width; x += 8)
                {
                    int[][] gm = Help.init2D<int>(8, 8);
                    for (int _y = 0; _y < 8; _y++)
                    {
                        for (int _x = 0; _x < 8; _x++)
                        {
                            var pind = 0;
                            var m = plane.GetPixel(x + _x, y + _y);
                            if ((pind = pallet.IndexOf(m)) == -1)
                            {
                                pallet.Add(m);
                                pind = pallet.Count;
                            }
                            gm[_x ][_y ] = pind;
                        }
                    }

                    if (!n.Tiles.Any(a => (last = colorsEqual(a.colors, gm)) != -1))
                    {
                        Console.WriteLine("Tiles: " + n.Tiles.Count);

                        Tiles dm;
                        n.Tiles.Add(dm = new Tiles() { colors = gm });
                        dm.setInfo(n);
                    }
                }
            }



            Console.WriteLine("tp");


            for (int y = 0; y < plane.Height; y += 16)
            {
                for (int x = 0; x < plane.Width; x += 16)
                {
                    Tile[] gm = new Tile[4];
                    for (int _y = 0; _y < 2; _y++)
                    {
                        for (int _x = 0; _x < 2; _x++)
                        {
                            int[][] cm = Help.init2D<int>(8, 8);
                            for (int __y = 0; __y < 8; __y++)
                            {
                                for (int __x = 0; __x < 8; __x++)
                                {
                                    var d = plane.GetPixel(x + _x * 8 + __x, y + _y * 8 + __y);
                                    cm[__x ][ __y ] = pallet.IndexOf(d);
                                }
                            }
                            last = -1;

                            Tiles mj;
                            if ((mj = n.Tiles.FirstOrDefault(a => (last = colorsEqual(a.colors, cm)) != -1)) != null)
                            {
                                Tile md;
                                gm[_x + _y * 2] = (md = new Tile(n.Tiles.IndexOf(mj), last));
                                md.setInfo(n);
                            }
                            else
                            {
                                Tile md;
                                gm[_x + _y * 2] = (md = new Tile(0, 0));
                                md.setInfo(n);
                            }
                        }
                    }
                    if (!n.Blocks.Any(a => a.Equals(gm)))
                    {
                        Console.WriteLine("peices: " + n.Blocks.Count);
                        n.Blocks.Add(new TilePiece(gm));
                    }
                }
            }

            Console.WriteLine("chunks ");

            var b = new SpecialBitmap(dir2 + "ChunkList.png");
            int inj = 0;
            var inc = 0;
            for (int offy = 0; offy < b.Height; offy += 128)
            {
                for (int offx = 0; offx < b.Width; offx += 128)
                {

                    int[][] j = Help.init2D<int>(8, 8);

                    for (int d = 0; d < 8; d++)
                    {
                        for (int k = 0; k < 8; k++)
                        {
                            int[] mc = new int[16 * 16];
                            for (int _x = 0; _x < 16; _x++)
                            {
                                for (int _y = 0; _y < 16; _y++)
                                {
                                    var m = b.GetPixel(k * 16 + offx + _x, d * 16 + offy + _y);
                                    var dl = m;
                                    mc[_x + _y * 16] = pallet.IndexOf(dl);
                                }
                            }
                            var ddf = n.Blocks.FirstOrDefault(a => a.Compare(mc));
                            if (ddf != null && n.Blocks.IndexOf(ddf) > -1)
                            {
                                j[k][d] = n.Blocks.IndexOf(ddf);
                                Console.WriteLine("chunk: " + n.Blocks.IndexOf(ddf));

                            }
                            else
                            {
                                Console.WriteLine(inj++);
                            }

                        }
                    }

                    TileChunk fd;
                    n.Chunks.Add(fd = new TileChunk(j));
                    fd.hLayer = Help.init2D<int>(8,8);
                    fd.setInfo(n);

                }
            }
            b.close();
            inj = 0;
            var highs = getColors(n, plane, pallet);
            int ind = 0;
            foreach (var high in highs)
            {
                
                var x = ind % n.LevelWidth;
                var y = ind / n.LevelWidth;

                ind++;

                var mcd = n.Chunks.FirstOrDefault(a => a.Compare(high));
                if (mcd != null)
                {
                    n.ChunkMap[x][y] = n.Chunks.IndexOf(mcd);
                }
                else
                {
                    Console.WriteLine(inj++);
                }
            }
            highs = getColors(n, dir2 + "plane highs.png", pallet);
            for (int index = 0; index < highs.Length; index++)
            {

                var x = index % n.LevelWidth;
                var y = index / n.LevelWidth;



                int[] high = highs[index];
                TileChunk ch = n.Chunks[n.ChunkMap[x][y]];
                ch.hLayer = Help.init2D<int>(8, 8);

                for (int i = 0; i < ch.tilePieces.Length; i++)
                {
                    int _x = i % 8;
                    int _y = i / 8;

                    TilePiece tp = n.Blocks[ch.tilePieces[_x][_y]];
                    bool good = true;
                    bool allbalck = true;
                    for (int j = 0; j < 16; j++)
                    {
                        for (int k = 0; k < 16; k++)
                        {
                            int p = tp.getPixel(j, k);
                            if (p != 0) allbalck = false;
                            if (high[(_x * 16 + j) + (_y * 16 + k) * 128] != p)
                            {
                                good = false;
                                break;
                            }
                        }
                    }
                    if (allbalck) continue;
                    ch.hLayer[_x][_y] = good ? 1 : 0;
                }

            }

            Console.WriteLine("solid ");

            solidLoad(n);
            n.Palette = pallet.Select(a =>
            {
                var c = Color.FromArgb(a);

                return
                    ((c.R.ToString("X").Length == 1 ? "0" : "") + c.R.ToString("X")) +
                    ((c.G.ToString("X").Length == 1 ? "0" : "") + c.G.ToString("X")) +
                    ((c.B.ToString("X").Length == 1 ? "0" : "") + c.B.ToString("X"));

            }).ToArray();

            save(n);
            Application.Exit();
            /*         n.highPlane = highs.Select(a =>
            {
                var firstOrDefault = n.Chunks.FirstOrDefault(bc => compareBitmap(bc, a));
                if (firstOrDefault != null)
                    return firstOrDefault.Index;
                else
                    return 0;
            }).ToArray();
               var lows = getColors(n, dir2 + "plane low.png");

               n.lowPlane = new List<int>(lows.Select(a =>
               {
                       var firstOrDefault = n.getChunk().FirstOrDefault(bc => compareBitmap(bc, a));
                if (firstOrDefault != null)
                    return firstOrDefault.Index;
                else
                    return (byte) 0;
               }).AsParallel());*/





            save(n);
            return n;
        }
        private void getAngleMap(Info n, SpecialBitmap b, Dictionary<string, int> keycache, int map)
        {


            Console.WriteLine("agaga ss");
            TileChunk curChunk = n.Chunks[n.ChunkMap[0][0]];
            for (int a = 0; a < b.Height; a += 16)
            {
                for (int bc = 0; bc < b.Width; bc += 16)
                {
                    int chunky = a / 128;
                    int chunkx = bc / 128;

                    curChunk = n.Chunks[n.ChunkMap[chunkx][chunky]];
                    byte[] dff = new byte[17];
                    int piecex = (bc - chunkx * 128) / 16;
                    int piecey = (a - chunky * 128) / 16;
                    if (chunkx == 4 && chunky == 15)
                    {
                        if (piecex == 6 && piecey == 2)
                        {
                            Console.WriteLine((chunkx * 128));
                        }
                    }


                    var lt = new int[2] { -1, -1 };
                    for (int i = 0; i < 2; i++)
                    {
                        foreach (var letter in letters)
                        {
                            bool notletter = false;
                            for (int x = 0; x < 8; x++)
                            {
                                for (int y = 0; y < 16; y++)
                                {
                                    var m = b.GetPixel((bc) + x + i * 8, (a) + y);
                                    var fc = letter.GetPixelColor(x, y);
                                    if (fc.A != 0)
                                    {
                                        if (m != fc.ToArgb())
                                        {
                                            notletter = true;
                                            break;
                                        }
                                    }
                                }
                                if (notletter) break;
                            }
                            if (!notletter)
                            {
                                lt[i] = Array.IndexOf(letters, letter);
                            }
                        }
                    }
                    if (lt[0] == -1 || lt[1] == -1)
                    {
                        continue;
                    }

                    if (map == 1)
                        curChunk.angleMap1[piecex][piecey] = lt[0].ToString("X") + lt[1].ToString("X");
                    else
                        curChunk.angleMap2[piecex][piecey] = lt[0].ToString("X") + lt[1].ToString("X");
                }
            }
            b.close();
        }