From bebd9b9a430694b74841b5e57453efff92503128 Mon Sep 17 00:00:00 2001 From: Nicolas Perrier Date: Mon, 22 Apr 2019 15:40:44 +0200 Subject: [PATCH] Fixed IndexOutOfRangeException --- .../Converter/PNGToSchematic.cs | 64 ++++++++++++------- SchematicToVoxCore/FileToVox.csproj | 2 +- .../Properties/launchSettings.json | 2 +- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/SchematicToVoxCore/Converter/PNGToSchematic.cs b/SchematicToVoxCore/Converter/PNGToSchematic.cs index c8058f2..5e7a4d5 100644 --- a/SchematicToVoxCore/Converter/PNGToSchematic.cs +++ b/SchematicToVoxCore/Converter/PNGToSchematic.cs @@ -157,7 +157,8 @@ private static Bitmap MakeGrayscale3(Bitmap original) private static Color[,] GetColors(Bitmap bitmap) { - Color[,] colors = new Color[bitmap.Height, bitmap.Width]; + int max = bitmap.Height > bitmap.Width ? bitmap.Height : bitmap.Width; + Color[,] colors = new Color[max, max]; for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) @@ -178,7 +179,14 @@ private static void AddMultipleBlocks(ref Schematic schematic, int minZ, int max private static void AddBlock(ref Schematic schematic, Block block) { - schematic.Blocks.Add(block); + try + { + schematic.Blocks.Add(block); + } + catch (OutOfMemoryException) + { + Console.WriteLine($"[ERROR] OutOfMemoryException. Block: {block.ToString()}"); + } } private static int GetHeight(Color color) @@ -192,38 +200,46 @@ private static void GenerateFromMinNeighbor(ref Schematic schematic, Color color { int height = GetHeight(color); - if (x - 1 > 0 && x + 1 < schematic.Width && y - 1 > 0 && y + 1 < schematic.Length) + try { - var colorLeft = _grayColors[x - 1, y]; - var colorTop = _grayColors[x, y - 1]; - var colorRight = _grayColors[x + 1, y]; - var colorBottom = _grayColors[x, y + 1]; + if (x - 1 > 0 && x + 1 < schematic.Width && y - 1 > 0 && y + 1 < schematic.Length) + { + var colorLeft = _grayColors[x - 1, y]; + var colorTop = _grayColors[x, y - 1]; + var colorRight = _grayColors[x + 1, y]; + var colorBottom = _grayColors[x, y + 1]; - int heightLeft = GetHeight(colorLeft); - int heightTop = GetHeight(colorTop); - int heightRight = GetHeight(colorRight); - int heightBottom = GetHeight(colorBottom); + int heightLeft = GetHeight(colorLeft); + int heightTop = GetHeight(colorTop); + int heightRight = GetHeight(colorRight); + int heightBottom = GetHeight(colorBottom); - var list = new List - { - heightLeft, heightTop, heightRight, heightBottom - }; + var list = new List + { + heightLeft, heightTop, heightRight, heightBottom + }; + + int min = list.Min(); + if (min < height) + { + AddMultipleBlocks(ref schematic, list.Min(), height, x, y, color); + } + else + { + int finalHeight = (height - 1 < 0) ? 0 : height - 1; + AddBlock(ref schematic, + new Block((short) x, (short) finalHeight, (short) y, color.ColorToUInt())); + } - int min = list.Min(); - if (min < height) - { - AddMultipleBlocks(ref schematic, list.Min(), height, x, y, color); } else { - int finalHeight = (height - 1 < 0) ? 0 : height - 1; - AddBlock(ref schematic, new Block((short)x, (short)finalHeight, (short)y, color.ColorToUInt())); + AddMultipleBlocks(ref schematic, 0, height, x, y, color); } - } - else + catch (IndexOutOfRangeException e) { - AddMultipleBlocks(ref schematic, 0, height, x, y, color); + Console.WriteLine($"[ERROR] x: {x}, y: {y}, schematic width: {schematic.Width}, schematic length: {schematic.Length}"); } } } diff --git a/SchematicToVoxCore/FileToVox.csproj b/SchematicToVoxCore/FileToVox.csproj index a123a79..0bacecb 100644 --- a/SchematicToVoxCore/FileToVox.csproj +++ b/SchematicToVoxCore/FileToVox.csproj @@ -3,7 +3,7 @@ Exe netcoreapp2.1 - SchematicToVoxCore.Program + FileToVox.Program true win-x64;linux-x64;osx-x64 diff --git a/SchematicToVoxCore/Properties/launchSettings.json b/SchematicToVoxCore/Properties/launchSettings.json index 942bb32..3a298cc 100644 --- a/SchematicToVoxCore/Properties/launchSettings.json +++ b/SchematicToVoxCore/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "SchematicToVoxCore": { "commandName": "Project", - "commandLineArgs": "--i ../heightmap.png --o ../heightmap --hm 10 --e" + "commandLineArgs": "--i ../JF.png --o ../JF --hm 1000 --e" } } } \ No newline at end of file