Skip to content

Commit 4ace814

Browse files
committed
extend gif benchmarks
1 parent 71357d2 commit 4ace814

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Drawing.Imaging;
5+
using BenchmarkDotNet.Attributes;
6+
using SixLabors.ImageSharp.Formats.Gif;
7+
using SixLabors.ImageSharp.Processing;
8+
using SixLabors.ImageSharp.Processing.Processors.Quantization;
9+
using SixLabors.ImageSharp.Tests;
10+
using SDImage = System.Drawing.Image;
11+
12+
namespace SixLabors.ImageSharp.Benchmarks.Codecs;
13+
14+
public abstract class DecodeEncodeGif
15+
{
16+
private Stream outputStream;
17+
18+
protected abstract GifEncoder Encoder { get; }
19+
20+
[Params(TestImages.Gif.Leo, TestImages.Gif.Cheers)]
21+
public string TestImage { get; set; }
22+
23+
private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage);
24+
25+
[GlobalSetup]
26+
public void Setup() => this.outputStream = new MemoryStream();
27+
28+
[GlobalCleanup]
29+
public void Cleanup() => this.outputStream.Close();
30+
31+
[Benchmark(Baseline = true)]
32+
public void SystemDrawing()
33+
{
34+
this.outputStream.Position = 0;
35+
using SDImage image = SDImage.FromFile(this.TestImageFullPath);
36+
image.Save(this.outputStream, ImageFormat.Gif);
37+
}
38+
39+
[Benchmark]
40+
public void ImageSharp()
41+
{
42+
this.outputStream.Position = 0;
43+
using Image image = Image.Load(this.TestImageFullPath);
44+
image.SaveAsGif(this.outputStream, this.Encoder);
45+
}
46+
}
47+
48+
public class DecodeEncodeGif_DefaultEncoder : DecodeEncodeGif
49+
{
50+
protected override GifEncoder Encoder => new();
51+
}
52+
53+
public class DecodeEncodeGif_CoarsePaletteEncoder : DecodeEncodeGif
54+
{
55+
protected override GifEncoder Encoder => new()
56+
{
57+
Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse })
58+
};
59+
}

tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@
1212

1313
namespace SixLabors.ImageSharp.Benchmarks.Codecs;
1414

15-
[Config(typeof(Config.ShortMultiFramework))]
16-
public class EncodeGif
15+
public abstract class EncodeGif
1716
{
1817
// System.Drawing needs this.
1918
private Stream bmpStream;
2019
private SDImage bmpDrawing;
2120
private Image<Rgba32> bmpCore;
2221

23-
// Try to get as close to System.Drawing's output as possible
24-
private readonly GifEncoder encoder = new GifEncoder
25-
{
26-
Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse })
27-
};
22+
protected abstract GifEncoder Encoder { get; }
2823

29-
[Params(TestImages.Bmp.Car, TestImages.Png.Rgb48Bpp)]
24+
[Params(TestImages.Gif.Leo, TestImages.Gif.Cheers)]
3025
public string TestImage { get; set; }
3126

3227
[GlobalSetup]
@@ -61,6 +56,19 @@ public void GifSystemDrawing()
6156
public void GifImageSharp()
6257
{
6358
using var memoryStream = new MemoryStream();
64-
this.bmpCore.SaveAsGif(memoryStream, this.encoder);
59+
this.bmpCore.SaveAsGif(memoryStream, this.Encoder);
6560
}
6661
}
62+
63+
public class EncodeGif_DefaultEncoder : EncodeGif
64+
{
65+
protected override GifEncoder Encoder => new();
66+
}
67+
68+
public class EncodeGif_CoarsePaletteEncoder : EncodeGif
69+
{
70+
protected override GifEncoder Encoder => new()
71+
{
72+
Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse })
73+
};
74+
}

0 commit comments

Comments
 (0)