Skip to content

Commit 56639b8

Browse files
committed
fix palette import/export issues
1 parent fd42a32 commit 56639b8

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

AvaGui/ViewModels/SubObjectTypes/ImageTableViewModel.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,23 @@ public async Task ImportImages()
216216
});
217217

218218
var sortedH = sorted.ToImmutableHashSet();
219-
if (G1Provider.G1Elements.Count != sortedH.Count)
220-
{
221-
throw new ArgumentOutOfRangeException($"Directory doesn't contain the same number of images as expected Directory={sortedH.Count} Expected={G1Provider.G1Elements.Count}");
222-
}
223219

224220
var g1Elements = new List<G1Element32>();
225221
var i = 0;
226222
foreach (var file in sorted)
227223
{
228224
var img = Image.Load<Rgba32>(file);
229225
Images[i] = img;
230-
var currG1 = G1Provider.G1Elements[i++];
231-
currG1.ImageData = PaletteMap.ConvertRgba32ImageToG1Data(img, currG1.Flags); // simply overwrite existing pixel data
226+
var currG1 = G1Provider.G1Elements[i];
227+
currG1 = currG1 with
228+
{
229+
Width = (int16_t)img.Width,
230+
Height = (int16_t)img.Height,
231+
Flags = currG1.Flags & ~G1ElementFlags.IsRLECompressed, // SawyerStreamWriter::SaveImageTable does this anyways
232+
ImageData = PaletteMap.ConvertRgba32ImageToG1Data(img, currG1.Flags)
233+
};
234+
G1Provider.G1Elements[i] = currG1;
235+
i++;
232236
}
233237

234238
this.RaisePropertyChanged(nameof(Bitmaps));

Dat/PaletteMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public PaletteMap(Color[] _palette)
4040
public Color[] PaletteColours => Palette.Select(x => x.Color).ToArray();
4141

4242
public (Color Color, byte Index) Transparent
43-
=> Palette[0];
43+
=> (Color.FromRgba(0, 0, 0, 0), 0); //Palette[0];
4444

4545
public (Color Color, byte Index)[] DirectXReserved
4646
=> Palette[1..7];

Tests/ImagePaletteConversionTests.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OpenLoco.Common.Logging;
33
using OpenLoco.Dat.FileParsing;
44
using SixLabors.ImageSharp;
5+
using SixLabors.ImageSharp.PixelFormats;
56

67
namespace OpenLoco.Dat.Tests
78
{
@@ -13,18 +14,43 @@ public class ImagePaletteConversionTests
1314
const string BasePalettePath = "Q:\\Games\\Locomotion\\Palettes\\";
1415
readonly ILogger Logger = new Logger();
1516

17+
[Test]
18+
public void TestWrite00000000ToIndex0()
19+
{
20+
var paletteFile = Path.Combine(BasePalettePath, "palette_32bpp3.png");
21+
var paletteMap = Image.Load<Rgba32>(paletteFile);
22+
paletteMap[0, 0] = Color.Transparent;
23+
paletteMap.SaveAsPng(paletteFile);
24+
}
25+
26+
[Test]
27+
public void PaletteIndex0IsTransparent()
28+
{
29+
var paletteFile = Path.Combine(BasePalettePath, "palette_32bpp3.png");
30+
var paletteMap = new PaletteMap(paletteFile);
31+
32+
Assert.That(paletteMap.Transparent.Color, Is.EqualTo(Color.Transparent));
33+
}
34+
35+
[Test]
36+
public void PaletteHasUniqueColours()
37+
{
38+
var paletteFile = Path.Combine(BasePalettePath, "palette_32bpp3.png");
39+
var paletteMap = new PaletteMap(paletteFile);
40+
41+
Assert.That(paletteMap.Transparent.Color, Is.EqualTo(Color.Transparent));
42+
Assert.That(paletteMap.PaletteColours.Length, Is.EqualTo(paletteMap.PaletteColours.ToHashSet().Count));
43+
}
44+
1645
[TestCase("AIRPORT1.DAT")]
1746
[TestCase("BALDWIN1.DAT")]
1847
[TestCase("FACTORY.DAT")]
1948
//[TestCase("INTERDEF.DAT")] // these files use different palettes
2049
//[TestCase("WATER1.DAT")] // these files use different palettes
2150
public void G1ElementToPNGAndBack(string objectSource)
2251
{
23-
var paletteFile = Path.Combine(BasePalettePath, "palette_32bpp.png");
52+
var paletteFile = Path.Combine(BasePalettePath, "palette_32bpp3.png");
2453
var paletteMap = new PaletteMap(paletteFile);
25-
26-
Assert.That(paletteMap.Transparent.Color, Is.EqualTo(Color.Transparent));
27-
2854
var obj = SawyerStreamReader.LoadFullObjectFromFile(Path.Combine(BaseObjDataPath, objectSource), Logger);
2955

3056
// convert g1 data into an image, and then back

palette.png

2.64 KB
Loading

0 commit comments

Comments
 (0)