Browse Source

Add tga decoder tests

af/merge-core
Brian Popow 6 years ago
parent
commit
3b48dc39dc
  1. 1
      .gitattributes
  2. 12
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  3. 4
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt
  4. 107
      tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
  5. 2
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj
  6. 8
      tests/ImageSharp.Tests/TestImages.cs
  7. 6
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs
  8. 3
      tests/Images/Input/Tga/bike_16bit.tga
  9. 3
      tests/Images/Input/Tga/bike_24bit.tga
  10. 3
      tests/Images/Input/Tga/bike_32bit.tga
  11. 3
      tests/Images/Input/Tga/bike_8bit.tga

1
.gitattributes

@ -68,6 +68,7 @@
*.gif binary
*.jpg binary
*.png binary
*.tga binary
*.ttf binary
*.snk binary
# diff as plain text

12
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<TPixel> pixelSpan = pixels.GetRowSpan(y);
Span<TPixel> pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1);
PixelOperations<TPixel>.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<TPixel> pixelSpan = pixels.GetRowSpan(y);
Span<TPixel> pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1);
PixelOperations<TPixel>.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<TPixel> pixelSpan = pixels.GetRowSpan(y);
Span<TPixel> pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1);
PixelOperations<TPixel>.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<TPixel> pixelSpan = pixels.GetRowSpan(y);
Span<TPixel> pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1);
PixelOperations<TPixel>.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();
}
}
}

4
src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt

@ -18,7 +18,7 @@
/// <summary>
/// Converts all pixels in 'source` span of <see cref="<#=pixelType#>"/> into a span of <typeparamref name="TPixel"/>-s.
/// </summary>
/// <param name="configuration">A <see cref="Configuration"/> to configure internal operations</param>
/// <param name="configuration">A <see cref="Configuration"/> to configure internal operations.</param>
/// <param name="source">The source <see cref="Span{T}"/> of <see cref="<#=pixelType#>"/> data.</param>
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param>
internal virtual void From<#=pixelType#>(Configuration configuration, ReadOnlySpan<<#=pixelType#>> source, Span<TPixel> destPixels)
@ -41,7 +41,7 @@
/// A helper for <see cref="From<#=pixelType#>(Configuration, ReadOnlySpan{<#=pixelType#>}, Span{TPixel})"/> that expects a byte span.
/// The layout of the data in 'sourceBytes' must be compatible with <see cref="<#=pixelType#>"/> layout.
/// </summary>
/// <param name="configuration">A <see cref="Configuration"/> to configure internal operations</param>
/// <param name="configuration">A <see cref="Configuration"/> to configure internal operations.</param>
/// <param name="sourceBytes">The <see cref="ReadOnlySpan{T}"/> to the source bytes.</param>
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param>
/// <param name="count">The number of pixels to convert.</param>

107
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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
{
image.DebugSave(provider);
CompareWithReferenceDecoder(provider, image);
}
}
[Theory]
[WithFile(Bit16, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_Uncompressed_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
{
image.DebugSave(provider);
CompareWithReferenceDecoder(provider, image);
}
}
[Theory]
[WithFile(Bit24, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_Uncompressed_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
{
image.DebugSave(provider);
CompareWithReferenceDecoder(provider, image);
}
}
[Theory]
[WithFile(Bit32, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_Uncompressed_32Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
{
image.DebugSave(provider);
CompareWithReferenceDecoder(provider, image);
}
}
private void CompareWithReferenceDecoder<TPixel>(TestImageProvider<TPixel> provider, Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
string path = TestImageProvider<TPixel>.GetFilePathOrNull(provider);
if (path == null)
{
throw new InvalidOperationException("CompareToOriginal() works only with file providers!");
}
TestFile testFile = TestFile.Create(path);
Image<Rgba32> magickImage = this.DecodeWithMagick<Rgba32>(Configuration.Default, new FileInfo(testFile.FullPath));
ImageComparer.Exact.VerifySimilarity(image, magickImage);
}
private Image<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo)
where TPixel : struct, IPixel<TPixel>
{
using (var magickImage = new MagickImage(fileInfo))
{
var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height);
Span<TPixel> resultPixels = result.GetPixelSpan();
using (IPixelCollection pixels = magickImage.GetPixelsUnsafe())
{
byte[] data = pixels.ToByteArray(PixelMapping.RGBA);
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
configuration,
data,
resultPixels,
resultPixels.Length);
}
return result;
}
}
}
}

2
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -14,7 +14,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Magick.NET-Q16-AnyCPU" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="7.14.4" />
<PackageReference Include="Moq" />
<PackageReference Include="System.Drawing.Common" />
<PackageReference Include="xunit.runner.console" />

8
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";
}
}
}

6
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;
}
}
}
}

3
tests/Images/Input/Tga/bike_16bit.tga

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3b2bc922e2397ce8cd8b1e7792f20e2c7edad68ad8fac037a5b91bba5148a80b
size 97518

3
tests/Images/Input/Tga/bike_24bit.tga

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:26d55794c9b012b07517d1129630ccc35ca56015cbdd481debea00826130f925
size 146268

3
tests/Images/Input/Tga/bike_32bit.tga

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cb3774b695d2409f3e7584dc67ce7b3d8a75377c194e7f33e4cc315b3ae36a35
size 195018

3
tests/Images/Input/Tga/bike_8bit.tga

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