Skip to content

Commit

Permalink
Avoid quantization if the --color-limit argument isn't set or that th…
Browse files Browse the repository at this point in the history
…e original picture has less than 256 colors
  • Loading branch information
Zarbuz committed Jun 3, 2020
1 parent 8c9a2ef commit 514a9b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions SchematicToVoxCore/Converter/Image/PNGToSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ private Schematic WriteSchematicFromImage()
gr.DrawImage(bitmapColor, new Rectangle(0, 0, clone.Width, clone.Height));
}

System.Drawing.Image image = quantizer.QuantizeImage(clone, 10, 70, _colorLimit);
bitmapColor = new Bitmap(image);
if (_colorLimit != 256 || bitmapColor.CountColor() > 256)
{
System.Drawing.Image image = quantizer.QuantizeImage(clone, 10, 70, _colorLimit);
bitmapColor = new Bitmap(image);
}

}
else if (_color)
{
System.Drawing.Image image = quantizer.QuantizeImage(clone, 10, 70, _colorLimit);
bitmap = new Bitmap(image);
if (_colorLimit != 256 || clone.CountColor() > 256)
{
System.Drawing.Image image = quantizer.QuantizeImage(clone, 10, 70, _colorLimit);
bitmap = new Bitmap(image);
}
}

Bitmap bitmapBlack = Grayscale.MakeGrayscale3(bitmap);
Expand Down
18 changes: 18 additions & 0 deletions SchematicToVoxCore/Extensions/FctExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ public static int GetColorIntensity(this Color color)
return color.R + color.G + color.B;
}

public static int CountColor(this Bitmap bitmap)
{
List<Color> colors = new List<Color>();
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color color = bitmap.GetPixel(i, j);
if (!colors.Contains(color))
{
colors.Add(color);
}
}
}

return colors.Count;
}

public static uint ColorToUInt(this Color color)
{
return (uint)((color.A << 24) | (color.R << 16) |
Expand Down

0 comments on commit 514a9b2

Please sign in to comment.