mirror of https://github.com/SixLabors/ImageSharp
5 changed files with 177 additions and 105 deletions
@ -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<Rgba32>(config, ms)) |
|
||||
{ |
|
||||
return image.Size(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -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<Rgba32> 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<Rgba32>(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)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue