Skip to content

Commit

Permalink
Merge branch 'fix/palette_index'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Jun 22, 2021
2 parents c03bc1e + 1d5baa4 commit 3e14822
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SchematicToVoxCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private static bool SchematicToVox(AbstractToSchematic converter)

if (INPUT_PALETTE_FILE != null)
{
PaletteSchematicConverter converterPalette = new PaletteSchematicConverter(INPUT_PALETTE_FILE, COLOR_LIMIT);
PaletteSchematicConverter converterPalette = new PaletteSchematicConverter(INPUT_PALETTE_FILE);
schematic = converterPalette.ConvertSchematic(schematic);
return writer.WriteModel(CHUNK_SIZE, FormatOutputDestination(OUTPUT_PATH), converterPalette.GetPalette(), schematic);
}
Expand Down
12 changes: 9 additions & 3 deletions SchematicToVoxCore/Schematics/PaletteSchematicConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace FileToVox.Schematics
public class PaletteSchematicConverter
{
private List<Color> _colors;
public PaletteSchematicConverter(string palettePath, int colorLimit)
public PaletteSchematicConverter(string palettePath)
{
_colors = new List<Color>();
Bitmap bitmap = new Bitmap(palettePath);
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
if (_colors.Count < colorLimit)
if (_colors.Count < 256)
{
_colors.Add(bitmap.GetPixel(x, y));
}
Expand All @@ -31,10 +31,16 @@ public List<Color> GetPalette()
return _colors;
}

public List<uint> GetPaletteUint()
{
List<uint> palette = _colors.Select(color => color.ColorToUInt()).ToList();
return palette;
}

public Schematic ConvertSchematic(Schematic schematic)
{
Console.WriteLine("[INFO] Started to convert all colors of blocks to match the palette");
Schematic newSchematic = new Schematic();
Schematic newSchematic = new Schematic(GetPaletteUint());
List<uint> colors = schematic.UsedColors;
Dictionary<uint, int> paletteDictionary = new Dictionary<uint, int>();
foreach (uint color in colors)
Expand Down
16 changes: 12 additions & 4 deletions SchematicToVoxCore/Schematics/Schematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,22 @@ public ushort Length
private int mMaxX;
private int mMaxY;
private int mMaxZ;
private readonly bool mCanReorderPalette = true;

public Schematic()
{
UsedColors = new List<uint>();
CreateAllRegions();
}

public Schematic(List<Voxel> voxels)
public Schematic(List<uint> palette) : this()
{
UsedColors = palette;
mCanReorderPalette = false;
}

public Schematic(List<Voxel> voxels) : this()
{
UsedColors = new List<uint>();
CreateAllRegions();
AddVoxels(voxels);
}

Expand Down Expand Up @@ -352,7 +357,10 @@ private void AddColorInUsedColors(uint color)
if (UsedColors.Count < MAX_COLORS_IN_PALETTE && !UsedColors.Contains(color))
{
UsedColors.Add(color);
UsedColors = UsedColors.OrderByDescending(c => c).ToList();
if (mCanReorderPalette)
{
UsedColors = UsedColors.OrderByDescending(c => c).ToList();
}
}
}

Expand Down

0 comments on commit 3e14822

Please sign in to comment.