Skip to content

Commit c88411a

Browse files
committed
treat all pixels with Alpha=0 the same way
1 parent 6b4d3f5 commit c88411a

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

Dat/FileParsing/SawyerStreamWriter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,29 @@ static uint8_t RotateLeft(uint8_t value, uint8_t shift)
283283
return (uint8_t)((value << shift) | (value >> (8 - shift)));
284284
}
285285

286-
// this is ugly as all hell but it works. plenty of room for cleanup and optimisation
287286
public static byte[] EncodeRLEImageData(G1Element32 img)
287+
=> EncodeRLEImageData(img.Flags, img.ImageData, img.Width, img.Height);
288+
289+
// this is ugly as all hell but it works. plenty of room for cleanup and optimisation
290+
public static byte[] EncodeRLEImageData(G1ElementFlags flags, byte[] imageData, int width, int height)
288291
{
289292
using var ms = new MemoryStream();
290293

291294
var lines = new List<List<(int StartX, List<byte> RunBytes)>>();
292295

293296
// calculate the segments per line in the input image
294-
foreach (var line in img.ImageData.Chunk(img.Width))
297+
foreach (var line in imageData.Chunk(width))
295298
{
296299
List<(int StartX, List<byte> RunBytes)> segments = [];
297-
for (var x = 0; x < img.Width;)
300+
for (var x = 0; x < width;)
298301
{
299302
// find the start of a segment. previous pixel may be a segment
300303
if (line[x] != 0x0)
301304
{
302305
// find the end
303306
var startOfSegment = x;
304307
List<byte> run = [];
305-
while (x < img.Width && line[x] != 0x0 && run.Count < 127) // runs can only be 127 bytes in length. if the run is truly longer, then it gets split into multiple runs
308+
while (x < width && line[x] != 0x0 && run.Count < 127) // runs can only be 127 bytes in length. if the run is truly longer, then it gets split into multiple runs
306309
{
307310
run.Add(line[x]);
308311
x++;
@@ -322,7 +325,7 @@ public static byte[] EncodeRLEImageData(G1Element32 img)
322325
// write source pointers. will be (2 * img.Height) bytes. need to know RLE data first to know the offsets
323326
var headerOffset = lines.Count * 2;
324327
var bytesTotal = 0;
325-
for (var yy = 0; yy < img.Height; ++yy)
328+
for (var yy = 0; yy < height; ++yy)
326329
{
327330
// bytes per previous line is the sum of all the bytes in the runs plus the number of line segments * 2
328331
var bytesPreviousLine = yy == 0

Dat/PaletteMap.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public PaletteMap(Color[] _palette)
4040
public (Color Color, byte Index)[] Palette { get; set; }
4141

4242
public static (Color Color, byte Index) Transparent
43-
=> (Color.FromRgba(0, 0, 0, 0), 0); //Palette[0];
43+
=> (TransparentPixel, 0); //Palette[0];
44+
45+
public static readonly Color TransparentPixel = Color.FromRgba(0, 0, 0, 0);
4446

4547
public (Color Color, byte Index)[] TextRendering
4648
=> Palette[1..7];
@@ -179,6 +181,11 @@ public bool TryConvertG1ToRgba32Bitmap(G1Element32 g1Element, out Image<Rgba32>?
179181

180182
byte ColorToPaletteIndex(Color c)
181183
{
184+
if (c.ToPixel<Rgba32>().A == 0)
185+
{
186+
c = TransparentPixel;
187+
}
188+
182189
var reserved = ReservedColours.Where(cc => cc.Color == c);
183190
if (reserved.Any())
184191
{

Dat/Types/G1Element32.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ public ReadOnlySpan<byte> Write()
5757
}
5858

5959
public byte[] GetImageDataForSave()
60-
=> Flags.HasFlag(G1ElementFlags.IsRLECompressed)
61-
? SawyerStreamWriter.EncodeRLEImageData(this)
62-
: ImageData;
60+
=> GetImageDataForSave(Flags, ImageData, Width, Height);
61+
62+
public static byte[] GetImageDataForSave(G1ElementFlags flags, byte[] imageData, int width, int height)
63+
=> flags.HasFlag(G1ElementFlags.IsRLECompressed)
64+
? SawyerStreamWriter.EncodeRLEImageData(flags, imageData, width, height)
65+
: imageData;
6366

6467
public bool Validate() => true;
6568
}

Gui/ViewModels/SubObjectTypes/ImageTableViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ void UpdateImage(Image<Rgba32> img, int index)
379379
{
380380
Width = (int16_t)img.Width,
381381
Height = (int16_t)img.Height,
382-
Flags = currG1.Flags & ~G1ElementFlags.IsRLECompressed, // SawyerStreamWriter::SaveImageTable does this anyways
382+
Flags = currG1.Flags,
383383
ImageData = PaletteMap.ConvertRgba32ImageToG1Data(img, currG1.Flags),
384384
XOffset = currG1.XOffset,
385385
YOffset = currG1.YOffset

0 commit comments

Comments
 (0)