Skip to content

Commit e717332

Browse files
committed
Add test making sure not always RLE packets are written
1 parent 5fff1e4 commit e717332

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private byte FindUnEqualPixels<TPixel>(Span<TPixel> pixelRow, int xStart)
301301
TPixel nextPixel = pixelRow[x];
302302
if (currentPixel.Equals(nextPixel))
303303
{
304-
return (byte)Math.Max(0, unEqualPixelCount - 1);
304+
return unEqualPixelCount;
305305
}
306306

307307
unEqualPixelCount++;
@@ -314,7 +314,7 @@ private byte FindUnEqualPixels<TPixel>(Span<TPixel> pixelRow, int xStart)
314314
currentPixel = nextPixel;
315315
}
316316

317-
return (byte)Math.Max(0, unEqualPixelCount - 1);
317+
return unEqualPixelCount;
318318
}
319319

320320
private IMemoryOwner<byte> AllocateRow(int width, int bytesPerPixel)

tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ public void TgaEncoder_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bm
5656
[MemberData(nameof(TgaBitsPerPixelFiles))]
5757
public void TgaEncoder_WithCompression_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel)
5858
{
59-
var options = new TgaEncoder()
60-
{
61-
Compression = TgaCompression.RunLength
62-
};
63-
64-
TestFile testFile = TestFile.Create(imagePath);
59+
var options = new TgaEncoder() { Compression = TgaCompression.RunLength };
60+
var testFile = TestFile.Create(imagePath);
6561
using (Image<Rgba32> input = testFile.CreateRgba32Image())
6662
{
6763
using (var memStream = new MemoryStream())
@@ -121,6 +117,24 @@ public void TgaEncoder_Bit24_WithRunLengthEncoding_Works<TPixel>(TestImageProvid
121117
public void TgaEncoder_Bit32_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel32)
122118
where TPixel : unmanaged, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, TgaCompression.RunLength);
123119

120+
[Theory]
121+
[WithFile(WhiteStripesPattern, PixelTypes.Rgba32, 2748)]
122+
public void TgaEncoder_DoesNotAlwaysUseRunLengthPackets<TPixel>(TestImageProvider<TPixel> provider, int expectedBytes)
123+
where TPixel : unmanaged, IPixel<TPixel>
124+
{
125+
// The test image has alternating black and white pixels, which should make using always RLE data inefficient.
126+
using (Image<TPixel> image = provider.GetImage())
127+
{
128+
var options = new TgaEncoder() { Compression = TgaCompression.RunLength };
129+
using (var memStream = new MemoryStream())
130+
{
131+
image.Save(memStream, options);
132+
byte[] imageBytes = memStream.ToArray();
133+
Assert.Equal(expectedBytes, imageBytes.Length);
134+
}
135+
}
136+
}
137+
124138
[Theory]
125139
[WithFile(Bit32BottomLeft, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel32)]
126140
[WithFile(Bit24BottomLeft, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel24)]

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ public static class Tga
546546
public const string NoAlphaBits32BitRle = "Tga/32bit_rle_no_alphabits.tga";
547547

548548
public const string Github_RLE_legacy = "Tga/Github_RLE_legacy.tga";
549+
public const string WhiteStripesPattern = "Tga/whitestripes.png";
549550
}
550551

551552
public static class Webp
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)