Skip to content

Commit d3f3eec

Browse files
Merge pull request #1789 from SixLabors/af/benchmark/SkiaBitmap_DecodeToTargetSize
Add "decode to target size" skia variant to benchkarks
2 parents 099a676 + bfcf134 commit d3f3eec

2 files changed

Lines changed: 38 additions & 17 deletions

File tree

tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressBenchmarks.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ private void ForEachImage(Action<string> action, int maxDegreeOfParallelism)
4242
1
4343
};
4444

45-
[Benchmark(Baseline = true)]
45+
[Benchmark]
4646
[ArgumentsSource(nameof(ParallelismValues))]
4747
public void SystemDrawing(int maxDegreeOfParallelism) => this.ForEachImage(this.runner.SystemDrawingResize, maxDegreeOfParallelism);
4848

49-
[Benchmark]
49+
[Benchmark(Baseline = true)]
5050
[ArgumentsSource(nameof(ParallelismValues))]
5151
public void ImageSharp(int maxDegreeOfParallelism) => this.ForEachImage(this.runner.ImageSharpResize, maxDegreeOfParallelism);
5252

@@ -62,6 +62,10 @@ private void ForEachImage(Action<string> action, int maxDegreeOfParallelism)
6262
[ArgumentsSource(nameof(ParallelismValues))]
6363
public void SkiaBitmap(int maxDegreeOfParallelism) => this.ForEachImage(this.runner.SkiaBitmapResize, maxDegreeOfParallelism);
6464

65+
[Benchmark]
66+
[ArgumentsSource(nameof(ParallelismValues))]
67+
public void SkiaBitmapDecodeToTargetSize(int maxDegreeOfParallelism) => this.ForEachImage(this.runner.SkiaBitmapDecodeToTargetSize, maxDegreeOfParallelism);
68+
6569
[Benchmark]
6670
[ArgumentsSource(nameof(ParallelismValues))]
6771
public void NetVips(int maxDegreeOfParallelism) => this.ForEachImage(this.runner.NetVipsResize, maxDegreeOfParallelism);

tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressRunner.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Drawing.Imaging;
88
using System.IO;
99
using System.Linq;
10+
using System.Runtime.CompilerServices;
1011
using System.Runtime.InteropServices;
1112
using System.Threading.Tasks;
1213
using ImageMagick;
@@ -32,13 +33,6 @@ public enum JpegKind
3233
public class LoadResizeSaveStressRunner
3334
{
3435
private const int Quality = 75;
35-
private const string ImageSharp = nameof(ImageSharp);
36-
private const string SystemDrawing = nameof(SystemDrawing);
37-
private const string MagickNET = nameof(MagickNET);
38-
private const string NetVips = nameof(NetVips);
39-
private const string MagicScaler = nameof(MagicScaler);
40-
private const string SkiaSharpCanvas = nameof(SkiaSharpCanvas);
41-
private const string SkiaSharpBitmap = nameof(SkiaSharpBitmap);
4236

4337
// Set the quality for ImagSharp
4438
private readonly JpegEncoder imageSharpJpegEncoder = new() { Quality = Quality };
@@ -133,7 +127,7 @@ private void IncreaseTotalMegapixels(int width, int height)
133127
this.TotalProcessedMegapixels += pixels / 1_000_000.0;
134128
}
135129

136-
private string OutputPath(string inputPath, string postfix) =>
130+
private string OutputPath(string inputPath, [CallerMemberName]string postfix = null) =>
137131
Path.Combine(
138132
this.outputDirectory,
139133
Path.GetFileNameWithoutExtension(inputPath) + "-" + postfix + Path.GetExtension(inputPath));
@@ -175,12 +169,12 @@ public void SystemDrawingResize(string input)
175169
using var encoderParams = new EncoderParameters(1);
176170
using var qualityParam = new EncoderParameter(Encoder.Quality, (long)Quality);
177171
encoderParams.Param[0] = qualityParam;
178-
resized.Save(this.OutputPath(input, SystemDrawing), this.systemDrawingJpegCodec, encoderParams);
172+
resized.Save(this.OutputPath(input), this.systemDrawingJpegCodec, encoderParams);
179173
}
180174

