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); }
public override Schematic WriteSchematic() { int height = Directory.GetFiles(_path, "*.*", SearchOption.AllDirectories).Count(s => s.EndsWith(".png")); Console.WriteLine("[INFO] Count files in the folder : " + height); List <Block> blocks = new List <Block>(); Bitmap bitmapColor = null; if (_inputColorFile != null) { bitmapColor = new Bitmap(_inputColorFile); if (bitmapColor.Width > 256 || bitmapColor.Height > 1) { throw new ArgumentException("[ERROR] The input color file must have a dimension of 256x1 px"); } } int maxWidth = 0; int maxLength = 0; using (ProgressBar progressbar = new ProgressBar()) { string[] files = Directory.GetFiles(_path); for (int i = 0; i < files.Length; i++) { string file = files[i]; Bitmap bitmap = new Bitmap(file); DirectBitmap directBitmap = new DirectBitmap(bitmap); for (int x = 0; x < directBitmap.Width; x++) { for (int y = 0; y < directBitmap.Height; y++) { Color color = directBitmap.GetPixel(x, y); if (color != Color.Empty && color != Color.Transparent && color != Color.Black && (color.R != 0 && color.G != 0 && color.B != 0)) { if (_inputColorFile != null) { double distance = Math.Sqrt(Math.Pow((height / 2) - x, 2) + Math.Pow((height / 2) - y, 2)); float range = (float)Math.Abs(distance / (height / 2)); // range = range > 1 ? 1 : range; color = bitmapColor.GetPixel((int)(range * (bitmapColor.Width - 1)), 0); } maxWidth = maxWidth < directBitmap.Width ? directBitmap.Width : maxWidth; maxLength = maxLength < directBitmap.Height ? directBitmap.Height : maxLength; if (_excavate) { CheckNeighbor(ref blocks, directBitmap, color, i, x, y); } else { blocks.Add(new Block((ushort)x, (ushort)i, (ushort)y, color.ColorToUInt())); } } } } directBitmap.Dispose(); progressbar.Report(i / (float)files.Length); } } Schematic schematic = new Schematic(); schematic.Height = (ushort)height; schematic.Width = (ushort)maxWidth; schematic.Length = (ushort)maxLength; LoadedSchematic.HeightSchematic = schematic.Height; LoadedSchematic.LengthSchematic = schematic.Length; LoadedSchematic.WidthSchematic = schematic.Width; List <Block> list = Quantization.ApplyQuantization(blocks, _colorLimit); schematic.Blocks = list.ToHashSet(); Console.WriteLine("[LOG] Done."); return(schematic); }