From a92e8f972ea68b57e0685760df254a932c66fa85 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Thu, 28 Apr 2022 07:16:59 +0100 Subject: [PATCH 1/2] Fix bug in compressed output The problem was twofold: 1. Somehow I'd accidentally removed of the internal call to `GenerateBitSpans` at some point. I've added that back in now. 2. Because of the recent change to using `SpriteColours.White` instead of `Color.White`, the `==` comparison was failing because `Color.White` is a 'named colour' and `SpriteColours.White` isn't. I've changed it to use `SpriteColours.White` _and_ to check the `ToArgb()` representation instead, which should be the same regardless of whether a colour is 'named' or not. --- .../ABSpriteEditor/Sprites/IO/SpriteCompression.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs b/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs index dd41a2f..3830e6f 100644 --- a/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs +++ b/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs @@ -35,9 +35,11 @@ private static IEnumerable EnumerateCompressedBytes(Bitmap bitmap, IEnumer { var writer = new BitWriter(); - writer.WriteBit(bitmap.GetPixel(0, 0) == Color.White); + var firstPixel = bitmap.GetPixel(0, 0); - foreach (var span in bytes) + writer.WriteBit(firstPixel.ToArgb() == SpriteColours.White.ToArgb()); + + foreach (var span in GenerateBitSpans(bytes)) { WriteCompressedLength(writer, span - 1); } From 95fbf6016e59176348cb6d0382694c733379a5d0 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Thu, 28 Apr 2022 07:19:02 +0100 Subject: [PATCH 2/2] Add an extra sanity check as a precaution I don't think the odds of this function receiving a 0x0 image are high, but just to be on the safe side. --- .../ABSpriteEditor/Sprites/IO/SpriteCompression.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs b/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs index 3830e6f..3ec64e7 100644 --- a/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs +++ b/ABSpriteEditor/ABSpriteEditor/Sprites/IO/SpriteCompression.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; // @@ -33,9 +34,15 @@ public static IEnumerable EnumerateCompressedMaskBytes(Bitmap bitmap) private static IEnumerable EnumerateCompressedBytes(Bitmap bitmap, IEnumerable bytes) { - var writer = new BitWriter(); + if ((bitmap.Width < 1) || (bitmap.Height < 1)) + throw new ArgumentException("bitmap must be at least 1x1"); + + return EnumerateCompressedBytes(bitmap.GetPixel(0, 0), bytes); + } - var firstPixel = bitmap.GetPixel(0, 0); + private static IEnumerable EnumerateCompressedBytes(Color firstPixel, IEnumerable bytes) + { + var writer = new BitWriter(); writer.WriteBit(firstPixel.ToArgb() == SpriteColours.White.ToArgb());