From 1d497d6622c55bf3c51a296e83c4b28f36a78a17 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Thu, 3 Sep 2020 14:42:30 +0200 Subject: [PATCH] Try to reproduce #96 --- .../NetVips.Samples/Samples/RandomCropper.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/samples/NetVips.Samples/Samples/RandomCropper.cs b/samples/NetVips.Samples/Samples/RandomCropper.cs index 0b917202..e2107663 100644 --- a/samples/NetVips.Samples/Samples/RandomCropper.cs +++ b/samples/NetVips.Samples/Samples/RandomCropper.cs @@ -5,7 +5,7 @@ namespace NetVips.Samples using System.Threading.Tasks; /// - /// See: https://github.com/kleisauke/net-vips/issues/58 + /// See: https://github.com/kleisauke/net-vips/issues/96 /// public class RandomCropper : ISample { @@ -31,11 +31,34 @@ public Image RandomCrop(Image image, int tileSize) public string Execute(string[] args) { - using var fileStream = File.OpenRead(Filename); - using var image = Image.NewFromStream(fileStream); + NetVips.LeakSet(true); Parallel.For(0, 1000, new ParallelOptions {MaxDegreeOfParallelism = NetVips.ConcurrencyGet()}, - i => RandomCrop(image, TileSize).WriteToFile($"x_{i}.png")); + i => + { + using var input = File.OpenRead(Filename); + + using var source = new SourceCustom(); + source.OnRead += (buffer, length) => input.Read(buffer, 0, length); + source.OnSeek += (offset, origin) => input.Seek(offset, origin); + + using var output = File.OpenWrite($"x_{i}.png"); + + using var target = new TargetCustom(); + target.OnWrite += (buffer, length) => + { + output.Write(buffer, 0, length); + return length; + }; + target.OnFinish += () => output.Close(); + + using var image = Image.NewFromSource(source, access: Enums.Access.Sequential); + RandomCrop(image, TileSize).WriteToTarget(target, ".png"); + } + ); + + GC.Collect(); + GC.WaitForPendingFinalizers(); return "Done!"; }