Beispiel #1
0
        /// <summary>
        /// Run Work
        /// </summary>
        /// <param name="info"></param>
        private void RunWork(PatchInfo info)
        {
            //1. Create Intermediatemap
            int     size = Program.gensize;
            FastMap work = new FastMap(new Bitmap(size, size, PixelFormat.Format24bppRgb), "work");

            work.Lock();

            // Init the map with pink
            for (int dx = 0; dx < size; dx++)
            {
                for (int dy = 0; dy < size; dy++)
                {
                    work.SetPixel(dx, dy, Color.Magenta);
                }
            }

            // Do all the work
            {
                //1.a) Layers
                FastMap patch = null;
                for (int layer = -1; layer < info.Layers.Count; layer++)
                //for (int layer = 0; layer < info.Layers.Count; layer++)
                //for (int layer = 0; layer < Math.Min(info.Layers.Count, 1); layer++)
                {
                    //Update Status
                    if (layer >= 0)
                    {
                        switch (layer % 3)
                        {
                        case 0: UpdateStatus(info.X, info.Y, Status.WorkingR); break;

                        case 1: UpdateStatus(info.X, info.Y, Status.WorkingG); break;

                        case 2: UpdateStatus(info.X, info.Y, Status.WorkingB); break;
                        }
                    }

                    //Load patch (and resize to working layer size)
                    if (layer % 3 == 0)
                    {
                        if (patch != null)
                        {
                            patch.Dispose();
                        }

                        Texture file     = Images.LoadFromFile(string.Format(info.Patchmap, info.X, info.Y, layer / 3), "");
                        Bitmap  imgSmall = Images.GetBitmap(file);
                        Bitmap  img      = resize(imgSmall, size, 0);
                        imgSmall.Dispose();
                        patch = new FastMap(img, "patch");
                        file.Dispose();
                        patch.Lock();
                    }

                    //Load Texture
                    FastMap src;

                    if (layer == -1)
                    {
                        //Base Texture
                        Bitmap brush = null;
                        try
                        {
                            Bitmap Btex = (Bitmap)Image.FromFile(string.Format(info.BaseTex, info.X, info.Y));
                            brush = new Bitmap(Btex, size, size);
                            Btex.Dispose();
                        }
                        catch (Exception ex)
                        {
                            string path = string.Format(info.BaseTex, info.X, info.Y).Replace(".bmp", ".dds");
                            //Load Layer tex
                            Texture file = Images.LoadFromFile(path, "");
                            brush = Images.GetBitmap(file);
                            file.Dispose();
                        }

                        src = new FastMap(brush, "base");
                    }
                    else
                    {
                        //Infos
                        string[] line  = info.Layers[layer].Split(',');
                        string   tex   = line[2];
                        int      count = int.Parse(line[9]);

                        string path = Program.terraintex + Path.DirectorySeparatorChar + tex + ".dds";
                        if (!File.Exists(path))
                        {
                            continue;
                        }

                        //Load Layer tex
                        Texture file  = Images.LoadFromFile(path, "");
                        Bitmap  brush = Images.GetBitmap(file);
                        file.Dispose();

                        //Tile (8 = 1:1 || < 8 = bigger|| > 8 = smaller
                        int    brushsize = size * 8 / count;
                        int    imagesize = Math.Min(brushsize, size);
                        Bitmap map       = new Bitmap(imagesize, imagesize, PixelFormat.Format24bppRgb);

                        //Fill
                        using (Graphics g = Graphics.FromImage(map))
                        {
                            Rectangle Rsrc = new Rectangle((info.X * brush.Width / 8 / count),
                                                           (info.Y * brush.Height / 8 / count),
                                                           brush.Width,
                                                           brush.Height);
                            Rectangle Rdst = new Rectangle(0, 0, brushsize, brushsize);
                            g.DrawImage(brush, Rdst, Rsrc, GraphicsUnit.Pixel);
                        }
                        brush.Dispose();

                        src = new FastMap(map, "layer tex");
                    }

                    //Create Map
                    int srcW = src.Bitmap.Width;
                    int srcH = src.Bitmap.Height;
                    src.Lock();
                    for (int x = 0; x < size; x++)
                    {
                        for (int y = 0; y < size; y++)
                        {
                            //1. Texture Info
                            const int WEIRD_OFFSET = 0; // 8?
                            // rx, ry = real xy
                            int rx = Math.Min(Math.Max(x - WEIRD_OFFSET, 0), Program.gensize);
                            int ry = Math.Min(Math.Max(y - WEIRD_OFFSET, 0), Program.gensize);
                            // brush pos
                            int px = ((rx) % (srcW));
                            int py = ((ry) % (srcH));

                            //2. Color Mapping
                            Color col = work.GetPixel(x, y);
                            Color add = src.GetPixel(px, py);
                            int   val = 0;

                            if (layer == -1)
                            {
                                val = byte.MaxValue;
                            }
                            else
                            {
                                Color pc = patch.GetPixel(rx, ry);

                                switch (layer % 3)
                                {
                                case 0: val = pc.R; break;

                                case 1: val = pc.G; break;

                                case 2: val = pc.B; break;
                                }

                                if (layer == 0)
                                {
                                    val = 255; //R = start
                                }
                            }

                            col = lerp(col, add, val);

                            work.SetPixel(x, y, col);
                        }
                    }
                    src.Release();
                    src.Dispose();
                }
            }
            work.Release();

            //2. Create Results
            //=> Resultmap
            {
                using (Bitmap res = resize(work.Bitmap, Program.targetsize, 0)) {
                    res.Save(Program.output + Path.DirectorySeparatorChar + string.Format("gen{0:D2}-{1:D2}.png", info.X, info.Y), ImageFormat.Png);
                }
            }
            //Lod Map & Tex Map
            {
                using (Bitmap res = resize(work.Bitmap, 256, 0))
                {
                    res.Save(Program.output + Path.DirectorySeparatorChar + string.Format("tex{0:D2}-{1:D2}.bmp", info.X, info.Y), ImageFormat.Bmp);
                    Texture t = Images.GetTexture(res);
                    Images.SaveToFile(t, Program.output + Path.DirectorySeparatorChar + string.Format("lod{0:D2}-{1:D2}.dds", info.X, info.Y), ImageFileFormat.Dds);
                    t.Dispose();
                }
            }
            work.Dispose();
        }