Browse Source

Add test making sure not always RLE packets are written

pull/2197/head
Brian Popow 4 years ago
parent
commit
e7173328ba
  1. 4
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
  2. 26
      tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Tga/whitestripes.png

4
src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

@ -301,7 +301,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
TPixel nextPixel = pixelRow[x];
if (currentPixel.Equals(nextPixel))
{
return (byte)Math.Max(0, unEqualPixelCount - 1);
return unEqualPixelCount;
}
unEqualPixelCount++;
@ -314,7 +314,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
currentPixel = nextPixel;
}
return (byte)Math.Max(0, unEqualPixelCount - 1);
return unEqualPixelCount;
}
private IMemoryOwner<byte> AllocateRow(int width, int bytesPerPixel)

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

@ -56,12 +56,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[MemberData(nameof(TgaBitsPerPixelFiles))]
public void TgaEncoder_WithCompression_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel)
{
var options = new TgaEncoder()
{
Compression = TgaCompression.RunLength
};
TestFile testFile = TestFile.Create(imagePath);
var options = new TgaEncoder() { Compression = TgaCompression.RunLength };
var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image())
{
using (var memStream = new MemoryStream())
@ -121,6 +117,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaEncoder_Bit32_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel32)
where TPixel : unmanaged, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, TgaCompression.RunLength);
[Theory]
[WithFile(WhiteStripesPattern, PixelTypes.Rgba32, 2748)]
public void TgaEncoder_DoesNotAlwaysUseRunLengthPackets<TPixel>(TestImageProvider<TPixel> provider, int expectedBytes)
where TPixel : unmanaged, IPixel<TPixel>
{
// The test image has alternating black and white pixels, which should make using always RLE data inefficient.
using (Image<TPixel> image = provider.GetImage())
{
var options = new TgaEncoder() { Compression = TgaCompression.RunLength };
using (var memStream = new MemoryStream())
{
image.Save(memStream, options);
byte[] imageBytes = memStream.ToArray();
Assert.Equal(expectedBytes, imageBytes.Length);
}
}
}
[Theory]
[WithFile(Bit32BottomLeft, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel32)]
[WithFile(Bit24BottomLeft, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel24)]

1
tests/ImageSharp.Tests/TestImages.cs

@ -546,6 +546,7 @@ namespace SixLabors.ImageSharp.Tests
public const string NoAlphaBits32BitRle = "Tga/32bit_rle_no_alphabits.tga";
public const string Github_RLE_legacy = "Tga/Github_RLE_legacy.tga";
public const string WhiteStripesPattern = "Tga/whitestripes.png";
}
public static class Webp

3
tests/Images/Input/Tga/whitestripes.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2bc5d67ce368d2a40fb99df994c6973287fca2d8c8cff78227996f9acb5c6e1e
size 127
Loading…
Cancel
Save