181175
public void ImageSharpResize(string input)
182176
{
183-
using FileStream output = File.Open(this.OutputPath(input, ImageSharp), FileMode.Create);
177+
using FileStream output = File.Open(this.OutputPath(input), FileMode.Create);
184178

185179
// Resize it to fit a 150x150 square
186180
using var image = ImageSharpImage.Load(input);
@@ -214,7 +208,7 @@ public void MagickResize(string input)
214208
image.Quality = Quality;
215209

216210
// Save the results
217-
image.Write(this.OutputPath(input, MagickNET));
211+
image.Write(this.OutputPath(input));
218212
}
219213

220214
public void MagicScalerResize(string input)
@@ -230,7 +224,7 @@ public void MagicScalerResize(string input)
230224
};
231225

232226
// TODO: Is there a way to capture input dimensions for IncreaseTotalMegapixels?
233-
using var output = new FileStream(this.OutputPath(input, MagicScaler), FileMode.Create);
227+
using var output = new FileStream(this.OutputPath(input), FileMode.Create);
234228
MagicImageProcessor.ProcessImage(input, output, settings);
235229
}
236230

@@ -246,7 +240,7 @@ public void SkiaCanvasResize(string input)
246240
canvas.DrawBitmap(original, 0, 0, paint);
247241
canvas.Flush();
248242

249-
using FileStream output = File.OpenWrite(this.OutputPath(input, SkiaSharpCanvas));
243+
using FileStream output = File.OpenWrite(this.OutputPath(input));
250244
surface.Snapshot()
251245
.Encode(SKEncodedImageFormat.Jpeg, Quality)
252246
.SaveTo(output);
@@ -264,7 +258,30 @@ public void SkiaBitmapResize(string input)
264258
}
265259

266260
using var image = SKImage.FromBitmap(resized);
267-
using FileStream output = File.OpenWrite(this.OutputPath(input, SkiaSharpBitmap));
261+
using FileStream output = File.OpenWrite(this.OutputPath(input));
262+
image.Encode(SKEncodedImageFormat.Jpeg, Quality)
263+
.SaveTo(output);
264+
}
265+
266+
public void SkiaBitmapDecodeToTargetSize(string input)
267+
{
268+
using var codec = SKCodec.Create(input);
269+
270+
SKImageInfo info = codec.Info;
271+
this.IncreaseTotalMegapixels(info.Width, info.Height);
272+
(int Width, int Height) scaled = this.ScaledSize(info.Width, info.Height, this.ThumbnailSize);
273+
SKSizeI supportedScale = codec.GetScaledDimensions((float)scaled.Width / info.Width);
274+
275+
using var original = SKBitmap.Decode(codec, new SKImageInfo(supportedScale.Width, supportedScale.Height));
276+
using SKBitmap resized = original.Resize(new SKImageInfo(scaled.Width, scaled.Height), SKFilterQuality.High);
277+
if (resized == null)
278+
{
279+
return;
280+
}
281+
282+
using var image = SKImage.FromBitmap(resized);
283+
284+
using FileStream output = File.OpenWrite(this.OutputPath(input, nameof(this.SkiaBitmapDecodeToTargetSize)));
268285
image.Encode(SKEncodedImageFormat.Jpeg, Quality)
269286
.SaveTo(output);
270287
}
@@ -275,7 +292,7 @@ public void NetVipsResize(string input)
275292
using var thumb = NetVipsImage.Thumbnail(input, this.ThumbnailSize, this.ThumbnailSize);
276293

277294
// Save the results
278-
thumb.Jpegsave(this.OutputPath(input, NetVips), q: Quality, strip: true);
295+
thumb.Jpegsave(this.OutputPath(input), q: Quality, strip: true);
279296
}
280297
}
281298
}

0 commit comments

Comments
 (0)