diff --git a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs index 09c0daa487..6c783fc990 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs @@ -1,38 +1,89 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +//// #define BIG_TESTS + using System.IO; + using BenchmarkDotNet.Attributes; + +using SixLabors.ImageSharp.Formats.Experimental.Tiff; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests; + using SDImage = System.Drawing.Image; using SDSize = System.Drawing.Size; namespace SixLabors.ImageSharp.Benchmarks.Codecs { - [Config(typeof(Config.ShortClr))] - public class DecodeTiff : BenchmarkBase + [Config(typeof(Config.ShortMultiFramework))] + public class DecodeTiff { - private byte[] tiffBytes; + private string prevImage = null; + + private byte[] data; + + private Configuration configuration; + +#if BIG_TESTS + private static readonly int BufferSize = 1024 * 68; + + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Path.Combine(TestImages.Tiff.Benchmark_Path, this.TestImage)); + + [Params( + TestImages.Tiff.Benchmark_BwFax3, + //// TestImages.Tiff.Benchmark_RgbFax4, + TestImages.Tiff.Benchmark_BwRle, + TestImages.Tiff.Benchmark_GrayscaleUncompressed, + TestImages.Tiff.Benchmark_PaletteUncompressed, + TestImages.Tiff.Benchmark_RgbDeflate, + TestImages.Tiff.Benchmark_RgbLzw, + TestImages.Tiff.Benchmark_RgbPackbits, + TestImages.Tiff.Benchmark_RgbUncompressed)] + public string TestImage { get; set; } + +#else + private static readonly int BufferSize = Configuration.Default.StreamProcessingBufferSize; private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); - [Params(TestImages.Tiff.RgbPackbits)] + [Params( + TestImages.Tiff.CcittFax3AllTermCodes, + TestImages.Tiff.HuffmanRleAllMakeupCodes, + TestImages.Tiff.GrayscaleUncompressed, + TestImages.Tiff.PaletteUncompressed, + TestImages.Tiff.RgbDeflate, + TestImages.Tiff.RgbLzwPredictor, + TestImages.Tiff.RgbPackbits, + TestImages.Tiff.RgbUncompressed)] public string TestImage { get; set; } +#endif [GlobalSetup] + public void Config() + { + if (this.configuration == null) + { + this.configuration = new Configuration(); + this.configuration.AddTiff(); + this.configuration.StreamProcessingBufferSize = BufferSize; + } + } + + [IterationSetup] public void ReadImages() { - if (this.tiffBytes == null) + if (this.prevImage != this.TestImage) { - this.tiffBytes = File.ReadAllBytes(this.TestImageFullPath); + this.data = File.ReadAllBytes(this.TestImageFullPath); + this.prevImage = this.TestImage; } } [Benchmark(Baseline = true, Description = "System.Drawing Tiff")] public SDSize TiffSystemDrawing() { - using (var memoryStream = new MemoryStream(this.tiffBytes)) + using (var memoryStream = new MemoryStream(this.data)) using (var image = SDImage.FromStream(memoryStream)) { return image.Size; @@ -42,8 +93,8 @@ public SDSize TiffSystemDrawing() [Benchmark(Description = "ImageSharp Tiff")] public Size TiffCore() { - using (var memoryStream = new MemoryStream(this.tiffBytes)) - using (var image = Image.Load(memoryStream)) + using (var ms = new MemoryStream(this.data)) + using (var image = Image.Load(this.configuration, ms)) { return image.Size(); } diff --git a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiffBig.cs b/tests/ImageSharp.Benchmarks/Codecs/DecodeTiffBig.cs deleted file mode 100644 index 9f8f53a370..0000000000 --- a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiffBig.cs +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.IO; - -using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Environments; -using BenchmarkDotNet.Jobs; -using BenchmarkDotNet.Reports; - -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Tests; - -using SDImage = System.Drawing.Image; -using SDSize = System.Drawing.Size; - -namespace SixLabors.ImageSharp.Benchmarks.Codecs -{ - [Config(typeof(DecodeTiffBig.Config.LongClr))] - public class DecodeTiffBig : BenchmarkBase - { - private class Config : SixLabors.ImageSharp.Benchmarks.Config - { - public class LongClr : Config - { - public LongClr() - { - this.AddJob( - Job.Default.WithRuntime(ClrRuntime.Net472).WithLaunchCount(1).WithWarmupCount(3).WithIterationCount(5), - Job.Default.WithRuntime(CoreRuntime.Core31).WithLaunchCount(1).WithWarmupCount(3).WithIterationCount(5), - Job.Default.WithRuntime(CoreRuntime.Core21).WithLaunchCount(1).WithWarmupCount(3).WithIterationCount(5)); - - this.SummaryStyle = SummaryStyle.Default.WithMaxParameterColumnWidth(60); - } - } - } - - private string prevImage = null; - - private byte[] data; - - private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Path.Combine(TestImages.Tiff.Benchmark_Path, this.TestImage)); - - [Params( - TestImages.Tiff.Benchmark_BwFax3, - //// TestImages.Tiff.Benchmark_RgbFax4, - TestImages.Tiff.Benchmark_BwRle, - TestImages.Tiff.Benchmark_GrayscaleUncompressed, - TestImages.Tiff.Benchmark_PaletteUncompressed, - TestImages.Tiff.Benchmark_RgbDeflate, - TestImages.Tiff.Benchmark_RgbLzw, - TestImages.Tiff.Benchmark_RgbPackbits, - TestImages.Tiff.Benchmark_RgbUncompressed)] - public string TestImage { get; set; } - - [IterationSetup] - public void ReadImages() - { - if (this.prevImage != this.TestImage) - { - this.data = File.ReadAllBytes(this.TestImageFullPath); - this.prevImage = this.TestImage; - } - } - - [Benchmark(Baseline = true, Description = "System.Drawing Tiff")] - public SDSize TiffSystemDrawing() - { - using (var memoryStream = new MemoryStream(this.data)) - using (var image = SDImage.FromStream(memoryStream)) - { - return image.Size; - } - } - - [Benchmark(Description = "ImageSharp Tiff")] - public Size TiffCore() - { - Configuration config = Configuration.Default.Clone(); - config.StreamProcessingBufferSize = 1024 * 64; - - config.ImageFormatsManager.AddImageFormat(Formats.Experimental.Tiff.TiffFormat.Instance); - config.ImageFormatsManager.AddImageFormatDetector(new Formats.Experimental.Tiff.TiffImageFormatDetector()); - config.ImageFormatsManager.SetDecoder(Formats.Experimental.Tiff.TiffFormat.Instance, new Formats.Experimental.Tiff.TiffDecoder()); - - using (var ms = new MemoryStream(this.data)) - using (var image = Image.Load(config, ms)) - { - return image.Size(); - } - } - } -} diff --git a/tests/ImageSharp.Benchmarks/Codecs/EncodeTga.cs b/tests/ImageSharp.Benchmarks/Codecs/EncodeTga.cs index bcb015e570..0e320e4a7f 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/EncodeTga.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/EncodeTga.cs @@ -40,17 +40,17 @@ public void Cleanup() } [Benchmark(Baseline = true, Description = "Magick Tga")] - public void BmpImageMagick() + public void TgaMagick() { using var memoryStream = new MemoryStream(); this.tgaMagick.Write(memoryStream, MagickFormat.Tga); } [Benchmark(Description = "ImageSharp Tga")] - public void BmpImageSharp() + public void TgaImageSharp() { using var memoryStream = new MemoryStream(); - this.tgaCore.SaveAsBmp(memoryStream); + this.tgaCore.SaveAsTga(memoryStream); } } } diff --git a/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs new file mode 100644 index 0000000000..c1927dc0f0 --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs @@ -0,0 +1,112 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System.Drawing.Imaging; +using System.IO; + +using BenchmarkDotNet.Attributes; + +using SixLabors.ImageSharp.Formats.Experimental.Tiff; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs +{ + [Config(typeof(Config.ShortMultiFramework))] + public class EncodeTiff + { + private System.Drawing.Image drawing; + private Image core; + + private Configuration configuration; + + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + + [Params(TestImages.Tiff.RgbUncompressed)] + public string TestImage { get; set; } + + [Params( + TiffEncoderCompression.None, + ////TiffEncoderCompression.Deflate, + TiffEncoderCompression.Lzw, + ////TiffEncoderCompression.PackBits, + TiffEncoderCompression.CcittGroup3Fax, + TiffEncoderCompression.ModifiedHuffman)] + public TiffEncoderCompression Compression { get; set; } + + [GlobalSetup] + public void ReadImages() + { + if (this.core == null) + { + this.configuration = new Configuration(); + this.configuration.AddTiff(); + + this.core = Image.Load(this.configuration, this.TestImageFullPath); + this.drawing = System.Drawing.Image.FromFile(this.TestImageFullPath); + } + } + + [GlobalCleanup] + public void Cleanup() + { + this.core.Dispose(); + this.drawing.Dispose(); + } + + [Benchmark(Baseline = true, Description = "System.Drawing Tiff")] + public void SystemDrawing() + { + ImageCodecInfo codec = FindCodecForType("image/tiff"); + using var parameters = new EncoderParameters(1); + parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)Cast(this.Compression)); + + using var memoryStream = new MemoryStream(); + this.drawing.Save(memoryStream, codec, parameters); + } + + [Benchmark(Description = "ImageSharp Tiff")] + public void TiffCore() + { + var encoder = new TiffEncoder() { Compression = this.Compression }; + using var memoryStream = new MemoryStream(); + this.core.SaveAsTiff(memoryStream, encoder); + } + + private static ImageCodecInfo FindCodecForType(string mimeType) + { + ImageCodecInfo[] imgEncoders = ImageCodecInfo.GetImageEncoders(); + + for (int i = 0; i < imgEncoders.GetLength(0); i++) + { + if (imgEncoders[i].MimeType == mimeType) + { + return imgEncoders[i]; + } + } + + return null; + } + + private static EncoderValue Cast(TiffEncoderCompression compression) + { + switch (compression) + { + case TiffEncoderCompression.None: + return EncoderValue.CompressionNone; + + case TiffEncoderCompression.CcittGroup3Fax: + return EncoderValue.CompressionCCITT3; + + case TiffEncoderCompression.ModifiedHuffman: + return EncoderValue.CompressionRle; + + case TiffEncoderCompression.Lzw: + return EncoderValue.CompressionLZW; + + default: + throw new System.ArgumentOutOfRangeException(nameof(compression)); + } + } + } +} diff --git a/tests/ImageSharp.Benchmarks/Config.cs b/tests/ImageSharp.Benchmarks/Config.cs index 0c40b482ad..16999ea7df 100644 --- a/tests/ImageSharp.Benchmarks/Config.cs +++ b/tests/ImageSharp.Benchmarks/Config.cs @@ -9,6 +9,7 @@ using BenchmarkDotNet.Diagnosers; using BenchmarkDotNet.Environments; using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Reports; namespace SixLabors.ImageSharp.Benchmarks { @@ -25,6 +26,7 @@ public Config() } #endif + this.SummaryStyle = SummaryStyle.Default.WithMaxParameterColumnWidth(40); } public class MultiFramework : Config