From 3b48dc39dcff1d424e94b34bbc842c5fb779bec8 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 6 Oct 2019 18:08:30 +0200 Subject: [PATCH] Add tga decoder tests --- .gitattributes | 1 + src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 12 +- .../PixelOperations{TPixel}.Generated.tt | 4 +- .../Formats/Tga/TgaDecoderTests.cs | 107 ++++++++++++++++++ .../ImageSharp.Tests/ImageSharp.Tests.csproj | 2 +- tests/ImageSharp.Tests/TestImages.cs | 8 ++ .../TestUtilities/TestEnvironment.Formats.cs | 6 +- tests/Images/Input/Tga/bike_16bit.tga | 3 + tests/Images/Input/Tga/bike_24bit.tga | 3 + tests/Images/Input/Tga/bike_32bit.tga | 3 + tests/Images/Input/Tga/bike_8bit.tga | 3 + 11 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs create mode 100644 tests/Images/Input/Tga/bike_16bit.tga create mode 100644 tests/Images/Input/Tga/bike_24bit.tga create mode 100644 tests/Images/Input/Tga/bike_32bit.tga create mode 100644 tests/Images/Input/Tga/bike_8bit.tga diff --git a/.gitattributes b/.gitattributes index b9a9ddd4c..195506770 100644 --- a/.gitattributes +++ b/.gitattributes @@ -68,6 +68,7 @@ *.gif binary *.jpg binary *.png binary +*.tga binary *.ttf binary *.snk binary # diff as plain text diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index a73f04bcd..abad2c5e7 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; @@ -108,7 +109,7 @@ namespace SixLabors.ImageSharp.Formats.Tga for (int y = 0; y < this.fileHeader.Height; y++) { this.currentStream.Read(row); - Span pixelSpan = pixels.GetRowSpan(y); + Span pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1); PixelOperations.Instance.FromGray8Bytes( this.configuration, row.GetSpan(), @@ -126,7 +127,7 @@ namespace SixLabors.ImageSharp.Formats.Tga for (int y = 0; y < this.fileHeader.Height; y++) { this.currentStream.Read(row); - Span pixelSpan = pixels.GetRowSpan(y); + Span pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1); PixelOperations.Instance.FromBgra5551Bytes( this.configuration, row.GetSpan(), @@ -144,7 +145,7 @@ namespace SixLabors.ImageSharp.Formats.Tga for (int y = 0; y < this.fileHeader.Height; y++) { this.currentStream.Read(row); - Span pixelSpan = pixels.GetRowSpan(y); + Span pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1); PixelOperations.Instance.FromBgr24Bytes( this.configuration, row.GetSpan(), @@ -162,7 +163,7 @@ namespace SixLabors.ImageSharp.Formats.Tga for (int y = 0; y < this.fileHeader.Height; y++) { this.currentStream.Read(row); - Span pixelSpan = pixels.GetRowSpan(y); + Span pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1); PixelOperations.Instance.FromBgra32Bytes( this.configuration, row.GetSpan(), @@ -201,6 +202,9 @@ namespace SixLabors.ImageSharp.Formats.Tga #endif this.currentStream.Read(buffer, 0, TgaFileHeader.Size); this.fileHeader = TgaFileHeader.Parse(buffer); + + // TODO: no meta data yet. + this.metadata = new ImageMetadata(); } } } diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt index 860301232..459924c31 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt @@ -18,7 +18,7 @@ /// /// Converts all pixels in 'source` span of into a span of -s. /// - /// A to configure internal operations + /// A to configure internal operations. /// The source of data. /// The to the destination pixels. internal virtual void From<#=pixelType#>(Configuration configuration, ReadOnlySpan<<#=pixelType#>> source, Span destPixels) @@ -41,7 +41,7 @@ /// A helper for that expects a byte span. /// The layout of the data in 'sourceBytes' must be compatible with layout. /// - /// A to configure internal operations + /// A to configure internal operations. /// The to the source bytes. /// The to the destination pixels. /// The number of pixels to convert. diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs new file mode 100644 index 000000000..d998ac292 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs @@ -0,0 +1,107 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; + +using ImageMagick; + +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats.Tga; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Tga +{ + using static TestImages.Tga; + + public class TgaDecoderTests + { + [Theory] + [WithFile(Grey, PixelTypes.Rgba32)] + public void TgaDecoder_CanDecode_Uncompressed_MonoChrome(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage(new TgaDecoder())) + { + image.DebugSave(provider); + CompareWithReferenceDecoder(provider, image); + } + } + + [Theory] + [WithFile(Bit16, PixelTypes.Rgba32)] + public void TgaDecoder_CanDecode_Uncompressed_16Bit(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage(new TgaDecoder())) + { + image.DebugSave(provider); + CompareWithReferenceDecoder(provider, image); + } + } + + [Theory] + [WithFile(Bit24, PixelTypes.Rgba32)] + public void TgaDecoder_CanDecode_Uncompressed_24Bit(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage(new TgaDecoder())) + { + image.DebugSave(provider); + CompareWithReferenceDecoder(provider, image); + } + } + + [Theory] + [WithFile(Bit32, PixelTypes.Rgba32)] + public void TgaDecoder_CanDecode_Uncompressed_32Bit(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage(new TgaDecoder())) + { + image.DebugSave(provider); + CompareWithReferenceDecoder(provider, image); + } + } + + private void CompareWithReferenceDecoder(TestImageProvider provider, Image image) + where TPixel : struct, IPixel + { + string path = TestImageProvider.GetFilePathOrNull(provider); + if (path == null) + { + throw new InvalidOperationException("CompareToOriginal() works only with file providers!"); + } + + TestFile testFile = TestFile.Create(path); + Image magickImage = this.DecodeWithMagick(Configuration.Default, new FileInfo(testFile.FullPath)); + ImageComparer.Exact.VerifySimilarity(image, magickImage); + } + + private Image DecodeWithMagick(Configuration configuration, FileInfo fileInfo) + where TPixel : struct, IPixel + { + using (var magickImage = new MagickImage(fileInfo)) + { + var result = new Image(configuration, magickImage.Width, magickImage.Height); + Span resultPixels = result.GetPixelSpan(); + + using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) + { + byte[] data = pixels.ToByteArray(PixelMapping.RGBA); + + PixelOperations.Instance.FromRgba32Bytes( + configuration, + data, + resultPixels, + resultPixels.Length); + } + + return result; + } + } + } +} diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 1ac5f8085..1f6b8b4d9 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -14,7 +14,7 @@ - + diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 146f2efcd..bd1a0d391 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -365,5 +365,13 @@ namespace SixLabors.ImageSharp.Tests public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin, Leo, Ratio4x1, Ratio1x4 }; } + + public static class Tga + { + public const string Bit32 = "Tga/bike_32bit.tga"; + public const string Bit24 = "Tga/bike_24bit.tga"; + public const string Bit16 = "Tga/bike_16bit.tga"; + public const string Grey = "Tga/bike_8bit.tga"; + } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index 7d0684722..e09b27c71 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -8,6 +8,7 @@ using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; namespace SixLabors.ImageSharp.Tests @@ -53,7 +54,8 @@ namespace SixLabors.ImageSharp.Tests { var cfg = new Configuration( new JpegConfigurationModule(), - new GifConfigurationModule() + new GifConfigurationModule(), + new TgaConfigurationModule() ); // Magick codecs should work on all platforms @@ -75,4 +77,4 @@ namespace SixLabors.ImageSharp.Tests return cfg; } } -} \ No newline at end of file +} diff --git a/tests/Images/Input/Tga/bike_16bit.tga b/tests/Images/Input/Tga/bike_16bit.tga new file mode 100644 index 000000000..00489d94a --- /dev/null +++ b/tests/Images/Input/Tga/bike_16bit.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b2bc922e2397ce8cd8b1e7792f20e2c7edad68ad8fac037a5b91bba5148a80b +size 97518 diff --git a/tests/Images/Input/Tga/bike_24bit.tga b/tests/Images/Input/Tga/bike_24bit.tga new file mode 100644 index 000000000..a5e46f794 --- /dev/null +++ b/tests/Images/Input/Tga/bike_24bit.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:26d55794c9b012b07517d1129630ccc35ca56015cbdd481debea00826130f925 +size 146268 diff --git a/tests/Images/Input/Tga/bike_32bit.tga b/tests/Images/Input/Tga/bike_32bit.tga new file mode 100644 index 000000000..70e755d1f --- /dev/null +++ b/tests/Images/Input/Tga/bike_32bit.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb3774b695d2409f3e7584dc67ce7b3d8a75377c194e7f33e4cc315b3ae36a35 +size 195018 diff --git a/tests/Images/Input/Tga/bike_8bit.tga b/tests/Images/Input/Tga/bike_8bit.tga new file mode 100644 index 000000000..ba11619a5 --- /dev/null +++ b/tests/Images/Input/Tga/bike_8bit.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75f3ce893f38d90767c692eb9628026c0421330934a5cbb686813ec26a9606d2 +size 48768