Skip to content

Commit

Permalink
Merge pull request #1496 from IldarKhayrutdinov/tiff-format-benchmarks
Browse files Browse the repository at this point in the history
#12 Implement Tiff Encoder benchmarks
  • Loading branch information
JimBobSquarePants committed Jan 26, 2021
2 parents 44af7d3 + ece80f1 commit a60ca65
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 105 deletions.
69 changes: 60 additions & 9 deletions tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Rgba32>(memoryStream))
using (var ms = new MemoryStream(this.data))
using (var image = Image.Load<Rgba32>(this.configuration, ms))
{
return image.Size();
}
Expand Down
93 changes: 0 additions & 93 deletions tests/ImageSharp.Benchmarks/Codecs/DecodeTiffBig.cs

This file was deleted.

6 changes: 3 additions & 3 deletions tests/ImageSharp.Benchmarks/Codecs/EncodeTga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
112 changes: 112 additions & 0 deletions tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
2 changes: 2 additions & 0 deletions tests/ImageSharp.Benchmarks/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;

namespace SixLabors.ImageSharp.Benchmarks
{
Expand All @@ -25,6 +26,7 @@ public Config()
}
#endif

this.SummaryStyle = SummaryStyle.Default.WithMaxParameterColumnWidth(40);
}

public class MultiFramework : Config
Expand Down

0 comments on commit a60ca65

Please sign in to comment.