Skip to content

Commit

Permalink
Fixed IndexOutOfRangeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Apr 22, 2019
1 parent 100547a commit bebd9b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
64 changes: 40 additions & 24 deletions SchematicToVoxCore/Converter/PNGToSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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)
Expand All @@ -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<int>
{
heightLeft, heightTop, heightRight, heightBottom
};
var list = new List<int>
{
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}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SchematicToVoxCore/FileToVox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<StartupObject>SchematicToVoxCore.Program</StartupObject>
<StartupObject>FileToVox.Program</StartupObject>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<ApplicationIcon />
Expand Down
2 changes: 1 addition & 1 deletion SchematicToVoxCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit bebd9b9

Please sign in to comment.