示例#1
0
        public static Schematic WriteSchematicIntern(Bitmap bitmap, Bitmap bitmapColor, HeightmapStep heightmapStep)
        {
            Schematic schematic = new Schematic();

            Bitmap       bitmapBlack       = Grayscale.MakeGrayscale3(bitmap);
            DirectBitmap directBitmapBlack = new DirectBitmap(bitmapBlack, heightmapStep.Height);
            DirectBitmap directBitmap      = new DirectBitmap(bitmap, 1);
            DirectBitmap directBitmapColor = new DirectBitmap(bitmapColor, 1);


            if (bitmap.Width > Schematic.MAX_WORLD_WIDTH || bitmap.Height > Schematic.MAX_WORLD_LENGTH)
            {
                throw new ArgumentException($"Image is too big (max size ${Schematic.MAX_WORLD_WIDTH}x${Schematic.MAX_WORLD_LENGTH} px)");
            }

            using (FileToVoxCore.Utils.ProgressBar progressbar = new FileToVoxCore.Utils.ProgressBar())
            {
                Console.WriteLine("[INFO] Started to write schematic from picture...");
                Console.WriteLine("[INFO] Picture Width: " + bitmap.Width);
                Console.WriteLine("[INFO] Picture Height: " + bitmap.Height);

                int size = bitmap.Width * bitmap.Height;
                int i    = 0;
                int w    = bitmap.Width;
                int h    = bitmap.Height;
                for (int x = 0; x < w; x++)
                {
                    for (int y = 0; y < h; y++)
                    {
                        Color color      = directBitmap.GetPixel(x, y);
                        Color finalColor = !string.IsNullOrEmpty(heightmapStep.ColorTexturePath) ? directBitmapColor.GetPixel(x, y) : (heightmapStep.EnableColor) ? color : Color.White;
                        if (color.A != 0)
                        {
                            if (heightmapStep.Height != 1)
                            {
                                if (heightmapStep.Excavate)
                                {
                                    GenerateFromMinNeighbor(ref schematic, directBitmapBlack, w, h, finalColor, x, y, heightmapStep.Height, heightmapStep.Offset, heightmapStep.RotationMode, heightmapStep.Reverse);
                                }
                                else
                                {
                                    int computeHeight = directBitmapBlack.GetHeight(x, y) + heightmapStep.Offset;
                                    AddMultipleBlocks(ref schematic, heightmapStep.Offset, computeHeight, x, y, finalColor, heightmapStep.RotationMode);
                                }
                            }
                            else
                            {
                                AddSingleVoxel(ref schematic, x, heightmapStep.Offset, y, finalColor, heightmapStep.RotationMode, heightmapStep.Reverse);
                            }
                        }
                        progressbar.Report((i++ / (float)size));
                    }
                }
            }

            Console.WriteLine("[INFO] Done.");
            return(schematic);
        }
