// 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); } } [Theory] [WithFile(GreyRle, PixelTypes.Rgba32)] public void TgaDecoder_CanDecode_RunLenghtEncoded_MonoChrome(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) { image.DebugSave(provider); CompareWithReferenceDecoder(provider, image); } } [Theory] [WithFile(Bit16Rle, PixelTypes.Rgba32)] public void TgaDecoder_CanDecode_RunLenghtEncoded_16Bit(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) { image.DebugSave(provider); CompareWithReferenceDecoder(provider, image); } } [Theory] [WithFile(Bit24Rle, PixelTypes.Rgba32)] public void TgaDecoder_CanDecode_RunLenghtEncoded_24Bit(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) { image.DebugSave(provider); CompareWithReferenceDecoder(provider, image); } } [Theory] [WithFile(Bit32Rle, PixelTypes.Rgba32)] public void TgaDecoder_CanDecode_RunLenghtEncoded_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; } } } }