Skip to content

Commit

Permalink
More optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Apr 22, 2019
1 parent bebd9b9 commit cd2cc1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
41 changes: 25 additions & 16 deletions SchematicToVoxCore/Converter/PNGToSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public static class PNGToSchematic
private static int _maxHeight;
private static bool _color;
private static bool _top;
private static bool _direction;
private static Color[,] _mainColors;
private static Color[,] _grayColors;
private static Color[,] _fileColors;


public static Schematic WriteSchematic(string path, string colorPath, int height, bool excavate, bool color, bool top)
{
_excavate = excavate;
Expand All @@ -33,22 +35,25 @@ private static Schematic WriteSchematicFromImage(string path, string colorPath)
{
FileInfo info = new FileInfo(path);
Bitmap bitmap = new Bitmap(info.FullName);
bitmap.RotateFlip(RotateFlipType.Rotate180FlipY);
_direction = bitmap.Width > bitmap.Height;

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

Bitmap grayScale = MakeGrayscale3(bitmap);
Bitmap bitmapBlack = MakeGrayscale3(bitmap);

_mainColors = GetColors(bitmap);
_grayColors = GetColors(grayScale);
_grayColors = GetColors(bitmapBlack);

if (bitmap.Width > 2016 || bitmap.Height > 2016)
{
Expand Down Expand Up @@ -76,11 +81,17 @@ private static Schematic WriteSchematicFromImage(string path, string colorPath)
int size = schematic.Width * schematic.Length;
for (int i = 0; i < size; i++)
{
int x = i % schematic.Width;
int y = i / schematic.Width;
Color color = _mainColors[y, x];
Color colorGray = _grayColors[y, x];
Color finalColor = (colorPath != null) ? _fileColors[y, x] : (_color) ? color : colorGray;
int x = i / schematic.Width;
int y = i % schematic.Width;
//if (_direction)
//{
// x = i / schematic.Width;
// y = i % schematic.Width;
//}

Color color = _mainColors[x, y];
Color colorGray = _grayColors[x, y];
Color finalColor = (colorPath != null) ? _fileColors[x, y] : (_color) ? color : colorGray;
if (color.A != 0)
{
if (_maxHeight != 1)
Expand Down Expand Up @@ -198,11 +209,10 @@ private static int GetHeight(Color color)

private static void GenerateFromMinNeighbor(ref Schematic schematic, Color color, int x, int y)
{
int height = GetHeight(color);

int height = GetHeight(_grayColors[x, y]);
try
{
if (x - 1 > 0 && x + 1 < schematic.Width && y - 1 > 0 && y + 1 < schematic.Length)
if (x - 1 >= 0 && x + 1 < _grayColors.GetLength(0) && y - 1 >= 0 && y + 1 < _grayColors.GetLength(1))
{
var colorLeft = _grayColors[x - 1, y];
var colorTop = _grayColors[x, y - 1];
Expand All @@ -215,9 +225,9 @@ private static void GenerateFromMinNeighbor(ref Schematic schematic, Color color
int heightBottom = GetHeight(colorBottom);

var list = new List<int>
{
heightLeft, heightTop, heightRight, heightBottom
};
{
heightLeft, heightTop, heightRight, heightBottom
};

int min = list.Min();
if (min < height)
Expand All @@ -228,16 +238,15 @@ private static void GenerateFromMinNeighbor(ref Schematic schematic, Color color
{
int finalHeight = (height - 1 < 0) ? 0 : height - 1;
AddBlock(ref schematic,
new Block((short) x, (short) finalHeight, (short) y, color.ColorToUInt()));
new Block((short)x, (short)finalHeight, (short)y, color.ColorToUInt()));
}

}
else
{
AddMultipleBlocks(ref schematic, 0, height, x, y, color);
}
}
catch (IndexOutOfRangeException e)
catch (IndexOutOfRangeException)
{
Console.WriteLine($"[ERROR] x: {x}, y: {y}, schematic width: {schematic.Width}, schematic length: {schematic.Length}");
}
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 ../JF.png --o ../JF --hm 1000 --e"
"commandLineArgs": "--i ../JF.png --o ../JF --hm 900 --c --e"
}
}
}

0 comments on commit cd2cc1d

Please sign in to comment.