示例#2
0
        private Schematic WriteSchematicFromImage()
        {
            Bitmap bitmap = new Bitmap(new FileInfo(_path).FullName);
            Bitmap clone  = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);

            using (Graphics gr = Graphics.FromImage(clone))
            {
                gr.DrawImage(bitmap, new Rectangle(0, 0, clone.Width, clone.Height));
            }
            Bitmap      bitmapColor = new Bitmap(bitmap.Width, bitmap.Height); //default initialization
            WuQuantizer quantizer   = new WuQuantizer();

            if (_colorPath != null)
            {
                bitmapColor = new Bitmap(new FileInfo(_colorPath).FullName);
                if (bitmap.Height != bitmapColor.Height || bitmap.Width != bitmapColor.Width)
                {
                    throw new ArgumentException("[ERROR] Image color is not the same size of the original image");
                }

                clone = new Bitmap(bitmapColor.Width, bitmapColor.Height, PixelFormat.Format32bppArgb);
                using (Graphics gr = Graphics.FromImage(clone))
                {
                    gr.DrawImage(bitmapColor, new Rectangle(0, 0, clone.Width, clone.Height));
                }

                System.Drawing.Image image = quantizer.QuantizeImage(clone);
                bitmapColor = new Bitmap(image);
            }
            else if (_color)
            {
                System.Drawing.Image image = quantizer.QuantizeImage(clone);
                bitmap = new Bitmap(image);
            }

            Bitmap bitmapBlack = Grayscale.MakeGrayscale3(bitmap);

            if (bitmap.Width > 2016 || bitmap.Height > 2016)
            {
                throw new Exception("Image is too big (max size 2016x2016 px)");
            }

            Schematic schematic = new Schematic
            {
                Width  = (ushort)bitmap.Width,
                Length = (ushort)bitmap.Height,
                Heigth = (ushort)_maxHeight,
                Blocks = new HashSet <Block>()
            };

            LoadedSchematic.LengthSchematic = schematic.Length;
            LoadedSchematic.WidthSchematic  = schematic.Width;
            LoadedSchematic.HeightSchematic = schematic.Heigth;


            using (ProgressBar progressbar = new ProgressBar())
            {
                Console.WriteLine("[LOG] Started to write schematic from picture...");
                Console.WriteLine("[INFO] Picture Width: " + schematic.Width);
                Console.WriteLine("[INFO] Picture Length: " + schematic.Length);

                int size = schematic.Width * schematic.Length;
                int i    = 0;
                for (int x = 0; x < schematic.Width; x++)
                {
                    for (int y = 0; y < schematic.Length; y++)
                    {
                        Color color      = bitmap.GetPixel(x, y);
                        Color colorGray  = bitmapBlack.GetPixel(x, y);
                        Color finalColor = (_colorPath != null) ? bitmapColor.GetPixel(x, y) : (_color) ? color : colorGray;
                        if (color.A != 0)
                        {
                            if (_maxHeight != 1)
                            {
                                if (_excavate)
                                {
                                    GenerateFromMinNeighbor(ref schematic, bitmapBlack, finalColor, x, y);
                                }
                                else
                                {
                                    int height = GetHeight(colorGray);
                                    if (_top)
                                    {
                                        int finalHeight = (height - 1 < 0) ? 0 : height - 1;
                                        AddBlock(ref schematic, new Block((ushort)x, (ushort)finalHeight, (ushort)y, finalColor.ColorToUInt()));
                                    }
                                    else
                                    {
                                        AddMultipleBlocks(ref schematic, 0, height, x, y, finalColor);
                                    }
                                }
                            }
                            else
                            {
                                Block block = new Block((ushort)x, (ushort)1, (ushort)y, finalColor.ColorToUInt());
                                AddBlock(ref schematic, block);
                            }
                        }
                        progressbar.Report((i++ / (float)size));
                    }
                }
            }

            Console.WriteLine("[LOG] Done.");
            return(schematic);
        }
        protected Schematic WriteSchematicIntern(Bitmap bitmap, Bitmap bitmapColor)
        {
            Schematic schematic = new Schematic();

            Bitmap       bitmapBlack       = Grayscale.MakeGrayscale3(bitmap);
            DirectBitmap directBitmapBlack = new DirectBitmap(bitmapBlack);
            DirectBitmap directBitmap      = new DirectBitmap(bitmap);
            DirectBitmap directBitmapColor = new DirectBitmap(bitmapColor);

            if (bitmap.Width > 2000 || bitmap.Height > 2000)
            {
                throw new Exception("Image is too big (max size 2000x2000 px)");
            }

            using (ProgressBar progressbar = new ProgressBar())
            {
                Console.WriteLine("[LOG] Started to write schematic from picture...");
                Console.WriteLine("[INFO] Picture Width: " + bitmap.Width);
                Console.WriteLine("[INFO] Picture Height: " + bitmap.Height);

                int size = bitmap.Width * bitmap.Height;
                int i    = 0;
                int w    = bitmap.Width;
                int h    = bitmap.Height;
                for (int x = 0; x < w; x++)
                {
                    for (int y = 0; y < h; y++)
                    {
                        Color color      = directBitmap.GetPixel(x, y);
                        Color finalColor = (ColorPath != null) ? directBitmapColor.GetPixel(x, y) : (Color) ? color : System.Drawing.Color.White;
                        if (color.A != 0)
                        {
                            if (MaxHeight != 1)
                            {
                                if (Excavate)
                                {
                                    GenerateFromMinNeighbor(ref schematic, directBitmapBlack, w, h, finalColor, x, y);
                                }
                                else
                                {
                                    int height = GetHeight(directBitmapBlack.GetPixel(x, y));
                                    if (Top)
                                    {
                                        int finalHeight = (height - 1 < 0) ? 0 : height - 1;
                                        AddBlock(ref schematic, new Voxel((ushort)x, (ushort)finalHeight, (ushort)y, finalColor.ColorToUInt()));
                                    }
                                    else
                                    {
                                        AddMultipleBlocks(ref schematic, 0, height, x, y, finalColor);
                                    }
                                }
                            }
                            else
                            {
                                Voxel voxel = new Voxel((ushort)x, 0, (ushort)y, finalColor.ColorToUInt());
                                AddBlock(ref schematic, voxel);
                            }
                        }
                        progressbar.Report((i++ / (float)size));
                    }
                }
            }

            Console.WriteLine("[LOG] Done.");
            return(schematic);
        }