From 7cc408f5ed6a27416dcd8f702e8dbe5a410cfc4f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 17 Mar 2017 00:04:27 +1100 Subject: [PATCH] Add indexed png encoder benchmark [skip ci] --- .../Image/EncodeIndexedPng.cs | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs new file mode 100644 index 000000000..ba383873c --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs @@ -0,0 +1,104 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Benchmarks.Image +{ + using System.IO; + + using BenchmarkDotNet.Attributes; + + using ImageSharp; + using ImageSharp.Formats; + using ImageSharp.Quantizers; + + /// + /// Benchmarks saving png files using different quantizers. System.Drawing cannot save indexed png files so we cannot compare. + /// + public class EncodeIndexedPng : BenchmarkBase + { + // System.Drawing needs this. + private Stream bmpStream; + private Image bmpCore; + + [Params(false)] + public bool LargeImage { get; set; } + + [Setup] + public void ReadImages() + { + if (this.bmpStream == null) + { + string path = this.LargeImage + ? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" + : "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; + this.bmpStream = File.OpenRead(path); + this.bmpCore = new Image(this.bmpStream); + this.bmpStream.Position = 0; + } + } + + [Cleanup] + public void Cleanup() + { + this.bmpStream.Dispose(); + this.bmpCore.Dispose(); + } + + [Benchmark(Baseline = true, Description = "ImageSharp Octree Png")] + public void PngCoreOctree() + { + using (MemoryStream memoryStream = new MemoryStream()) + { + PngEncoderOptions options = new PngEncoderOptions() { Quantizer = new OctreeQuantizer(), Quality = 256 }; + + this.bmpCore.SaveAsPng(memoryStream, options); + } + } + + [Benchmark(Description = "ImageSharp Octree NoDither Png")] + public void PngCoreOctreeNoDIther() + { + using (MemoryStream memoryStream = new MemoryStream()) + { + PngEncoderOptions options = new PngEncoderOptions { Quantizer = new OctreeQuantizer { Dither = false }, Quality = 256 }; + + this.bmpCore.SaveAsPng(memoryStream, options); + } + } + + [Benchmark(Description = "ImageSharp Palette Png")] + public void PngCorePalette() + { + using (MemoryStream memoryStream = new MemoryStream()) + { + PngEncoderOptions options = new PngEncoderOptions { Quantizer = new PaletteQuantizer(), Quality = 256 }; + + this.bmpCore.SaveAsPng(memoryStream, options); + } + } + + [Benchmark(Description = "ImageSharp Palette NoDither Png")] + public void PngCorePaletteNoDither() + { + using (MemoryStream memoryStream = new MemoryStream()) + { + PngEncoderOptions options = new PngEncoderOptions { Quantizer = new PaletteQuantizer { Dither = false }, Quality = 256 }; + + this.bmpCore.SaveAsPng(memoryStream, options); + } + } + + [Benchmark(Description = "ImageSharp Wu Png")] + public void PngCoreWu() + { + using (MemoryStream memoryStream = new MemoryStream()) + { + PngEncoderOptions options = new PngEncoderOptions() { Quantizer = new WuQuantizer(), Quality = 256 }; + + this.bmpCore.SaveAsPng(memoryStream, options); + } + } + } +} \ No newline at end of file