Skip to content

Commit

Permalink
Improved filter of files when there is multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Feb 10, 2022
1 parent 8b30488 commit 6e2693a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 94 deletions.
170 changes: 83 additions & 87 deletions SchematicToVoxCore/Converter/Image/MultipleImageToSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,100 +8,96 @@
namespace FileToVox.Converter.Image
{
public class MultipleImageToSchematic : AbstractToSchematic
{
private readonly bool mExcavate;
private readonly string mInputColorFile;
private readonly int mColorLimit;
private readonly List<string> mImages;
public MultipleImageToSchematic(List<string> images, bool excavate, string inputColorFile, int colorLimit)
{
mImages = images;
mExcavate = excavate;
mInputColorFile = inputColorFile;
mColorLimit = colorLimit;
}
{
private readonly bool mExcavate;
private readonly string mInputColorFile;
private readonly int mColorLimit;
private readonly List<string> mImages;
public MultipleImageToSchematic(List<string> images, bool excavate, string inputColorFile, int colorLimit)
{
mImages = images;
mExcavate = excavate;
mInputColorFile = inputColorFile;
mColorLimit = colorLimit;
}

public override Schematic WriteSchematic()
{
int height = mImages.Count;
Console.WriteLine("[INFO] Total images to process: " + mImages.Count);
public override Schematic WriteSchematic()
{
int height = mImages.Count;
Console.WriteLine("[INFO] Total images to process: " + mImages.Count);

List<Voxel> blocks = new List<Voxel>();
Bitmap bitmapColor = null;
if (mInputColorFile != null)
{
bitmapColor = new Bitmap(mInputColorFile);
if (bitmapColor.Width > 256 || bitmapColor.Height > 1)
{
throw new ArgumentException("[ERROR] The input color file must have a dimension of 256x1 px");
}
}
List<Voxel> blocks = new List<Voxel>();
Bitmap bitmapColor = null;
if (mInputColorFile != null)
{
bitmapColor = new Bitmap(mInputColorFile);
if (bitmapColor.Width > 256 || bitmapColor.Height > 1)
{
throw new ArgumentException("[ERROR] The input color file must have a dimension of 256x1 px");
}
}

using (ProgressBar progressbar = new ProgressBar())
{
for (int i = 0; i < mImages.Count; i++)
{
string file = mImages[i];
Console.WriteLine("[INFO] Reading file: " + file);
Bitmap bitmap = new Bitmap(file);
DirectBitmap directBitmap = new DirectBitmap(bitmap, 1);
for (int x = 0; x < directBitmap.Width; x++)
{
for (int y = 0; y < directBitmap.Length; 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 (mInputColorFile != 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);
}
for (int i = 0; i < mImages.Count; i++)
{
string file = mImages[i];
Console.WriteLine("[INFO] Reading file: " + file);
Bitmap bitmap = new Bitmap(file);
DirectBitmap directBitmap = new DirectBitmap(bitmap, 1);
for (int x = 0; x < directBitmap.Width; x++)
{
for (int y = 0; y < directBitmap.Length; 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 (mInputColorFile != 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);
}

if (mExcavate)
{
CheckNeighbor(ref blocks, directBitmap, color, i, x, y);
}
else
{
blocks.Add(new Voxel((ushort) x, (ushort) i, (ushort) y, color.ColorToUInt()));
}
}
}
}
directBitmap.Dispose();
progressbar.Report(i / (float)mImages.Count);
}
}
if (mExcavate)
{
CheckNeighbor(ref blocks, directBitmap, color, i, x, y);
}
else
{
blocks.Add(new Voxel((ushort)x, (ushort)i, (ushort)y, color.ColorToUInt()));
}
}
}
}
directBitmap.Dispose();
}

List<Voxel> list = Quantization.ApplyQuantization(blocks, mColorLimit);
Schematic schematic = new Schematic(list);
List<Voxel> list = Quantization.ApplyQuantization(blocks, mColorLimit);
Schematic schematic = new Schematic(list);

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

private void CheckNeighbor(ref List<Voxel> blocks, DirectBitmap bitmap, Color color, int i, int x, int y)
{
if (x - 1 >= 0 && x + 1 < bitmap.Width && y - 1 >= 0 && y + 1 < bitmap.Length)
{
Color left = bitmap.GetPixel(x - 1, y);
Color top = bitmap.GetPixel(x, y - 1);
Color right = bitmap.GetPixel(x + 1, y);
Color bottom = bitmap.GetPixel(x, y + 1);
private void CheckNeighbor(ref List<Voxel> blocks, DirectBitmap bitmap, Color color, int i, int x, int y)
{
if (x - 1 >= 0 && x + 1 < bitmap.Width && y - 1 >= 0 && y + 1 < bitmap.Length)
{
Color left = bitmap.GetPixel(x - 1, y);
Color top = bitmap.GetPixel(x, y - 1);
Color right = bitmap.GetPixel(x + 1, y);
Color bottom = bitmap.GetPixel(x, y + 1);

bool leftColor = left != Color.Empty && left != Color.Transparent && left != Color.Black && (left.R != 0 && left.G != 0 && left.B != 0);
bool topColor = top != Color.Empty && top != Color.Transparent && top != Color.Black && (top.R != 0 && top.G != 0 && top.B != 0);
bool rightColor = right != Color.Empty && right != Color.Transparent && right != Color.Black && (right.R != 0 && right.G != 0 && right.B != 0);
bool bottomColor = bottom != Color.Empty && bottom != Color.Transparent && bottom != Color.Black && (bottom.R != 0 && bottom.G != 0 && bottom.B != 0);
bool leftColor = left != Color.Empty && left != Color.Transparent && left != Color.Black && (left.R != 0 && left.G != 0 && left.B != 0);
bool topColor = top != Color.Empty && top != Color.Transparent && top != Color.Black && (top.R != 0 && top.G != 0 && top.B != 0);
bool rightColor = right != Color.Empty && right != Color.Transparent && right != Color.Black && (right.R != 0 && right.G != 0 && right.B != 0);
bool bottomColor = bottom != Color.Empty && bottom != Color.Transparent && bottom != Color.Black && (bottom.R != 0 && bottom.G != 0 && bottom.B != 0);

if (!leftColor || !topColor || !rightColor || !bottomColor)
{
blocks.Add(new Voxel((ushort) x, (ushort) i, (ushort) y, color.ColorToUInt()));
}
}
}
}
if (!leftColor || !topColor || !rightColor || !bottomColor)
{
blocks.Add(new Voxel((ushort)x, (ushort)i, (ushort)y, color.ColorToUInt()));
}
}
}
}
}
10 changes: 5 additions & 5 deletions SchematicToVoxCore/FileToVox.csproj.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>E:\Documents\FileToVox\SchematicToVoxCore\Properties\PublishProfiles\MainBuildFTV.pubxml</_LastSelectedProfileId>
</PropertyGroup>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>E:\Documents\FileToVox\SchematicToVoxCore\Properties\PublishProfiles\WindowsFTV.pubxml</_LastSelectedProfileId>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions SchematicToVoxCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ private static bool ProcessFile()
string[] files = INPUT_PATH.Split(";");
if (isFolder)
{
List<string> images = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png")).ToList();
List<string> images = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") && !string.IsNullOrEmpty(s)).ToList();
converter = new MultipleImageToSchematic(images, EXCAVATE, INPUT_COLOR_FILE, COLOR_LIMIT);
return SchematicToVox(converter);
}
if (files.Length > 0)
{
converter = new MultipleImageToSchematic(files.ToList(), EXCAVATE, INPUT_COLOR_FILE, COLOR_LIMIT);
converter = new MultipleImageToSchematic(files.Where(s => s.EndsWith(".png") && !string.IsNullOrEmpty(s)).ToList(), EXCAVATE, INPUT_COLOR_FILE, COLOR_LIMIT);
return SchematicToVox(converter);
}

Expand Down

0 comments on commit 6e2693a

Please sign in to comment.