Skip to content

Commit

Permalink
Try to reproduce #96
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Sep 3, 2020
1 parent 1ab30f2 commit 1d497d6
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions samples/NetVips.Samples/Samples/RandomCropper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NetVips.Samples
using System.Threading.Tasks;

/// <summary>
/// See: https://github.com/kleisauke/net-vips/issues/58
/// See: https://github.com/kleisauke/net-vips/issues/96
/// </summary>
public class RandomCropper : ISample
{
Expand All @@ -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!";
}
Expand Down

0 comments on commit 1d497d6

Please sign in to comment.