Skip to content

Commit

Permalink
Merge branch 'FEATURE/fractals'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Jun 30, 2020
2 parents 8d93320 + c54ef71 commit e65582f
Show file tree
Hide file tree
Showing 12 changed files with 708 additions and 776 deletions.
97 changes: 0 additions & 97 deletions SchematicToVoxCore/CA/RuleGeneric.cs

This file was deleted.

100 changes: 0 additions & 100 deletions SchematicToVoxCore/CA/RuleSet.cs

This file was deleted.

79 changes: 79 additions & 0 deletions SchematicToVoxCore/Converter/Image/DirectBitmap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace FileToVox.Converter.Image
{
public class DirectBitmap : IDisposable
{
public int[] Bits { get; }
public bool Disposed { get; private set; }
public int Height { get; }
public int Width { get; }


public DirectBitmap(Bitmap bitmap)
{
Width = bitmap.Width;
Height = bitmap.Height;
Bits = new int[Width * Height];
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);

unsafe
{
ImageToSchematic.RGB* p = (ImageToSchematic.RGB*) data.Scan0;
int last = p->argb;
int h = bitmap.Height;
int w = bitmap.Width;
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
int c = p->argb;
if (c == last)
{
SetPixel(x, y, c);
}
else
{
SetPixel(x, y, c);
last = c;
}

++p;
}
}
}
}

public void SetPixel(int x, int y, Color color)
{
int index = x + (y * Width);
int col = color.ToArgb();

Bits[index] = col;
}

public void SetPixel(int x, int y, int color)
{
int index = x + (y * Width);
Bits[index] = color;
}

public Color GetPixel(int x, int y)
{
int index = x + (y * Width);
int col = Bits[index];
Color result = Color.FromArgb(col);

return result;
}

public void Dispose()
{
if (Disposed) return;
Disposed = true;
}
}
}
63 changes: 42 additions & 21 deletions SchematicToVoxCore/Converter/Image/FolderImageToSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,80 @@ namespace FileToVox.Converter.Image
public class FolderImageToSchematic : AbstractToSchematic
{
private readonly bool _excavate;
public FolderImageToSchematic(string path, bool excavate) : base(path)
private readonly string _inputColorFile;
public FolderImageToSchematic(string path, bool excavate, string inputColorFile) : base(path)
{
_excavate = excavate;
_inputColorFile = inputColorFile;
}

public override Schematic WriteSchematic()
{
int height = Directory.GetFiles(_path).Length;
int height = Directory.GetFiles(_path, "*.png").Length;
Console.WriteLine("[INFO] Count files in the folder : " + height);

Schematic schematic = new Schematic();
schematic.Height = (ushort) height;
schematic.Width = (ushort)height;
schematic.Length = (ushort)height;
schematic.Blocks = new HashSet<Block>();

LoadedSchematic.LengthSchematic = schematic.Length;
LoadedSchematic.WidthSchematic = schematic.Width;
LoadedSchematic.HeightSchematic = schematic.Height;
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");
}
}

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);
for (int x = 0; x < bitmap.Width; x++)
DirectBitmap directBitmap = new DirectBitmap(bitmap);
for (int x = 0; x < directBitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
for (int y = 0; y < directBitmap.Height; y++)
{
Color color = bitmap.GetPixel(x, y);
if (color.A != 0 && (color.R != 0 && color.G != 0 && color.B != 0))
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))
{
//first initialization
if (schematic.Width == 0 || schematic.Length == 0)
{
schematic.Width = (ushort) bitmap.Width;
schematic.Length = (ushort) bitmap.Height;
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);
}

LoadedSchematic.LengthSchematic = schematic.Length;
LoadedSchematic.WidthSchematic = schematic.Width;
LoadedSchematic.HeightSchematic = schematic.Height;
}
if (_excavate)
{
CheckNeighbor(ref schematic, bitmap, color, i, x, y);
CheckNeighbor(ref schematic, directBitmap, color, i, x, y);
}
else
{
schematic.Blocks.Add(new Block((ushort) x, (ushort) i, (ushort) y, Color.AliceBlue.ColorToUInt()));
schematic.Blocks.Add(new Block((ushort) x, (ushort) i, (ushort) y, color.ColorToUInt()));
}
}
}
}

directBitmap.Dispose();
progressbar.Report(i / (float)files.Length);
}
}
Console.WriteLine("[LOG] Done.");
return schematic;
}

private void CheckNeighbor(ref Schematic schematic, Bitmap bitmap, Color color, int i, int x, int y)
private void CheckNeighbor(ref Schematic schematic, DirectBitmap bitmap, Color color, int i, int x, int y)
{
if (x - 1 >= 0 && x + 1 < bitmap.Width && y - 1 >= 0 && y + 1 < bitmap.Height)
{
Expand All @@ -77,9 +93,14 @@ private void CheckNeighbor(ref Schematic schematic, Bitmap bitmap, Color color,
Color right = bitmap.GetPixel(x + 1, y);
Color bottom = bitmap.GetPixel(x, y + 1);

if (color != left || color != top || right != color || color != bottom)
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)
{
schematic.Blocks.Add(new Block((ushort) x, (ushort) i, (ushort) y, Color.AliceBlue.ColorToUInt()));
schematic.Blocks.Add(new Block((ushort) x, (ushort) i, (ushort) y, color.ColorToUInt()));
}
}
}
Expand Down
Loading

0 comments on commit e65582f

Please sign in to comment.