From 4ef69db6ca6011d0d176f269c4f577269d6560d3 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Wed, 9 Sep 2020 11:25:02 +0200 Subject: [PATCH] Add S3 sample (#96) --- samples/NetVips.Samples/ISample.cs | 2 +- .../NetVips.Samples/NetVips.Samples.csproj | 4 ++ samples/NetVips.Samples/Program.cs | 18 +++--- samples/NetVips.Samples/Samples/Canny.cs | 6 +- .../Samples/CaptchaGenerator.cs | 4 +- samples/NetVips.Samples/Samples/Combine.cs | 6 +- samples/NetVips.Samples/Samples/Duotone.cs | 6 +- .../Samples/EmbedMultiplyConv.cs | 6 +- samples/NetVips.Samples/Samples/Emboss.cs | 6 +- samples/NetVips.Samples/Samples/GdiConvert.cs | 5 +- .../NetVips.Samples/Samples/GenerateEnums.cs | 7 ++- .../Samples/GenerateImageClass.cs | 4 +- .../Samples/GenerateImageOperators.cs | 4 +- samples/NetVips.Samples/Samples/HelloWorld.cs | 6 +- .../Samples/IdentifyExtension.cs | 4 +- .../NetVips.Samples/Samples/ImagePyramid.cs | 14 ++--- samples/NetVips.Samples/Samples/LeakTest.cs | 4 +- .../NetVips.Samples/Samples/NetworkStream.cs | 16 +++-- .../Samples/NewFromMemoryRef.cs | 4 +- .../NetVips.Samples/Samples/OnePointMosaic.cs | 5 +- .../NetVips.Samples/Samples/OperationRef.cs | 4 +- samples/NetVips.Samples/Samples/Progress.cs | 4 +- .../NetVips.Samples/Samples/RandomCropper.cs | 35 ++--------- .../NetVips.Samples/Samples/ShapeCropping.cs | 4 +- samples/NetVips.Samples/Samples/Smartcrop.cs | 6 +- samples/NetVips.Samples/Samples/Sobel.cs | 6 +- samples/NetVips.Samples/Samples/Stream.cs | 16 +++-- samples/NetVips.Samples/Samples/Thumbnail.cs | 6 +- .../Samples/ThumbnailPipeline.cs | 4 +- .../NetVips.Samples/Samples/ThumbnailS3.cs | 59 +++++++++++++++++++ .../Samples/ThumbnailStream.cs | 16 +++-- 31 files changed, 166 insertions(+), 125 deletions(-) create mode 100644 samples/NetVips.Samples/Samples/ThumbnailS3.cs diff --git a/samples/NetVips.Samples/ISample.cs b/samples/NetVips.Samples/ISample.cs index 553a139b..42a77605 100644 --- a/samples/NetVips.Samples/ISample.cs +++ b/samples/NetVips.Samples/ISample.cs @@ -6,6 +6,6 @@ public interface ISample string Category { get; } - string Execute(string[] args); + void Execute(string[] args); } } \ No newline at end of file diff --git a/samples/NetVips.Samples/NetVips.Samples.csproj b/samples/NetVips.Samples/NetVips.Samples.csproj index dd12eee9..e081baa0 100644 --- a/samples/NetVips.Samples/NetVips.Samples.csproj +++ b/samples/NetVips.Samples/NetVips.Samples.csproj @@ -22,4 +22,8 @@ + + + + diff --git a/samples/NetVips.Samples/Program.cs b/samples/NetVips.Samples/Program.cs index 3eee9252..6fdc914f 100644 --- a/samples/NetVips.Samples/Program.cs +++ b/samples/NetVips.Samples/Program.cs @@ -46,19 +46,17 @@ static void Main(string[] args) input = Console.ReadLine(); } - if (int.TryParse(input, out var userChoice) && TryGetSample(userChoice, out var sample)) + if (int.TryParse(input, out var userChoice)) { - Console.WriteLine($"Executing sample: {sample.Name}"); - var result = sample.Execute(sampleArgs); - Console.WriteLine("Sample successfully executed!"); - if (result != null) + if (!TryGetSample(userChoice, out var sample)) { - Console.WriteLine($"Result: {result}"); + Console.WriteLine("Sample doesn't exists, try again"); + continue; } - } - else - { - Console.WriteLine("Sample doesn't exists, try again"); + + Console.WriteLine($"Executing sample: {sample.Name}"); + sample.Execute(sampleArgs); + Console.WriteLine("Sample successfully executed!"); } // Clear any arguments diff --git a/samples/NetVips.Samples/Samples/Canny.cs b/samples/NetVips.Samples/Samples/Canny.cs index 680a39bf..f210943b 100644 --- a/samples/NetVips.Samples/Samples/Canny.cs +++ b/samples/NetVips.Samples/Samples/Canny.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + public class Canny : ISample { public string Name => "Canny"; @@ -7,7 +9,7 @@ public class Canny : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var im = Image.NewFromFile(Filename, access: Enums.Access.Sequential); @@ -22,7 +24,7 @@ public string Execute(string[] args) im.WriteToFile("canny.jpg"); - return "See canny.jpg"; + Console.WriteLine("See canny.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/CaptchaGenerator.cs b/samples/NetVips.Samples/Samples/CaptchaGenerator.cs index 02d93517..dd9b17d2 100644 --- a/samples/NetVips.Samples/Samples/CaptchaGenerator.cs +++ b/samples/NetVips.Samples/Samples/CaptchaGenerator.cs @@ -36,7 +36,7 @@ public Image Wobble(Image image) return image.Mapim(xy); } - public string Execute(string[] args) + public void Execute(string[] args) { var random = new Random(); @@ -98,7 +98,7 @@ public string Execute(string[] args) final.WriteToFile("captcha.jpg"); - return "See captcha.jpg"; + Console.WriteLine("See captcha.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Combine.cs b/samples/NetVips.Samples/Samples/Combine.cs index 40815d9d..bacdb2ef 100644 --- a/samples/NetVips.Samples/Samples/Combine.cs +++ b/samples/NetVips.Samples/Samples/Combine.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + /// /// From: https://github.com/libvips/lua-vips/blob/master/example/combine.lua /// @@ -14,7 +16,7 @@ public class Combine : ISample public const int Left = 100; public const int Top = 100; - public string Execute(string[] args) + public void Execute(string[] args) { var main = Image.NewFromFile(MainFilename, access: Enums.Access.Sequential); var watermark = Image.NewFromFile(WatermarkFilename, access: Enums.Access.Sequential); @@ -37,7 +39,7 @@ public string Execute(string[] args) combined.WriteToFile("combine.jpg"); - return "See combine.jpg"; + Console.WriteLine("See combine.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Duotone.cs b/samples/NetVips.Samples/Samples/Duotone.cs index ea3aae80..c154f99c 100644 --- a/samples/NetVips.Samples/Samples/Duotone.cs +++ b/samples/NetVips.Samples/Samples/Duotone.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + /// /// From: https://github.com/lovell/sharp/issues/1235#issuecomment-390907151 /// @@ -16,7 +18,7 @@ public class Duotone : ISample // #D8E74F as CIELAB triple public double[] Stop = { 88.12, -23.952, 69.178 }; - public string Execute(string[] args) + public void Execute(string[] args) { // Makes a lut which is a smooth gradient from start colour to stop colour, // with start and stop in CIELAB @@ -48,7 +50,7 @@ public string Execute(string[] args) // Finally, write the result back to a file on disk im.WriteToFile("duotone.jpg"); - return "See duotone.jpg"; + Console.WriteLine("See duotone.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/EmbedMultiplyConv.cs b/samples/NetVips.Samples/Samples/EmbedMultiplyConv.cs index b7bef5f5..23e8f5dd 100644 --- a/samples/NetVips.Samples/Samples/EmbedMultiplyConv.cs +++ b/samples/NetVips.Samples/Samples/EmbedMultiplyConv.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + /// /// From: https://github.com/libvips/ruby-vips#example /// @@ -10,7 +12,7 @@ public class EmbedMultiplyConv : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var im = Image.NewFromFile(Filename); @@ -35,7 +37,7 @@ public string Execute(string[] args) // finally, write the result back to a file on disk im.WriteToFile("embed-multiply-conv.jpg"); - return "See embed-multiply-conv.jpg"; + Console.WriteLine("See embed-multiply-conv.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Emboss.cs b/samples/NetVips.Samples/Samples/Emboss.cs index 52464b75..f9f748b7 100644 --- a/samples/NetVips.Samples/Samples/Emboss.cs +++ b/samples/NetVips.Samples/Samples/Emboss.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + public class Emboss : ISample { public string Name => "Emboss"; @@ -7,7 +9,7 @@ public class Emboss : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var im = Image.NewFromFile(Filename); @@ -42,7 +44,7 @@ public string Execute(string[] args) Image.Arrayjoin(images, across: 2).WriteToFile("emboss.jpg"); - return "See emboss.jpg"; + Console.WriteLine("See emboss.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/GdiConvert.cs b/samples/NetVips.Samples/Samples/GdiConvert.cs index a2b18db4..d6c2590d 100644 --- a/samples/NetVips.Samples/Samples/GdiConvert.cs +++ b/samples/NetVips.Samples/Samples/GdiConvert.cs @@ -1,5 +1,6 @@ namespace NetVips.Samples { + using System; using Extensions; using System.Drawing.Imaging; @@ -10,7 +11,7 @@ public class GdiConvert : ISample public const string Filename = "images/PNG_transparency_demonstration_1.png"; - public string Execute(string[] args) + public void Execute(string[] args) { var bitmap = new System.Drawing.Bitmap(Filename); @@ -61,7 +62,7 @@ public string Execute(string[] args) var image2 = bitmap.ToVips(); image2.WriteToFile("gdi-to-vips2.png");*/ - return "See gdi-to-vips.png"; + Console.WriteLine("See gdi-to-vips.png"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/GenerateEnums.cs b/samples/NetVips.Samples/Samples/GenerateEnums.cs index 6876d174..b86f3739 100644 --- a/samples/NetVips.Samples/Samples/GenerateEnums.cs +++ b/samples/NetVips.Samples/Samples/GenerateEnums.cs @@ -103,16 +103,17 @@ private string Generate() return stringBuilder.ToString(); } - public string Execute(string[] args) + public void Execute(string[] args) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - return "This example can only be run on Windows"; + Console.Error.WriteLine("This example can only be run on Windows"); + return; } File.WriteAllText("Enums.Generated.cs", Generate()); - return "See Enums.Generated.cs"; + Console.WriteLine("See Enums.Generated.cs"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/GenerateImageClass.cs b/samples/NetVips.Samples/Samples/GenerateImageClass.cs index 944e7f48..c1173092 100644 --- a/samples/NetVips.Samples/Samples/GenerateImageClass.cs +++ b/samples/NetVips.Samples/Samples/GenerateImageClass.cs @@ -770,11 +770,11 @@ private string Generate(string indent = " ") return stringBuilder.ToString(); } - public string Execute(string[] args) + public void Execute(string[] args) { File.WriteAllText("Image.Generated.cs", Generate()); - return "See Image.Generated.cs"; + Console.WriteLine("See Image.Generated.cs"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/GenerateImageOperators.cs b/samples/NetVips.Samples/Samples/GenerateImageOperators.cs index 2a74a335..8edf4144 100644 --- a/samples/NetVips.Samples/Samples/GenerateImageOperators.cs +++ b/samples/NetVips.Samples/Samples/GenerateImageOperators.cs @@ -355,10 +355,10 @@ public string GenerateOperators() return stringBuilder.ToString(); } - public string Execute(string[] args) + public void Execute(string[] args) { File.WriteAllText("Image.Operators.cs", GenerateOperators()); - return "See Image.Operators.cs"; + Console.WriteLine("See Image.Operators.cs"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/HelloWorld.cs b/samples/NetVips.Samples/Samples/HelloWorld.cs index 886cc314..acab54a2 100644 --- a/samples/NetVips.Samples/Samples/HelloWorld.cs +++ b/samples/NetVips.Samples/Samples/HelloWorld.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + /// /// From: https://github.com/libvips/lua-vips/blob/master/example/hello-world.lua /// @@ -8,12 +10,12 @@ public class HelloWorld : ISample public string Name => "Hello world"; public string Category => "Create"; - public string Execute(string[] args) + public void Execute(string[] args) { var image = Image.Text("Hello World!", dpi: 300); image.WriteToFile("hello-world.png"); - return "See hello-world.png"; + Console.WriteLine("See hello-world.png"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/IdentifyExtension.cs b/samples/NetVips.Samples/Samples/IdentifyExtension.cs index 1a63ff12..51e21280 100644 --- a/samples/NetVips.Samples/Samples/IdentifyExtension.cs +++ b/samples/NetVips.Samples/Samples/IdentifyExtension.cs @@ -63,7 +63,7 @@ public string GetExtensionNonTruncated(byte[] buffer) } } - public string Execute(string[] args) + public void Execute(string[] args) { Console.WriteLine("FindLoadBuffer function (non-truncated buffer)"); Console.WriteLine(GetExtension(File.ReadAllBytes("images/lichtenstein.jpg"))); @@ -76,8 +76,6 @@ public string Execute(string[] args) Console.WriteLine("vips-loader function (truncated buffer)"); Console.WriteLine(GetExtensionNonTruncated(Encoding.UTF8.GetBytes("GIF89a"))); - - return "All done!"; } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/ImagePyramid.cs b/samples/NetVips.Samples/Samples/ImagePyramid.cs index c4c5ab0a..badf60d7 100644 --- a/samples/NetVips.Samples/Samples/ImagePyramid.cs +++ b/samples/NetVips.Samples/Samples/ImagePyramid.cs @@ -11,7 +11,7 @@ public class ImagePyramid : ISample public const int TileSize = 50; public const string Filename = "images/sample2.v"; - public string Execute(string[] args) + public void Execute(string[] args) { // Build test image var im = Image.NewFromFile(Filename, access: Enums.Access.Sequential); @@ -20,26 +20,24 @@ public string Execute(string[] args) var cts = new CancellationTokenSource(); cts.CancelAfter(5000); - var progress = new Progress(percent => - { - Console.Write($"\r{percent}% complete"); - }); + var progress = new Progress(percent => Console.Write($"\r{percent}% complete")); // Uncomment to kill the image after 5 sec - im.SetProgress(progress/*, cts.Token*/); + im.SetProgress(progress /*, cts.Token*/); try { // Save image pyramid im.Dzsave("images/image-pyramid"); } - catch (VipsException exception) { + catch (VipsException exception) + { // Catch and log the VipsException, // because we may block the evaluation of this image Console.WriteLine("\n" + exception.Message); } Console.WriteLine(); - return "See images/image-pyramid.dzi"; + Console.WriteLine("See images/image-pyramid.dzi"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/LeakTest.cs b/samples/NetVips.Samples/Samples/LeakTest.cs index 747231f9..715fa29d 100644 --- a/samples/NetVips.Samples/Samples/LeakTest.cs +++ b/samples/NetVips.Samples/Samples/LeakTest.cs @@ -45,7 +45,7 @@ public class LeakTest : ISample /// /// Command-line arguments. /// Result. - public string Execute(string[] args) + public void Execute(string[] args) { NetVips.LeakSet(true); @@ -60,8 +60,6 @@ public string Execute(string[] args) // uncomment this line together with the `NObjects` variable in GObject // Console.WriteLine($"{GObject.NObjects} vips objects known to net-vips"); } - - return "All done!"; } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/NetworkStream.cs b/samples/NetVips.Samples/Samples/NetworkStream.cs index e3f87e93..e8065d73 100644 --- a/samples/NetVips.Samples/Samples/NetworkStream.cs +++ b/samples/NetVips.Samples/Samples/NetworkStream.cs @@ -11,19 +11,17 @@ public class NetworkStream : ISample public const string Uri = "https://images.weserv.nl/zebra.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { using var web = new WebClient(); - using (var stream = web.OpenRead(Uri)) - { - var image = Image.NewFromStream(stream, access: Enums.Access.Sequential); - Console.WriteLine(image.ToString()); + using var stream = web.OpenRead(Uri); + var image = Image.NewFromStream(stream, access: Enums.Access.Sequential); + Console.WriteLine(image.ToString()); - using var output = File.OpenWrite("stream-network.jpg"); - image.WriteToStream(output, ".jpg"); - } + using var output = File.OpenWrite("stream-network.jpg"); + image.WriteToStream(output, ".jpg"); - return "See stream-network.jpg"; + Console.WriteLine("See stream-network.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/NewFromMemoryRef.cs b/samples/NetVips.Samples/Samples/NewFromMemoryRef.cs index b3df83a0..d0894874 100644 --- a/samples/NetVips.Samples/Samples/NewFromMemoryRef.cs +++ b/samples/NetVips.Samples/Samples/NewFromMemoryRef.cs @@ -11,7 +11,7 @@ public class NewFromMemoryRef : ISample public string Name => "NewFromMemory reference test"; public string Category => "Internal"; - public string Execute(string[] args) + public void Execute(string[] args) { NetVips.CacheSetMax(0); @@ -34,8 +34,6 @@ public string Execute(string[] args) average = b.Avg(); Console.WriteLine($"After GC: {average}"); - - return "All done!"; } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/OnePointMosaic.cs b/samples/NetVips.Samples/Samples/OnePointMosaic.cs index 6e45b3d4..761ca75f 100644 --- a/samples/NetVips.Samples/Samples/OnePointMosaic.cs +++ b/samples/NetVips.Samples/Samples/OnePointMosaic.cs @@ -1,5 +1,6 @@ namespace NetVips.Samples { + using System; using System.Collections.Generic; using System.Linq; @@ -33,7 +34,7 @@ public class OnePointMosaic : ISample new[] {503, 959} }; - public string Execute(string[] args) + public void Execute(string[] args) { Image mosaicedImage = null; @@ -64,7 +65,7 @@ public string Execute(string[] args) mosaicedImage = mosaicedImage.Globalbalance(); mosaicedImage.WriteToFile("1-pt-mosaic.jpg"); - return "See 1-pt-mosaic.jpg"; + Console.WriteLine("See 1-pt-mosaic.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/OperationRef.cs b/samples/NetVips.Samples/Samples/OperationRef.cs index e2300917..0bcbc5e9 100644 --- a/samples/NetVips.Samples/Samples/OperationRef.cs +++ b/samples/NetVips.Samples/Samples/OperationRef.cs @@ -16,7 +16,7 @@ public class OperationRef : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { NetVips.CacheSetMax(0); @@ -55,8 +55,6 @@ public string Execute(string[] args) Interlocked.Decrement(ref count); }); - - return "All done!"; } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Progress.cs b/samples/NetVips.Samples/Samples/Progress.cs index 5b88c3b5..3ef39aee 100644 --- a/samples/NetVips.Samples/Samples/Progress.cs +++ b/samples/NetVips.Samples/Samples/Progress.cs @@ -10,7 +10,7 @@ public class Progress : ISample public const int TileSize = 50; public const string Filename = "images/sample2.v"; - public string Execute(string[] args) + public void Execute(string[] args) { // Build test image var im = Image.NewFromFile(Filename, access: Enums.Access.Sequential); @@ -26,7 +26,7 @@ public string Execute(string[] args) var avg = im.Avg(); - return "Done!"; + Console.WriteLine($"avg = {avg}"); } private void ProgressPrint(string name, VipsProgress progress) diff --git a/samples/NetVips.Samples/Samples/RandomCropper.cs b/samples/NetVips.Samples/Samples/RandomCropper.cs index e2107663..82b1c735 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/96 + /// See: https://github.com/kleisauke/net-vips/issues/58 /// public class RandomCropper : ISample { @@ -29,38 +29,13 @@ public Image RandomCrop(Image image, int tileSize) return image.Crop(x, y, width, height); } - public string Execute(string[] args) + public void Execute(string[] args) { - NetVips.LeakSet(true); + using var fileStream = File.OpenRead(Filename); + using var image = Image.NewFromStream(fileStream); Parallel.For(0, 1000, new ParallelOptions {MaxDegreeOfParallelism = NetVips.ConcurrencyGet()}, - 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!"; + i => RandomCrop(image, TileSize).WriteToFile($"x_{i}.png")); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/ShapeCropping.cs b/samples/NetVips.Samples/Samples/ShapeCropping.cs index 1a146223..ad63740a 100644 --- a/samples/NetVips.Samples/Samples/ShapeCropping.cs +++ b/samples/NetVips.Samples/Samples/ShapeCropping.cs @@ -33,7 +33,7 @@ public enum Shape public const bool Crop = true; - public string Execute(string[] args) + public void Execute(string[] args) { var image = Image.NewFromFile(Filename, access: Enums.Access.Sequential); var width = image.Width; @@ -76,7 +76,7 @@ public string Execute(string[] args) image.WriteToFile("shape.png"); - return "See shape.png"; + Console.WriteLine("See shape.png"); } /// diff --git a/samples/NetVips.Samples/Samples/Smartcrop.cs b/samples/NetVips.Samples/Samples/Smartcrop.cs index 60cf5351..86a48a76 100644 --- a/samples/NetVips.Samples/Samples/Smartcrop.cs +++ b/samples/NetVips.Samples/Samples/Smartcrop.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + public class Smartcrop : ISample { public string Name => "Smartcrop"; @@ -7,12 +9,12 @@ public class Smartcrop : ISample public const string Filename = "images/equus_quagga.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var image = Image.Thumbnail(Filename, 300, height: 300, crop: "attention"); image.WriteToFile("smartcrop.jpg"); - return "See smartcrop.jpg"; + Console.WriteLine("See smartcrop.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Sobel.cs b/samples/NetVips.Samples/Samples/Sobel.cs index e532d90c..37f20478 100644 --- a/samples/NetVips.Samples/Samples/Sobel.cs +++ b/samples/NetVips.Samples/Samples/Sobel.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + public class Sobel : ISample { public string Name => "Sobel"; @@ -7,7 +9,7 @@ public class Sobel : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var im = Image.NewFromFile(Filename, access: Enums.Access.Sequential); @@ -19,7 +21,7 @@ public string Execute(string[] args) im.WriteToFile("sobel.jpg"); - return "See sobel.jpg"; + Console.WriteLine("See sobel.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Stream.cs b/samples/NetVips.Samples/Samples/Stream.cs index 02905de1..8c0c561c 100644 --- a/samples/NetVips.Samples/Samples/Stream.cs +++ b/samples/NetVips.Samples/Samples/Stream.cs @@ -10,18 +10,16 @@ public class Stream : ISample public const string Filename = "images/PNG_transparency_demonstration_1.png"; - public string Execute(string[] args) + public void Execute(string[] args) { - using (var input = File.OpenRead(Filename)) - { - var image = Image.NewFromStream(input, access: Enums.Access.Sequential); - Console.WriteLine(image.ToString()); + using var input = File.OpenRead(Filename); + var image = Image.NewFromStream(input, access: Enums.Access.Sequential); + Console.WriteLine(image.ToString()); - using var output = File.OpenWrite("stream.png"); - image.WriteToStream(output, ".png"); - } + using var output = File.OpenWrite("stream.png"); + image.WriteToStream(output, ".png"); - return "See stream.png"; + Console.WriteLine("See stream.png"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/Thumbnail.cs b/samples/NetVips.Samples/Samples/Thumbnail.cs index 64ff0179..5c37f985 100644 --- a/samples/NetVips.Samples/Samples/Thumbnail.cs +++ b/samples/NetVips.Samples/Samples/Thumbnail.cs @@ -1,5 +1,7 @@ namespace NetVips.Samples { + using System; + public class Thumbnail : ISample { public string Name => "Thumbnail"; @@ -7,12 +9,12 @@ public class Thumbnail : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { var image = Image.Thumbnail(Filename, 300, height: 300); image.WriteToFile("thumbnail.jpg"); - return "See thumbnail.jpg"; + Console.WriteLine("See thumbnail.jpg"); } } } \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/ThumbnailPipeline.cs b/samples/NetVips.Samples/Samples/ThumbnailPipeline.cs index 0eff2e81..e036788d 100644 --- a/samples/NetVips.Samples/Samples/ThumbnailPipeline.cs +++ b/samples/NetVips.Samples/Samples/ThumbnailPipeline.cs @@ -43,7 +43,7 @@ public bool LoaderSupportPage(string loader) } #pragma warning disable CS0162 // Unreachable code detected - public string Execute(string[] args) + public void Execute(string[] args) { // If you set a number to zero (0), it will resize on the other specified axis. var width = 200; @@ -210,7 +210,7 @@ public string Execute(string[] args) {"strip", true} });*/ - return "See thumbnail.webp"; + Console.WriteLine("See thumbnail.webp"); } } #pragma warning restore CS0162 // Unreachable code detected diff --git a/samples/NetVips.Samples/Samples/ThumbnailS3.cs b/samples/NetVips.Samples/Samples/ThumbnailS3.cs new file mode 100644 index 00000000..c908f359 --- /dev/null +++ b/samples/NetVips.Samples/Samples/ThumbnailS3.cs @@ -0,0 +1,59 @@ +namespace NetVips.Samples +{ + using System; + using System.IO; + using System.Threading.Tasks; + using Amazon; + using Amazon.S3; + using Amazon.S3.Transfer; + + /// + /// See: + /// https://github.com/kleisauke/net-vips/issues/96 + /// https://libvips.github.io/libvips/2019/11/29/True-streaming-for-libvips.html + /// + public class ThumbnailS3 : ISample + { + public string Name => "Thumbnail from S3"; + public string Category => "Streaming"; + + private const string BucketName = "libvips-packaging"; + private const string KeyName = "zebra.jpg"; + + private static readonly RegionEndpoint BucketRegion = RegionEndpoint.EUWest1; + + public async Task Thumbnail() + { + using var client = new AmazonS3Client(BucketRegion); + + try + { + using var transferUtility = new TransferUtility(client); + await using var stream = await transferUtility.OpenStreamAsync(BucketName, KeyName); + using var thumbnail = Image.ThumbnailStream(stream, 300, height: 300); + await using var output = File.OpenWrite("thumbnail-s3.jpg"); + thumbnail.WriteToStream(output, ".jpg"); + + Console.WriteLine("See thumbnail-s3.jpg"); + } + catch (AmazonS3Exception e) + { + Console.WriteLine($"Error encountered. Message: '{e.Message}' when reading object"); + } + catch (Exception e) + { + Console.WriteLine($"Unknown encountered on server. Message: '{e.Message}' when reading object"); + } + } + + public void Execute(string[] args) + { + NetVips.LeakSet(true); + + Thumbnail().GetAwaiter().GetResult(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } +} \ No newline at end of file diff --git a/samples/NetVips.Samples/Samples/ThumbnailStream.cs b/samples/NetVips.Samples/Samples/ThumbnailStream.cs index c9d0bae4..0c685202 100644 --- a/samples/NetVips.Samples/Samples/ThumbnailStream.cs +++ b/samples/NetVips.Samples/Samples/ThumbnailStream.cs @@ -10,18 +10,16 @@ public class ThumbnailStream : ISample public const string Filename = "images/lichtenstein.jpg"; - public string Execute(string[] args) + public void Execute(string[] args) { - using (var input = File.OpenRead(Filename)) - { - var thumbnail = Image.ThumbnailStream(input, 300, height: 300); - Console.WriteLine(thumbnail.ToString()); + using var input = File.OpenRead(Filename); + var thumbnail = Image.ThumbnailStream(input, 300, height: 300); + Console.WriteLine(thumbnail.ToString()); - using var output = File.OpenWrite("thumbnail-stream.jpg"); - thumbnail.WriteToStream(output, ".jpg"); - } + using var output = File.OpenWrite("thumbnail-stream.jpg"); + thumbnail.WriteToStream(output, ".jpg"); - return "See thumbnail-stream.jpg"; + Console.WriteLine("See thumbnail-stream.jpg"); } } } \ No newline at end of file